height:100% doesn't work in that way for static elements. You should: Define the height/min-height for the main Growing the main using flexbox Using position (for this example I don't recommend this option) Answer from Jopzik on reddit.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Reference › Properties › align-items
align-items - CSS | MDN
/* Basic keywords */ align-items: normal; align-items: stretch; /* Positional alignment */ /* align-items does not take left and right values */ align-items: center; align-items: start; align-items: end; align-items: flex-start; align-items: flex-end; align-items: self-start; align-items: self-end; align-items: anchor-center; /* Baseline alignment */ align-items: baseline; align-items: first baseline; align-items: last baseline; /* Overflow alignment (for positional alignment only) */ align-items: safe center; align-items: unsafe center; /* Global values */ align-items: inherit; align-items: initial; align-items: revert; align-items: revert-layer; align-items: unset;
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › utilities › flex
Flex · Bootstrap
Use align-content utilities on flexbox containers to align flex items together on the cross axis. Choose from start (browser default), end, center, between, around, or stretch.
Discussions

Why isn’t “align-items: center” not working here?
height:100% doesn't work in that way for static elements. You should: Define the height/min-height for the main Growing the main using flexbox Using position (for this example I don't recommend this option) More on reddit.com
🌐 r/css
7
7
September 22, 2024
html - Aligning elements left, center and right in flexbox - Stack Overflow
Then each section becomes a (nested) flex container which allows you to vertically and horizontally align the links using flex properties. Now the left and right items are pinned to the edges of the container and the middle item is perfectly centered (even though the left and right items are ... More on stackoverflow.com
🌐 stackoverflow.com
Flex align left and center - HTML & CSS - SitePoint Forums | Web Development & Design Community
Using CSS flex, how do I get two items horizontal with each other at the top of a page, one aligned left, the other aligned center vertically? I have margin left 0 for the first item. If I use margin auto for the other, the left edge of the item centers vertically, not the item itself. More on sitepoint.com
🌐 sitepoint.com
0
July 20, 2024
html - Aligning elements left and center with flexbox - Stack Overflow
I.e. Center one item, and put another item on the right? 2018-11-13T11:30:46.913Z+00:00 ... @Benisburgers Just put your content to div with class right and leave div with class left empty. 2018-11-13T11:41:13.303Z+00:00 ... @Benisburgers Nothing moves anywhere, content is aligned left and it ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tailwind CSS
tailwindcss.com › docs › align-items
align-items - Flexbox & Grid - Tailwind CSS
Use the items-start utility to ... class="py-12">02</div> <div class="py-8">03</div></div> Use the items-center utility to align items along the center of the container's cross axis:...
🌐
W3Schools
w3schools.com › cssref › css3_pr_align-items.php
CSS align-items property
Center the alignments for all the ... it Yourself" examples below. The align-items property specifies the default alignment for items inside a flexbox or grid container....
🌐
Kyle Shevlin's Blog
kyleshevlin.com › align-items-center-vs-text-align-center
align-items: center vs. text-align: center | Kyle Shevlin
When we change align-items to center, we shrink all the children to their minimum content width.
Find elsewhere
🌐
SitePoint
sitepoint.com › html & css
Flex align left and center - HTML & CSS - SitePoint Forums | Web Development & Design Community
July 20, 2024 - Using CSS flex, how do I get two items horizontal with each other at the top of a page, one aligned left, the other aligned center vertically? I have margin left 0 for the first item. If I use margin auto for the other…
🌐
PrimeFlex
primeflex.org › alignitems
Align Items - PrimeFlex
Items are located at the center of the container. ... <div class="flex align-items-center flex-wrap" style="min-height: 200px"> <div class="flex align-items-center justify-content-center bg-primary font-bold border-round m-2" style="min-width: ...
Top answer
1 of 9
144

Add third empty element:

<div class="parent">
  <div class="left">Left</div>
  <div class="center">Center</div>
  <div class="right"></div>
</div>

And the following style:

.parent {
  display: flex;
}
.left, .right {
  flex: 1;
}

Only left and right are set to grow and thanks to the facts that...

  • there are only two growing elements (doesn't matter if empty) and
  • that both get same widths (they'll evenly distribute the available space)

...center element will always be perfectly centered.

This is much better than accepted answer in my opinion because you do not have to copy left content to right and hide it to get same width for both sides, it just magically happens (flexbox is magical).


In action:

.parent {
  display: flex;
}

.left,
.right {
  flex: 1;
}


/* Styles for demonstration */
.parent {
  padding: 5px;
  border: 2px solid #000;
}
.left,
.right {
  padding: 3px;
  border: 2px solid red;
}
.center {
  margin: 0 3px;
  padding: 3px;
  border: 2px solid blue;
}
<div class="parent">
  <div class="left">Left</div>
  <div class="center">Center</div>
  <div class="right"></div>
</div>

2 of 9
33

EDIT: See Solo's answer below, it is the better solution.


The idea behind flexbox is to provide a framework for easily aligning elements with variable dimensions within a container. As such, it makes little sense to provide a layout where the width of one element is totally ignored. In essence, that is exactly what absolute positioning is for, as it takes the element out of the normal flow.

As far as I know, there is no nice way of doing this without using position: absolute;, so I would suggest using it... but If you REALLY don't want to, or can't use absolute positioning then I suppose you could use one of the following workarounds.


If you know the exact width of the "Left" div, then you could change justify-content to flex-start (left) and then align the "Center" div like this:

#center {
    position: relative;
    margin: auto;
    left: -{half width of left div}px;
}

If you do not know the width, then you could duplicate "Left" on the right side, use justify-content: space-between;, and hide the new right element: Just to be clear, this is really, really ugly... better to use absolute positioning than to duplicate content. :-)

#parent {
  align-items: center;
  border: 1px solid black;
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  width: 500px;
}
#right {
    opacity: 0;
}
<div id="parent">
  <span id="left">Left</span>
  <span id="center">Center</span>
  <span id="right">Left</span>
</div>

🌐
Bootstrap Shuffle
bootstrapshuffle.com › classes › flexbox › align-items-*-center
align-items-*-center - Bootstrap CSS class
<div class="d-flex align-items-center"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <!-- responsive variations --> <div class="d-flex align-items-sm-center">...</div> <div class="d-flex align-items-md-center">...</div> <div class="d-flex align-items-lg-center">...</div> <div class="d-flex align-items-xl-center">...</div>Copy code
🌐
CSS-Tricks
css-tricks.com › almanac › properties › a › align-items
align-items | CSS-Tricks
April 19, 2025 - The align-items property accepts ... margin edge of the items is placed on the cross-end line · center: items are centered in the cross-axis ·...
🌐
W3Schools
w3schools.com › css › css3_flexbox_container.asp
CSS Flexbox Container
align-items - Aligns the flex items when they do not use all available space on the cross-axis (vertically)
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Guides › Flexible_box_layout › Aligning_items
Aligning items in a flex container - CSS | MDN
In this next live example, the flex container has align-items: flex-start, which means the items are all aligned to the start of the cross axis. Using the first-child selector, the first item is set to align-self: stretch. Another item with the selected class has align-self: center set.
🌐
Overleaf
overleaf.com › learn › latex › Text_alignment
Text alignment - Overleaf, Online LaTeX Editor
LaTeX does have built-in commands for changing the typeset alignment of text: ragged-right (\raggedright) ragged-left (\raggedleft) centred (\centering) together with corresponding environments: ragged-right (flushleft environment) ragged-left (flushright environment) centred (centering environment) However, the ragged2e package provides some refinements which improve upon these standard LaTeX commands and environments.
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › utilities › flex
Flex · Bootstrap v5.3
Use justify-content utilities on flexbox containers to change the alignment of flex items on the main axis (the x-axis to start, y-axis if flex-direction: column). Choose from start (browser default), end, center, between, around, or evenly.
🌐
React Native
reactnative.dev › docs › flexbox
Layout with Flexbox · React Native
1 month ago - This only has effect when items are wrapped to multiple lines using flexWrap. flex-start (default value) Align wrapped lines to the start of the container's cross axis. flex-end Align wrapped lines to the end of the container's cross axis. stretch (default value when using Yoga on the web) Stretch wrapped lines to match the height of the container's cross axis. center Align wrapped lines in the center of the container's cross axis.
🌐
Shuffle
shuffle.dev › bootstrap › classes › flexbox › align-items-*-center
Bootstrap CSS class: align-items-*-center
<div class="d-flex align-items-center"> <div class="p-2">Flex item 1</div> <div class="p-2">Flex item 2</div> <div class="p-2">Flex item 3</div> </div> <!-- responsive variations --> <div class="d-flex align-items-sm-center">...</div> <div class="d-flex align-items-md-center">...</div> <div class="d-flex align-items-lg-center">...</div> <div class="d-flex align-items-xl-center">...</div>