The justify-content property aligns single-line text horizontally, but its alignment may not be consistent when the text expands over

Seeking a deeper understanding of flexbox alignment, I decided to experiment with aligning content using only flexbox properties. One puzzling observation is how "justify-content" centers items with single lines of text (flex-item-1 and flex-item-5), but not the others. Can anyone point out what I might be overlooking?

To see my CSS example in action, check out this link: https://codepen.io/altroboy/pen/NWXNBxZ

.flex-container {
  display: flex;
  flex-wrap: wrap;
  width: 50%;
  height: 50vh;
  background: hsl(120, 0%, 80%);
  justify-content: center;
}

.flex-item {
  flex: 0 1 30%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-item-1 {
  background: hsl(0, 80%, 50%);
}

.flex-item-2 {
  background: hsl(60, 80%, 50%);
}

.flex-item-3 {
  background: hsl(120, 80%, 50%);
}

.flex-item-4 {
  background: hsl(180, 80%, 50%);
}

.flex-item-5 {
  background: hsl(240, 80%, 50%);
}

.flex-item-6 {
  background: hsl(300, 80%, 50%);
}
<div class="flex-container">
  <div class="flex-item flex-item-1">11111 11111 11111 11111</div>
  <div class="flex-item flex-item-2">22222 22222 33333 22222 22222 22222 22222</div>
  <div class="flex-item flex-item-3">33333 33333 33333 33333 33333 33333 33333 33333 33333 33333</div>
  <div class="flex-item flex-item-4">44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 </div>
  <div class="flex-item flex-item-5">55555 55555 55555 55555 </div>
  <div class="flex-item flex-item-6">66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 </div>
</div>

(Still learning, so feel free to suggest any additional information!)

Answer №1

If I'm off track, feel free to correct me. The issue is that each of your container flex-item-1 ... flex-item-6 has specific width and height settings which result in a overflow situation. In such cases, the excess content will align to the Left. If you want them to align at the center, simply add this code:

text-align: center !important;

.flex-container {
  display: flex;
  flex-wrap: wrap;
  width: 50%;
  height: 50vh;
  background: hsl(120, 0%, 80%);
  justify-content: center;
}

.flex-item {
  flex: 0 1 30%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-item-1 {
  background: hsl(0, 80%, 50%);
}

.flex-item-2 {
  background: hsl(60, 80%, 50%);
  text-align: center !important;
}

.flex-item-3 {
  background: hsl(120, 80%, 50%);
  text-align: right !important;
}

.flex-item-4 {
  background: hsl(180, 80%, 50%);
}

.flex-item-5 {
  background: hsl(240, 80%, 50%);
}

.flex-item-6 {
  background: hsl(300, 80%, 50%);
  text-align: center !important;
}
<div class="flex-container">
  <div class="flex-item flex-item-1">11111 11111 11111 11111</div>
  <div class="flex-item flex-item-2">22222 22222 33333 22222 22222 22222 22222</div>
  <div class="flex-item flex-item-3">33333 33333 33333 33333 33333 33333 33333 33333 33333 33333</div>
  <div class="flex-item flex-item-4"><span style="text-align: justify">44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 44444 </span></div>
  <div class="flex-item flex-item-5">55555 55555 55555 55555 </div>
  <div class="flex-item flex-item-6">66666 66666 66666 66666 66666 66666 66666 66666 66666 66666 </div>
</div>

https://i.stack.imgur.com/S6Quh.png

To have them all appear on a single line, apply font-size: x-small;

https://i.stack.imgur.com/KRfkK.png

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

What is the best way to switch the site header in JavaScript or jQuery?

I have created a Bootstrap menu design and I am looking to add a sliding functionality to it. My goal is to hide the menu when scrolling down and display it when scrolling up (slide-down / slide-up). For implementing this feature, I plan to utilize jQuery ...

Combining classes in SCSS to create more versatile styles

Here is some code snippet: input { border:1px solid black; color:#FFF; } Another snippet: .input { padding:0px 5px 0px 3px; } How can we use SASS/SCSS to achieve the following: input.input This will apply the .input class directly to ...

What is the best way to incorporate a fadeIn effect while using the .attr method?

I am working with a line of code that switches between classes class1 and class4 to update the background of my website. I would like to incorporate a fadeIn effect each time the class changes. Can you help me with this? Thank you! Below is the code snipp ...

I'm struggling to locate the space that should be between these elements

After accidentally starting in quirks mode without declaring a doctype, I realized my mistake and corrected it by declaring a doctype. However, there is a persistent issue with a space between .car-box-img and car-box that I can't seem to locate in t ...

Comparing React's Styled-components, JSS, and Emotion: Which is

As a CTO, I am faced with the decision of choosing between three popular CSS-in-JS libraries: jss emotion styled-component. I will keep this question focused and avoid straying into subjective territory. If necessary, I will answer it myself by asking sp ...

Base64 image failing to display in Internet Explorer

Is there a way to efficiently handle the base 64 form of an image in different browsers? The code I have achieves this as shown below: import base64 import requests img=requests.get('https://example.com/hello.jpg') base=base64.b64encode(img.cont ...

The function iframe.scrollTo() is not effective for scrolling through Excel or PowerPoint documents on an iPad

I am trying to create a custom scrolling feature for iframe content specifically for iPad. Currently, I have set up my iframe like this: <div id="scroller" style="height: 300px; width: 100%; overflow: auto;"> <iframe height="100%" id="iframe" ...

"Subtle Website Background Fade Effect When Menu is Hovered Over

Click on the following link to observe a transition effect that occurs on the body of the website when interacting with the main menu system: Here is the website Do you know what this effect is called and how it can be integrated into a website? ...

No feedback received after sending keys using Selenium

Whenever I execute my code using PhantomJS and Selenium, the result appears to be correct. However, when it comes to using send_keys function, the code gets stuck without any errors, answers, or progress. I am puzzled and eager to find out why this is ha ...

What steps can I take to make the cards in bootstrap 5.3 responsive for mobile devices?

I'm struggling with my code and need some help. I've been editing it, but things seem to be getting worse instead of better. Specifically, I can't figure out how to set up my container, row, and columns in Bootstrap properly. My site uses ca ...

PHP sending only a single space in an email

My HTML form works perfectly fine, except for one field! The issue arises with a particular text field that I fill out using a button and a short JavaScript function. Could this be causing a conflict with the PHP code? The problematic text field is: inpu ...

Angular can be used to compare two arrays and display the matching values in a table

Having two arrays of objects, I attempted to compare them and display the matching values in a table. While looping through both arrays and comparing them by Id, I was able to find three matches. However, when trying to display these values in a table, onl ...

Whenever I include an onClick event to a div element, the entire webpage fails to display

Currently taking on the task of developing a seat booking website. I am encountering an issue with adding an event listener to a particular div element, which should trigger a function to store the value of the div. However, upon implementing the onClick e ...

Utilizing bootstrap for a responsive design that includes max-height constraints and horizontal

Seeking to modify the appearance of my Angular Template by incorporating Bootstrap and a custom CSS file. <!-- index.html --> <div static-include="partial/example.html"></div> The static-include directive, sourced from Stack Overflow, p ...

Having trouble finding two p-col-6 elements side by side in the PrimeNG FlexGrid with Angular?

I have integrated Flex Grid into my Angular7 project. In the initial state, I am able to display two p-col-6 elements side by side without any issues. However, when I try to rearrange them in p-col-12, they no longer align properly. Here is a detailed expl ...

CSS: Only one out of three <div>'s has the styles applied in JSFiddle

When working on my project, I encountered an issue while trying to add CSS styles to three <div> structures with unique IDs. Strangely, making small changes to unrelated CSS caused elements to go from being stylized to losing their style. If you com ...

Instructions on how to add an ExternalLink to a webpage linking back to the previous page

I'm in the process of creating an error page for my app, and I'm trying to figure out how to add a Wicket ExternalLink that takes users back to the previous page. I believe I need to store the parameters or entire URL of the last visited page, b ...

Automatic Email Reply that Automatically populates the recipient field with the sender's information

I'm curious to know if it's possible to create a mailto link that automatically populates the TO: field with the email of the original sender, without including the email address in the link itself. For instance: "a href="mailto:ORIGINALSENDER? ...

Can you explain the contrast between img.height and img.style.height?

I'm currently in the process of resizing a series of images by iterating through an array and adjusting their sizes. if(items[0].height > 700 || items[0].width > 700){ items[0].style.height = "700px"; items[0].style.width = "700px"; } As ...

Troubleshooting CSS navigation bar hamburger dilemma

Currently, I am facing an issue with a toggle button on my website. When I click on the hamburger menu, the navigation bar does not show up even though the toggle feature is working perfectly fine. I have tried combining different sources to solve this pro ...