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 margin is not respected in this case?

I can’t figure out why a margin is not respected when I resize to mobile view. If you resize the window, left margin is respected, however, right margin is not respected.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.banner-container {
  position: relative;
}

.banner-image {
  height: 200px;
  width: 100%;
  background-color: rebeccapurple;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

.banner-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 560px;
  width: 100%;
  background-color: rgba(255, 255, 255, 0.5);
  text-align: center;
  font-size: 32px;
  padding: 24px 0;
  margin: 0 10px;
}

@media screen and (max-width: 768px) {
  .banner-text {
    font-size: 24px;
  }
}
    <div class="banner-container">
      <div class="banner-image"></div>
      <span class="banner-text">Candidate Membership</span>
    </div>

I’d appreciate if someone could explain what I’m doing wrong. Thanks.

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 :

The margin is there, but it is added to the 100% width you defined. So the element’s width including margins goes beyond the container width.

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