两个 50% 宽度的 div 之间的间隙
我有一个宽度为 181px 的容器
。我的容器 div 中有两个子
元素,每个元素的宽度均为 50%。我的问题是我的每个子
元素的宽度仅为 90px,导致两者之间有 1px 的间隙。对于消除差距有什么建议吗?
I have a container <div>
with a width of 181px.
I have two child <div>
elements in my container div each with a width of 50%.
My problem is that each of my child <div>
elements has a width of only 90px, resulting in a 1px gap between the two.
Any suggestions for removing the gap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将一个
div
的宽度设置为50%
,另一个可以使用消耗其余宽度的标准块行为:Set the width of one
div
as50%
and the other can use the standard block behavior of consuming the rest of the width:将它们设计为花车?
style them as floats?
您无法将 181 个像素分成两半。像素是显示器上的一个像素,您无法使用 2 50% 的元素将 181px 分成两半。一个需要是 90px,一个需要是 91px。
如果你将它们都浮动到相同的方向,无论是向左还是向右,它们之间的间隙就会消失,但另一侧可能仍然有 1px 的间隙——如果这很重要的话。如果这些元素有边框并且使用浮动,那么您将遇到一系列全新的跨浏览器问题。确保进行一些跨浏览器测试。
You can't split 181 pixels in half. A pixel is a pixel on a display and you can't get 2 50% elements to split the 181px in half. One needs to be 90px and one needs to be 91px.
If you float them both to the same direction, either left or right, the gab between them would go away but you would probably still have a 1px gap on the other side--if that matters. If you have borders on these elements and you're using floats then you'll have a whole new set of cross-browser issues. Make sure you do some cross-browser testing.
那么该值将四舍五入为整数。
这是一个非常好的线程,其中包含更多细节。
CSS 宽度中的小数位是否受到尊重?
Well the value get rounded to integer.
Here is a very good thread which goes into much more detail.
Are the decimal places in a CSS width respected?