Search code examples
htmlcssweb

CSS - Changing "article" margins affects my top navbar placement too - how to fix?


I'm teaching myself HTML and CSS and building my first site. I'm trying to make my homepage have a centered article section, with a fixed navbar at the top of the page. When I add margins to my "article" section in my stylesheet, it moves the material in the article section, but it also moves the navbar down with it too. That doesn't seem right, and I can't figure out how to keep the navbar in place.

I've tried looking for any conflicting or redundant lines in CSS through some trial-and-error, and even moved the "article" section closer to the top of the stylesheet in case there was some kind of cascading problem I wasn't seeing, but I haven't found a solution yet.

I should mention for my navbar, I was previously using "ul" items for the navbar in HTML and styling it with CSS accordingly based on the W3 schools tutorials on navbars, and I wasn't having any issues with margins. I recently reworked the HTML for the navbar so it used "div" classes instead, and once I did that, I started encountering this problem. Here's the relevant HTML:

<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <title>Site Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="Site Title" content="??????">
    
    <meta property="og:title" content="Site Title">
    <meta property="og:type" content="website">
    <meta property="og:url" content="#">
    <meta property="og:image" content="#">
    
    <link rel="stylesheet" href="styles.css">
     
  </head>

  <body>
    
    <div class="topnav">
      <div class="dropdown">
        <button class="dropbtn">Links</button>
          <div class="dropdown-content">
            <a href=#>Link 1</a>
            <a href=#>Link 2</a>
            <a href=#>Link 3</a>
          </div>
        </div>
      <a href=#>Link 4</a>
      <a href=#>Link 5</a>
      <a href=#>Link 6</a>
      <a href=#>Link 7</a>
      <a target=# class="split" href="#">
        <img src="#"></a>
    </div>

  <main>
        
    <article>
      <img src="#" alt="#" style="max-width:100%">

      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p>
        
      <span>Date</span>
      <p>-----------------------</p>
    </article>

And the relevant CSS (I'm including the navbar code just in case there are any conflicts in there):

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, 
pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article,
aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, 
output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    background-color: #bfa485;

}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
    
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

/* Diagnosis CSS for broken stuff */
div:empty, span:empty,
li:empty, p:empty,
td:empty, th:empty {padding: 0.5em; background: yellow;}

*[style], font, center {outline: 5px solid red;}
*[class=""], *[id=""] {outline: 5px dotted red;}

img[alt=""] {border: 3px dotted red;}
img:not([alt]) {border: 5px solid red;}
img[title=""] {outline: 3px dotted fuchsia;}
img:not([title]) {outline: 5px solid fuchsia;}

table:not([summary]) {outline: 5px solid red;}
table[summary=""] {outline: 3px dotted red;}
th {border: 2px solid red;}
th[scope="col"], th[scope="row"] {border: none;}

a[title=""] {outline: 3px dotted red;}
a[href="#"] {background: lime;}
a[href=""] {background: fuchsia;}

/* || typography */
body {
  overflow-x: auto;  
}

article {
  text-align: center;
}

h1, h2, h3 {
  font-weight: normal;
  font-style: normal;
  color: #301f1a;
  margin-top: 1.5em;
  text-align: left;
}

p, input {
  margin: 2em 7em auto;
  color: #301f1a;
}

.topnav {
  position: fixed;
  width: 100%;
  background-color: #bfa485;
  margin: 0em auto;
  color: #301f1a;
}

/* Style the links inside the navigation bar */
.topnav a {
  float: right;
  display: block;
  color: #301f1a;
  text-align: center;
  padding: 0px 16px 0px; 
  text-decoration: none;
}

/* Create a left-aligned (split) link inside the navigation bar */
.topnav a.split {
  float: left;
  background-color: #bfa485;
  color: #301f1a;
  
}

/* Style The Dropdown Button */
.dropdown .dropbtn {
  color: #301f1a;
  background-color: #bfa485;
  padding: 10px;
  border: none;
  cursor: pointer;
  outline: none;
  margin: 0;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  float: right;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #bfa485;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  float: none;
  color: #333;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

Solution

  • Lets Start from the beginning, Your Navbar...

    Faulty code:

    Your Code

    Expected Outcome:

    enter image description here

    Firstly I changed the display of your nav to flex. This breaks your order, so i fixed it using flex-direction: row-reverse and added a margin of auto to your image.

    Now your buttons will look weird, they are not centered. Lets fix them using justify-content: center and align-items: center. After this fix we can remove text-align: center; as it is redundant

    .topnav a {
      text-align: center;
      display: flex;
      justify-content: center; /* <--- */
      align-items: center; /* <--- */
      color: #301f1a;
      /* text-align: center; */
      padding: 0px 16px 0px; 
      text-decoration: none;
    }
    
    .topnav {
      position: fixed;
      width: 100%;
      background-color: #bfa485;
      margin: 0em auto;
      color: #301f1a;
      display: flex; /* <--- */
      flex-direction: row-reverse /* <--- */
    }
    
    .topnav a.split {
      background-color: #bfa485;
      color: #301f1a;
      margin-right: auto
    }
    

    Your Navbar is now fixed. Here, This is your new CSS code.

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, 
    pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article,
    aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, 
    output, ruby, section, summary, time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
        background-color: #bfa485;
    
    }
    
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
        display: block;
    }
    body {
        line-height: 1;
        
    }
    ol, ul {
        list-style: none;
    }
    blockquote, q {
        quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
    
    /* Diagnosis CSS for broken stuff */
    div:empty, span:empty,
    li:empty, p:empty,
    td:empty, th:empty {padding: 0.5em; background: yellow;}
    
    *[style], font, center {outline: 5px solid red;}
    *[class=""], *[id=""] {outline: 5px dotted red;}
    
    img[alt=""] {border: 3px dotted red;}
    img:not([alt]) {border: 5px solid red;}
    img[title=""] {outline: 3px dotted fuchsia;}
    img:not([title]) {outline: 5px solid fuchsia;}
    
    table:not([summary]) {outline: 5px solid red;}
    table[summary=""] {outline: 3px dotted red;}
    th {border: 2px solid red;}
    th[scope="col"], th[scope="row"] {border: none;}
    
    a[title=""] {outline: 3px dotted red;}
    a[href="#"] {background: lime;}
    a[href=""] {background: fuchsia;}
    
    /* || typography */
    body {
      overflow-x: auto;  
    }
    
    article {
      text-align: center;
    }
    
    h1, h2, h3 {
      font-weight: normal;
      font-style: normal;
      color: #301f1a;
      margin-top: 1.5em;
      text-align: left;
    }
    
    p, input {
      margin: 2em 7em auto;
      color: #301f1a;
    }
    
    .topnav {
      position: fixed;
      width: 100%;
      background-color: #bfa485;
      margin: 0em auto;
      color: #301f1a;
      display: flex; /* <--- */
      flex-direction: row-reverse /* <--- */
    }
    
    /* Style the links inside the navigation bar */
    .topnav a {
      text-align: center;
      display: flex;
      justify-content: center; /* <--- */
      align-items: center; /* <--- */
      color: #301f1a;
      /* text-align: center; */
      padding: 0px 16px 0px; 
      text-decoration: none;
    }
    
    /* Create a left-aligned (split) link inside the navigation bar */
    .topnav a.split {
      background-color: #bfa485;
      color: #301f1a;
      margin-right: auto
    }
    
    /* Style The Dropdown Button */
    .dropdown .dropbtn {
      color: #301f1a;
      background-color: #bfa485;
      padding: 10px;
      border: none;
      cursor: pointer;
      outline: none;
      margin: 0;
    }
    
    /* The container <div> - needed to position the dropdown content */
    .dropdown {
      float: right;
    }
    
    /* Dropdown Content (Hidden by Default) */
    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #bfa485;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
    
    /* Links inside the dropdown */
    .dropdown-content a {
      float: none;
      color: #333;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
      text-align: left;
    }
    
    /* Show the dropdown menu on hover */
    .dropdown:hover .dropdown-content {
      display: block;
    }
    

    page

    Now that your navbar is fixed, Lets deal with your issue of navbar obstructing the image.

    To the .topnav class, lets give it a top property of 0.

    .topnav {
      position: fixed;
      width: 100%;
      top: 0;/* <--- */
      background-color: #bfa485;
      margin: 0em auto;
      color: #301f1a;
      display: flex; 
      flex-direction: row-reverse;
    }
    

    Now this ensures that your .topnav div of position fixed always stays on top

    Here is a Codepen If you need it