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
Related
Suppose, we have child element positioned at the top right corner of the parent (think of the badge over the icon button).
My problem is: I need child's center to stick to parent's right edge, but currently when badge content widen, its center shifts to the left:
enter image description here
Live sandbox:
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#parent {
width: 150px;
height: 150px;
background: aquamarine;
position: relative;
}
#child {
width: 150px;
height: 100px;
background: orange;
position: absolute;
right: -50px;
top: -50px;
border-radius: 50px;
}
<div id="wrapper">
<div id="parent">
<div id="child"></div>
</div>
</div>
You can use transform: translate(50%, -50%)
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#parent {
width: 150px;
height: 150px;
background: aquamarine;
position: relative;
}
#child {
width: 150px;
height: 100px;
background: orange;
position: absolute;
right: 0;
top: 0;
border-radius: 50px;
transform: translate(50%, -50%)
}
<div id="wrapper">
<div id="parent">
<div id="child"></div>
</div>
</div>
can anyone help me on how to remove the huge white space between the two images? Both images are in their respective divs with layer effects when hovered. I have tried changing display to inline-block and setting font-size to 0 but nothing works. I also want the two images to be at the center when adjusted. I may have incorrectly apply the mentioned efforts to different classes or divs throughout the process but I am not sure where I did wrong.
Attached are the html and css along with a screenshot of how it looks like on local server. I hope the attachments are useful. Thank you.
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.campus-col{
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.campus-col img{
width: 100%;
display: block;
}
.layer{
background: transparent;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
transition: 0.5s;
}
.layer:hover{
background: rgba(226,0,0,0.7);
}
.layer h3{
width: 100%;
font-weight: 500;
color: #fff;
font-size: 26px;
bottom: 0;
left: 50%;
transform: translateX(-50%);
position: absolute;
opacity: 0;
transition: 0.5s;
}
.layer:hover h3{
bottom: 49%;
opacity: 1;
<div class="row">
<div class="campus-col">
<img src="#">
<div class="layer">
<h3>TEXT</h3>
</div>
</div>
<div class="campus-col">
<img src="#">
<div class="layer">
<h3>MESSENGER</h3>
</div>
</div>
</div>
Like this?
If so you just need to use display: flex and align-items: flex-start
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.row {
display: flex;
align-items: flex-start
}
.campus-col{
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.campus-col img{
width: 100%;
display: block;
}
.layer{
background: transparent;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
transition: 0.5s;
}
.layer:hover{
background: rgba(226,0,0,0.7);
}
.layer h3{
width: 100%;
font-weight: 500;
color: #fff;
font-size: 26px;
bottom: 0;
left: 50%;
transform: translateX(-50%);
position: absolute;
opacity: 0;
transition: 0.5s;
text-align: center;
}
.layer:hover h3{
bottom: 49%;
opacity: 1;
<div class="row">
<div class="campus-col">
<img src="https://via.placeholder.com/150">
<div class="layer">
<h3>TEXT</h3>
</div>
</div>
<div class="campus-col">
<img src="https://via.placeholder.com/150">
<div class="layer">
<h3>MESSENGER</h3>
</div>
</div>
</div>
Try to make row flex container, then align content to center, with gap you can make space between images:
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.row {
display: flex;
justify-content: center;
gap: 1em;
}
.campus-col{
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
}
.campus-col img{
width: 100%;
display: block;
}
.layer{
background: transparent;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
transition: 0.5s;
}
.layer:hover{
background: rgba(226,0,0,0.7);
}
.layer h3{
width: 100%;
font-weight: 500;
color: #fff;
font-size: 26px;
bottom: 0;
left: 50%;
transform: translateX(-50%);
position: absolute;
opacity: 0;
transition: 0.5s;
}
.layer:hover h3{
bottom: 49%;
opacity: 1;
<div class="row">
<div class="campus-col">
<img src="https://picsum.photos/200">
<div class="layer">
<h3>TEXT</h3>
</div>
</div>
<div class="campus-col">
<img src="https://picsum.photos/200">
<div class="layer">
<h3>MESSENGER</h3>
</div>
</div>
</div>
you can use bootstrap class for width .campus-col or use custom width
You can use (justify-content: center) to center the children in the flex displayed parent, in short: center the .img in .row.
Then you can add margin for spaces between them (the method used in the code below).
Or you can use (justtify-content: space-between) and set the width of the parent (.row), then each .img will be at the edge or it's direction (left or right)
Check this for more detalis: A Complete Guide to Flexbox
The Code:
.row {
display: flex;
justify-content: center;
}
.img {
width: 200px;
height: 300px;
border: 1px solid;
border-radius: 6px;
}
.img {
margin: 0 20px;
}
<div class="row">
<div class="img img1"></div>
<div class="img img2"></div>
</div>
Solution based on your code:
Edited:
.row {
display: flex;
justify-content: center;
}
.campus-col{
height: 200px; /* delete later, added to see the changes */
border: 1px solid #ddd; /* delete later, added to see the changes */
margin: 0 10px; /* add/remove spaces (left right of each one) */
}
The Code:
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.row {
display: flex;
justify-content: center;
}
.campus-col{
flex-basis: 32%;
border-radius: 10px;
margin-bottom: 30px;
position: relative;
overflow: hidden;
height: 200px;
border: 1px solid #ddd;
margin: 0 10px;
}
.campus-col img{
width: 100%;
display: block;
}
.layer{
background: transparent;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
transition: 0.5s;
}
.layer:hover{
background: rgba(226,0,0,0.7);
}
.layer h3{
width: 100%;
font-weight: 500;
color: #fff;
font-size: 26px;
bottom: 0;
left: 50%;
transform: translateX(-50%);
position: absolute;
opacity: 0;
transition: 0.5s;
}
.layer:hover h3{
bottom: 49%;
opacity: 1;
}
<div class="row">
<div class="campus-col">
<img src="#">
<div class="layer">
<h3>TEXT</h3>
</div>
</div>
<div class="campus-col">
<img src="#">
<div class="layer">
<h3>MESSENGER</h3>
</div>
</div>
</div>
I want my image hovers to look like this,
but For some reason, my CSS styling is not working. You may notice the #content in each of the CSS styling options. That is because I only wanted these styles to apply to a certain section of my website. I looked online and used the W3Schools resource, yet for some reason, it still doesn’t work. My images do not have the hover effect.
#content.container {
position: relative;
width: 50%;
}
#content img {
width: 100%;
height: auto;
border-radius: 50%;
}
#content .column {
float: left;
width: 33.33%;
padding: 5px;
}
#content .row::after {
content: "";
clear: both;
display: table;
}
#content .overlay {
position: absolute;
bottom: 100%;
left: 0;
right: 0;
background-color: #008CBA;
overflow: hidden;
width: 100%;
height: 0;
transition: .5s ease;
border-radius: 50%;
}
#content .img:hover .overlay {
bottom: 0;
height: 100%;
opacity: 0.5;
}
#content.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<div class="container">
<div class="column">
<div class="team">
<div class="img">
<img src="/static/img/team/team-1.jpg" alt="" style="width: 200px;">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<h3 class="team-prof">
Dr. Pawan Kumar Kesari
</h3>
</div>
</div>
</div>
It seems you haven't followed THIS guide very well.
You also have not identified what #content is.
You need to revise how you target IDs, Classes and Elements in CSS.
Here is your code remodified to follow the guide, plus I added in #content for you.
#content .img {
position: relative;
width: 50%;
}
#content img {
display: block;
width: 200px;
height: auto;
border-radius: 50%;
}
#content .overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 200px;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
border-radius: 50%;
}
#content .img:hover .overlay {
opacity: 1;
}
#content .text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<div id="content">
<div class="container">
<div class="column">
<div class="team">
<div class="img">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<h3 class="team-prof">
Dr. Pawan Kumar Kesari
</h3>
</div>
</div>
</div>
</div>
There were flaws in your code. There was no shared container. There were no spaces in the css rules between the identifier and the classes. #content .overlay had a height: 0 - this makes the object invisible. My answer isn't perfect, but I've tidied up the code a bit.
.main {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.main_content {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 1000px;
}
.main_content_box {
display: flex;
flex-direction: column;
align-items: center;
cursor: pointer;
}
.main_content_box_image {
position: relative;
}
.main_content_box:hover > .main_content_box_image p {
display: flex;
}
.main_content_box:hover > .main_content_box_image img {
filter: brightness(25%);
}
.main_content_box:hover a {
color: #337ab7;
}
.main_content_box_image img {
width: 250px;
height: 250px;
border-radius: 125px; /* or 50%*/
transition: .5s;
}
.main_content_box_image p {
display: none;
position: absolute;
top: 40%;
left: 20%;
right: 0;
font-size: 14px;
color: #fff;
}
.main_content_box a {
color: #333;
font-size: 16px;
font-weight: bold;
text-decoration: none;
margin: 20px 0 0 0;
}
<div class="main">
<div class="main_content">
<div class="main_content_box">
<div class="main_content_box_image">
<img src="https://icdn.lenta.ru/images/2019/11/01/13/20191101130724350/pwa_vertical_1024_f1555b2890fcb39f7ecc85cf65e73fc5.png" alt="">
<p>MBBS, General Physician</p>
</div>
Dr. Pawan Kumar Kesari
</div>
<div class="main_content_box">
<div class="main_content_box_image">
<img src="https://icdn.lenta.ru/images/2019/11/01/13/20191101130724350/pwa_vertical_1024_f1555b2890fcb39f7ecc85cf65e73fc5.png" alt="">
<p>MBBS MD(Medicine) General Physician</p>
</div>
Dr. Nitin Kumar Srivastava
</div>
<div class="main_content_box">
<div class="main_content_box_image">
<img src="https://icdn.lenta.ru/images/2019/11/01/13/20191101130724350/pwa_vertical_1024_f1555b2890fcb39f7ecc85cf65e73fc5.png" alt="">
<p>MBBS General Consultant and Diabetician</p>
</div>
Dr. (Mrs) Halima
</div>
</div>
</div>
So, i'm triyng´to vertically center 2 divs with flexbox, it centers on all major browsers but it doesn't center properly on IE11.
Searching about this i found that there's a problem centering divs with min-height, but i'm not using it, so i don't know what is wrong or missing.
JSFiddle
.container {
position: relative;
display: flex;
align-items: center;
}
img {
max-width: 100%;
}
.prev {
width: 40px;
height: 40px;
background: rgba(0,0,0,.8);
position: absolute;
left: 30px;
}
.next {
width: 40px;
height: 40px;
background: rgba(0,0,0,.8);
position: absolute;
right: 30px;
}
<div class="container">
<img src="https://www.w3schools.com/css/trolltunga.jpg">
<div class="prev"></div>
<div class="next"></div>
</div>
You can remove the flexbox rules, and instead vertically center your divs using transform...
fiddle
.container {
position: relative;
}
img {
max-width: 100%;
display: block;
/* added */
}
.prev,
.next {
top: 50%;
transform: translateY(-50%);
width: 40px;
height: 40px;
background: rgba(0, 0, 0, .8);
position: absolute;
}
.prev {
left: 30px;
}
.next {
right: 30px;
}
<div class="container">
<img src="https://www.w3schools.com/css/trolltunga.jpg">
<div class="prev"></div>
<div class="next"></div>
</div>
Alternatively, if you want to keep the flexbox rules, you can add top, bottom and margin properties to satisfy IE.
fiddle
.container {
position: relative;
display: flex;
align-items: center;
}
img {
max-width: 100%;
display: block; /* added */
}
.prev,
.next {
top: 0;
bottom: 0;
margin: auto;
width: 40px;
height: 40px;
background: rgba(0, 0, 0, .8);
position: absolute;
}
.prev {
left: 30px;
}
.next {
right: 30px;
}
<div class="container">
<img src="https://www.w3schools.com/css/trolltunga.jpg">
<div class="prev"></div>
<div class="next"></div>
</div>
I'm trying to replicate this, essentially:
So basically two 50% <div>'s side-by-side, with some form of absolute positioning (I assume) to achieve the left box to go over the top of the right box (the red line is just representing the middle of the viewport)
Any hints? Thanks :)
.container {
width: 500px;
height: 300px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
.box1 {
width: 100%;
height: 100%;
background-color: blue;
transform: skewX(-20deg) translateX(-40%);
position: absolute;
z-index: 10;
}
.box2 {
width: 100%;
height: 100%;
background-color: red;
z-index: 0;
}
Should be pretty simple with CSS3.
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
I offer a version without the transformation, using pseudoelement. It is faster and does not distort the text.
.container {
width: 500px;
height: 300px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
.box1 {
width: 50%;
height: 100%;
background-color: blue;
position: absolute;
left: 0;
}
.box1::after{
background: linear-gradient(to bottom right, blue 50%, transparent 0);
content: " ";
width: 20%;
height: 100%;
position: absolute;
left: 100%;
z-index: 1;
}
.box2 {
width: 50%;
height: 100%;
background-color: red;
position: absolute;
right: 0;
}
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
Try this
.wrapper {
overflow-x: hidden;
}
.outer {
position: absolute;
width: 2000px;
left: 50%;
bottom: 0;
margin-left: -1000px;
display: flex;
justify-content: center;
align-items: center;
}
.left__inner {
background: goldenrod;
padding: 24px 48px;
flex: 1;
transform: skew(45deg);
display: flex;
justify-content: flex-end;
}
.right__inner {
background: #222;
padding: 24px 48px;
flex: 1;
transform: skew(45deg);
}
.left__text,
.right__text {
transform: skew(-45deg);
span {
font-weight: 200;
font-size: 36px;
text-transform: uppercase;
}
}
.left__text {
color: #3c3c3c;
}
.right__text {
color: Goldenrod;
}
<div class="wrapper">
<div class="outer">
<div class="left__inner">
<div class="left__text">
<span> so skewy</span>
</div>
</div>
<div class="right__inner">
<div class="right__text">
<span>span much angle</span>
</div>
</div>
</div>
</div>
I would do it like this
this is just an example, not a ready-made solution ))
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
.container {
display: flex;
}
.container div {
width: 50%;
height: 200px;
position: relative;
}
.container .left:before {
content: '';
position: absolute;
right: 0;
top: 0;
height: 100%;
transform: skewY(-1.5deg);
background: inherit;
}