skip to Main Content

I’m new with HTMLL/CSS and I’m trying to create a layout in and I (think) calculated perfectly the widths and heights of all my divs but the website seems to be is scrollable horizontally which is undesirable. Do you know why?
The desired output is a fixed sidebar (not scrollable), a header (also fixed) and a body (which will be scrollable)

#left-content {
  width: 15vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  border: 1px solid black;
}

#right-content {
  width: 85vw;
  height: 100vh;
  position: absolute;
  top: 0;
  left: 15vw;
  border: 1px solid black;
}

#header {
  position: fixed;
  height: 10vh;
  width: 100%;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  align-content: center;
  text-align: center;
  border: 1px solid black;
}

#center-content-footer {
  height: 100vh - 10vh;
  width: 100%;
  position: absolute;
  top: 10vh;
  left: 0;
  border: 1px solid black;
}

#center-content {
  height: 100vh - 10vh;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border: 1px solid black;
}

#footer {
  height: 10vh;
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border: 1px solid black;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="index.css">
</head>

<body>

  <div id="left-content">
  </div>

  <div id="right-content">
    <div id="header">
      <h1>Header</h1>
    </div>

    <div id="center-content-footer">
      <div id="center-content">

      </div>

    </div>
  </div>



</body>

</html>

6

Answers


  1. Border widths are not included in the width property, so if you add borders you need to subtract that amount from with width. I removed all of the borders and it seems to be working as expected, but you could leave the borders there and change width of those elements to width: calc(85vw - 2px), and that should do the same thing. Also, I added a calc() function around the #center-content-footer and #center-content height, but that wasn’t really breaking anything.

    #left-content {
      width: 15vw;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 0;
    }
    
    #right-content {
      width: 85vw;
      height: 100vh;
      position: absolute;
      top: 0;
      left: 15vw;
    }
    
    #header {
      position: fixed;
      height: 10vh;
      width: 100%;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      align-content: center;
      text-align: center;
    }
    
    #center-content-footer {
      height: calc(100vh - 10vh);
      width: 100%;
      position: absolute;
      top: 10vh;
      left: 0;
    }
    
    #center-content {
      height: calc(100vh - 10vh);
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    
    #footer {
      height: 10vh;
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="index.css">
    </head>
    
    <body>
    
      <div id="left-content">
        <ul>
          <li>left text</li>
          <li>left text</li>
          <li>left text</li>
          <li>left text</li>
          <li>left text</li>
          <li>left text</li>
        </ul>
      </div>
    
      <div id="right-content">
        <div id="header">
          <h1>Header</h1>
        </div>
    
        <div id="center-content-footer">
          <div id="center-content">
            <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>
            
            <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>
            
            <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>
            
            <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>
    
          </div>
    
        </div>
      </div>
    
    
    
    </body>
    
    </html>
    Login or Signup to reply.
  2. Here are my changes. But this solution is far from perfect.

    Quick Tip: Try to use e.g. google chromes or firefox dev tools. There you can see pretty good which rule is or isn’t applied.

    #right-content {
      position: fixed;
    }
    
    #center-content-footer {
      height: 100%;
      overflow: auto;
    }
    
    #center-content {
      // height: 100vh - 10vh;
    }
    
    #left-content {
      width: 15vw;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 0;
      border: 1px solid black;
    }
    
    #right-content {
      width: 85vw;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 15vw;
      border: 1px solid black;
    }
    
    #header {
      position: fixed;
      height: 10vh;
      width: 100%;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      align-content: center;
      text-align: center;
      border: 1px solid black;
    }
    
    #center-content-footer {
      height: 100%;
      width: 100%;
      overflow: auto;
      position: absolute;
      top: 10vh;
      left: 0;
      border: 1px solid black;
    }
    
    #center-content {
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      border: 1px solid black;
    }
    
    #footer {
      height: 10vh;
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      border: 1px solid black;
    }
    
    span {
      display: block;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="index.css">
    </head>
    
    <body>
      <div id="left-content">
      </div>
    
      <div id="right-content">
        <div id="header">
          <h1>Header</h1>
        </div>
        <div id="center-content-footer">
          <div id="center-content">
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
            <span>Lorem Ipsum</span>
    
          </div>
        </div>
      </div>
    </body>
    
    </html>
    Login or Signup to reply.
  3. this is because body tag has default margin or padding try to apply padding:0 and margin:0 to body

    Login or Signup to reply.
  4. Your code is perfectly fine. Just add this to the CSS:

    * {
      box-sizing: border-box;
    }
    

    This will include the padding and the border-width in the width of the element. Without this, the padding and border-width are added to the width you set. That’s the reason why you get the unexpected little scrolling.
    You don’t need to set position: fixed on #right-content or to slightly adjust the widths you already set.
    Read more about box-sizing.

    Login or Signup to reply.
  5. As said by woundedstevenjones, it comes with the borders not being taken into account on width and height. Using calc() to have the right size is appropriate, but a simpler solution would be to use box-sizing: border-box; instead.

    The box-sizing CSS property sets how the total width and height of an
    element is calculated.

    https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

    /* To simplify example, I have use the "star" selector to let the property beeing apply for all element. It shouldn't be appropriate in other contexts */
    * {
      box-sizing: border-box;
    }
    
    #left-content {
      width: 15vw;
      height: 100vh;
      position: fixed;
      top: 0;
      left: 0;
      border: 1px solid black;
    }
    
    #right-content {
      width: 85vw;
      height: 100vh;
      position: absolute;
      top: 0;
      left: 15vw;
      border: 1px solid black;
    }
    
    #header {
      position: fixed;
      height: 10vh;
      width: 100%;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      align-content: center;
      text-align: center;
      border: 1px solid black;
    }
    
    #center-content-footer {
      height: 100vh - 10vh;
      width: 100%;
      position: absolute;
      top: 10vh;
      left: 0;
      border: 1px solid black;
    }
    
    #center-content {
      height: 100vh - 10vh;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      border: 1px solid black;
    }
    
    #footer {
      height: 10vh;
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      border: 1px solid black;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="index.css">
    </head>
    
    <body>
    
      <div id="left-content">
      </div>
    
      <div id="right-content">
        <div id="header">
          <h1>Header</h1>
        </div>
    
        <div id="center-content-footer">
          <div id="center-content">
    
          </div>
    
        </div>
      </div>
    
    
    
    </body>
    
    </html>
    Login or Signup to reply.
  6. You may relay on a grid system to avoid position, sizing and box-sizing.
    A reset on body’s margin would be necessary (unless otherwise, see second snippet).

    I would also recommand to use tags (HTML5) with a meaning, so the code is easier to read too.

    body {
      display: grid;
      grid-template-rows: auto 1fr auto;
      height: 100vh;
      margin: 0;
    }
    main {
      display: grid;
      grid-template-columns:200px 1fr;
      overflow:hidden;
    }
    main section {
      overflow:auto;
      order:1;
    }
    main aside {
    }
    header,aside,section,footer {
      border:solid;
      margin:5px;
    }
    
    /*demo */
    body:hover :is(header,aside,section,footer) {padding:1em}
    p.demo {display:none;background:silver}
    section:hover p.demo {
      display:revert;
    }
    <header> header</header>
    <main>
      <section>Content 
      <p class="demo"><br>see the scrollbar<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></p>
      End</section>
      <aside>aside</aside>
    </main>
    <footer>footer</footer>

    No margin reset on body through grid or flex

    html {
      display: grid;
      height: 100vh;
    }
    /* uncomment to test  */
    /* a grid of a single cell(body her) works. it can also be flex *//*
    html:hover {
      display: flex;
    }
    but requires to set body on full avalaible width
    body {
      flex-grow:1;
    }
    */
     
    body {
      display: grid;
      grid-template-rows: auto 1fr auto;
    }
    
    main {
      display: grid;
      grid-template-columns: 200px 1fr;
      overflow: hidden;
    }
    
    main section {
      overflow: auto;
      order: 1;
    }
    
    main aside {}
    
    header,
    aside,
    section,
    footer {
      border: solid;
      margin: 5px;
    }
    
    
    /*demo */
    
    body:hover :is(header, aside, section, footer) {
      padding: 1em
    }
    
    p.demo {
      display: none;
      background: silver
    }
    
    section:hover p.demo {
      display: revert;
    }
    <header> header</header>
    <main>
      <section>Content
        <p class="demo"><br>see the scrollbar<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></p>
        End</section>
      <aside>aside</aside>
    </main>
    <footer>footer</footer>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search