使用 CSS 在子 Div 之间划分元素的宽度
我有不同数量的内联块 div,我希望它们总共占据其父级的 100%。这可以在没有 JavaScript 的情况下完成吗?我能想到的唯一方法是使用表格,但仅将表格用于布局目的当然是不好的做法。
|----------------------|
|{ div 1 }{ div 2 }|
or
|{div 1}{div 2}{div 3}|
|----------------------|
我也尝试过 { display:block;浮动:左; }
但这似乎没有什么区别。
I have a varying number of inline-block divs that I want to collectively take up 100% of their parent. Can this be done without JavaScript? The only way I can think of is with a table but it's of course bad practice to use a table solely for layout purposes.
|----------------------|
|{ div 1 }{ div 2 }|
or
|{div 1}{div 2}{div 3}|
|----------------------|
I have also tried { display:block; float:left; }
but it doesn't seem to make a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在内部 div 上使用
display:table-cell
来执行此操作。为了让浏览器使内部 div 表现得像表格单元格,它还需要两层包含元素:一层充当表格,另一层充当表格行。对于这样的结构:
使用此 CSS:
一个很好的结构是用 DIV 包装 UL:DIV 充当表格,UL 充当行,LI 充当表格单元格。
这种技术在较旧的浏览器中并没有得到很好的支持 - 对于任何早于 IE8 的浏览器,你完全不走运。
如果您需要更多示例代码,请告诉我!
You can use
display:table-cell
on your inner divs to do this. For the browser to make the inner divs behave like table cells, it also needs two layers of containing elements: one to acts as the table, and another to act as the table-row.For a structure like this:
Use this CSS:
A nice structure to use is a UL wrapped in a DIV: the DIV acts as a table, the UL as a row, and the LI's as table-cells.
This technique is not well supported in older browsers - for anything older than IE8, you're out of luck entirely.
Let me know if you need more sample code than that!
您可以在这里利用 css3 的优势。我也面临这个问题,现在我已经使用下面的示例代码修复了这个问题
谢谢&问候,
林格什拉姆
You can utilize css3 benefits here. I was also facing this issue now i have fixed that using below example code
Thanks & Regards,
Lingeshram
接受的答案错过了一个重要的CSS属性,这是工作所必需的:
这是正确的答案:
HTML:
CSS:
The accepted answer missed an important CSS property which is necessary to work:
This is the correct answer:
HTML:
CSS:
我想阐述一下@lingeshram 的回答。 Flexbox 已经发展到现在,我认为这确实是现在的解决方法。如果您必须支持旧版浏览器,请务必先检查 caniuse。
这是一个非常好的指南,介绍了您的不同方式可以使用弹性盒。
I'd like to expound on @lingeshram's answer. Flexboxes have come so far that I think it's really the way to do it now. If you have to support old browsers, be sure to check caniuse first.
Here is a pretty good guide to the different ways you can use flexbox.