I wanna make a simple responsive site with 3 images in-front of a background, each of them a button that gets overlayed when you hover it. The problem is when I add the additional containers in order to do the image hover overlays, justify-content and row-wrap stop working as they should and they no longer center the images properly. If I remove all the container divs everything goes back to normal.
.background {
display: inline-block;
position: relative;
min-width: 100%;
max-width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
.mountain {
pointer-events: none;
position: absolute;
width: 100%;
min-height: 100%;
z-index: -1;
}
.tabs {
display: flex;
justify-content: space-between;
align-content: space-between;
flex-flow: row wrap;
gap: 10px;
padding-top: 200px;
}
.container1 {
position: relative;
left: 80px;
}
.container1:hover .portraits {
opacity: 0.5;
}
.portraits {
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
.container2 {
position: relative;
width: 480px;
max-height: 100%;
}
.container2:hover .secondtab {
opacity: 0.5;
}
.secondtab {
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
.container3 {
position: relative;
margin-right: 80px;
;
}
.container3:hover .thirdtab {
opacity: 0.5;
}
.thirdtab {
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
<div class="background">
<img class="mountain" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864">
<div class="buttons">
<button class="Contacts">CONTACTS</button>
<button class="Aboutme">ABOUT ME</button>
<section class="tabs">
<div class="container1">
<input class="portraits" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
<div class="container2">
<input class="secondtab" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
<div class="container3">
<input class="thirdtab" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
</section>
Change space-between to space-around on your .tabs class. Then, remove any values you have set on your flex items. For example, I removed the left: 80px value you had set on container1 and removed the margin-right from container3.
Setting those values are being counterproductive to the flexbox as you are instructing the browser to position those containers in a relatively static position within the parent. This will be much more responsive if you allow flex do its job.
.background {
display: inline-block;
position: relative;
min-width: 100%;
max-width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
.mountain {
pointer-events: none;
position: absolute;
width: 100%;
min-height: 100%;
z-index: -1;
}
.tabs {
display: flex;
justify-content: space-around;
align-content: space-between;
flex-flow: row wrap;
padding-top: 200px;
}
.container1 {
display: flex;
position: relative;
}
.container1:hover .portraits {
opacity: 0.5;
}
.portraits {
display: flex;
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
.container2 {
display: flex;
position: relative;
width: 480px;
max-height: 100%;
}
.container2:hover .secondtab {
opacity: 0.5;
}
.secondtab {
display: flex;
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
.container3 {
display: flex;
position: relative;
}
.container3:hover .thirdtab {
opacity: 0.5;
}
.thirdtab {
display: flex;
margin: auto;
max-width: 100%;
opacity: 1;
transition: .5s ease;
backface-visibility: hidden;
width: 480px;
max-height: 100%;
}
<div class="background">
<img class="mountain" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864">
<div class="buttons">
<button class="Contacts">CONTACTS</button>
<button class="Aboutme">ABOUT ME</button>
</div>
<section class="tabs">
<div class="container1">
<input class="portraits" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
<div class="container2">
<input class="secondtab" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
<div class="container3">
<input class="thirdtab" type="image" src="https://scontent.fsof8-1.fna.fbcdn.net/v/t1.15752-9/248678344_166048722401558_3753835307514045389_n.jpg?_nc_cat=109&ccb=1-5&_nc_sid=ae9488&_nc_ohc=hMjalJL0QVMAX8jUsDI&_nc_ht=scontent.fsof8-1.fna&oh=03_AVLASJWuh-jugLUe4XS8djFYOc8Ps3PK3bA8Ue4D9zHEMg&oe=62801864"
/>
</div>
</section>
</div>
Edit ~ If you don't want the images to wrap, remove all the static (fixed) widths and use this:
.container1,
.container2,
.container3 {
width: calc(100%/3);
}
Related
I'm trying to create a landing page where it shows 2 logos. On hover a background image should appear on the left hand side of the page but should fill from top to bottom. At the moment it's only filling the div container of the logo. I'm not that good at coding and got help with creating these codes.
Code Below
body {
background-color: white;
}
.container {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.row {
display: flex;
width: 80%;
}
.left-logo,
.right-logo {
width: 50%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.left-logo img,
.right-logo img {
opacity: 0;
transition: opacity 5s;
}
.left-bg,
.right-bg {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-size: cover;
background-position: center;
opacity: 0;
transition: opacity 0.5s;
}
.left-logo:hover .left-bg {
opacity: 1;
background-image: url(images/bg-left.png);
}
.right-logo:hover .right-bg {
opacity: 1;
background-image: url(images/bg-right.png);
}
.left-logo img,
.right-logo img {
opacity: 1;
}
<body>
<div class="container">
<div class="row">
<div class="left-logo">
<img src="images/logo-left.png" alt="Left Logo">
<div class="left-bg"></div>
</div>
<div class="right-logo">
<img src="images/logo-right.png" alt="Right Logo">
<div class="right-bg"></div>
</div>
</div>
</div>
</body>
Tried to put the image in the css code where left-bg is but still didn't work.
Try adding height: 100vh; and width: 100%; to the .row class and margin:0; to the body tag as follows:
body {
background-color: white;
margin: 0;
}
.container {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.row {
display: flex;
width: 100%;
height: 100vh;
}
.left-logo, .right-logo {
width: 50%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.left-logo img, .right-logo img {
opacity: 0;
transition: opacity 5s;
}
.left-bg, .right-bg {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-size: cover;
background-position: center;
opacity: 0;
transition: opacity 0.5s;
}
.left-logo:hover .left-bg {
opacity: 1;
background-image: url(https://lumiere-a.akamaihd.net/v1/images/sa_pixar_virtualbg_coco_16x9_9ccd7110.jpeg);
}
.right-logo:hover .right-bg {
opacity: 1;
background-image: url(https://lumiere-a.akamaihd.net/v1/images/sa_pixar_virtualbg_coco_16x9_9ccd7110.jpeg);
}
.left-logo img, .right-logo img {
opacity: 1;
}
<head>
<title>Landing Page</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="left-logo">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSOyCtPSy5vZ6NNKe6nmW5aEAWDfv2R3bV72g&usqp=CAU" alt="Left Logo">
<div class="left-bg"></div>
</div>
<div class="right-logo">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSOyCtPSy5vZ6NNKe6nmW5aEAWDfv2R3bV72g&usqp=CAU" alt="Right Logo">
<div class="right-bg"></div>
</div>
</div>
</div>
</body>
I am having an issue with scrollable images that shouldn't be scrollable. I thought that overflow: hidden; should fix that issue, but as of yet no dice.
Below is just a snippet from my code. In my code, there are 4 "a" elements within the wrapper class.
HTML
<a href="Link to other page">
<div class="container">
<img class="image" src="image.jpg">
<div class="overlay"></div>
<div class="overlay2"></div>
<div class="overlayText">Title</div>
</div>
</a>
CSS
.wrapper {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 55vh;
grid-auto-columns: 41vw;
background-color: #eef7e4;
gap: 1vh;
padding-left: 1vh;
padding-right: 1vh;
overflow: hidden;
}
.container {
align-items: center;
display: flex;
position: relative;
overflow: hidden;
}
.image {
overflow: hidden;
}
.overlay {
position: absolute;
width: 0;
height: 100%;
background-color: #e32827;
opacity: 80%;
transition: 0.7s ease;
z-index: 2;
overflow: hidden;
}
.overlay2 {
position: absolute;
width: 0;
height: 100%;
background-color: #eef7e4;
opacity: 80%;
transition: 0.5s ease;
overflow: hidden;
}
.overlayText {
position: absolute;
width: 50%;
height: 100%;
opacity: 0;
color: #eef7e4;
transition: 0.6s ease;
text-align: center;
margin-top: 50vh;
z-index: 3;
overflow: hidden;
}
As you can see by the CSS above, the thought of overflow: hidden; was not working for me. I'm fairly new to webpage design, and after looking through a lot online, the only fix that I found was overflow: hidden; which as stated above isn't working here.
Thank you guys for the help!
Found the problem, I needed to give .container a width & height
.container {
align-items: center;
display: flex;
position: relative;
width: 100%;
height: 100%;
}
give your image width & height .image {height: 100vh;width: 100vw;}
I am trying to make an image slider with a scrollbar in the bottom. The images should be shown in fullscreen with the the active image in the center and an image in the left side and right side of the active image as shown in the image.
The scrollbar should not be sticking to the slider. I tried to implement it in the following way. But couldn't fix it.
Updated with the help from #kip .
I need to remove a class for the active image that will be shown in the center and add a class to all other images. 'class name : img-slider-other'
<style>
.slider {
width: 100%;
text-align: center;
overflow: hidden;
display: flex;
justify-content: center;
}
.slides {
display: flex;
overflow-x: scroll;
margin-bottom: 50px;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
width: 100%;
}
.slides::-webkit-scrollbar {
width: 10px;
height: 20px;
}
.slides::-webkit-scrollbar-thumb {
background-color: #737373;
border-radius: 10px;
}
.slides::-webkit-scrollbar-track {
background: transparent;
border: 1px solid #ccc;
max-width: 60% !important;
margin-left: 30.75rem;
margin-right: 30.75rem;
}
.slides::-webkit-scrollbar-track-piece {
max-width: 200px;
background-color: #fff;
height: 200px;
border: 1px solid #ccc;
/* border-radius: 10px; */
}
/* .slides::-webkit-scrollbar-corner {
border-radius: 10px;
} */
.slides>div {
scroll-snap-align: center;
flex-shrink: 0;
/* width: 100%;
max-width: 100%; */
width: 60%;
max-width: 60%;
/* height: auto; */
height: 500px;
/* For Temp */
margin-right: 50px;
margin-bottom: 50px;
border-radius: 10px;
/* background: #eee; */
transform-origin: center center;
transform: scale(1);
transition: transform 0.5s;
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 100px;
}
img {
/* object-fit: cover; */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
.img-slider-other {
height: 80%;
top: auto;
opacity: 0.5;
}
</style>
<div class="slider">
<div class="slides" id="slides">
<div id="slide-1">
<img src="c1.png" />
</div>
<div id="slide-2">
<img src="c2.png" />
</div>
<div id="slide-3">
<img src="c3.png" />
</div>
<div id="slide-4">
<img src="c4.png" />
</div>
<div id="slide-5">
<img src="c5.png" />
</div>
</div>
</div>
<script>
$(document).ready(function() {
var IDs = [];
var selctedImg = 'slide-3';
$(".slides").find("div").each(function() {
IDs.push(this.id);
});
document.getElementById(selctedImg).scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest",
callback: scrollEvent()
}
);
function scrollEvent() {
IDs.forEach(element => {
console.log("el", element)
if (selctedImg !== element) {
$('#' + element).children('img').addClass('img-slider-other');
}
});
}
});
</script>
Something like this?
.slider {
width: 100%;
text-align: center;
overflow: hidden;
}
.slides {
display: flex;
overflow-x: auto;
margin-bottom:50px;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
}
.slides::-webkit-scrollbar {
width: 10px;
height: 20px;
}
.slides::-webkit-scrollbar-thumb {
background: black;
border-radius: 10px;
}
.slides::-webkit-scrollbar-track {
background: transparent;
border: 1px solid #ccc;
max-width: 60% !important;
margin-left:25vw;
margin-right:25vw;
}
.slides::-webkit-scrollbar-track-piece {
max-width: 200px;
background-color: #337ab7;
height: 200px;
}
.slides>div {
scroll-snap-align: start;
flex-shrink: 0;
width:70%;
max-width: 70%;
/* height: auto; */
height: 500px;
/* For Temp */
margin-right: 50px;
margin-bottom:50px;
border-radius: 10px;
background: #eee;
transform-origin: center center;
transform: scale(1);
transition: transform 0.5s;
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 100px;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slider">
<div class="slides">
<div id="slide-1">
1
</div>
<div id="slide-2">
2
</div>
<div id="slide-3">
3
</div>
<div id="slide-4">
4
</div>
<div id="slide-5">
5
</div>
</div>
</div>
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I have two div that each of them has an image and a caption.
I want image center vertically or fill div element without stretching or Any unusual resizing.
I searched and use any suggestion Like How TO - Center Elements Vertically- CSS Layout - Horizontal & Vertical Align and answers of some questions in stackoverflow, but none worked.
I want use and display this elements responsive
I use this pictures for test:
.border_slide {
overflow: hidden;
}
.other_titles
{
margin-top: 5px;
height: 120px;
}
.other_titles div
{
width:49.5%;
margin-bottom: 5px;
}
.other_titles_cap
{
position: absolute;
z-index: 100;
width:100%;
background-color: rgba(69,69,69,0.4);
height: 70px;
margin-top: 80px;
transition: all 0.3s ease 0s;
}
/*.other_titles_cap:hover
{
margin-top: 0;
height: 150px;
}*/
.title2
{
height: 150px;
background-color: #9bfff0;
/*margin-left: 15px;*/
float: right;
}
.title2 img
{
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title2:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title2:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
.title3
{
height: 150px;
background-color: #ffd5c4;
float: left;
}
.title3 img
{
/* transform: translate(7%,-10%); */
-ms-transform:translate(7%,-10%);
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title3:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title3:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
<div class="other_titles">
<div class="border_slide title2">
<div class="other_titles_cap">title of Post2</div>
<img class="" src="https://i.stack.imgur.com/k6HuQ.jpg" style="width:100%">
</div>
<div class="border_slide title3">
<div class="other_titles_cap">title of Post3</div>
<img class="" src="https://i.stack.imgur.com/90ten.jpg" style="width:100%">
</div>
</div>
I Mostly need this for responsive mode in width size below 520px.
You can test this here:
jsfiddle
Something like below snippet.
*, *:before, *:after{box-sizing: border-box;}
.other_titles{
display: flex;
justify-content: space-between;
flex-flow: wrap;
}
.border_slide {
width: calc(50% - 10px);
margin-bottom: 5px;
overflow: hidden;
position: relative;
height: 150px;
}
.border_slide img{
transition: all 0.4s ease 0s;
display: block;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
position: absolute;
}
.other_titles_cap{
position: absolute;
z-index: 100;
width: 100%;
background-color: rgba(69,69,69,0.5);
height: 70px;
bottom: 0;
left: 0;
transition: all 0.3s ease 0s;
padding: 10px;
font-size: 16px;
color: #fff;
}
.border_slide:hover .other_titles_cap{
margin-top: 0;
height: 150px;
background-color: rgba(0,0,0,0.75);
}
.border_slide:hover img{
width: 115% !important;
opacity: 0.8;
transform: scale(1.2);
}
<div class="other_titles">
<div class="border_slide">
<img src="https://i.stack.imgur.com/k6HuQ.jpg">
<div class="other_titles_cap">Title of Post2</div>
</div>
<div class="border_slide">
<img src="https://i.stack.imgur.com/90ten.jpg">
<div class="other_titles_cap">Title of Post3</div>
</div>
</div>
Here you go with a solution
.border_slide {
overflow: hidden;
}
.other_titles
{
display: flex;
flex-direction: row;
align-items: center;
height: 100vh;
justify-content: space-around;
}
.other_titles div
{
width:49.5%;
margin-bottom: 5px;
}
.other_titles_cap
{
position: absolute;
z-index: 100;
width:100%;
background-color: rgba(69,69,69,0.4);
height: 70px;
margin-top: 80px;
transition: all 0.3s ease 0s;
}
/*.other_titles_cap:hover
{
margin-top: 0;
height: 150px;
}*/
.title2
{
height: 150px;
background-color: #9bfff0;
/*margin-left: 15px;*/
float: right;
}
.title2 img
{
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title2:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title2:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
.title3
{
height: 150px;
background-color: #ffd5c4;
float: left;
}
.title3 img
{
/* transform: translate(7%,-10%); */
-ms-transform:translate(7%,-10%);
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title3:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title3:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
<div class="other_titles">
<div class="border_slide title2">
<div class="other_titles_cap">title of Post2</div>
<img class="" src="https://i.stack.imgur.com/k6HuQ.jpg" style="width:100%">
</div>
<div class="border_slide title3">
<div class="other_titles_cap">title of Post3</div>
<img class="" src="https://i.stack.imgur.com/90ten.jpg" style="width:100%">
</div>
</div>
jsfiddle
Solution is using flex css.
Check the class other_titles
.other_titles {
display: flex;
flex-direction: row;
align-items: center;
height: 100vh;
justify-content: space-around;
}
I have created a flex container with 3 flex items.
First flex item contains 2 classes (.flex-item and .overlay).
I want to overlay image over the flex item. I tried it by adding z-index and position but it's not working.
.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.flex-item {
margin-right: 50px;
margin-bottom: 50px;
flex: 0 0 15em;
border: 2px solid red;
height: 100px;
}
.overlay {
background: url(https://image.freepik.com/free-icon/check-circle_318-31777.jpg) no-repeat center;
background-size: 100px 80px;
z-index: 110;
position: relative;
opacity: 0.6; /* Real browsers */
filter: alpha(opacity=60); /* MSIE */
height: 170px;
}
<div class="flex-container">
<div class="flex-item overlay">
Image
<img src="http://7bna.net/images/home-images/home-images-0.jpg" />
</div>
<div class="flex-item">
</div>
<div class="flex-item">
</div>
</div>
Please see the code in codepen
Your check is background and house is content so background can't be above content. Move check to another element.
.flex-container{
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.flex-item{
margin-right: 50px;
margin-bottom: 50px;
flex: 0 0 15em;
border:2px solid red;
height:100px;
}
.overlay:before {
background: url(https://image.freepik.com/free-icon/check-circle_318-31777.jpg) no-repeat center;
background-size: 100px 80px;
z-index: 110;
position: absolute;
opacity: 0.6; /* Real browsers */
filter: alpha(opacity=60); /* MSIE */
height: 170px;
width: 170px;
display: block;
content: '';
}
<div class="flex-container">
<div class="flex-item overlay">
Image
<img src="http://7bna.net/images/home-images/home-images-0.jpg" />
</div>
<div class="flex-item">
</div>
<div class="flex-item">
</div>
</div>
In your CSS, the .overlay class establishes a background image on the parent element.
In your HTML, the img element places an image as a child of the .overlay parent.
Per the rules of z-index stacking contexts, a child cannot appear behind the parent. Therefore, the background image (parent) cannot appear in front of the img (child).
That's why your img is always out front, irrespective of z-index.
But there is an easy solution to this problem. Use an absolutely-positioned pseudo-element:
.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.flex-item {
margin-right: 50px;
margin-bottom: 50px;
flex: 0 0 15em;
border: 2px solid red;
min-height: 100px;
display: flex;
flex-direction: column;
position: relative;
}
img {
width: 100%;
min-height: 0; /* https://stackoverflow.com/q/36247140/3597276 */
}
.overlay::after {
background: url(https://image.freepik.com/free-icon/check-circle_318-31777.jpg) no-repeat center;
width: 100%;
height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
background-size: contain;
opacity: 0.6;
content: "";
}
<div class="flex-container">
<div class="flex-item overlay">
<img src="http://7bna.net/images/home-images/home-images-0.jpg" />
</div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
revised codepen
.container {
position: relative;
width: 50%;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align:center;
}
<div class="container">
<img src="http://7bna.net/images/home-images/home-images-0.jpg" alt="Avatar" class="image">
<div class="overlay">
<div class="text"><img src="https://image.freepik.com/free-icon/check-circle_318-31777.jpg" width="50%" />
</div>
</div>
</div>
use position: absolute not relative in .overlay