CSS 边距自动居中并带有上边距 - 有效吗?
这是使 div 居中并应用上边距的有效 CSS 吗?
div {
margin: 0 auto;
margin-top: 30px;
}
Is this valid CSS to center the div and also apply a top margin?
div {
margin: 0 auto;
margin-top: 30px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用以下内容指定边距:
Which isshorthand for:
Which isshorthand for:
现在您已经了解了可以指定
margin
和/或padding
的不同方式;选择权在你。就优先顺序而言,后面的定义将适用;如规范中所定义。
正如其他人提到的,您可能需要指定固定宽度才能看到您的
div
居中......Use the following to specify margins:
Which is shorthand for:
Which is shorthand for:
Now that you know the different ways
margin
s, and/orpadding
, can be specified; the choice is yours.As far a precedence is concerned the later definition will apply; as defined in the spec.
As others have mentioned, you'll likely need to specify a fixed width in order to see your
div
centered ...是的,但是关于将 div 居中,您还需要对其应用
width
。yes, but with regards to centering the div you'll also want to apply
width
to it.是的。他们是对的:
我通常使用 90% 宽度作为一个很好的起点。
Yes. And they're right:
I generally use 90% width as a good starting point.
是的,因为 margin: 0 auto 将顶部和底部设置为 0,将左侧和右侧设置为 auto,因此将顶部设置为 30px 与说 margin : 30px auto 0 auto; 是一样的;
yes it is, because margin: 0 auto is setting top and bottom to 0 and left and right to auto so setting top to 30px is just the same as saying margin : 30px auto 0 auto;
它是有效的,但可以更短,如下所示:
div {margin: 30px auto 0;}
当您只给出三个值时,中间的值将应用于左侧和右侧。
It's valid, but it can be shorter like this:
div {margin: 30px auto 0;}
When you only give three values, the middle value is applied to both left and right sides.
是的,这是有效的。
margin-top
将覆盖margin
规则。尽管您可能想添加一个
width
来使其居中。Yes, it's valid.
margin-top
will override themargin
rule.Though you might wanna add a
width
to center it.我不明白为什么不......你也可以将其缩短为:
I don't see why not...you could also shorten this to: