Как сделать элемент в блоке полупрозрачным при наведении курсора, при этом чтобы псевдоэлемент остался непрозрачным?
Пытался дать псевдоэлементу (подчеркивание) отдельные свойства по hover'у.
Проверил в dev tools, что псевдоэлементу при ховере присваивается значение:
opacity: 1;
но все равно весь элемент становится полупрозрачным:
.menu__item {
position: relative;
margin-right: 26px;
font: 500 18px/21px Roboto, arial, helvetica, sans-serif;
color: #000;
text-decoration: none;
transition: opacity .5s;
}
.menu__item:hover:after {
position: absolute;
display: block;
content: '';
left: 0;
width: 100%;
height: 3px;
background: #0056EC;
}
.menu__item:hover {
opacity: .5;
}
.menu__item:after {
position: absolute;
display: block;
content: '';
left: 0;
width: 0;
height: 3px;
background: #0056EC;
opacity: 0;
transition: opacity .5s, width .5s;
}
.menu__item:hover:after {
opacity: 1;
width: 100%;
}
<a class="menu__item" href="#">blog</a>
Ответы (2 шт):
Автор решения: Vasily
→ Ссылка
Что бы сделать сам элемент полупрозрачным и при этом сохранить "непрозрачность" псевдоэлемента after при событии hover:
.menu__item {
position: relative;
font: 500 18px/21px Roboto, arial, helvetica, sans-serif;
color: #000;
text-decoration: none;
margin-right: 26px;
transition: color 0.5s;
}
.menu__item:hover {
color: #d8d8d8; // или rgba(0, 0, 0, 50%)
}
.menu__item::after {
content: "";
position: absolute;
display: block;
left: 0;
width: 0;
height: 3px;
background: #0056EC;
transition: width 0.5s;
}
.menu__item:hover::after {
width: 100%;
}
<a class="menu__item" href="#">blog</a>
Или:
.menu__item {
position: relative;
font: 500 18px/21px Roboto, arial, helvetica, sans-serif;
color: #000;
text-decoration: none;
margin-right: 26px;
}
.menu__item::before {
content: "";
position: absolute;
display: block;
opacity: 0;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: white;
transition: opacity 0.5s;
}
.menu__item:hover::before {
opacity: 0.5;
}
.menu__item::after {
content: "";
position: absolute;
display: block;
left: 0;
width: 0;
height: 3px;
background: #0056EC;
transition: width 0.5s;
}
.menu__item:hover::after {
width: 100%;
}
<a class="menu__item" href="#">blog</a>
Если же Вы хотите получить эффект fade in/out при событии hover по псевдоэлементу, то:
.menu__item {
position: relative;
font: 500 18px/21px Roboto, arial, helvetica, sans-serif;
color: #000;
text-decoration: none;
margin-right: 26px;
transition: opacity .5s;
}
.menu__item::after {
content: "";
position: absolute;
display: block;
opacity: 0;
left: 0;
width: 0;
height: 3px;
background: #0056EC;
transition: opacity .5s, width .5s;
}
.menu__item:hover::after {
opacity: 1;
width: 100%;
}
<a class="menu__item" href="#">blog</a>
Автор решения: CbIPoK2513
→ Ссылка
Для текста используйте прозрачность через цвет с использованием rgba().
.menu__item {
position: relative;
margin-right: 26px;
font: 500 18px/21px Roboto, arial, helvetica, sans-serif;
color: #000;
text-decoration: none;
transition: color .5s;
}
.menu__item:hover:after {
position: absolute;
display: block;
content: '';
left: 0;
width: 100%;
height: 3px;
background: #0056EC;
}
.menu__item:hover {
color: rgba(0,0,0,.5); // opacity: .5;
}
.menu__item:after {
position: absolute;
display: block;
content: '';
left: 0;
width: 0;
height: 3px;
background: #0056EC;
opacity: 0;
transition: opacity .5s, width .5s;
}
.menu__item:hover:after {
opacity: 1;
width: 100%;
}
<a class="menu__item" href="#">blog</a>