浮动元素上的填充

发布于 2024-10-03 09:04:43 字数 194 浏览 3 评论 0原文

如何向 float:right 项目添加填充而不让它弄乱一切?填充不是应该在内部而不是外部起作用吗? 看看绿色部分上的一些填充会发生什么:http://lauradifazio.altervista.org/cms/

How do I add padding to a float:right item without having it to mess up everything? Isn't padding supposed to work on the inside not the outside?
Look at what happens with some padding on the green part: http://lauradifazio.altervista.org/cms/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭水袖舞倾城 2024-10-10 09:04:43

您的元素(任何元素,无论是否浮动)占用的总空间如下:

totalWidth = contentWidth + margin + border + padding

当您使用 CSS 指定 width 属性时,您只需设置上述方程的 contentWidth

例如,如果您尝试将一个元素放入给定的空间量中,则需要考虑所有宽度因素,而不仅仅是一个。因此,如果您只有 200 像素的空间,并且您希望内容周围有 5 像素的边距、1 像素的边框和 10 像素的填充,则必须按如下方式进行操作:

contentWidth = availableWidth - margin - border - padding 
contentWidth = 200px - (2x5px) - (2x1px) - (2x10px)
contentWidth = 200px - 10px - 2px - 20px
contentWidth = 168px

**note that the (2xNN) refers to the fact that you have to 
  consider both the impact to both the left and right side 
  of the element in the total.

因此,在 CSS 中,您可以将元素样式设置为:

width: 168px;
border: 1px <color> <type>;
padding: 10px;
margin: 5px;

这就是 W3C(即标准)盒子模型的工作原理。您还可以强制使用其他盒子模型,使用 box-sizing CSS 属性来定义您希望盒子模型如何工作,但您应该尽可能使用标准盒子模型。

The total space your element (any element, floated or not) takes up is as follows:

totalWidth = contentWidth + margin + border + padding

When you specify a width property with CSS, you're only setting the contentWidth of the above equation.

For example, if you were trying to fit an element into a given amount of space, you need to take all of the width factors into consideration, not just one. So, if you only have 200px of space, and you want a 5px margin around the content, with a 1px border, and 10px of padding, you would have to work it as follows:

contentWidth = availableWidth - margin - border - padding 
contentWidth = 200px - (2x5px) - (2x1px) - (2x10px)
contentWidth = 200px - 10px - 2px - 20px
contentWidth = 168px

**note that the (2xNN) refers to the fact that you have to 
  consider both the impact to both the left and right side 
  of the element in the total.

So in your CSS, you would style the element as:

width: 168px;
border: 1px <color> <type>;
padding: 10px;
margin: 5px;

This is how the W3C (i.e. the standard) box model works. There are other box models you can force, using the box-sizing CSS property to define how you want your box model to work, but you should use the standard box model where possible.

神经大条 2024-10-10 09:04:43

请记住,填充会添加到您的宽度上。因此,如果包含 5 像素的填充,则 200 像素的宽度实际上是 210 像素。正确的宽度应该是 190px。

Remember that padding is added on to your width. So your 200px width is actually 210px if you include the 5px padding. The correct width should be 190px.

深空失忆 2024-10-10 09:04:43

元素的宽度包括填充。如果向元素添加内边距,则必须相应地减小宽度和高度。

The width of an element does not include padding. If you add padding to an element, you must decrease width and height accordingly.

还不是爱你 2024-10-10 09:04:43

您需要补偿元素的宽度。在您的情况下,将 div 的宽度设置为 190px 而不是 200px,因为您有 5px 的填充。

有用的资源:http://www.yourhtmlsource.com/stylesheets/cssspacing.html

You need to compensate for the width of the element. In your case, make the div's width 190px instead of 200px since you have 5px of padding.

Helpful Resource: http://www.yourhtmlsource.com/stylesheets/cssspacing.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文