Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why align to center in flexbox work so strange?

I am trying to align to center very simple page. But can’t understand why it’s every time moving to left side. It should be at center.

@media (width < 1600px) { 
  body {
     width: 80%; 
     background-color: #ee5011;
  }            
}
@media (width >= 1600px) { 
  body {
     width: 60%;
     background-color: #deaa19;
  }            
}
body {    
   width: 90%;
   margin: 0;
   display: flex;
   align-items: center;
   justify-content: center;
   flex-direction: column;
}
main {
   padding-left: 1em;
   padding-right: 1em;
   overflow-wrap: break-word; 
   background-color: #ffffff;
}
<header>
   <a href="/"><img alt="Logo"  height="70"></a>
</header>
<main>
   <h2>Step-by-step guide</h2>
   <div>
      <p>After thaeffffffffffffffff  s fsdf dsf sd fdsf sf sdf sdf sd sd fdsd s s s ffffffffffffffffffffffffffffваы ы аыв аыва ыва ыва ыв вы авы ыв авы ыв ыв fffffffffffffffffffffff eeeeeeeeeeeeeed allowing you to add and delete sections and subsections from the project.</p>
   </div>
</main>

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s because the body is 90% wide and the body itself is not aligned to the center, only the content inside. if you want to center the body that is not 100% wide you can to use horizontal margin auto;

@media (width < 1600px) { 
  body {
     width: 80%; 
     background-color: #ee5011;
  }            
}
@media (width >= 1600px) { 
  body {
     width: 60%;
     background-color: #deaa19;
  }            
}
body {    
   width: 90%;
   margin: 0 auto;
   display: flex;
   align-items: center;
   justify-content: center;
   flex-direction: column;
}
main {
   padding-left: 1em;
   padding-right: 1em;
   overflow-wrap: break-word; 
   background-color: #ffffff;
}
<header>
   <a href="/"><img alt="Logo"  height="70"></a>
</header>
<main>
   <h2>Step-by-step guide</h2>
   <div>
      <p>After thaeffffffffffffffff  s fsdf dsf sd fdsf sf sdf sdf sd sd fdsd s s s ffffffffffffffffffffffffffffваы ы аыв аыва ыва ыва ыв вы авы ыв авы ыв ыв fffffffffffffffffffffff eeeeeeeeeeeeeed allowing you to add and delete sections and subsections from the project.</p>
   </div>
</main>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading