What's new

Closed Css | navigation in display: flex

Status
Not open for further replies.

_iamkurt

Honorary Poster
Joined
Aug 13, 2017
Posts
463
Reaction
308
Points
224
Let us create a basic navigation with just simple understanding on how to use display: flex;

Here is the markup:
HTML:
<nav>
      <a href="#">home</a>
      <a href="#">tech</a>
      <a href="#">news</a>
      <a href="#">media</a>
      <a href="#">contact</a>
</nav>

Here is our CSS rules.
Code:
body{
      padding: 0;
      margin: 0;
}

nav{
      background-color: rgba(0,0,0,0.9);
      color: #fff;
      padding: 0;
      margin: 0;
      display: flex;
      flex-wrap: wrap;
      justify-content:center;
     
      a{
            color: inherit;
            display: inline-block;
            margin: 1rem;
            text-decoration: none;
      }
}

Display flex has been applied to nav element, and all the child element inside the nav, which are the a tags, will be displayed horizontally by default, then apply:

Code:
justify-content:center;

which gives you this layout shown below:
59tLyJ.png
Let us get the spacing done by adding the margin rules to a tag:

Code:
margin: 1rem;

zISBMs.png

If you wish to get the wrapping issue for nav element, see:

lUR1d9.png

Just add this rule below:

Code:
flex-wrap: wrap;

SOYkZd.png

If you wish to see this in action, please see:
You do not have permission to view the full content of this post. Log in or register now.

pwuIMX.png
 

Attachments

Status
Not open for further replies.
Back
Top