1

I'm new to web development and I'm trying to do a bit of exercise with flex which i want to try the header of this:

...but somehow I can't make the header have a white background:

body {
  background: gray;
}

header {
  background: white;
}

ul.header-nav {
  list-style: none;
  display: flex;
}

ul.header-nav.left {
  float: left;
}

ul.header-nav.right {
  float: right;
}
<header>
  <nav>
    <ul class="header-nav left">
      <li>Home</li>
      <li>Plans</li>
      <li>Products</li>
      <li>About</li>
      <li id="gift">Gift</li>
    </ul>
    <ul class="header-nav right">
      <li>Help</li>
      <li>Sign In</li>
      <li>Cart</li>
    </ul>
  </nav>
</header>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Amrin
  • 33
  • 5

3 Answers3

1

Use display:flex instead of float

body{
    background: gray;
}

header{
    background: white;
}

nav{
      display: flex;
}

ul.header-nav{
    list-style: none;
    display: flex;
}

ul.header-nav.left{
    float: none;
}

ul.header-nav.right{
    float: none;
}
<header>
    <nav>
        <ul class="header-nav left">
            <li>Home</li>
            <li>Plans</li>
            <li>Products</li>
            <li>About</li>
            <li id="gift">Gift</li>
        </ul>
        <ul class="header-nav right">
            <li>Help</li>
            <li>Sign In</li>
            <li>Cart</li>
        </ul>
    </nav>
</header>
Hiren Vaghasiya
  • 5,454
  • 1
  • 11
  • 25
1

You don't need float if you already decided to use flex.

body {
  background: gray;
}

header {
  background: white;
}

ul.header-nav {
  list-style: none;
  display: flex;
}

nav {
  display: flex;
  justify-content: space-between;
}
<header>
  <nav>
    <ul class="header-nav">
      <li>Home</li>
      <li>Plans</li>
      <li>Products</li>
      <li>About</li>
      <li id="gift">Gift</li>
    </ul>
    <ul class="header-nav">
      <li>Help</li>
      <li>Sign In</li>
      <li>Cart</li>
    </ul>
  </nav>
</header>
Smollet777
  • 1,618
  • 1
  • 16
  • 15
1

The problem your is not showing the backgroundcolor, is because it's height is 0px. You can use browser dev tools to verify that. Here was a similar problem discussed Why My Css Code Is Not Working For My Html Header And Footer?

Also you don't need to use float after using display:flex .

* {
  margin: 0;
  padding: 0;
}

body { background: gray; }
header { background: white; }

nav {
  display: flex;
  justify-content: space-between;
}

ul.header-nav {
  list-style: none;
  display: flex;
  padding: 20px;
}


ul.header-nav li {
  margin-right:10px;
}
<header>
  <nav>
    <ul class="header-nav left">
      <li>Home</li>
      <li>Plans</li>
      <li>Products</li>
      <li>About</li>
      <li id="gift">Gift</li>
    </ul>
    <ul class="header-nav right">
      <li>Help</li>
      <li>Sign In</li>
      <li>Cart</li>
    </ul>
  </nav>
</header>
turbopasi
  • 3,327
  • 2
  • 16
  • 43