CSS !重要规则不覆盖文本对齐

发布于 2024-11-24 08:43:51 字数 557 浏览 2 评论 0原文

a {
        font-size: 8pt;
        text-align: left !important;
        text-decoration: none;
  }

.main {
        text-align: center;
  }

 <div class="main">
    <a href="#new_york_city">New York City</a><br />
    <a href="#long_island">Long Island</a><br />
    <a href="#upstate">Upstate New York</a><br />
</div>

这是我所拥有的紧凑版本,对于我来说,使用 Firefox 5,链接仍然居中,即使我在“text-align: left”上使用 !important。这令人困惑和恼人,因为我认为 !important 是最高的特异性并凌驾于所有其他规则之上。

这是怎么回事?

a {
        font-size: 8pt;
        text-align: left !important;
        text-decoration: none;
  }

.main {
        text-align: center;
  }

 <div class="main">
    <a href="#new_york_city">New York City</a><br />
    <a href="#long_island">Long Island</a><br />
    <a href="#upstate">Upstate New York</a><br />
</div>

This is a compact version of what I have and for me, using Firefox 5, the links are STILL centered, even though I I'm using !important on the "text-align: left". It's confusing and irritating because i thought !important was the highest specificity and overrode all other rules.

What's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

掩耳倾听 2024-12-01 08:43:51

文本对齐需要在锚链接的父元素上设置,您不能告诉锚链接将其自身向左对齐,因为它具有固定宽度。您需要从 .main 部分中删除 text-align:center;,或者向 div 添加另一个类,例如“alignLeft”,并使用 !important 应用对齐方式统治那里。

The text alignment needs to be set on the parent element of the anchor-links, you cannot tell an anchor-link to align itself to the left as it is of a fixed width. You need to either remove text-align:center; from the .main section, or add another class to the div like 'alignLeft' and apply the alignment with the !important rule there.

神也荒唐 2024-12-01 08:43:51

根据您具体在做什么,这可能会起作用:

.main a {
  display:block;
  font-size: 8pt;
  text-align: left !important;
  text-decoration: none;
}

文本对齐只能在块元素(例如 div)上起作用。 “span”和“a”默认是内联的,但 display:block 可以改变这一点。

Depending on what exactly you're doing, this may work:

.main a {
  display:block;
  font-size: 8pt;
  text-align: left !important;
  text-decoration: none;
}

Text-align can only work on a block element (such as a div). "span" and "a" are inline by default, but display:block can change that.

梦过后 2024-12-01 08:43:51

默认情况下,锚点是一个内联元素,在您的情况下,这意味着它只有所需的宽度,因此它实际上是左对齐的,但仅在其内部对齐。由于它嵌套在 main 中(大概是一个块元素),因此 main 位于 a 标记的中心。

将锚点放在另一个块元素中并左对齐,或者将其设置为块。

An anchor is an inline element by default, which in your case means it's only as wide as it needs to be, so it really is aligning left but only within itself. Since it's nested within main presumably a block element, main in centering the a tag.

Either put the anchor in another block element and align that left, or set it to block.

第几種人 2024-12-01 08:43:51

这不起作用,因为您的 a 链接是没有指定宽度的 inline 元素。没有任何东西可以居中,因为整个元素的宽度都是a

要解决此问题,请将

  1. .main div 设置为 text-align:left;
  2. a 链接包装在 p< /code> 并给它 text-align:left;

This is not working because your a links are inline elements without a specified width. There is nothing to center because the entire element is the width of the a.

To fix this, either

  1. set the .main div to text-align:left; or
  2. wrap the a links in a p and give it text-align:left;
守望孤独 2024-12-01 08:43:51

如果您查看此fiddle,您会发现链接仍然内联因此,文本的对齐方式没有任何意义(因为元素将围绕其内容折叠)。

如果您将链接 inline-blocksblocks 设置为具有定义的宽度,则可以对齐其中的文本,如 另一个小提琴

现在,如果您希望链接位于容器的左侧,float:left

If you look at this fiddle, you'll see that the links are still inline and therefore, the text's alignment doesn't count for anything (since the element will collapse around its contents).

If you make the links inline-blocks or blocks with a defined width, you can justify the text within them, as shown in another fiddle.

Now, if you want the links up against the left side of the container, float:left as in this fiddle.

野稚 2024-12-01 08:43:51

链接本身居中。将它们包裹在其他东西中并将其左对齐。

<div class="main">
 <div class="left">
    <a href="#new_york_city">New York City</a><br />
    <a href="#long_island">Long Island</a><br />
    <a href="#upstate">Upstate New York</a><br />
  </div>
</div>

The links themselves are centered. Wrap them in something else and left align that.

<div class="main">
 <div class="left">
    <a href="#new_york_city">New York City</a><br />
    <a href="#long_island">Long Island</a><br />
    <a href="#upstate">Upstate New York</a><br />
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文