Home > Blockchain >  Why margin is not respected in this case?
Why margin is not respected in this case?

Time:11-23

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

CodePudding user response:

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.

CodePudding user response:

.banner-text in media width : 80%

* {
  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;
  }
  .banner-text{
      width: 80%;
  }
}
     <div class="banner-container">
      <div class="banner-image"></div>
      <span class="banner-text">Candidate Membership</span>
    </div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related