Adding A Bottom Border On Hover, CSS Only
Solution 1:
One option to get the same result without affecting the size of your link (borders append to the outside of the element) is to use an inset box-shadow.
Which in your example would be to change your a:hover to the following:
#links a:hover {
box-shadow: inset 0 -10px 0 0 cyan;
}
You can see the fiddle here: https://jsfiddle.net/kLLxkdw4/
Solution 2:
Just assign the border-bottom property on :hover:
#links a:hover{
border-bottom: 3px solid #00f; /* or whatever colour you'd prefer */
outline: 3px solid black;
}
If the phrase 'anchored image' should be taken to mean the img is within the a element, then I'd suggest:
#links a:hover img {
border-bottom: 3px solid #00f; /* or whatever you'd prefer */
}
Solution 3:
Try this code I just wrote specially for you. This solution answers your question about adding a border-bottom on :hover. It even goes far beyond that and changes the background of the whole #link element on :hover as well with CSS transitions.
HTML Markup
<div id="links">
<a href="#">Example Link</a>
</div>
CSS Effets & Transitions
#links a{
color:#fafafa;
text-decoration:none;
background-color:#8b0000;
padding:15px;
border-radius:5px;
font-size:1.3em;
transition: all 0.3s ease-in-out;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
}
#links a:hover{
background-color:#fff;
color:#002f5b;
border-bottom:5px solid #002f5b;
}
Solution 4:
If you want it to work with an img inside your a, you can use a pseudo-element on the anchor, and then apply the border to that. Also. To avoid the border from appending to the outside of the link, you should use an inset box-shadow instead:
a {
display: inline-block;
position: relative;
}
a:hover:before {
content: '';
width: 100%;
height: 100%;
position: absolute;
left: 0;
bottom: 0;
box-shadow: inset 0 -20px 0 #11c0e5;
}
a img {
display: block;
}
<a href="#">
<img src="http://oi65.tinypic.com/241jlzk.jpg"/>
</a>
Solution 5:
Put your image in a div and add a mask div.
#mask {
position: absolute;
z-index: 2;
box-sizing: border-box;
height: 100%;
width: 100%;
top: 0;
left: 0;
border-bottom: 3px solid transparent;
}
#mask:hover {
border-bottom: 3px solid cyan;
}
#outerDiv {
display: inline-block;
position: relative;
}
img {
display: block;
}
<div id='outerDiv'>
<img src="https://www.gravatar.com/avatar/087039a00851e75ff483470e3aba89c9?s=32&d=identicon&r=PG" />
<div id='mask'></div>
</div>
Post a Comment for "Adding A Bottom Border On Hover, CSS Only"