Warm tip: This article is reproduced from stackoverflow.com, please click
css html

How do I position a in-between two in a navigation bar?

发布于 2020-03-27 10:22:51

I'm trying to create a navigation bar at the top the page, with the logo in the centre and a list either side. Each list containing three links to pages in the site.

For some reason, the logo doesn't scroll with the page, but .nav, #left and #right do scroll normally.

The page is live here:

afsmma.com

Here's the code:

.nav {
    margin: auto;
    height: 60px;
    width: 100%;
    max-width: 900px;
    background: #efefef;
    font-family: 'Assistant' , sans-serif;
    font-weight: 700;
}

#left {
    margin: 0;
    padding-top: 17px;
    padding-left: 20px;
    float: left;
    list-style: none;
}

#left li {
    display: inline-block;
    padding-right: 15px;
    font-size: 12pt;
}

.afs {
    position: absolute;
    text-align: center;
    width: 100%;
    max-width: 900px;

}

.afs img {
    margin: 5px auto 0px auto;
    height: 50px;
}

#right {
    float: right;
    margin: 0;
    padding-top: 17px;
    padding-right: 20px;
    list-style: none; 
}

#right li {
    display: inline-block;
    padding-left: 15px;
    font-size: 12pt;
}

.hero {
    height: 100%;
    background: url(/images/ph.jpg) 50% 50% no-repeat;
    background-size: cover;
}
<div class="nav">
  <ul id="left">
      <li>HOME</li>
      <li>EVENTS</li>
      <li>TICKETS</li>
  </ul>

  <div class="afs">
      <img src="images/LogoCyan.svg">
  </div>

  <ul id="right">
      <li>SHOP</li>
      <li>GALLERY</li>
      <li>CONTACT</li>
  </ul>
</div>
<div class="hero"></div>
Questioner
Jack Scott
Viewed
119
Vikas Saini 2019-07-03 22:44

A bit of Flex will solve it easily and very cleanly.

Avoid position:absolute and use flex,

DEMO : https://jsfiddle.net/vikas_saini/v6uxh3sc/1/

CSS:

.nav {
    margin: auto;
    height: 60px;
    width: 100%;
    max-width: 900px;
    background: #efefef;
    display:flex;
    justify-content: space-between;
    font-family: 'Assistant' , sans-serif;
    font-weight: 700;
}

#left {
    margin: 0;
    display:flex;
    padding-top: 17px;
    padding-left: 20px;
    float: left;
    list-style: none;
}

#left li {
    display: inline-block;
    padding-right: 15px;
    font-size: 12pt;
}

.afs {
    flex-grow: 1;  
    text-align: center;
    width: 100%;
    max-width: 900px;

}

.afs img {
    margin: 5px auto 0px auto;
    height: 50px;
}

#right {
    display:flex;
    float: right;
    margin: 0;
    padding-top: 17px;
    padding-right: 20px;
    list-style: none; 
}

#right li {
    display: inline-block;
    padding-left: 15px;
    font-size: 12pt;
}

.hero {
    height: 200vh;
    background: url(/images/ph.jpg) 50% 50% no-repeat;
    background-size: cover;
}

HTML:

<div class="nav">

            <ul id="left">
                <li>HOME</li>
                <li>EVENTS</li>
                <li>TICKETS</li>
            </ul>

            <div class="afs">
                <div class="image-container">
                      <img src="images/LogoCyan.svg">
                </div>
            </div>

            <ul id="right">
                <li>SHOP</li>
                <li>GALLERY</li>
                <li>CONTACT</li>
            </ul>
        </div>
         <div class="hero"></div>