How to align elements correctly to each other?












0















So I have a menu item "header" (dark color) on the left side. And on the right side I will have my hero "main" page and bellow "sections". The only scroll I want to have is up/down and no side to side, and I want to center the main / sections pages.



So first "header" item will be:



width: 80px;


And now, I want the other elements such as "main" to stick to that menu item. Currently my problem is that I have to keep changing numbers for "hero" page so it will align correctly at the center, but it's a pain and i cant seem to get the numbers right, i always get either page is like 2px behind the menu or opposite.



So is there any way to rewrite the code so the "main" section will stick to the side of "menu" bar automatically, even if I change the menu width ?



https://codepen.io/MariusZMM/pen/xmPraj



<div id="header"></div>

<div id="main">
<div class="box"></div>

<div id="sections">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
doloribus.
</p>
</div>
</div>


CSS



body {
margin: 0;
padding: 0;
}

#header {
background: #222629;
height: 100%;
left: 0;
position: fixed;
text-align: center;
top: 0;
z-index: 1;
overflow-y: auto;

/* Menu size*/
width: 80px;
}

#main {
position: absolute;
height: 100%;
background-color: #856030;

/* Hero page size*/
margin-left: 6.25%;
width: 97.5%;
}

.box {
width: 100%;
position: relative;
height: 100%;
}

#sections {
position: absolute;
background-color: #347424;
}









share|improve this question





























    0















    So I have a menu item "header" (dark color) on the left side. And on the right side I will have my hero "main" page and bellow "sections". The only scroll I want to have is up/down and no side to side, and I want to center the main / sections pages.



    So first "header" item will be:



    width: 80px;


    And now, I want the other elements such as "main" to stick to that menu item. Currently my problem is that I have to keep changing numbers for "hero" page so it will align correctly at the center, but it's a pain and i cant seem to get the numbers right, i always get either page is like 2px behind the menu or opposite.



    So is there any way to rewrite the code so the "main" section will stick to the side of "menu" bar automatically, even if I change the menu width ?



    https://codepen.io/MariusZMM/pen/xmPraj



    <div id="header"></div>

    <div id="main">
    <div class="box"></div>

    <div id="sections">
    <p>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
    dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
    doloribus.
    </p>
    </div>
    </div>


    CSS



    body {
    margin: 0;
    padding: 0;
    }

    #header {
    background: #222629;
    height: 100%;
    left: 0;
    position: fixed;
    text-align: center;
    top: 0;
    z-index: 1;
    overflow-y: auto;

    /* Menu size*/
    width: 80px;
    }

    #main {
    position: absolute;
    height: 100%;
    background-color: #856030;

    /* Hero page size*/
    margin-left: 6.25%;
    width: 97.5%;
    }

    .box {
    width: 100%;
    position: relative;
    height: 100%;
    }

    #sections {
    position: absolute;
    background-color: #347424;
    }









    share|improve this question



























      0












      0








      0








      So I have a menu item "header" (dark color) on the left side. And on the right side I will have my hero "main" page and bellow "sections". The only scroll I want to have is up/down and no side to side, and I want to center the main / sections pages.



      So first "header" item will be:



      width: 80px;


      And now, I want the other elements such as "main" to stick to that menu item. Currently my problem is that I have to keep changing numbers for "hero" page so it will align correctly at the center, but it's a pain and i cant seem to get the numbers right, i always get either page is like 2px behind the menu or opposite.



      So is there any way to rewrite the code so the "main" section will stick to the side of "menu" bar automatically, even if I change the menu width ?



      https://codepen.io/MariusZMM/pen/xmPraj



      <div id="header"></div>

      <div id="main">
      <div class="box"></div>

      <div id="sections">
      <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
      dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
      doloribus.
      </p>
      </div>
      </div>


      CSS



      body {
      margin: 0;
      padding: 0;
      }

      #header {
      background: #222629;
      height: 100%;
      left: 0;
      position: fixed;
      text-align: center;
      top: 0;
      z-index: 1;
      overflow-y: auto;

      /* Menu size*/
      width: 80px;
      }

      #main {
      position: absolute;
      height: 100%;
      background-color: #856030;

      /* Hero page size*/
      margin-left: 6.25%;
      width: 97.5%;
      }

      .box {
      width: 100%;
      position: relative;
      height: 100%;
      }

      #sections {
      position: absolute;
      background-color: #347424;
      }









      share|improve this question
















      So I have a menu item "header" (dark color) on the left side. And on the right side I will have my hero "main" page and bellow "sections". The only scroll I want to have is up/down and no side to side, and I want to center the main / sections pages.



      So first "header" item will be:



      width: 80px;


      And now, I want the other elements such as "main" to stick to that menu item. Currently my problem is that I have to keep changing numbers for "hero" page so it will align correctly at the center, but it's a pain and i cant seem to get the numbers right, i always get either page is like 2px behind the menu or opposite.



      So is there any way to rewrite the code so the "main" section will stick to the side of "menu" bar automatically, even if I change the menu width ?



      https://codepen.io/MariusZMM/pen/xmPraj



      <div id="header"></div>

      <div id="main">
      <div class="box"></div>

      <div id="sections">
      <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
      dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
      doloribus.
      </p>
      </div>
      </div>


      CSS



      body {
      margin: 0;
      padding: 0;
      }

      #header {
      background: #222629;
      height: 100%;
      left: 0;
      position: fixed;
      text-align: center;
      top: 0;
      z-index: 1;
      overflow-y: auto;

      /* Menu size*/
      width: 80px;
      }

      #main {
      position: absolute;
      height: 100%;
      background-color: #856030;

      /* Hero page size*/
      margin-left: 6.25%;
      width: 97.5%;
      }

      .box {
      width: 100%;
      position: relative;
      height: 100%;
      }

      #sections {
      position: absolute;
      background-color: #347424;
      }






      html css






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 9:08









      A.H.Nuri

      819510




      819510










      asked Dec 31 '18 at 7:04









      MariusMarius

      758




      758
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I think I have figured it out, updated on CodePen
          https://codepen.io/MariusZMM/pen/Newvqb



          So what I've done, is used "left" for "main" section and set it to same px count as menu. Then also added "left: 0;" for "header"



          Would this be the correct way to handles this or is there a better way?



          body {
          margin: 0;
          padding: 0;
          }

          #header {
          background: #222629;
          height: 100%;
          left: 0;
          position: fixed;
          text-align: center;
          top: 0;
          z-index: 1;
          overflow-y: auto;

          /* Menu size*/
          width: 80px;
          }

          #main {
          position: absolute;
          height: 100%;
          right: 0;

          background-color: #2c2419;

          /* Hero page size*/
          left: 80px;
          width: auto;
          }

          .box {
          width: 100%;
          position: relative;
          height: 100%;
          }

          #sections {
          position: absolute;
          background-color: #347424;
          width: 100%;
          }





          share|improve this answer































            0














            Based on my understanding i modified the code like below.
            other method to adjust the width dynamically is using javascript.






            body {
            margin: 0;
            padding: 0;
            }

            #header {
            background: #222629;
            height: 100%;
            left: 0;
            position: fixed;
            text-align: center;
            top: 0;
            z-index: 1;
            overflow-y: auto;

            /* Menu size*/
            width: 6%;
            }

            #main {
            position: absolute;
            height: 100%;
            background-color: #856030;

            /* Hero page size*/
            margin-left: 6%;
            width: 94%;
            }

            .box {
            width: 100%;
            position: relative;
            height: 100%;
            }

            #sections {
            position: absolute;
            background-color: #347424;
            }

              <div id="header"></div>

            <div id="main">
            <div class="box"></div>

            <div id="sections">
            <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
            dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
            doloribus.
            </p>
            </div>
            </div>








            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984581%2fhow-to-align-elements-correctly-to-each-other%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I think I have figured it out, updated on CodePen
              https://codepen.io/MariusZMM/pen/Newvqb



              So what I've done, is used "left" for "main" section and set it to same px count as menu. Then also added "left: 0;" for "header"



              Would this be the correct way to handles this or is there a better way?



              body {
              margin: 0;
              padding: 0;
              }

              #header {
              background: #222629;
              height: 100%;
              left: 0;
              position: fixed;
              text-align: center;
              top: 0;
              z-index: 1;
              overflow-y: auto;

              /* Menu size*/
              width: 80px;
              }

              #main {
              position: absolute;
              height: 100%;
              right: 0;

              background-color: #2c2419;

              /* Hero page size*/
              left: 80px;
              width: auto;
              }

              .box {
              width: 100%;
              position: relative;
              height: 100%;
              }

              #sections {
              position: absolute;
              background-color: #347424;
              width: 100%;
              }





              share|improve this answer




























                0














                I think I have figured it out, updated on CodePen
                https://codepen.io/MariusZMM/pen/Newvqb



                So what I've done, is used "left" for "main" section and set it to same px count as menu. Then also added "left: 0;" for "header"



                Would this be the correct way to handles this or is there a better way?



                body {
                margin: 0;
                padding: 0;
                }

                #header {
                background: #222629;
                height: 100%;
                left: 0;
                position: fixed;
                text-align: center;
                top: 0;
                z-index: 1;
                overflow-y: auto;

                /* Menu size*/
                width: 80px;
                }

                #main {
                position: absolute;
                height: 100%;
                right: 0;

                background-color: #2c2419;

                /* Hero page size*/
                left: 80px;
                width: auto;
                }

                .box {
                width: 100%;
                position: relative;
                height: 100%;
                }

                #sections {
                position: absolute;
                background-color: #347424;
                width: 100%;
                }





                share|improve this answer


























                  0












                  0








                  0







                  I think I have figured it out, updated on CodePen
                  https://codepen.io/MariusZMM/pen/Newvqb



                  So what I've done, is used "left" for "main" section and set it to same px count as menu. Then also added "left: 0;" for "header"



                  Would this be the correct way to handles this or is there a better way?



                  body {
                  margin: 0;
                  padding: 0;
                  }

                  #header {
                  background: #222629;
                  height: 100%;
                  left: 0;
                  position: fixed;
                  text-align: center;
                  top: 0;
                  z-index: 1;
                  overflow-y: auto;

                  /* Menu size*/
                  width: 80px;
                  }

                  #main {
                  position: absolute;
                  height: 100%;
                  right: 0;

                  background-color: #2c2419;

                  /* Hero page size*/
                  left: 80px;
                  width: auto;
                  }

                  .box {
                  width: 100%;
                  position: relative;
                  height: 100%;
                  }

                  #sections {
                  position: absolute;
                  background-color: #347424;
                  width: 100%;
                  }





                  share|improve this answer













                  I think I have figured it out, updated on CodePen
                  https://codepen.io/MariusZMM/pen/Newvqb



                  So what I've done, is used "left" for "main" section and set it to same px count as menu. Then also added "left: 0;" for "header"



                  Would this be the correct way to handles this or is there a better way?



                  body {
                  margin: 0;
                  padding: 0;
                  }

                  #header {
                  background: #222629;
                  height: 100%;
                  left: 0;
                  position: fixed;
                  text-align: center;
                  top: 0;
                  z-index: 1;
                  overflow-y: auto;

                  /* Menu size*/
                  width: 80px;
                  }

                  #main {
                  position: absolute;
                  height: 100%;
                  right: 0;

                  background-color: #2c2419;

                  /* Hero page size*/
                  left: 80px;
                  width: auto;
                  }

                  .box {
                  width: 100%;
                  position: relative;
                  height: 100%;
                  }

                  #sections {
                  position: absolute;
                  background-color: #347424;
                  width: 100%;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 7:26









                  MariusMarius

                  758




                  758

























                      0














                      Based on my understanding i modified the code like below.
                      other method to adjust the width dynamically is using javascript.






                      body {
                      margin: 0;
                      padding: 0;
                      }

                      #header {
                      background: #222629;
                      height: 100%;
                      left: 0;
                      position: fixed;
                      text-align: center;
                      top: 0;
                      z-index: 1;
                      overflow-y: auto;

                      /* Menu size*/
                      width: 6%;
                      }

                      #main {
                      position: absolute;
                      height: 100%;
                      background-color: #856030;

                      /* Hero page size*/
                      margin-left: 6%;
                      width: 94%;
                      }

                      .box {
                      width: 100%;
                      position: relative;
                      height: 100%;
                      }

                      #sections {
                      position: absolute;
                      background-color: #347424;
                      }

                        <div id="header"></div>

                      <div id="main">
                      <div class="box"></div>

                      <div id="sections">
                      <p>
                      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                      dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                      doloribus.
                      </p>
                      </div>
                      </div>








                      share|improve this answer




























                        0














                        Based on my understanding i modified the code like below.
                        other method to adjust the width dynamically is using javascript.






                        body {
                        margin: 0;
                        padding: 0;
                        }

                        #header {
                        background: #222629;
                        height: 100%;
                        left: 0;
                        position: fixed;
                        text-align: center;
                        top: 0;
                        z-index: 1;
                        overflow-y: auto;

                        /* Menu size*/
                        width: 6%;
                        }

                        #main {
                        position: absolute;
                        height: 100%;
                        background-color: #856030;

                        /* Hero page size*/
                        margin-left: 6%;
                        width: 94%;
                        }

                        .box {
                        width: 100%;
                        position: relative;
                        height: 100%;
                        }

                        #sections {
                        position: absolute;
                        background-color: #347424;
                        }

                          <div id="header"></div>

                        <div id="main">
                        <div class="box"></div>

                        <div id="sections">
                        <p>
                        Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                        dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                        doloribus.
                        </p>
                        </div>
                        </div>








                        share|improve this answer


























                          0












                          0








                          0







                          Based on my understanding i modified the code like below.
                          other method to adjust the width dynamically is using javascript.






                          body {
                          margin: 0;
                          padding: 0;
                          }

                          #header {
                          background: #222629;
                          height: 100%;
                          left: 0;
                          position: fixed;
                          text-align: center;
                          top: 0;
                          z-index: 1;
                          overflow-y: auto;

                          /* Menu size*/
                          width: 6%;
                          }

                          #main {
                          position: absolute;
                          height: 100%;
                          background-color: #856030;

                          /* Hero page size*/
                          margin-left: 6%;
                          width: 94%;
                          }

                          .box {
                          width: 100%;
                          position: relative;
                          height: 100%;
                          }

                          #sections {
                          position: absolute;
                          background-color: #347424;
                          }

                            <div id="header"></div>

                          <div id="main">
                          <div class="box"></div>

                          <div id="sections">
                          <p>
                          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                          dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                          doloribus.
                          </p>
                          </div>
                          </div>








                          share|improve this answer













                          Based on my understanding i modified the code like below.
                          other method to adjust the width dynamically is using javascript.






                          body {
                          margin: 0;
                          padding: 0;
                          }

                          #header {
                          background: #222629;
                          height: 100%;
                          left: 0;
                          position: fixed;
                          text-align: center;
                          top: 0;
                          z-index: 1;
                          overflow-y: auto;

                          /* Menu size*/
                          width: 6%;
                          }

                          #main {
                          position: absolute;
                          height: 100%;
                          background-color: #856030;

                          /* Hero page size*/
                          margin-left: 6%;
                          width: 94%;
                          }

                          .box {
                          width: 100%;
                          position: relative;
                          height: 100%;
                          }

                          #sections {
                          position: absolute;
                          background-color: #347424;
                          }

                            <div id="header"></div>

                          <div id="main">
                          <div class="box"></div>

                          <div id="sections">
                          <p>
                          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                          dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                          doloribus.
                          </p>
                          </div>
                          </div>








                          body {
                          margin: 0;
                          padding: 0;
                          }

                          #header {
                          background: #222629;
                          height: 100%;
                          left: 0;
                          position: fixed;
                          text-align: center;
                          top: 0;
                          z-index: 1;
                          overflow-y: auto;

                          /* Menu size*/
                          width: 6%;
                          }

                          #main {
                          position: absolute;
                          height: 100%;
                          background-color: #856030;

                          /* Hero page size*/
                          margin-left: 6%;
                          width: 94%;
                          }

                          .box {
                          width: 100%;
                          position: relative;
                          height: 100%;
                          }

                          #sections {
                          position: absolute;
                          background-color: #347424;
                          }

                            <div id="header"></div>

                          <div id="main">
                          <div class="box"></div>

                          <div id="sections">
                          <p>
                          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                          dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                          doloribus.
                          </p>
                          </div>
                          </div>





                          body {
                          margin: 0;
                          padding: 0;
                          }

                          #header {
                          background: #222629;
                          height: 100%;
                          left: 0;
                          position: fixed;
                          text-align: center;
                          top: 0;
                          z-index: 1;
                          overflow-y: auto;

                          /* Menu size*/
                          width: 6%;
                          }

                          #main {
                          position: absolute;
                          height: 100%;
                          background-color: #856030;

                          /* Hero page size*/
                          margin-left: 6%;
                          width: 94%;
                          }

                          .box {
                          width: 100%;
                          position: relative;
                          height: 100%;
                          }

                          #sections {
                          position: absolute;
                          background-color: #347424;
                          }

                            <div id="header"></div>

                          <div id="main">
                          <div class="box"></div>

                          <div id="sections">
                          <p>
                          Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla sunt repudiandae unde doloremque eos sed
                          dolorem sit quae totam impedit. Exercitationem ad alias quae ipsam, maxime molestias quibusdam fugit
                          doloribus.
                          </p>
                          </div>
                          </div>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 7:32









                          RAGINROSERAGINROSE

                          1657




                          1657






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984581%2fhow-to-align-elements-correctly-to-each-other%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas