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

Is it possible to use the .focus() event on an entire form in JQuery? If so, how

Take a look at this HTML snippet: <form ....> <textarea>....</textarea <input .... /> </form> I'm trying to set up a help section that appears when the user focuses on any form element, and disappears when they lose ...

Positioning the label for a Material-UI text field with an icon adornment when the shrink property is set

https://i.stack.imgur.com/E85yj.png Utilizing Material-UI's TextField, I have an Icon embedded within. The issue arises when the label "Your good name here" overlaps the icon instead of being placed beside it. This problem emerged after I included ...

adjusting array based on screen size fluctuations

Imagine having two arrays of images named landscape_images[] and portrait_images[]. One is for the landscape view and the other for the portrait view. When the screen width is in landscape mode, the wide resolution images will be displayed on the body. C ...

What could be the reason for the child element not occupying the full height of its parent container?

* { padding: 0; margin: 0; box-sizing: border-box; } body { margin: 50px; } .navbar { display: flex; align-items: center; justify-content: center; color: darkgreen; font-family: 'Vollkorn', serif; font-size: 1.2rem; font-w ...

Content towering over the footer

Can anyone help me figure out how to create a footer that appears behind the content when you scroll towards the end of a website? I've tried looking for tutorials online, but haven't found exactly what I'm looking for. Most of what I'v ...

Browser displaying a CSS error: "Invalid property name" while applying pseudo-element :after

I encountered an issue in Chrome and Explorer while attempting to set a CSS property for a pseudo element :after. (I'm trying to create a styled burger nav icon) The error message I received was: 'Unknown property name' This happened wh ...

Connecting to an element within a separate node (XSLT)

I have an XML document that includes a list of companies. My goal is to use XSLT to create links that point to the <link> child of the next company node. To provide a clearer picture, here is a snippet of sample XML data I am working with: <portf ...

Identifying copy and paste behavior within an HTML form using .NET CORE MVC 2.1

I inherited a project at my current company and one of the tasks involves allowing users to paste multiple ISBN numbers from Excel into a search field on my Index page (HTML). Sometimes, the ISBNs are poorly formatted with letters or special symbols mixed ...

How Webpack 4 Utilizes splitChunks to Create Numerous CSS Files

I need to create multiple CSS files using webpack 4 with the min-css-extract-plugin and splitChunks plugin. Here is my webpack configuration. const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const path = require("path"); module.exports = ...

Updating the default color of selected text within a webpage's content

Is there a way to modify the default blue color that appears when content is selected on a webpage? I am wondering how to change this selection color to a custom color of choice. ...

What is the process of adding a phone number with an extension on an iPhone web application?

When building a web application, inserting the following number: <a href="tel:888-123-4567">call now</a> The iPhone will automatically convert that link into an instant call function when clicked. But what should I do if the number includes ...

Create a custom div class specifically designed for an image button

Apologies if my title is a bit misleading, I struggled to find the right phrasing. Currently, I have images linked to various social networks using href. For instance, clicking on the facebook icon directs to my Facebook page. The layout looks like this: ...

Modify the appearance of the elements dynamically by applying styles from the CSS file without relying on the

In my CSS file, I have the following styles: .myClass { ... } .myClass:focus { ... } Am I able to modify these styles from my code? For instance, can I change the focused style of an input element when a button is pressed? ...

Battle of Cookies and User Preferences: Who Will Prevail?

Recently, I encountered a challenge while developing an ecommerce site. After facing numerous issues, I believe I may have finally found a solution. (For more details on the problem and my question, click here) The expected scenario: Users checkout and p ...

What methods can be used to control access to document.styleSheets data, and what is the purpose behind doing so?

Recently, I came across the document.styleSheets API, which allows you to access stylesheets used by a website. Using this API is simple, for example, document.styleSheets[0].cssRules will provide all CSS rules for the first stylesheet on a page. When I t ...

Leveraging an AngularJS variable within an iframe

Is it possible to use a variable inside an iframe src or ng-src attribute? I've tried different variables but none seem to be recognized. For example: <iframe ng-src="http://www.example.com/?name={{test}}"> </iframe> The variable test ju ...

Use jQuery to parse the JSON below and then showcase the information in an HTML table

Can anyone assist me with parsing the following JSON using jQuery and presenting it in an HTML table? I want to showcase the values of "key" and "doc_count" in an HTML table. Your help would be greatly appreciated. Please find the JSON data below: { ...

placing a div inside another div

As I work on my website, I have created several panels with a repeating texture. To enhance the visual appeal of the site, I decided to add colored divs and adjust opacity for a tint effect instead of using separate images. The issue I'm facing is th ...

Set a dynamic Active Class on various divisions by utilizing their respective indexes

I have two divs with the same class. When I click on an anchor tag within one of the elements, I want to add a class to the corresponding position in the second div as well. Mirror: JSFiddle Here is the jQuery code snippet: $(document).on('click ...

JQuery - Triggering mouseenter/hover event only on top-level menu items, excluding the nested submenu items (list items that are not within nested lists)

I have a complex navigation menu structure with nested lists, and I'm struggling to trigger an event only on the top-level items when hovered. Despite trying different jQuery selectors and methods, such as using the NOT selector or targeting direct ch ...