Recently I worked on a project where we had to make a custom image gallery. The requirement needed me to place many components like icons, texts, another image, and many other things on the image. While doing that, I learned about the steps to overlay the components on the image which I would like to share. Let’s build a video player page to understand how the overlaying of icons works in CSS. Let’s consider the below template. We have an image on which we need to place the play button in the center and like button at the top-right corner of the image.
Video PlayerHover over to play the videoclass="container">href="#"class="like-button"title="Like Button">class="fa fa-heart fa-1x">src="https://staticvideos.pexels.com/videos/1448735/pictures/preview-0.jpg"alt="Beautiful Image"class="image">class="overlay">href="#"class="play-icon"title="Video Play">class="fa fa-play">
We have styled the components and now they are ready to be placed on the image. Normally when we write the elements, the components are stacked vertically like below. But we need to overlay the two icons on the image in the position we want. We can achieve it by using the following steps.
Step 1. Assign the Positions
This is the most important step. The first step to enclose the components along with the image as one element and assign the CSS property position: relative . And the icons we need to place on the image are assigned the property position: absolute . In this case, we are adding the class .container( which encloses the image and the icons) position: relative , and the icons we overlay are assigned the property position: absolute . You can know more about the position property in CSS below. CSS Property: Position
Step 2. Assign z-index higher than the image to the icons if necessary
In this case, I am assigning z-index to the div of icons ( .overlay and .like-button ) which is higher than that of the image so that they are overlayed on the image. In some cases, it is not required to add if the default indices of the components are favorable to our requirement. But make sure to check and add. We have added the positions and z-index in the below code.
Step 3. Position the icons accordingly using the other CSS properties
Since we have our icons placed on the image, let’s move the play button to the center and the like button to the top-right end of the image using few properties.
Узнать, как создать эффект значка наложения на изображение при наведении курсора.
Наложение иконки на изображение
Наведите указатель мыши на изображение, чтобы увидеть эффект наложения.
Создать наложение иконки на изображение
Шаг 1) Добавить HTML:
Пример
Шаг 2) Добавить CSS:
Пример
/* Контейнер, необходимый для размещения наложения. Отрегулируйте ширину по мере необходимости */ .container position: relative; width: 100%; max-width: 400px; >
Как сделать hover effect с иконкой поверх картинки?
Мне необходимо с помощью css и его :hover наложить градиент и icon поверх картинки. Выглядеть это должно примерно вот так, у меня же получается это только вот так:
Подскажите как сделать пожалуйста. И еще возможно есть картинки вставлять через html, а не через css?
Такой вариант пробовали
Да, конечно. Этот вопрос актуален, только если найдется ответ на вопрос выше. Как вставить картинки в html я знаю:)
Нажимая «Принять все файлы cookie» вы соглашаетесь, что Stack Exchange может хранить файлы cookie на вашем устройстве и раскрывать информацию в соответствии с нашей Политикой в отношении файлов cookie.
Спасибо! Хотел ещё спросить, как сделать так, чтобы это появлялось при :hover, при этом не сдвигая figcaption-ы и чтобы «background-color: rgba(45, 45, 45, 0.5);» (поверх) картинки при ховере появлялся тоже? Заранее, спасибо 😉
Реализация с появлением при :hover , а позиционирование выполнено на Flexbox .
Een dynamische quiz in teams, die wordt geleid door een enthousiaste en bekwame quizmaster. Doormiddel van beeld en geluid wordt een breed scala aan vragen voorgelegd. Het raden van tunes, videofragmenten, teksten, foto’s met de hand op de quiz-knop of na teamberaad.