CSS !重要规则不覆盖文本对齐
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
文本对齐需要在锚链接的父元素上设置,您不能告诉锚链接将其自身向左对齐,因为它具有固定宽度。您需要从 .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.根据您具体在做什么,这可能会起作用:
文本对齐只能在块元素(例如 div)上起作用。 “span”和“a”默认是内联的,但 display:block 可以改变这一点。
Depending on what exactly you're doing, this may work:
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.
默认情况下,锚点是一个内联元素,在您的情况下,这意味着它只有所需的宽度,因此它实际上是左对齐的,但仅在其内部对齐。由于它嵌套在
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 withinmain
presumably a block element,main
in centering thea
tag.Either put the anchor in another block element and align that left, or set it to block.
这不起作用,因为您的
a
链接是没有指定宽度的inline
元素。没有任何东西可以居中,因为整个元素的宽度都是a
。要解决此问题,请将
.main div
设置为text-align:left;
或a
链接包装在p< /code> 并给它
text-align:left;
This is not working because your
a
links areinline
elements without a specified width. There is nothing to center because the entire element is the width of thea
.To fix this, either
.main div
totext-align:left;
ora
links in ap
and give ittext-align:left;
如果您查看此fiddle,您会发现链接仍然
内联
因此,文本的对齐方式没有任何意义(因为元素将围绕其内容折叠)。如果您将链接
inline-blocks
或blocks
设置为具有定义的宽度,则可以对齐其中的文本,如 另一个小提琴。现在,如果您希望链接位于容器的左侧,
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
orblocks
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.链接本身居中。将它们包裹在其他东西中并将其左对齐。
The links themselves are centered. Wrap them in something else and left align that.