Wrong layout when adding button to different images - html

I want to add button to images (display a button on top of an image with center position). Thing goes well if the image is 100% width. But if it's 50% width, the button isn't centered on top of the image.
.container {
position: relative;
width: 100%;
max-width: 1000px;
}
.container img {
width: 100%;
height: auto;
}
.container .btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
<h2>Button on Image</h2>
<p>Add a button to an image:</p>
<div class="container">
<img src="img_snow.jpg" alt="Snow" style="width:100%">
<button class="btn">Button</button>
</div>
<div class="container">
<img src="img_snow.jpg" alt="Snow" style="width: 200px; float:left;margin-left:15%">
<button class="btn">Button</button>
</div>
Wrong output:
Click to see the image
Thank you so so much!!

The button will stay in the center of container because the absolute position looks for nearest relative element.
now you have two options:
1- set image in center of container by adding a margin-left:25%
2- set container width to 50% instead of image width
look at this please :
<h2>Button on Image</h2>
<p>Add a button to an image:</p>
<div class="container">
<img src="https://picsum.photos/800/600" alt="Snow" style="width:100%">
<button class="btn">Button</button>
</div>
<div class="container">
<img src="https://picsum.photos/200/200" alt="Snow" style="width:50%; margin-left:25%;">
<button class="btn">Button</button>
</div>
<div class="container" style="width:50%">
<img src="https://picsum.photos/200/200" alt="Snow" style="">
<button class="btn">Button</button>
</div>
Code Pen

I suggest you style it like this. The container is dimensioned by the image size, and the button is positioned absolutely relative to the container at 50% horizontally and vertically by the half of its own dimensions, then translated leftwards and upwards by the half of the button's dimensions (-50%, -50%).
.container {
position: relative;
display: inline-block;
}
.container>img {
display: block;
}
.container>button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
<div class="container">
<img src="https://www.cinematheque.fr/cache/media/01-films/la_maison_du_docteur-e/s,725-75eea0.jpg">
<button>Button</button>
</div>
<div class="container">
<img style="width: 200px;" src="https://www.cinematheque.fr/cache/media/01-films/la_maison_du_docteur-e/s,725-75eea0.jpg">
<button>Button</button>
</div>

Related

Placing div on image

How i can place my div with button, form and other content on an image like this picture. I have already placed the text on this image using this code:
.img-wrap {
position: relative;
}
.img-wrap h1 {
position: absolute;
left: 41.5%;
bottom: 45%;
background: rgba(255, 255, 255, 0.9);
height: 20%;
width: 17%;
padding: 3% 0px 0px 2.4%;
font-size: 2.3vw;
font-weight: bold;
color: rgba(0, 0, 0, 1);
font-family: Alegreya Sans black;
}
<div class="container-fluid">
<div class="row">
<div class="img-wrap">
<img src="...\images\main-foto.jpeg" class="img-responsive">
<h1>Funny Island</h1>
</div>
</div>
</div>
I tried to place my div on the same way, but it doesn't work.
.img-wrap {
position: relative;
}
.img-wrap div {
position: absolute;
width: 1000px;
height: 900px;
background-color: grey;
}
<div class="container-fluid">
<div class="row">
<div class="img-wrap">
<img src="...\images\main-foto.jpeg" class="img-responsive">
<h1>Funny Island</h1>
<div>
</div>
</div>
</div>
</div>
Thanks for the help
Add the image as a background image on .img-wrap. You can then add the buttons, text etc inside the div.
.img-wrap {
position: relative;
background-image: url( 'http://placekitten.com/500/300' );
background-size: cover;
text-align: center;
padding: 50px;
color: red;
}
<div class="container-fluid">
<div class="row">
<div class="img-wrap">
<h1>Funny Island</h1>
<div>
<p>Some text here</p>
<button>Button here</button>
</div>
</div>
</div>
</div>
You can use a negative on themargin-bottom of the element that you wish to contain the image. This will pull the element containing your form content up and above the element with the image inside it. See below for a demo:
.image-container {
height: 50vh;
background: red;
margin-bottom: -200px;
}
.content-container {
height: 150vh;
max-width: 960px;
margin: auto;
background: blue;
}
<div class="image-container"></div>
<div class="content-container"></div>
Another option is to add top or left properties to img-wrap div to position it where you want.
.img-wrap div{
position: absolute;
width:1000px;
height: 900px;
background-color: grey;
top:100px;
left:100px;
}

How do I put text/headings over an image?

How would I put headings in the center of each of these images? For example over the first one "Music" in the center of the image
<div class="row">
<div class="item">
<img src="images/music_cover.png"/>
</div>
<div class="item">
<img src="images/event_cover.png"/>
</div>
<div class="item">
<img src="images/lights_cover.png"/>
</div>
</div>
The below is the CSS code:
/* Container holding the image and the text */
.container {
position: relative;
text-align: center;
color: white;
}
/* Bottom left text */
.bottom-left {
position: absolute;
bottom: 8px;
left: 16px;
}
/* Top left text */
.top-left {
position: absolute;
top: 8px;
left: 16px;
}
/* Top right text */
.top-right {
position: absolute;
top: 8px;
right: 16px;
}
/* Bottom right text */
.bottom-right {
position: absolute;
bottom: 8px;
right: 16px;
}
/* Centered text */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
The below is the HTML code.
<div class="container">
<img src="img_snow_wide.jpg" alt="Snow" style="width:100%;">
<div class="bottom-left">Bottom Left</div>
<div class="top-left">Top Left</div>
<div class="top-right">Top Right</div>
<div class="bottom-right">Bottom Right</div>
<div class="centered">Centered</div>
</div>
I hope this should work for you.
Suppose you introduced these headings into your HTML:
<div class="row">
<div class="item">
<h2>Heading 1</h2>
<img src="images/music_cover.png"/>
</div>
<div class="item">
<h2>Heading 2</h2>
<img src="images/event_cover.png"/>
</div>
<div class="item">
<h2>Heading 3</h2>
<img src="images/lights_cover.png"/>
</div>
</div>
You can achieve a quick centered heading result, over your images, by introducing the follow css:
.item {
position:relative;
}
.item h2 {
position:absolute;
text-align:center;
top:50%;
left:0;
right:0;
line-height:1;
margin-top:-0.5em;
}
Something like this I would imagine. Create a container that contains both your image and an absolute div to contain the text
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.item {
position: relative;
text-align: center;
color: white;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<div class="item">
<img src="images/music_cover.png" style="width:100%;"/>
<div class="centered">Centered</div>
</div>
</html>
Make the images backgrounds, use the different properties for position, attachment, size, etc. to get what you need
See https://developer.mozilla.org/en-US/docs/Web/CSS/background
Or a more reliable and scalable approach: put the image into an SVG via a tag (see https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_Image_Tag) and add text to the SVG

Fill flexible container with centered square component

What I try to achieve is shown on following pictures:
Outer rectangle is a div container that can have any size and ratio (it gets resized with browser window resizing), and inside is a component (filled on pictures) that should maintain a 1:1 ratio (square), and should be centered in the container. So its sides are described with the formula min(container_width, container_height).
Any ideas on how to do this?
This can be achieved with a combination of three things:
Flexbox
CSS variables
The calc() function
Flexbox can be used to ensure that the inner element is both horizontally and vertically centered. This is achieved with only three different rules on the container:
display: flex;
align-items: center;
justify-content: center;
The key to making the inner element stay square while the parent has a variable width is to base both thew width and height of the child off of the height of the parent.
In the following, I'm basing both the width and height of the inner square off of the height of the parent container (divided by four). Considering the height and width of the child is defined by the same --value as the height of the parent, it will always remain square and proportionate:
:root {
--value: 200px;
}
.container {
display: flex;
align-items: center;
justify-content: center;
border: 2px solid black;
height: var(--value);
}
.box {
background: black;
height: calc(var(--value) / 4);
width: calc(var(--value) / 4);
}
<div class="container">
<div class="box">
</div>
</div>
Note that this will also work if you base your CSS variable off of the viewport height with the vh unit:
:root {
--value: 50vh;
}
.container {
display: flex;
align-items: center;
justify-content: center;
border: 2px solid black;
height: var(--value);
}
.box {
background: black;
height: calc(var(--value) / 4);
width: calc(var(--value) / 4);
}
<div class="container">
<div class="box">
</div>
</div>
Hope this helps! :)
I think you can achieve your goal with the following code. You will need to set your image as the background of the inner div instead of using a <img> directly.
.wrapper {
width: 100%;
height: 300px;
padding: 10px;
box-sizing: border-box;
border: 1px solid red;
}
.inner {
background: url('http://lorempixel.com/output/abstract-q-c-300-300-7.jpg') no-repeat center center;
background-size: contain;
width: 100%;
height: 100%;
}
<div class="wrapper">
<div class="inner"></div>
</div>
Solution 2: using <img> and set the position to absolute.
.wrapper {
position: relative;
top: 0;
left: 0;
width: 100%;
height: 300px;
border: 1px solid red;
box-sizing: border-box;
}
.wrapper img {
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
max-width: 100%;
max-height: 100%;
}
<div class='wrapper'>
<img src='http://lorempixel.com/output/abstract-q-c-300-300-7.jpg'>
</div>
You can do it like this:
html, body {width:100%;margin:0}
.container {
position: relative;
height: 300px; /* needs to be at least the height of the image */
max-height: 100vh; /* enables vertical responsiveness */
border: 1px solid Skyblue;
}
img {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%); /* perfectly centered inside the container */
display: block; /* removes bottom margin/white-space */
max-width: 100%; /* horizontal responsiveness */
max-height: 100vh; /* vertical responsiveness */
}
<div class="container">
<img src="http://lorempixel.com/300/300" alt="">
</div>
If the square is an image in this case you can do something like this :
.container {
position:relative;
text-align: center;
border: 1px solid;
background:#f2f2f5;
}
img {
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
max-height: 100%;
max-width: 100%;
}
<div class="container" style="width:400px;height:100px;">
<img src="https://lorempixel.com/400/400/" />
</div>
<div class="container" style="width:200px;height:400px;">
<img src="https://lorempixel.com/400/400/" />
</div>
<div class="container" style="width:400px;height:400px;">
<img src="https://lorempixel.com/400/400/" />
</div>
<div class="container" style="width:50px;height:600px;">
<img src="https://lorempixel.com/400/400/" />
</div>
<div class="container" style="width:600px;height:50px;">
<img src="https://lorempixel.com/400/400/" />
</div>
You need to pay attention when using 100% with height as this will depend on the parent of the container and if nothing specified the height will be 0 and thus the image too :
.container {
position: relative;
text-align: center;
border: 1px solid;
background: #f2f2f5;
}
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-height: 100%;
max-width: 100%;
}
<!-- this one will not show -->
<div class="container" style="height:100%;">
<img src="https://lorempixel.com/400/400/" />
</div>
<div style="height:200px">
<!-- this one will show -->
<div class="container" style="height:100%;">
<img src="https://lorempixel.com/400/400/" />
</div>
</div>
if you want to use a div instead of image you can consider the image inside the div and use fit-content value for the width/height and the trick is to make the image not visible and add another div for text content (or anything else).
Pay attention as fit-content is not a standard so not supported by all browser. So you can consider this solution as a pseudo-solution than a generic one
.container {
position: relative;
text-align: center;
border: 1px solid;
background: #f2f2f5;
}
.content {
display: block;
background: red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-height: 100%;
max-width: 100%;
height: fit-content;
width: fit-content;
}
.content img {
visibility: hidden;
z-index: -999;
position: relative;
max-height: 100%;
max-width: 100%;
}
.text {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
}
<div class="container" style="width:400px;height:100px;">
<div class="content">
<img src="https://lorempixel.com/400/400/" />
<div class="text"> lorem ipsum lorem ipsum </div>
</div>
</div>
<div class="container" style="width:200px;height:400px;">
<div class="content">
<img src="https://lorempixel.com/400/400/" />
<div class="text"> lorem ipsum lorem ipsum </div>
</div>
</div>
<div class="container" style="width:400px;height:400px;">
<div class="content">
<img src="https://lorempixel.com/400/400/" />
<div class="text"> lorem ipsum lorem ipsum </div>
</div>
</div>
<div class="container" style="width:50px;height:600px;">
<div class="content">
<img src="https://lorempixel.com/400/400/" />
<div class="text"> lorem ipsum lorem ipsum </div>
</div>
</div>
<div class="container" style="width:600px;height:50px;">
<div class="content">
<img src="https://lorempixel.com/400/400/" />
<div class="text"> lorem ipsum lorem ipsum </div>
</div>
</div>

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:

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;
}