Как изменить с помощью медиа запросов ссылку на картинку?
Предположим, что у нас есть обычный header:
Html:
<header class="header">
<div class="container">
<div class="logo">
<span class="home">Home</span>
<a class="first_nav_link" href="#">work</a>
<a class="first_nav_link" href="#">faq</a>
</div>
<a class="second_nav_link" href="#">log in</a>
</div>
</header>
CSS:
.header {
width: 100%;
max-width: 1600px;
height: 100%;
padding-top: 10px;
font-family: 'Montserrat', sans-serif;
background-color: #111111;
}
.container {
display: flex;
justify-content: space-between;
padding-bottom: 10px;
margin: 0px 20px 0px 20px;
}
.logo {
color: #fff;
margin: 0 15px;
font-family: 'Montserrat', sans-serif;
letter-spacing: 1.5px;
text-transform: uppercase;
}
.home {
font-weight: bold;
font-size: 20px;
}
.first_nav_link {
color: #fff;
font-family: 'Montserrat', sans-serif;
font-weight: normal;
letter-spacing: 1.5px;
text-decoration: none;
margin: 10px;
}
.second_nav_link {
color: #fff;
text-decoration: none;
font-family: 'Montserrat', sans-serif;
font-weight: normal;
letter-spacing: 1.5px;
text-transform: uppercase;
}
Как для определенной ссылки изменить ее текст на изображение или иконку?То есть, при экране с максимальной шириной 480 пикселей нужно поменять текст в ссылке на иконку.
Ответы (2 шт):
Автор решения: Денис Степанов
→ Ссылка
a::before {
content: "It's a link";
}
@media all and (max-width: 580px) {
a::before {
content: "";
display: block;
width: 30px;
height: 30px;
background-image: url(https://image.flaticon.com/icons/svg/2037/2037710.svg);
background-size: contain;
}
}
<a href="#"></a>
Автор решения: frozencoast
→ Ссылка
Примерно вот так, попробуйте поиграть с шириной окна вашего браузера, чтобы увидеть как работает пример:
.header {
width: 100%;
max-width: 1600px;
height: 100%;
padding-top: 10px;
font-family: 'Montserrat', sans-serif;
background-color: #111111;
}
.container {
display: flex;
justify-content: space-between;
padding-bottom: 10px;
margin: 0px 20px 0px 20px;
}
.logo {
color: #fff;
margin: 0 15px;
font-family: 'Montserrat', sans-serif;
letter-spacing: 1.5px;
text-transform: uppercase;
}
.home {
font-weight: bold;
font-size: 20px;
}
.first_nav_link {
color: #fff;
font-family: 'Montserrat', sans-serif;
font-weight: normal;
letter-spacing: 1.5px;
text-decoration: none;
margin: 10px;
}
.second_nav_link {
color: #fff;
text-decoration: none;
font-family: 'Montserrat', sans-serif;
font-weight: normal;
letter-spacing: 1.5px;
text-transform: uppercase;
}
@media (max-width: 480px) {
.home,
.first_nav_link,
.second_nav_link {
overflow: hidden;
display: inline-block;
width: 32px;
height: 32px;
background-color: #fff;
border-radius: 3px;
vertical-align: center;
margin: 0;
}
.home:before,
.first_nav_link:before,
.second_nav_link:before {
content: '';
display: block;
width: 100%;
height: 100%;
background-position: center center;
background-repeat: no-repeat;
}
.home:before {
background-image: url(https://i.stack.imgur.com/zXyN4.png);
}
.first_nav_link:nth-of-type(1):before {
background-image: url(https://i.stack.imgur.com/Oempp.png);
}
.first_nav_link:nth-of-type(2):before {
background-image: url(https://i.stack.imgur.com/q7q5j.png);
}
.second_nav_link:before {
background-image: url(https://i.stack.imgur.com/DwZ7Q.png);
}
}
<header class="header">
<div class="container">
<div class="logo">
<span class="home">Home</span>
<a class="first_nav_link" href="#">work</a>
<a class="first_nav_link" href="#">faq</a>
</div>
<a class="second_nav_link" href="#">log in</a>
</div>
</header>