CSS 边距自动居中并带有上边距 - 有效吗?

发布于 2025-01-02 23:37:57 字数 116 浏览 0 评论 0原文

这是使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

海之角 2025-01-09 23:37:57

使用以下内容指定边距:

div { margin: 30px auto 0; }

Which isshorthand for:

div { margin : 30px auto 0 auto; } /* margin: [top] [right] [bottom] [left]; */

Which isshorthand for:

div {
    margin-top: 30px;
    margin-right: auto;
    margin-bottom: 0;
    margin-left: auto;
}

现在您已经了解了可以指定 margin 和/或 padding 的不同方式;选择权在你。

就优先顺序而言,后面的定义将适用;如规范中所定义。

要查找元素/属性组合的值,用户代理必须应用以下排序顺序:

  1. 查找适用于目标媒体类型的相关元素和属性的所有声明。如果关联的选择器与相关元素匹配,则声明适用。
  2. 声明的主要排序是按权重和来源进行的:对于普通声明,作者样式表会覆盖用户样式表,而用户样式表会覆盖默认样式表。对于“!important”声明,用户样式表会覆盖作者样式表,而作者样式表又会覆盖默认样式表。 “!important”声明覆盖普通声明。导入的样式表与导入它的样式表具有相同的来源。
  3. 二级排序是根据选择器的特殊性进行的:更具体的选择器将覆盖更通用的选择器。伪元素和伪类分别算作普通元素和类。
  4. 最后,按指定的顺序排序:如果两个规则具有相同的权重、来源和特异性,则指定的后者获胜。导入样式表中的规则被视为位于样式表本身中的任何规则之前。

除了对各个声明进行“!important”设置之外,此策略还赋予作者的样式表比读者的样式表更高的权重。因此,用户代理让用户能够关闭特定样式表的影响(例如通过下拉菜单)非常重要。

正如其他人提到的,您可能需要指定固定宽度才能看到您的 div 居中......

Use the following to specify margins:

div { margin: 30px auto 0; }

Which is shorthand for:

div { margin : 30px auto 0 auto; } /* margin: [top] [right] [bottom] [left]; */

Which is shorthand for:

div {
    margin-top: 30px;
    margin-right: auto;
    margin-bottom: 0;
    margin-left: auto;
}

Now that you know the different ways margins, and/or padding, can be specified; the choice is yours.

As far a precedence is concerned the later definition will apply; as defined in the spec.

To find the value for an element/property combination, user agents must apply the following sorting order:

  1. Find all declarations that apply to the element and property in question, for the target media type. Declarations apply if the associated selector matches the element in question.
  2. The primary sort of the declarations is by weight and origin: for normal declarations, author style sheets override user style sheets which override the default style sheet. For "!important" declarations, user style sheets override author style sheets which override the default style sheet. "!important" declaration override normal declarations. An imported style sheet has the same origin as the style sheet that imported it.
  3. The secondary sort is by specificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively.
  4. Finally, sort by order specified: if two rules have the same weight, origin and specificity, the latter specified wins. Rules in imported style sheets are considered to be before any rules in the style sheet itself.

Apart from the "!important" setting on individual declarations, this strategy gives author's style sheets higher weight than those of the reader. It is therefore important that the user agent give the user the ability to turn off the influence of a certain style sheet, e.g., through a pull-down menu.

As others have mentioned, you'll likely need to specify a fixed width in order to see your div centered ...

浅浅 2025-01-09 23:37:57

是的,但是关于将 div 居中,您还需要对其应用 width

yes, but with regards to centering the div you'll also want to apply width to it.

停顿的约定 2025-01-09 23:37:57

是的。他们是对的:

div { width: 90%; margin : 30px auto 0 auto; }

我通常使用 90% 宽度作为一个很好的起点。

Yes. And they're right:

div { width: 90%; margin : 30px auto 0 auto; }

I generally use 90% width as a good starting point.

冧九 2025-01-09 23:37:57

是的,因为 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;

-黛色若梦 2025-01-09 23:37:57

它是有效的,但可以更短,如下所示:

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.

灼疼热情 2025-01-09 23:37:57

是的,这是有效的。 margin-top 将覆盖 margin 规则。

尽管您可能想添加一个 width 来使其居中。

Yes, it's valid. margin-top will override the margin rule.

Though you might wanna add a width to center it.

幸福还没到 2025-01-09 23:37:57

我不明白为什么不......你也可以将其缩短为:

div {margin: 30px auto 0;}

I don't see why not...you could also shorten this to:

div {margin: 30px auto 0;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文