Achieving perfect alignment through the horizontal centering of three flexbox items with identical widths

Struggling to center these 3 items horizontally using flexbox, all of the same width. HTML:

 <div id="contact_flex_container">
    <div id="fb">
        <img src="img/contact/fb.png" class="contact_img">
        <h3>Title1</h3>
    </div>

    <div id="yt">
        <img src="img/contact/yt.png" class="contact_img">
        <h3>Title2</h3>
    </div>

    <div id="mail">
        <img src="img/contact/mail.png" class="contact_img">
        <h3>Title3</h3>
    </div>
</div>

CSS:

#contact_flex_container {
    display: flex;
    flex-flow: row wrap;
    text-align: center;
    background-color: red;
    width: auto;
    justify-content: space-around;
}

.contact_img {
    width: 40px;
    height: 40px;
}

#fb {
    flex-basis: 0;
    flex-grow: 1;
}

#yt {
    flex-basis: 0;
    flex-grow: 1;
}

#mail {
    flex-basis: 0;
    flex-grow: 1;
}

Tried various methods like margin adjustments and justify-content settings, but they don't seem to work. Not sure what's missing or where I'm going wrong. Any suggestions?

Answer №1

The flex properties you are applying to the flex-items result in them all being expanded to their maximum size (in this case, 33% of the container).

To fix this, simply remove those properties and change the parent to justify-content:center.

#contact_flex_container {
  display: flex;
  flex-flow: row wrap;
  text-align: center;
  background-color: red;
  width: auto;
  justify-content: center
}
.contact_img {
  width: 40px;
  height: 40px;
}
#fb {} #yt {} #mail {}
<div id="contact_flex_container">
  <div id="fb">
    <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
    <h3>Title1</h3>
  </div>

  <div id="yt">
    <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
    <h3>Title2</h3>
  </div>

  <div id="mail">
    <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
    <h3>Title3</h3>
  </div>
</div>

If needed, consider the additional requirements:

An extra wrapper is needed with an inline-flex display style.

#contact_flex_container {
  display: flex;
  flex-flow: row wrap;
  text-align: center;
  background-color: red;
  width: auto;
  justify-content: center
}
.wrap {
  display: inline-flex;
}
.wrap > div {
  flex: 1;
  border: 1px solid grey;
}
.contact_img {
  width: 40px;
  height: 40px;
}
<div id="contact_flex_container">
  <div class="wrap">

    <div id="fb">
      <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
      <h3>Lorem ipsum dolor sit.</h3>
    </div>

    <div id="yt">
      <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
      <h3>Lorem ipsum.</h3>
    </div>

    <div id="mail">
      <img src="http://lorempixel.com/image_output/abstract-q-c-50-50-5.jpg" class="contact_img">
      <h3>Lorem</h3>
    </div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Glistening HTML Element along with DT::renderDataTable result$

My website is built using HTML and connected to Shiny through the use of matching IDs in HTML Elements. To display table data on the site, the code snippet below functions properly: server.R output$InfoTable <- renderTable({...}) index.html <div ...

Having trouble with Django routing redirecting me to the incorrect URL

I am currently working on developing a small poll application using Django. The main page of the app includes buttons that allow users to create new polls and delete existing ones. For the delete functionality, I have set up a route that should direct the ...

How to align text with an image as the size of the image adjusts

I'm brainstorming creative ways to handle a webpage section where the image will be swapped out with various images (with widths up to 620px) and a text caption is positioned over it. I aim for the text to adjust its absolute position based on the wid ...

In Python using Selenium, the text property of a WebElement may cut off duplicate whitespaces

Today, I came across an interesting observation regarding the text property of WebElement. Here is a PDF link that sparked my curiosity: https://i.stack.imgur.com/1cbPj.png Upon closer inspection, I noticed that the file name contains triple whitespace. W ...

Can footer.html be omitted from the base.html file when extending it for Django?

I'm using base.html as my template which includes {% include 'footer.html' %} in every page. However, there are certain pages where I don't want the footer to appear. Is there a way to exclude the footer on specific pages using some sor ...

Placing a logo to the left of a UL list

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> & ...

Discovering the bootstrap of a specific segment: A step-by-step guide

I encountered an issue when trying to paste a menu from one responsive HTML template to another. While both templates are designed to be responsive, the menu alignment gets distorted when viewed on mobile devices. I am unsure of how to extract only the bo ...

Tips for resolving issues with media queries in CSS

After deciding to create both desktop and mobile versions of my site, I attempted to utilize media queries in CSS. Unfortunately, after coding them, I discovered that they were not functioning as expected. Seeking a solution, I turned to Youtube where I ca ...

What steps can be taken to transform example.com/blog.html into example.com/blog?

www.awesomenewgames.com/articles I need to change the URL of this page from ending with /articles to simply /blog. So the desired result should be: www.awesomenewgames.com/blog What steps do I need to take to make this change? Any guidance on this matte ...

Is it possible to disable other options when a checkbox is checked, but enable them all when unchecked using

I am trying to create a group of checkbox buttons where if one is checked, the others should be disabled. When the checked checkbox is unchecked, all checkboxes should be enabled. However, my current code is experiencing issues where all checkboxes become ...

React - CSS Transition resembling a flip of a book page

As I delve into more advanced topics in my journey of learning React and Front Web Dev, I discovered the ReactCSSTransitionGroup but learned that it is no longer maintained so we now use CSSTransitionGroup. I decided to create a small side project to expe ...

Struggling to apply size in percentage to several images used as a background

Displaying two background images with different positioning: html { background: top left no-repeat url(http://wallpapers.pupazzo.org/animals/Peek-a-Boo_Red_Fox_Kit.jpg), top right no-repeat url(http://www.dcwild.com/images/Fox-Kit-Rive ...

Using CSS modules with React libraries

Currently working on a React website, I decided to eject my project in order to use css modules styles. After running npm run eject, I made additional configurations in the webpack.config.dev.js and webpack.config.prod.js files. However, I encountered an i ...

Position the div alignment to the top within the table cell

I'm having trouble aligning the div under the header to the right. Here is my HTML code and a photo for reference: HTML Code: <td> <div class="schedule-content"> <div class="timestamp& ...

Mobile navigation menu options on the left and right sides

I've encountered an issue with my mobile navigation. I have two navigation menus - one on the left and one on the right, designed to slide out when triggered. The left menu functions correctly with a smooth transition, but the right menu abruptly pops ...

When using HTML select tags, make sure to submit the actual text of the selected option instead of just the value attribute

There seems to be an issue with a form that I am submitting via POST where the option text is being sent instead of the option value. Here is my select declaration: <select name = "newfreq"> <option val='1'>every week</option ...

Looking to utilize Python Selenium for downloading a PDF file

I am currently working on automating the process of downloading PDFs using Selenium Webdriver in Python An issue I've encountered is that the download button is hidden within an embed tag in the HTML code <embed width="100%" height="100%" name="p ...

Construct a table from JSON data

I am having trouble generating a table from JSON data. It seems like there may be an issue with my link, but there could be something else going on as well. I am not seeing any data displayed in my table and the error message I am getting is: Cannot read ...

Measuring the characters within an HTML element

Wondering if there's a way to calculate the number of letters in the inner text of an HTML element without including the inner elements' texts? I experimented with the ".getText()" method of "WebElements" using the Selenium library, but it inclu ...

Enhancing Transparency of WMS Layers in OpenLayers

I need help figuring out how to add transparency to a WMS layer in openlayers. Here is the current javascript code for a non-transparent layer: var lyr_GDPSETAAirtemperatureC = new ol.layer.Tile({ source: new ol.source.TileWMS(({ ...