Я порекомендую вам перейти к ширине изображения вместо высоты и установить height: auto;
, а затем использовать функцию calc
a {
font-family: sans-serif;
color: black;
font-size: 2rem;
text-decoration: none;
position: relative;
}
a:after{
content: "";
position:absolute;
bottom: 0;
right:0px;
height: 1rem;
width: calc(100% - 5rem);/* 100% - (width of the image)*/
transition: 0.5s;
border-bottom-width: 0.2rem;
border-bottom: solid .2rem green;
}
a img {
height: auto;
width: 5rem;
}
a:hover:after,
a:focus:after,
a:active:after {
box-shadow: inset 0 -0.5rem 0 green;
border-color: green;
}
a img,
a:hover img,
a:focus img,
a:active img {
border-bottom-width: 0;
box-shadow: none;
}
<a href="#">
<img src="https://cdn.vox-cdn.com/thumbor/heXu37IbDvVy6Qbo1wbPjNvi6Ys=/0x0:712x423/1200x800/filters:focal(385x120:497x232)/cdn.vox-cdn.com/uploads/chorus_image/image/55531035/Screen_Shot_2017_06_30_at_3.17.00_PM.0.png" alt="Juste... Pickle-rick"> My
link from the hell
</a>
Вы можете заключить текст в элемент span
a {
font-family: sans-serif;
color: black;
font-size: 2rem;
text-decoration: none;
}
a span{
transition: 0.5s;
border-bottom: solid .2rem green;
}
a img {
width: auto;
height: 5rem;
}
a:hover span,
a:focus span,
a:active span{
box-shadow: inset 0 -0.5rem 0 green;
border-color: green;
}
a img,
a:hover img,
a:focus img,
a:active img {
border-bottom-width: 0;
box-shadow: none;
}
<a href="#">
<img src="https://cdn.vox-cdn.com/thumbor/heXu37IbDvVy6Qbo1wbPjNvi6Ys=/0x0:712x423/1200x800/filters:focal(385x120:497x232)/cdn.vox-cdn.com/uploads/chorus_image/image/55531035/Screen_Shot_2017_06_30_at_3.17.00_PM.0.png" alt="Juste... Pickle-rick"> <span>My link from the hell</span>
</a>
или вы можете использовать Соотношение сторон (изображение)
a {
font-family: sans-serif;
color: black;
font-size: 2rem;
text-decoration: none;
position: relative;
}
a:after{
content: "";
position:absolute;
bottom: 0;
right:0px;
height: 1rem;
width: calc(100% - 5rem*1.5);/* 100% - (height of the image)x1.5*/
transition: 0.5s;
border-bottom-width: 0.2rem;
border-bottom: solid .2rem green;
}
a img {
width: auto;
height: 5rem;
}
a:hover:after,
a:focus:after,
a:active:after {
box-shadow: inset 0 -0.5rem 0 green;
border-color: green;
}
a img,
a:hover img,
a:focus img,
a:active img {
border-bottom-width: 0;
box-shadow: none;
}
<a href="#">
<img src="https://cdn.vox-cdn.com/thumbor/heXu37IbDvVy6Qbo1wbPjNvi6Ys=/0x0:712x423/1200x800/filters:focal(385x120:497x232)/cdn.vox-cdn.com/uploads/chorus_image/image/55531035/Screen_Shot_2017_06_30_at_3.17.00_PM.0.png" alt="Juste... Pickle-rick"> My
link from the hell
</a>