Html 如何使用 CSS 类在 td 元素内对齐 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7063201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align a div inside td element using CSS class
提问by MonsterMMORPG
It is working with this way
它正在以这种方式工作
<td align="center">
But I want to use CSS class.
但我想使用 CSS 类。
I defined class like this way but no luck
我这样定义类但没有运气
td
{
vertical-align: middle;
text-align: center;
margin-left: auto;
margin-right: auto;
align: center;
}
Vertical align is working and text align is working for text. But it does not align div inside td with this way. I want to align div inside td.
垂直对齐正在工作,文本对齐正在为文本工作。但是它不会以这种方式将 td 中的 div 对齐。我想在 td 中对齐 div。
回答by Luuk
div { margin: auto; }
This will center your div.
这将使您的 div 居中。
Div by itself is a blockelement. Therefor you need to define the style to the div how to behave.
Div 本身就是一个块元素。因此,您需要定义 div 的样式如何表现。
回答by martin
I cannot help you much without a small (possibly reduced) snippit of the problem. If the problem is what I think it is then it's because a divby default takes up 100% width, and as such cannot be aligned.
如果没有一个小的(可能减少的)问题片段,我无法帮助你。如果问题是我认为的那样,那是因为默认情况下div占用100% width,因此无法对齐。
What you may be after is to align the inline elements inside the div (such as text) with text-align:center;otherwise you may consider setting the div to display:inline-block;
您可能想要的是将 div 内的内联元素(例如文本)与text-align:center; 对齐。否则你可以考虑将 div 设置为display:inline-block;
If you do go down the inline-block route then you may have to consider my favorite IE hack.
如果您确实沿着内联块路线走,那么您可能必须考虑我最喜欢的 IE hack。
width:100px;
display:inline-block;
zoom:1; //IE only
*display:inline; //IE only
Happy Coding :)
快乐编码:)