CSS hyperlink style for text only links - html

On my website I have a global CSS rule for hyperlinks:
a {
color: #1F497D;
text-decoration: none;
}
a:hover {
color: #FFFFFF;
background-color: #1F497D;
}
This works well for text-only hyperlinks - the link gets a background colour applied to it on hover.
Contact Us
However this is causing issues with other link types, for example:
<a href="image.png">
<img src="thumb.png" alt="" />
</a>
In the above example, the image is a transparent PNG, therefore the background colour applied by the a:hover rule can be seen on hover.
Is there any way we can adjust the global CSS rule so that it only applies to text links? I know I can add a separate class for these links but as there are so many I would prefer an easier solution.

You could create a class to anchor tags that are not texts:
a.not-a-text:hover {
background-color: transparent;
}
Them in the HTML:
<a class="not-a-text" href="image.png">
<img src="thumb.png" alt="" />
</a>

Since you asked for an "easier" solution (rather than adding classes), you could apply a style using an attribute selector (where a elements have an href attribute that end with a png extension) to have a transparent background.
Something like this:
a[href$=".png"]:hover{
background-color: transparent;
}
Here's a fiddle (has a red hover which you could set to transparent):
http://jsfiddle.net/sb6xvztm/

You should use CSS classes on the links instead of tags because it will make maintenance much easier.
.text-link {
color: #1f497d;
text-decoration: none;
}
.text-link:hover {
color: #FFFFFF;
background-color: #1F497D;
}
<a class="text-link" href="contact.html">Contact</a>
<a class="img-link"><img src="someimage.jpg" alt="alt"></a>

If you are not against your site running jQuery I suggest this SIMPLE solution:
jQuery('a').hover(function(){
jQuery(this).has('img').css('background', 'none');
});
Working Example Here http://jsfiddle.net/a9h9wbnv/

I would definitely use a new class. Then, just use any decent text editor (like Notepad++) to find and replace:
FIND: <a href="*"> <img
REPLACE: <a class="image-link" href="*"> <img
And in your CSS:
a.image-link:hover {
background-color: transparent;
}

So you want only the text hyperlink to have the hover?
If so, you can simply give the the 'Contact Us' a different class name or a specific ID.
<a href="image.png">
<img src="thumb.png" alt="" />
</a>
<a id = "contact" href="contact.html">Contact Us</a>
a {
color: #1F497D;
text-decoration: none;
}
#contact:hover {
color: #FFFFFF;
background-color: #1F497D;
}

Related

Html :visited selector not working when using target="_blank" in IE11

In html link not change color for visited link when target attribute set "_blank" on IE11.
css
#lnk:visited
{
color: red;
}
html
<a id="lnk" href="some url" target="_blank">click me</a>
After click "a" changes color to red, but when refresh the page "a" not changes color to red on IE11.
Use anchor tag selector instead
Plunker: https://plnkr.co/edit/6d3IM0?p=preview
<style>
a:visited {
color: red;
}
</style>
click me
<style>
a#lnk:visited {
color: red;
}
</style>
<a id="lnk" href="some url" target="_blank">click me</a>
1.First way you can add the same if you want all the link to act same regarding color.
a:visited {
color: red;
}
2.second way only specific to your link ID
a#lnk:visited {
color: red;
}
Also you can use "vlink Attribute" from HTML to specify the color of visited links in a document.
Add a class on onclick event
.visited
{
color: red !important;
}
<a class="" id="lnk" href="some url" target="test.php" onclick="this.className='visited' ">click me</a>
Run code snippet and click Hide results and then run .

Color in link tag doesn't work but color in div in link tag works?

Why doesn't this work (in terms of text color):
.navbarDivText {
color: #DAA520;
}
.navbarDiv {
width: 150px;
background-color: inherit;
margin-left: 10px;
}
<li class="navbarDiv" >
Main Page
</li>
But this does:
<li class="navbarDiv" >
<a href="index.php">
<div class = "navbarDivText">Main Page</div>
</a>
</li>
In this there are two cases if you give the color for list it will change the color of list and the anchor remained blue by default with underline.
If you also want to to change the color of anchor in a list you should have also give the styling for anchor means text-decoration and color whatever style you want.
See example here hope this will help you. Link
More demo: Here
Bootstrap is probably overriding this...
You'll need to be more specific e.g.
a.navbarDivText {
color: #DAA520;
}
You may have to use:
a.navbarDivText {
color: #DAA520!important;
}
If for some reason that doesn't work...
On a side note you should only put a div inside an a tag if you are using the HTML5 doctype, which I imagine you are.
When a link is clicked the browser will give it a color to show it has been visited. So you could try the below :visited selector. If that doesn't work then it is likely that another style is overriding your style. As mentioned in the comments, inspect the element in the developer console and see if your style is being overridden.
.navbarDivText:link, .navbarDivText:visited
{
color: #DAA520;
}
Works fine to me, clear cookies and try.
.navbarDivText {
color: #DAA520;
}
.navbarDiv {
width: 150px;
background-color: green;
margin-left: 10px;
}
<li class="navbarDiv">Main Page</li>
Could do this as well,
.navbarDiv a, a:visited {
color: #DAA520;
}
or
.navbarDivText:link, .navbarDivText:visited {
color: #DAA520;
}

Preventing some elements in a class to inherit style from it's parent class

Let's say I have HTML that looks like this
<div class="abc">
<a>some link</a>
<a class="xyz">another link</a>
</div>
and CSS that looks like this
.abc a
{
color: #fff;
}
a.xyz
{
color: #aaa;
}
The problem is second link with class xyz inherits the color #fff from it's parent div. Is there a way to make it inherit the color from class xyz? Also the concept of :first child and or :last child wont help because I just stated 2 links here which is not practical.
What you probably need to do is to make your CSS more specific. Without all of your code it's hard to say how specific you need to get.
.abc a
{
color: #fff;
}
.abc a.xyz
{
color: #aaa;
}
<div class="abc">
<a>some link</a>
<a class="xyz">another link</a>
</div>
Reference: CSS Specificity

Hide un-classed element, and show it's sibling?

I've got some HTML code I can't modify. I don't want to use JS/jQuery to do this, would like to get it done with cross-browser friendly CSS.
The HTML looks like this:
<ul class="list">
<li class="item">
<a href-"#">Item One</a> |
</li>
<li class="item">
<a href-"#">Item Two</a> |
</li>
<li class="item">
<a href-"#">Item Three</a> |
</li>
</ul>
It's got those stupid pipes in there to break up the list. I Want to hide those, and show the <a> elements. I don't just want to make the text color the same as the background either. I'd like an equivalent of display: none;
You can set the font-size of the li to 0 and give it a transparent color, then set those properties back to normal on the a:
li.item {
font-size: 0;
color: transparent;
}
li.item a {
font-size: 16px;
color: #000;
}
This makes the li text invisible and have no size whatsoever, but keeps the a element styled as it should.
JSFiddle demo.
Note that I've used transparency here as (as far as I recall) Safari has a problem with fully hiding the font when its size is set to 0.
Simply:
.item {
color: transparent;
}
.item a {
color: #000;
}
JS Fiddle demo.
This means that the text of the li is invisible/transparent (though it can still be selected), but the text colour of the a element is made visible.

Keep underline on anchor text but not on font awesome icon in anchor

I have the following markup:
<a href="#" title="Feedback" id="feedbacktogglenav">
Feedback
<i class="icon-comment"></i>
</a>
On hover, I would like the text to be underlined but not the font-awesome icon. I know you can do "text-decoration:none" on the icon (which works) but when you hover on the text part it still adds the underline to the icon. Any ideas?
http://jsfiddle.net/ZZEQd/
I've discovered a way of doing this without needing an extra span tag, it works in every browser I've tried it in (FF/Chrome/Safari/Opera)... except IE8 (I haven't tested in IE 9 or 10 either).
Just declare the icon as display:inline-block, no more underline on hover.
Example: http://jsfiddle.net/J432G/
HTML:
<a href="#" title="Feedback" id="feedbacktogglenav">
Feedback<i class="icon-comment"></i>
</a>
CSS:
a{
text-decoration: none;
}
a:hover{
text-decoration: underline;
}
i:after{
content: '{icon}';
display: inline-block;
padding-left: 1em;
}
...but when you hover on the text part it still adds the underline to the icon. Any ideas?
To get this to work you'd need to contain the link text within a separate element (a span, ideally):
<a href="#">
<span>Feedback</span>
<i class="icon-comment"></i>
</a>
Now you can remove the text-decoration from the link completely and assign it only to the span (on hover):
a {
text-decoration:none;
}
a:hover span {
text-decoration:underline;
}
JSFiddle demo.
The only way to do this reliably is to assign text-decoration:none to the first parent element i.e the initial a tag.
If you cannot add a span element (let's assume you just have acccess to CSS), it should work withfloat:left or right on your icon element.
See: CSS linked images are being underlined ("a" display is set to block)
Someone just responded but deleted their response. The solution was:
#utilitynav a i {text-decoration:none;}
Thank you mystery person!
http://jsfiddle.net/ZZEQd/2/
Just add a span tag in your HTML and you should fine.
HTML:
<a href="#" title="Feedback" id="feedbacktogglenav">
<span class="linkHover">Feedback</span>
<i class="icon-comment"></i>
</a>
CSS
a {
text-decoration:none;
}
.linkHover:hover{
text-decoration: underline;
}
#utilitynav .icon-comment {
font-size: 12px;
margin-left: 3px;
position: absolute;
top: 2px;
}
#utilitynav .icon-comment:hover {
text-decoration:none;
}
#utilitynav #feedbacktogglenav {
margin-right: 12px;
}
I added a class to your span for it wouldn't effect future span tags
If you have a situation where you can't use :before or :after (because the specific icon is set by the content, not by the global style, for example) and you also don't want to have to go in and put <span> around the link text everywhere... one other option is to put padding on the link, and absolutely position the icon over the padding.
HTML
Feedback <i class="icon-comment fas fa-link"></i>
CSS
a {
position: relative;
text-decoration:none;
padding-left: 20px;
}
a:hover {
text-decoration:underline;
}
a i {
position: absolute;
left: 0;
}
JSFiddle: http://jsfiddle.net/w6bL5m8k/1/
I'm sure there are probably all kinds of special conditions under which this isn't going to work out... but for simple situations it should be fine.