Chris Barr
Chris Barr

Reputation: 34012

Horizontal centering a flexbox item, with an uneven number of items

I've got a flex container, with a logo and some links. I want the logo to be centered, and the links over on the right. The best I can seem to do is center the logo in the remaining space, which is not centered on the container.

Here are some of my attempts so far:

.header{background:#CCC;padding:1rem; margin-bottom:2rem;}
.logo{background:skyblue;text-align:center;height:30px;width:200px;}

/*-----------------------*/

#header1 {
  display: flex;
  justify-content: space-between;
  align-items: center;  
}
#header1-logo {
  justify-self: center;
}
#header1-nav {
  justify-self: flex-end;
}

/*-----------------------*/

#header2 {
  display: flex;
  justify-content: center;
  align-items: center;
}
#header2-logo {
  margin: 0 auto;
}
<header id="header1" class="header">
  <div id="header1-logo" class="logo">LOGO</div>
  <nav id="header1-nav">
    <a href="#">foo</a>
    <a href="#">bar</a>
    <a href="#">baz</a>
  </nav>
</header>

<header id="header2" class="header">
  <div id="header2-logo" class="logo">LOGO</div>
  <nav id="header2-nav">
    <a href="#">foo</a>
    <a href="#">bar</a>
    <a href="#">baz</a>
  </nav>
</header>

How can I center the logo in the actual center of the flex container?

Upvotes: 2

Views: 2012

Answers (2)

Adam Lichter
Adam Lichter

Reputation: 140

You're pretty close I think. I've had good success in the past just adding a "blank-element" with a flex of 1 before the logo element. The logo, and the links element should also have a flex of 1, this will ensure that they all take up the same amount of space on the screen (1/3rd)

#header {
  display: flex;
  justify-content: center;
  align-items: center;
  border: grey solid 2px;
}
#header-logo {
  flex: 1;
  margin: 0 auto;
  border: black solid 2px;
}
#blank-Element {
  flex:1;
}
#header-nav {
  flex:1;
  // other styles here

}
<header id="header" >
  <div id="blank-Element"></div> 
  <div id="header-logo">LOGO</div>
    <nav id="header-nav">
      <a href="#">foo</a>
      <a href="#">bar</a>
      <a href="#">baz</a>
    </nav>
</header>

edit: left in the borders so you can see what it's doing easily

Check here for all sorts of good flexbox tips: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Upvotes: 1

coops
coops

Reputation: 1714

Not using flexbox exclusively, but an option would be to absolute position the logo?

.header{background:#CCC;padding:1rem; margin-bottom:2rem;}
.logo{background:skyblue;text-align:center;height:30px;width:200px;}

/*-----------------------*/

#header1 {
  display: flex;
  justify-content: space-between;
  align-items: center;  
}
#header1-logo {
  position: absolute;
  left:0;
  right:0;
  margin:auto;
}
#header1-nav {
  margin-left:auto;
}
<header id="header1" class="header">
  <div id="header1-logo" class="logo">LOGO</div>
  <nav id="header1-nav">
    <a href="#">foo</a>
    <a href="#">bar</a>
    <a href="#">baz</a>
  </nav>
</header>

Upvotes: 2

Related Questions