防止填充使元素变大?
我有一个 70%
宽度的元素,它漂浮在另一个 30%
宽度的元素旁边。但是,当我添加 25px
填充时,元素会扩展并破坏格式。
有什么方法可以使填充增加内容与元素边缘的距离,而不是使元素更大?
.seventy {
float: left;
width: 70%;
background-color: lightsalmon;
}
.thirty {
float: left;
width: 30%;
background-color: lightgreen;
}
.padded {
padding: 25px; /* Forces box onto next line */
}
<div>Works:</div>
<div class="seventy">70% wide</div>
<div class="thirty">30% wide</div>
<br><br>
<div>Broken:</div>
<div class="seventy">70% wide</div>
<div class="thirty padded">30% wide, padded</div>
I have an element with a 70%
width, and it is floating beside another element with 30%
width. However, when I add 25px
of padding, the element expands and breaks the format.
Is there any way to make padding increase the contents' distance from the element's edge, as opposed to making the element bigger?
.seventy {
float: left;
width: 70%;
background-color: lightsalmon;
}
.thirty {
float: left;
width: 30%;
background-color: lightgreen;
}
.padded {
padding: 25px; /* Forces box onto next line */
}
<div>Works:</div>
<div class="seventy">70% wide</div>
<div class="thirty">30% wide</div>
<br><br>
<div>Broken:</div>
<div class="seventy">70% wide</div>
<div class="thirty padded">30% wide, padded</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
border-box
模型时,内边距包含在框大小中。有关详细信息,请参阅此处。When you use the
border-box
model, the padding is included in the box size. See here for details.我会在该元素内创建另一个相同类型的元素(我可以猜它是一个 div 吗?),并将该元素设置为具有 25px 的填充/边距。
例如:
I would create another element of the same type (may I guess it's a div?) inside the element and set that one to have a padding/margin of 25px.
For example: