htmlcsscss-floatwidthclearfix

Element widths not aligning themselves properly


I'm really struggling here trying to figure out what is going on. I have an HTML with a header, a sidebar, and a central content page.

The sidebar and central content are in the same div, which also acts as their clearfix. I floated the sidebar to the left and the content to the right, but instead of aligning themselves to each other neatly, the content div falls down.

HTML

<body>
        <header>
            <img src="./img/Logo_Color.png" alt="Logo">
            <h1>Batch Documentation</h1>
        </header>
        <div class="clearfix">
            <div class="sidebar">
                <nav>
                    <ul>
                        <li><a href="#">Overview</a></li>
                        <li><a href="#">Fl Overview</a></li>
                        <li><a href="#">PF2 Overview</a></li>
                        <li><a href="#">Inputs</a></li>
                        <li><a href="#">Outputs</a></li>
                        <li><a href="#">Appendix A</a></li>
                        <li><a href="#">Appendix B</a></li>
                    </ul>
                </nav>
            </div>
            <main>    

            <div class="centerContent">
                <h2>Overview</h2>
                <p>
                    Integer in qui primam te ipsa-plenus Crescere Effectum Septembrem.  Me quo 700% octavas ad imperium caduca si eros eius orci arcu te mirum consumere, meritos modo diam eros ti, in cras diuturna interpres, semente, securitas sem odio dignitatis reuiescat.
                    Lius quam charybdium nonullis sem inibunt illum, civibus eius arendom, Indolem te e licentiose te maiestatem molestias typi combinatur sagittis successus nomine, reniam eos te-feroces assueverunt.
                    Saepe non, Fervore 2000 galliae nibh eu ea ut:
                </p>
                <code>Hello</code>
            </div>
            </main>
        </div>
    </body>

CSS

* {
    font-family: 'Roboto', sans-serif;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    list-style-type: none;

  }

body {
  margin: auto;
  width: 1265px;
  background-color: #eae0ff;
}

main {
  display: inline-block
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

header {
  margin: 15px 10px 20px 10px;
}

.sidebar {
  width: 25%;
  margin: 5px 0px 10px 10px;
  padding-right: 20px;
  float: left;
  background-color: #ccccff;
}

.centerContent {

  width: 75%;
  margin: auto;
  padding-left: 20px;
  border: 3px solid #73ad21;
  float: right;
}

li {
  margin-top: 5px;
  margin-bottom: 5px;
}

code {
  width: 90%;
  font-family: 'Source Code Pro', monospace;
  color: #43892a;
  background-color: #000;
  display: block;
}

I am especially concerned because the box sizing is set to border box, and the display is inline block. The sidebar has 25% width whereas the main content has 75%, yet it seems that the margins and padding are being added to their dimensions rather than being included in the % calculation.


Solution

  • I Dont know why you using floats now, i attached a same with small flexbox layout. hope it helps.

    * {
      font-family: 'Roboto', sans-serif;
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    
    body {
      margin: auto;
      background-color: #eae0ff;
    }
    
    main {
      display: inline-block
    }
    
    .clearfix{
    display:flex;
    }
    
    header {
      margin: 15px 10px 20px 10px;
    }
    
    .sidebar {
      width: 25%;
      margin: 0px 0px 10px 10px;
      padding-right: 20px;
      background-color: #ccccff;
      flex: 0 0 auto;
    }
    
    .centerContent {
      width: 75%;
      margin: auto;
      padding-left: 20px;
      border: 3px solid #73ad21;
    }
    
    li {
      margin-top: 5px;
      margin-bottom: 5px;
    }
    
    code {
      width: 90%;
      font-family: 'Source Code Pro', monospace;
      color: #43892a;
      background-color: #000;
      display: block;
    }
    <body>
      <header>
        <img src="./img/Logo_Color.png" alt="Logo">
        <h1>Batch Documentation</h1>
      </header>
      <div class="clearfix">
        <div class="sidebar">
          <nav>
            <ul>
              <li><a href="#">Overview</a></li>
              <li><a href="#">Fl Overview</a></li>
              <li><a href="#">PF2 Overview</a></li>
              <li><a href="#">Inputs</a></li>
              <li><a href="#">Outputs</a></li>
              <li><a href="#">Appendix A</a></li>
              <li><a href="#">Appendix B</a></li>
            </ul>
          </nav>
        </div>
        <main>
    
          <div class="centerContent">
            <h2>Overview</h2>
            <p>
              Integer in qui primam te ipsa-plenus Crescere Effectum Septembrem. Me quo 700% octavas ad imperium caduca si eros eius orci arcu te mirum consumere, meritos modo diam eros ti, in cras diuturna interpres, semente, securitas sem odio dignitatis reuiescat.
              Lius quam charybdium nonullis sem inibunt illum, civibus eius arendom, Indolem te e licentiose te maiestatem molestias typi combinatur sagittis successus nomine, reniam eos te-feroces assueverunt. Saepe non, Fervore 2000 galliae nibh eu ea ut:
            </p>
            <code>Hello</code>
          </div>
        </main>
      </div>
    </body>