CSS !important rule not overriding text alignment
Asked Answered
S

6

6
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?

Sternpost answered 15/7, 2011 at 18:2 Comment(1)
Whoopsy, had a bit of a dumb moment there. Yeah, just realized the container has to have the alignments, not the text/links, so setting 'text-align: left' on them essentially does nothing. I did find, however, that 'float: left' works well.Sternpost
M
5

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.

Misfile answered 15/7, 2011 at 18:5 Comment(0)
C
1

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>
Crespo answered 15/7, 2011 at 18:6 Comment(0)
I
1

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.

Individuation answered 15/7, 2011 at 18:7 Comment(0)
T
1

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.

Terminus answered 15/7, 2011 at 18:9 Comment(0)
S
1

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;
Sandy answered 15/7, 2011 at 18:10 Comment(0)
I
1

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.

Intercessor answered 15/7, 2011 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.