如何将表格水平放置在 div 的中心?垂直
我们可以将图像设置为
的背景图像,例如:<style>
#test {
background-image: url(./images/grad.gif);
background-repeat: no-repeat;
background-position: center center;
width:80%;
margin-left:10%;
height:200px;
background-color:blue;
}
</style>
<div id="test"></div>
我需要在
的中心水平设置一个表格 &垂直。是否有使用 CSS
的跨浏览器解决方案?We can set a image as background image of a <div>
like:
<style>
#test {
background-image: url(./images/grad.gif);
background-repeat: no-repeat;
background-position: center center;
width:80%;
margin-left:10%;
height:200px;
background-color:blue;
}
</style>
<div id="test"></div>
I need to set a table at the center of a <div>
horizontally & vertically. Is there a cross-browser solution using CSS
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
居中是 CSS 中最大的问题之一。但是,存在一些技巧:
要使表格水平居中,您可以将左右边距设置为自动:
要垂直居中,唯一的方法是使用 javascript:
不可能使用
vertical-align:middle
因为表格是一个块而不是内联元素。编辑
这里是一个总结 CSS 居中解决方案的网站: http://howtocenterincss.com/
Centering is one of the biggest issues in CSS. However, some tricks exist:
To center your table horizontally, you can set left and right margin to auto:
To center it vertically, the only way is to use javascript:
No
vertical-align:middle
is possible as a table is a block and not an inline element.Edit
Here is a website that sums up CSS centering solutions: http://howtocenterincss.com/
这对我有用:
这可以垂直和水平对齐。
Here's what worked for me:
And this aligned it vertically and horizontally.
要水平居中定位,您可以说
width: 50%;边距:自动;
。据我所知,这是跨浏览器的。对于垂直对齐,您可以尝试vertical-align:middle;
,但它可能仅适用于文本。不过还是值得一试。To position horizontally center you can say
width: 50%; margin: auto;
. As far as I know, that's cross browser. For vertical alignment you can tryvertical-align:middle;
, but it may only work in relation to text. It's worth a try though.只需将
margin: 0 auto;
添加到您的表格
即可。无需向div
添加任何属性注意:为 div 添加背景颜色以可视化表格与其中心的对齐方式
Just add
margin: 0 auto;
to yourtable
. No need of adding any property todiv
Note: Added background color to div to visualize the alignment of table to its center
此外,如果您想水平和垂直居中 -而不是采用流程设计(在这种情况下,应用以前的解决方案)-您可以这样做:
absolute
或相对
定位(我称之为内容
)。wrapper
)。wrapper
div 内声明表本身。注意:您无法摆脱包装 div,因为这种样式不能直接在表格上工作,所以我使用 div 来包装它并定位它,而表格在 div 内部流动。
Additionally, if you want to center both horizontally and vertically -instead of having a flow-design (in such cases, the previous solutions apply)- you could do:
absolute
orrelative
positioning (I call itcontent
).wrapper
).wrapper
div.Note: You cannot get rid of the wrapper div, since this style does not work directly on tables, so I use a div to wrap it and position it, while the table is flowed inside the div.
您可以使用以下技术将框垂直和水平居中:
外部容器:
display: table;
内部容器:
display: table-cell;
>vertical-align: middle;
text-align: center;
内容框:
display: inline-block;
如果您使用此技术,只需添加您的表(以及您想要与之一起使用的任何其他内容)到内容框。
演示:
另请参阅这个小提琴!
You can center a box both vertically and horizontally, using the following technique :
The outher container :
display: table;
The inner container :
display: table-cell;
vertical-align: middle;
text-align: center;
The content box :
display: inline-block;
If you use this technique, just add your table (along with any other content you want to go with it) to the content box.
Demo :
See also this Fiddle!
我发现我必须包含
“margin: 0 auto”才能对表起作用。
I discovered that I had to include
for "margin: 0 auto" to work for tables.
由于我还不能发表评论,我将在这里将我的评论发布到jdlin。如果您想在不设置表格本身样式的情况下完成此操作,您可以这样做:
As I can't comment yet, I will post my comment to jdlin here. If you want to accomplish this without styling the table itself, you could do:
加载完成后在底部使用简单的 javascript ...
Using simple javascript at the bottom after loading finishes ...