I have a CSS rule like so:
a {
line-height: 50px;
display: inline-block;
text-decoration: none;
font-size: 16px;
color: white;
}
And this HTML:
<a href="/test">test</a>
How can I stop the CSS rule applying to just this element?
I have a CSS rule like so:
a {
line-height: 50px;
display: inline-block;
text-decoration: none;
font-size: 16px;
color: white;
}
And this HTML:
<a href="/test">test</a>
How can I stop the CSS rule applying to just this element?
If a selector matches an element, then it matches the element.
You have three options:
You can use class selectors (.foo
) as opposed to element selectors (a
) in your CSS declarations.
So instead of doing this:
CSS
a {
line-height: 50px;
display: inline-block;
text-decoration: none;
font-size: 16px;
color: white;
}
HTML
<a href="/test">test</a>
You can do this:
CSS
.foo {
line-height: 50px;
display: inline-block;
text-decoration: none;
font-size: 16px;
color: white;
}
HTML
<a class="foo" href="/test">test</a>
When you don't want your a
element to have those styles, simply omit the class or apply a different class you may have declared.
Inline Styles
Another option, which solves your problem but doesn't really conform to best practices (on several levels), is to use inline styles.
Since you're saying you don't want the external (or embedded) styles to apply, you therefore just want the a
styles to be different. You can accomplish your goal with something like this:
<a href="/test" style="your preferred styles here">test</a>
The inline styles will take precedence over other styles in this case.
NOTE that inline styles take precedence over external and embedded styles except in cases when the external and embedded declarations contain !important
and the inline style does not contain !important
.
Thank you everyone for the suggestions, but the following answer worked perfectly for me:
a.none {
line-height: inherit;
display: inherit;
text-decoration: inherit;
font-size: inherit;
color: inherit;
}
Create that class and then attach it to the a tag like this:
<a class="none" href="/test">test</a>
Someone posted this before and then deleted it, i don't know why, but thank you!