0

This is my code so far. The issue is that the top bar is too thin, it's meant to have 20px padding around the top links but in total the top bar is only 40px high, so it seems it isn't taking into account the height of the links themselves (i.e the bar should be taller). How do I fix this?

    body {
      padding: 0;
      margin: 0;
      background-color: white;
      font-family: sans-serif;
    }
    
    header {
      display: block;
      padding: 20px;
      background-color: #F9F9F9;
    }
    
    #headerbar {
      position: relative;
    }
    
    .container {
      width: 80%;
      max-width: 1000px;
      margin: 0 auto;
    }
    
    .logo {
      position: absolute;
      left: 0;
    }
    
    .post {
      position: absolute;
      right: 0;
    }
   <header>
      <div id="headerbar" class="container">
        <a href="#" class="logo">Logo</a>
        <a href="#" class="post">Post</a>
      </div>
    </header>
    
Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • 1
    Because logo is `position: absolute` it's essentially floating on top of the header, not in it - therefore padding is working as it should – StudioTime Jan 26 '16 at 07:50
  • @DarrenSweeney Is there a way to make the padding or a margin apply? Is the only option to use floats or specify a height? – Jimmy Jan 26 '16 at 07:51

4 Answers4

1

As @Darren said in the comment, the issue lies in absolute positioning (See this answer for more details how it works). One way to fix this is to use float, and clear after it.

Clearfix styles from HTML5Boilerplate:

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

    body {
      padding: 0;
      margin: 0;
      background-color: white;
      font-family: sans-serif;
    }
    
    header {
      display: block;
      padding: 20px;
      background-color: #F9F9F9;
    }
    
    #headerbar {
      position: relative;
    }
    
    .container {
      width: 80%;
      max-width: 1000px;
      margin: 0 auto;
    }
    
    .logo {
      float:left;
    }
    
    .post {
      float:right;
    }
    
    .clearfix:before,
    .clearfix:after {
        content: " "; /* 1 */
        display: table; /* 2 */
    }

    .clearfix:after {
        clear: both;
    }
   <header>
      <div id="headerbar" class="container clearfix">
        <a href="#" class="logo">Logo</a>
        <a href="#" class="post">Post</a>
      </div>
    </header>

JSFiddle

Community
  • 1
  • 1
Vucko
  • 20,555
  • 10
  • 56
  • 107
0

Changes to .logo and .post classes

    body {
      padding: 0;
      margin: 0;
      background-color: white;
      font-family: sans-serif;
    }
    
    header {
      display: block;
      padding: 20px;
      background-color: #F9F9F9;
    }
    
    #headerbar {
      position: relative;
    }
    
    .container {
      width: 80%;
      max-width: 1000px;
      margin: 0 auto;
    }
    
    .logo {
      position: relative;
    }
    
    .post {
      position: relative;
      float: right;
    }
   <header>
      <div id="headerbar" class="container">
        <a href="#" class="logo">Logo</a>
        <a href="#" class="post">Post</a>
      </div>
    </header>
    
StudioTime
  • 22,603
  • 38
  • 120
  • 207
0

body {
      padding: 0;
      margin: 0;
      background-color: white;
      font-family: sans-serif;
    }
    
    header {
      display: block;
     
      background-color: #F9F9F9;
    }
    
    #headerbar {
      
    padding: 20px;
      position: relative;
    }
    
    .container {
      width: 80%;
      max-width: 1000px;
      margin: 0 auto;
    }
    
    .logo {
      padding-bottom:50px;
      
     
    }
    
    .post {
      float:right;
    }
<header>
      <div id="headerbar" class="container">
        <a href="#" class="logo">Logo</a>
        <a href="#" class="post">Post</a>
      </div>
    </header>
code
  • 2,115
  • 1
  • 22
  • 46
0

On a side note, I do not see any box sizing, font sizes and line heights added. These should all be working together to help correct spacing issues throughout devices.

Jamie Paterson
  • 426
  • 3
  • 10