Group images that contains image overlay to the centre of the page - html

I am trying to center a group (a table with 3x3) of pictures to the center of the webpage, I manage to do it before adding image overlay to it. But since I added image overlay, the images are appearing on top left of the webpage. How do i group them and center them, also how am I supposed to get the image location so that when I set the image overlay, it goes to the specific picture as each picture will have different image overlay text.
CSS
.container {
position: relative;
width: 100px;
height: 100px;
}
.image {
display: block;
width: 100px;
height: 100px;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: red;
font-size: 20px;
font-weight: bold;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
HTML
<div style="text-align:center">
<div class="container">
<img src="wheel1.jpg" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="wheels2.jpg" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="wheel3.jpg" class="image"">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
`

You could center it using flexbox. Change your main div
<div style="text-align-center;">
......
</div>
to
<div style="display: flex; flex-direction: column;align-items: center;">
.....
</div>
And it should work.
.wrapper{
display: flex;
flex-direction: row;
justify-content: center;
}
.container {
position: relative;
width: 100px;
height: 100px;
}
.image {
display: block;
width: 100px;
height: 100px;
border: 1px solid red;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: red;
font-size: 20px;
font-weight: bold;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div class="wrapper">
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
</div>
Here is the fiddle.

Related

center icon + text on css hovered image

good day, i am trying to center my icon and text when i hover an image. my problem is that they dont seem to center themselves. ill show both my html and css. also, i am using bootstrap 4. i tried to center both the icon and text using the "overlay" and "overlay-text" tag but both are suspended at the top. any help would be appreciated.
Code:
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #f08300;
}
.container:hover .overlay {
opacity: 1;
}
.overlay-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;
}
.spacer {
margin: 20px 0px 20px 0px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12 text-align-center">
<h2 class="bold_font">BROWSE OUR CARDS</h2><br>
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_beauty.jpg">
<div class="overlay">
<img src="img/icon-beauty-white.png" class="img-responsive center-block card-icon-sm">
<div class="spacer overlay-text">BEAUTY</div>
</div>
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_health.jpg">
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_wellness.jpg">
</div>
</div>
</div>
You are on the right track, Wrap the content of what you want to show on the overlay and add CSS to that and adjust the images and text accordingly.
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #f08300;
}
.container:hover .overlay {
opacity: 1;
}
.overlay-content {
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;
}
.spacer {
margin: 20px 0px 20px 0px;
}
<div class="container">
<div class="row">
<div class="col-md-12 text-align-center">
<h2 class="bold_font">BROWSE OUR CARDS</h2><br>
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_beauty.jpg">
<div class="overlay">
<div class="overlay-content">
<img src="img/icon-beauty-white.png" class="img-responsive center-block card-icon-sm">
<div class="spacer overlay-text">BEAUTY</div>
</div>
</div>
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_health.jpg">
</div>
<div class="col-md-4 col-xs-12">
<img class="img-responsive" src="img/_stock_mwc_wellness.jpg">
</div>
</div>
</div>
Try to using flexboxes for a container and wrap icon and icon-text to div with class e.g. .icon-wrapper:
.overlay {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #f08300;
}
.overlay-text {
color: white;
font-size: 20px;
text-align: center;
}
.icon-wrapper {
display: flex;
flex-direction: column;
align-items: center;
}
You could wrap the image content together in a relative element and remove absolute position from the text.
something like this: http://jsfiddle.net/tus0kdcq/
update: http://jsfiddle.net/jimmyLi/kruz6eqn
pls try this css:
<style>
.overlay{
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
</style>

getting two divs to display side-by-side

How do I get the two images contained within the two divs to display side-by-side? I've tried changing the variables within container as display: inline-block; and float: left; as suggested by some other threads, but those did not work the way I tried them.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 50%;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background-color: #008CBA;
overflow: hidden;
width: 100%;
height: 0;
transition: .5s ease;
}
.container:hover .overlay {
height: 100%;
}
.text {
white-space: nowrap;
color: white;
font-size: 20px;
position: absolute;
overflow: hidden;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<h2>Slide in Overlay from the Bottom</h2>
<p>Hover over the image to see the effect.</p>
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
</body>
</html>
How it looks:
I want these to be displayed side-by-side, not on top of one-another.
If you wrap the two containers in a div set to display: flex you'll be fine.
<div class="wrapper">
<div class="container">
<!-- your content -->
</div>
<div class="container">
<!-- your content -->
</div>
</div>
.wrapper {
display: flex;
}
I hope it helps :)
Simply add float:left to the container class.
Result:

Round div with child div at the axis

Hi I have a design i'm trying to recreate using html and css
and this is what i have been able to create so far
this is my html structure
1:
my current html structure looks like with the round borders . when i try to position the inner div at the edges of the round border as in the design the
inner div images get cut. please can you help me fix this
thanks
<div id="container">
<div id="tshirt">image</div>
<div id="tshirt">image</div>
<div id="bag">image</div>
</div>
This should get you started.
.outercircle {
height: 500px;
width: 500px;
border-radius: 100%;
border: 2px dashed purple;
margin: 10% auto;
position: relative;
}
.item {
padding: 10px;
background: purple;
position: absolute;
transition: background .2s;
}
.item img {
border-radius: 100%;
}
.item-1 {
top: 50%;
left: -50px;
transform: translateY(-50%);
}
.item-2 {
top: 50%;
right: -50px;
transform: translateY(-50%);
}
.item-3 {
left: 50%;
top: -50px;
transform: translateX(-50%);
}
.item-4 {
left: 50%;
bottom: -50px;
transform: translateX(-50%);
}
.item:hover {
background: black;
}
<div class="outercircle">
<div class="item item-1">
<img src="http://placehold.it/100x100" alt="" />
</div>
<div class="item item-2">
<img src="http://placehold.it/100x100" alt="" /></div>
<div class="item item-3">
<img src="http://placehold.it/100x100" alt="" /></div>
<div class="item item-4">
<img src="http://placehold.it/100x100" alt="" /></div>
</div>

Implementing Image Overlays in case of Multiple Images

I tried to get an example from w3schools running, but in my case there are multiple images. The overlay is displayed for the entire window instead of the 1st image (I plan to do this for all images later).
Here is my code:
.flex-container {
display: flex;
}
.col1 {
position: relative;
display: block;
}
.img1-wrap {
display: block;
}
.image {
display: block;
/*width: 100%;*/
height: auto;
}
.overlay {
display: block;
position: absolute;
bottom: 0;
left: 0;
right: 0;
background-color: #008CBA;
overflow: hidden;
/*width: 100%;*/
height: 20%;
transition: .5s ease;
}
.img1-wrap:hover .overlay {
height: 100%;
}
.text {
white-space: nowrap;
color: white;
font-size: 20px;
position: absolute;
overflow: hidden;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div class="flex-container">
<div class="col col-1">
<div class="img1-wrap">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image2">
</div>
<div class="col col-3">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image3">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image4">
</div>
</div>
Add position:relative to .img1-wrap and give a try.
When you make a element positioned absolute, Make sure its parent
is positioned relative
CSS
.img1-wrap {
display: block;
position: relative;
}
Is this what you want to achive Working Fiddle
Hope this helps..
It seems you are not using positioning the right way. Use relative for each element block with the text container.
You should restructure your HTML & CSS in such way:
.flex-container {
position: relative;
display: flex;
}
.img1-wrap {
position: relative;
overflow: hidden;
width: 200px;
}
.image {
width: 100%;
}
.overlay {
display: block;
position: absolute;
bottom: 0;
left: 0;
right: 0;
background-color: #008CBA;
overflow: hidden;
/*width: 100%;*/
height: 20%;
transition: .5s ease;
}
.img1-wrap:hover .overlay {
height: 100%;
}
.text {
white-space: nowrap;
color: white;
font-size: 20px;
position: absolute;
overflow: hidden;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div class="flex-container">
<div class="img1-wrap">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="img1-wrap">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="img1-wrap">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="img1-wrap">
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
</div>
Hope this helps and this is what you're trying to achieve.

Issue horizontally centering an element with absolute position

I have searched for similar questions however unfortunatley the left:50% soultion does not work here.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally. Adding left: 50% doesn't actually center it however as the container has a with of 85% I also tried 42.5% but this didn't work either.
I've removed all unnecessary code.
HTML:
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
CSS:
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
}
If you add this rule, where flex: 1 tells the flex items (in this case the first div and the last img) to take all the available space (and since they are 2 they share it 50/50)
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
And the use left: 50%, transform: translate(-50%) like this it will work
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
Added borders on the two so it clearly shows
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
border: 1px solid gray;
}
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
.leftLanding div:first-child {
background: lightblue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
</div>
To center an absolutely positioned element horizontally, use a combination of left: 50%; transform: translateX(-50%); and it will center it relative to it's closest non-static positioned ancestor.
Though I'm not sure what you're trying to do with this layout, so not sure if that's really what you're looking for, but added some borders/background colors to show the children are centered horizontally.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally.
This will center .imageCenter in .leftLanding.
.leftLanding {
display: flex;
position: relative;
width: 85%;
transition: all 0.5s ease;
background: #aaa;
border: 1px solid blue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translateX(-50%);
background: #eee;
border: 1px solid red;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
Try to set the .imageCenter to width:100% and style display:block margin:auto to the img tag
.imageCenter {
position: absolute;
width: 100%;
height: 30%;
align-self: center;
z-index: 100;
}
img{
display: block;
margin: auto;
}