宽度问题:当边框 != none 时为 50% (CSS)
problem with width:50% when border != none
take a look
http://jsfiddle.net/5nYSf/
result should be like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将两个 50% 宽的元素并排放置,然后您可以在每个元素内放置另一个具有边距和边框的元素: http://jsfiddle.net/5nYSf/47/
You can put two elements beside each other that are 50% wide, then you can put another element inside each that has to margin and border: http://jsfiddle.net/5nYSf/47/
有一个简单的方法:
添加 { 框大小:边框框; } 在 .attachments 中
并且 50% 宽度也将包含边框
There is a easy way:
add { box-sizing: border-box; } in .attachments
and the 50% width will contain the border too
诀窍是,边框和边距不包含在高度/宽度计算中。因此,如果您的元素宽度为 100px,边框为 2px,左边距为 10px,则将占用 114px 的水平空间。 (边框被计算两次:左和右。)IIRC,填充也不包括在内,但我不确定这一点。
有多种选择可以解决这个问题。您可以在两个元素上使用
width:49%
,或者在第一个元素上使用width:50%
,然后让第二个元素占据其余元素。如果两个元素必须占用完全相同的空间,则可以将每个元素包含在其自己的
div
中。这些 div 将没有边框/边距/填充,并且恰好占据 50% 的空间,并且将在内部元素上指定边框。最后一个正在运行的方法:
http://jsfiddle.net/5nYSf/35/
The trick is, border and margin are not included in height/width calculation. So, if you have element with width 100px, border 2px and left margin 10px, the 114px of horizontal space will be taken up. (Border is counted twice: left and right.) IIRC, padding is not included too, but I'm not sure about that.
There're several options to solve this. You can have
width:49%
on both elements orwidth:50%
on first and make second to take up the rest.If both elements must take exactly equal space, you can include each in its own
div
. Those divs will have no border/margin/padding and take up exactly 50% of space and border will be specified on inner element.The last method in action:
http://jsfiddle.net/5nYSf/35/
如果您的边框是固定宽度,您可以使用 CSS 中的 calc() 函数从元素宽度中减去边框长度。
http://jsfiddle.net/5nYSf/277/
If your borders are a fixed width you could substract the border lenght from your element width using the calc() function in your CSS.
http://jsfiddle.net/5nYSf/277/
边框是除了定义的宽度之外的,因此 50% + 50% + 3px 边框(两侧)最终为 100% + 12px,比包含元素 (100%) 大 12px。尝试使用 49% 或其他一些尺寸,为边框留出 12 像素。
Border is in addition to the defined width, so 50% + 50% + 3px border (on both sides) ends up being 100% + 12px, which is 12px bigger than the containing element (100%). Try using 49% or some other measurement that will leave 12px for the border.
不要忘记考虑这些边距(以显示浅灰色区域),并且您不能使用 height:100% ,原因与不能使用 width:50% 的原因相同(如其他人此处所述)
Don't forget to factor in those margins (to show up light grey areas) and that you can't use height:100% for the same reasons you can't use width:50% (as described by others here)
边框扩大了盒子。
50%+边框> 50%
你必须减少宽度:
the border expands the box.
50% + border > 50%
you have to decrease the width: