【问题标题】:Align attribute still available in HTML5?Align 属性在 HTML5 中仍然可用吗?
【发布时间】:2013-11-04 22:30:07
【问题描述】:

是否仍然可以将 align 属性添加到 HTML 元素以对齐元素本身?所以它不是通过 CSS 设置的,而是在元素标签本身中设置的,如下所示:

<div align="center"></div>

这仍然会居中,还是会忽略该属性?

【问题讨论】:

  • 该属性已被弃用,使用 css 值,如 text-align
  • &lt;div class="main" style="text-align:center;"&gt; 现在的问题是......你已经有了一个类,为什么不在css的类中声明呢?
  • @david 和 gio:OP 明确要求对齐 div 本身,而不是 div 的内联内容。
  • 使用{边距:0自动; }
  • align 属性在 HTML 4 中已弃用,并在 HTML 5 中删除。不要使用它:改用 CSS。

标签: css html alignment


【解决方案1】:

正如 Mike W 在 cmets 中指出的那样:

align 属性在 HTML 4 中已被弃用,并在 HTML 5 中被移除。 不要使用它:改用 CSS。

话虽如此,这里有一些方法可以让它居中,即使你说你有更多的元素与那个类。

给这个特定的元素内联样式:

<div class="main" style="margin: auto;">

在您的 CSS 中更加具体。该元素可能是没有任何其他 .main 婴儿的元素的子元素,因此您可以使用 CSS 中的父元素来指定该元素:

.parent-class > .main {margin: auto;} /* If the parent has a class */
#parent-id > .main {margin: auto;} /* If the parent has an ID. This one is prefered, to avoid misunderstandings */

如果不是上述情况,并且在单个父项中有多个.main 实例,则可以使用nth-child 选择器(或first-childlast-child)。例如,如果要居中的元素是父元素中的第三个子元素,请使用此代码。

.main:nth-child(3) {margin: auto;}

【讨论】:

    【解决方案2】:

    你为什么不使用 &lt;div class="main" style="margin:0 auto;"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多