Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dashboard header using CSS Flexbox

Tags:

I want to create something like this using CSS Flexbox:

enter image description here

I usually achieve this using floats and lots of positioning tweaks, adding margins, padding, percentage values on top, bottom, left and right of the elements to adjust it to a certain viewport, but it seem to break-off in different viewports.

I know it is not right way, I end up writing too much CSS and still doesn't look consistent on different viewports.

Getting the Logo perfectly on the center and aligning them vertically is where I'm struggling.

Here is the HTML code.

<nav class="navbar">
    <div class="sidebar-toggle">
        <button class="sidebar-toggler" type="button">☰</button>
    </div>
    <div class="headerLogo">
        <a href="" class="logo">Logo</a>
    </div>
    <div class="headerLinks">
        <a href="">link1</a>
        <a href="">link2</a>
    </div>
    <div class="notifications">
        <div class="mailIcon"></div>
    </div>    
</nav>
like image 427
anoop chandran Avatar asked Aug 25 '16 15:08

anoop chandran


People also ask

Can you use flexbox for header?

Flexbox in ActionWhen flexbox is applied to the header element, it will make all the child items in the same line. Then, all you need is to apply justify-content to distribute the spacing between them.

Can you use flexbox in CSS?

The flex container As with all properties in CSS, some initial values are defined, so when creating a flex container all of the contained flex items will behave in the following way. Items display in a row (the flex-direction property's default is row ). The items start from the start edge of the main axis.

Can you use display Grid and flexbox together?

Of course CSS grid and flexbox can work together in a layout. You can use flexbox inside CSS grid and vice versa. For instance, you could use flexbox to center an element like a button both vertically and horizontally within a particular grid cell (since centering with other CSS methods is … tricky).

Is flexbox better than CSS grid?

The main difference is that you can use CSS Grid to create two-dimensional layouts. In contrast, you can only use Flexbox to create one-dimensional layouts. That means you can place components along the X- and Y-axis in CSS Grid and only one axis in Flexbox. Let's walk through each model below starting with CSS Grid.


2 Answers

You can just use justify-content: space-between and get right spacing and align-items: center for vertical align.

.navbar,
.links {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
<nav class="navbar">
  <div class="sidebar-toggle">
    <button class="sidebar-toggler" type="button">☰</button>
  </div>
  <div class="headerLogo">
    <a href="" class="logo">Logo</a>
  </div>
  <div class="links">
    <div class="headerLinks">
      <a href="">link1</a>
      <a href="">link2</a>
    </div>
    <div class="notifications">
      <div class="mailIcon">Mail</div>
    </div>
  </div>
</nav>

If you want logo perfectly centered then you need to remove it from elements flow with position: absolute;

.navbar,
.links {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}
.headerLogo {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<nav class="navbar">
  <div class="sidebar-toggle">
    <button class="sidebar-toggler" type="button">☰</button>
  </div>
  <div class="headerLogo">
    <a href="" class="logo">Logo</a>
  </div>
  <div class="links">
    <div class="headerLinks">
      <a href="">link1</a>
      <a href="">link2</a>
    </div>
    <div class="notifications">
      <div class="mailIcon">Mail</div>
    </div>
  </div>
</nav>
like image 189
Nenad Vracar Avatar answered Sep 25 '22 16:09

Nenad Vracar


I've slightly modified your markup to achieve the logo centered. There is no need at all to use absolute positioning. The idea is to end up with 3 containers of the same width equaly distributed along the main axis (row), so that the logo can fall just in the middle of the layout.

.navbar{
    display:flex;
    align-items:center;
}

.sidebar-toggle{
    display:flex;
    flex-basis:33.3%
}

.headerLogo{
    display:flex;
    justify-content:center;
    flex-basis:33.3%
}

.headerLinks{
    display:flex;
    justify-content:space-around;
    flex-basis:33.3%
}

I've included the .mailIcon div in the third div. You can distribute its content internally using similar styling if you need to.

<nav class="navbar">
    <div class="sidebar-toggle">
        <button class="sidebar-toggler" type="button">☰</button>
    </div>
    <div class="headerLogo">
        <a href="" class="logo">Logo</a>
    </div>
    <div class="headerLinks">
        <a href="">link1</a>
        <a href="">link2</a>
        <div class="mailIcon"></div>
    </div>
</nav>

You can play around with flexbox with this toy I've created: http://algid.com/Flex-Designer

like image 41
Mario Vázquez Avatar answered Sep 25 '22 16:09

Mario Vázquez