Why are borders causing div to overflow container?

喜欢而已 提交于 2019-12-20 04:22:57

问题


I have a section set to a fixed width and a 100% width div inside of it with a 5 pixel border.

Looks fine but you can tell the containing div is a bit off center and it wouldn't be without the border, which I need to match the client comp.

The code is rather straightforward:

#info {
  max-width: 980px;
  margin: 0 auto;
}
.info-box {
  border: 5px solid #0033A0;
  display: inline-block;
  text-align: center;
  padding: 48px 0;
  width: 100%;
}
<section id="info">
  <div class="info-box">SOME CONTENT</div>
</section>

The only thing I can think of is to make the width of the .info-box to be 98% or something like that, but that's still not going to truly work. So will anything?

BTW, I already tried adding relative positioning, set display to inline instead of inline-block....none of which worked.


回答1:


Add box-sizing: border-box; to your info-box class

.info-box {
    background: rgba(248, 243, 232, 0.5) none repeat scroll 0 0;
    border: 5px solid #0033a0;
    box-sizing: border-box;
    display: inline-block;
    padding: 48px 0;
    text-align: center;
    width: 100%;
}

box-sizing is better explained here https://css-tricks.com/box-sizing/



来源:https://stackoverflow.com/questions/39585402/why-are-borders-causing-div-to-overflow-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!