11

I'm trying to align a top menu which consists of 3 blocks of content.

What I'm trying to achieve is this:

  • block 1: left aligned
  • block 2: centered horizontally
  • block 3: right aligned

If all 3 blocks were the same size, I could use flexbox (as in the snippet), but they're not, so it doesn't produce the output I require.

Instead, flexbox puts equal space between the 3 blocks - resulting in the middle block being aligned off-center.

I was wondering if this could be achieved with flexbox, or if not, another solution. This needs to work robustly in production so a 'Grid' solution is not applicable as there is insufficient support.

.container {
  margin: 20px 0;
}

.row {
  background-color: lime;
  display: flex;
  justify-content: space-between;
}

.item {
  background-color: blue;
  color: #fff;
  padding: 16px;
}
<div class="container">
  <div class="row">
    <div class="item">left, slightly longer</div>
    <div class="item">center, this item is much longer</div>
    <div class="item">right</div>
  </div>
</div>
John Ohara
  • 2,821
  • 3
  • 28
  • 54
  • I've looked at the 'duplicate' answers. This is not the same scenario. All the flex and auto margin solutions in the post rely on 'same-size' blocks, which is a key difference here. Grid isn't supported enough. – John Ohara Mar 28 '19 at 08:52
  • can you give "flex-grow: 1;" for the ".item" class and check – Rajesh Mar 28 '19 at 09:02
  • I had [the same question](https://stackoverflow.com/questions/60576458/left-center-right-align-three-elements-with-variable-width-in-container) and came across the excellent [In CSS Flexbox, why are there no “justify-items” and “justify-self” properties?](https://stackoverflow.com/questions/32551291/in-css-flexbox-why-are-there-no-justify-items-and-justify-self-properties), worth a read and with answers providing several solutions different from [the currently accepted answer below](https://stackoverflow.com/a/55393886/1619432). – handle Mar 07 '20 at 10:52

4 Answers4

12

You can consider flex-grow:1;flex-basis:0% for the left and right elements then use text-align to align content inside. I have added an extra wrapper to keep the background only around the text.

The trick is to calculate the free space by removing only the middle content and split it equally to the left and right element.

.container {
  margin: 20px 0;
  padding-top:10px;
  background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/
}

.row {
  background-color: lime;
  display: flex;
  color: #fff;
}

.item:not(:nth-child(2)) {
  flex-basis: 0%;
  flex-grow: 1;
}

.item:last-child {
  text-align: right;
}

.item span{
  background-color: blue;
  display:inline-block;
  padding: 16px;
  border: 2px solid red;
  box-sizing:border-box;
}
<div class="container">
  <div class="row">
    <div class="item"><span>left, slightly longer</span></div>
    <div class="item"><span>center, this item is much longer</span></div>
    <div class="item"><span>right</span></div>
  </div>
</div>

You can also do the same by keeping the element close. Simply adjust text-align:

.container {
  margin: 20px 0;
  padding-top: 10px;
  background: linear-gradient(#000, #000) center/5px 100% no-repeat; /*the center*/
}

.row {
  background-color: lime;
  display: flex;
  color: #fff;
}

.item:not(:nth-child(2)) {
  flex-basis: 0%;
  flex-grow: 1;
}

.item:first-child {
  text-align: right;
}

.item span {
  background-color: blue;
  display: inline-block;
  padding: 16px;
  border: 2px solid red;
  box-sizing: border-box;
}
<div class="container">
  <div class="row">
    <div class="item"><span>left, slightly longer</span></div>
    <div class="item"><span>center, this item is much longer</span></div>
    <div class="item"><span>right</span></div>
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
1

I asked what seems to be a very similar question and stack overflow directed me here. The response from @Paolamoralesval inspired me to realise the required effect can be achieved in CSS grid. Now that grid support is pretty much universal I hope that this meets everyone's needs. This solution is I believe fully responsive to window size as well as height and width of the header items as you should see if you resize the window where you view the snippet.

.header {
  grid-row: 1;
  grid-column: 1;
  display: grid;
  grid-template-rows: min-content;
  grid-template-columns: 1fr 1fr 1fr;
}
.header-left {
  justify-self: start;
  align-self: center;
  text-align: left;
  background-color: red;
}
.header-center {
  justify-self: center;
  align-self: center;
  text-align: center;
  background-color: green;
}
.header-right {
  justify-self: end;
  align-self: center;
  text-align: right;
  background-color: blue;
}
.shrink-kitty {
    width: 200px;
}
<html>
  <body>
    <div class="page">
      <div class="header">
          <div class="header-left">
            <img class="shrink-kitty" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1280px-Kittyply_edit1.jpg"/><br/>
            By David Corby<br/>
            Edited by: <a href="//commons.wikimedia.org/wiki/User:Arad" title="User:Arad">Arad</a><br/><a href="//commons.wikimedia.org/wiki/File:Kittyplya03042006.JPG" title="File:Kittyplya03042006.JPG">Image:Kittyplya03042006.JPG<a><br/><a href="https://creativecommons.org/licenses/by/2.5" title="Creative Commons Attribution 2.5">CC BY 2.5</a>, <a href="https://commons.wikimedia.org/w/index.php?curid=1839754">Link</a>
          </div>
          <div class="header-center">In the middle</div>
          <div class="header-right">
            Much much much much more on the right hand side</br>
            Indeed two lines
          </div>
      </div>
      <div class="body">Body of the page</div>
      <div class="footer">At the bottom</div>
    </div>
  </body>
</html>
Iain MacKay
  • 319
  • 3
  • 7
0

can you give flex-grow:1 for the item class and check

.item {
background-color: blue;
color: #fff;
padding: 16px;
flex-grow:1;
}

Hope this is what you are looking for

Rajesh
  • 254
  • 1
  • 6
-1

Alternative using display table (an ancient supported grid).

Quote from https://www.w3schools.com/cssref/pr_tab_table-layout.asp

If no widths are present on the first row, the column widths are divided equally across the table, regardless of content inside the cells

.container {
   display: table;
   table-layout: fixed
 } // would divide cells equally along table's 100% width. 
.row {
   display: table-row
 }
.item {
   display: table-cell
 }