horizontal center div's with a absolute position - html

I have 3 divs inside a section. The divs position is absolute because I want a really small gap between the 3 divs, but when I want to horizontal the divs nothing happens. What should I do?
#columnL {
width: 412px;
height: 705px;
position: absolute;
left: 0;
}
#columnM {
width: 412px;
height: 705px;
right: 0;
left: 0;
position: absolute;
}
#columnR {
width: 412px;
height: 705px;
position: absolute;
right: 0;
}
<section id="blabla">
<div id="columnL">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnM">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnR">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
</section>

Make a parent div with a relative positioning rule. Like this:
#blabla {
position: relative;
}
Indicate the margin: auto for each card - #columnL, #columnM and #columnR.
And push the width: 100% for the img tag. Like this:
img {
width: 100%;
}
But why do you need absolute positioning?
#blabla {
position: relative;
}
#columnL {
width: 412px;
height: 705px;
position: absolute;
left: 0;
margin: auto;
}
#columnM {
width: 412px;
height: 705px;
right: 0;
left: 0;
position: absolute;
margin: auto;
}
#columnR {
width: 412px;
height: 705px;
position: absolute;
right: 0;
margin: auto;
}
img {
width: 100%;
}
<section id="blabla">
<div id="columnL">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnM">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnR">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
</section>
Solution with flex and absolute positioning.
The absolute in this solution is an additional parent block, with a class of .absolute, as well as a flexible rule that sets the indentation by the gap rule.
#blabla {
position: relative;
}
.absolute {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: space-between;
gap: 30px;
}
#columnL {
width: 412px;
height: 705px;
}
#columnM {
width: 412px;
height: 705px;
}
#columnR {
width: 412px;
height: 705px;
}
img {
width: 100%;
}
<section id="blabla">
<div class="absolute">
<div id="columnL">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnM">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
<div id="columnR">
<div id="afbeeldingL">
<img src="https://www.spiralex.nl/wp-content/uploads/2021/01/110-14018-b-Petronas-V2017.jpg">
</div>
<div id="tekstL">
<h1 id="hoofdstuk1">TEXT</h1>
</div>
</div>
</div>
</section>

Related

CSS fit image to container

I am new to HTML and CSS and I am trying to learn as I develop on a project.
I am trying to make an image grid and when the mouse hovers the image an overlay text appears. The problem is that the image inside the container has some lateral gaps that I can't get remove: https://i.stack.imgur.com/weDkb.png
The HTML code is as follows:
<!-- language: lang-html -->
<body>
<h1>
Art Page!
</h1>
<div class="row">
<div class="column">
<a href="#">
<div class="container">
<img src="images/owl.jpg" class="image">
<div class="overlay">
<div class="text">Example 1</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<img src="images/girl_freedom.jpg" class="image">
<div class="overlay">
<div class="text">Example 2</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<img src="images/soldiers.jpg" class="image">
<div class="overlay">
<div class="text">Example 3</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<img src="images/brain.jpg" class="image">
<div class="overlay">
<div class="text">Example 4</div>
</div>
</div>
</a>
</div>
</div>
</body>
and the css style sheet is:
<!-- language: lang-css -->
body {
background-color: #fff;
padding: 0;
margin: 0;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
.column {
flex: auto;
}
.column img {
vertical-align: middle;
}
.container {
position: relative;
max-width: 100%;
}
.image {
display: block;
height: 100%;
max-width: 100%;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background-color: black;
}
.container:hover .overlay {
opacity: 0.5;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
Thanks in advance for your help!
Best regards
Or you have to equalize the image size with the template using the code below
change this
.image {
display: block;
height: 100%;
max-width: 100%;
}
to this:
.image {
display: block;
height: 100%;
width: 100%;
}
demo in jsfiddle
Or you have to put the image and the transparent layer inside a div and set Position: Relative.
body {
background-color: #fff;
padding: 0;
margin: 0;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
.column {
flex: auto;
text-align: center;
}
.column img {
vertical-align: middle;
}
.container {
position: relative;
max-width: 100%;
}
.image {
display: block;
height: 100%;
width: 100%;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background-color: black;
}
.container:hover .overlay {
opacity: 0.5;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
.img-container {
display: inline-table;
margin: auto;
position: relative;
}
<h1>
Art Page!
</h1>
<div class="row">
<div class="column">
<a href="#">
<div class="container">
<div class="img-container">
<img src=" https://www.w3schools.com/bootstrap/ny.jpg" class="image">
<div class="overlay">
<div class="text">Example 1</div>
</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<div class="img-container">
<img src=" https://www.w3schools.com/bootstrap/ny.jpg" class="image">
<div class="overlay">
<div class="text">Example 1</div>
</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<div class="img-container">
<img src=" https://www.w3schools.com/bootstrap/ny.jpg" class="image">
<div class="overlay">
<div class="text">Example 1</div>
</div>
</div>
</div>
</a>
</div>
<div class="column">
<a href="#">
<div class="container">
<div class="img-container">
<img src=" https://www.w3schools.com/bootstrap/ny.jpg" class="image">
<div class="overlay">
<div class="text">Example 1</div>
</div>
</div>
</div>
</a>
</div>
</div>

html: add link to overlapping images [pic]

with a lot of trialnerror and grinding on stack, I (we) have managed to get two images to overlap in a very specific way:
This has been achieved like so:
.container {
width: 900px;
margin: auto;
}
.centered {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container_2 {
position: relative;
padding-top: 41.3%;
}
.imageContainer {
width: 70%;
position: absolute;
top: 0;
}
.psuedoContainer {
position: relative;
padding-top: 59%;
}
.psuedoHolder {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
img {
height: 100%;
width: auto;
}
<div class="centered">
<div class="container">
<div class="component">
<div class="container_2">
<div class="imageContainer" style="left: 0;">
<div class="psuedoContainer">
<div class="psuedoHolder" style="text-align: left;">
<img src="./img/img_codemesomething.png" />
<a href="page_appDev.html"/>
</div>
</div>
</div>
<div class="imageContainer" style="right: 0;">
<div class="psuedoContainer">
<div class="psuedoHolder" style="text-align: right;">
<img src="./img/img_recordmesomething.png" />
<a href="http://google.de"/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
And as you can see, there are two links included into the main two pictures (HTML).
However, only one of the two is working (the google one isnt). This is probably because one images is sort of "above" the other one, and therefore the lower one can never be clicked. At least it cannot be clicked just like it is right now.
How would I go on about adding a link to both images, that, wherever the user clicks, except for in the white part between both images, the user gets directed to a different page?
Thank you :)
you can wrap the images with the a tag
.container {
width: 900px;
margin: auto;
}
.centered {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container_2 {
position: relative;
padding-top: 41.3%;
}
.imageContainer {
width: 70%;
position: absolute;
top: 0;
}
.psuedoContainer {
position: relative;
padding-top: 59%;
}
.psuedoHolder {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
img {
height: 100%;
width: auto;
}
<div class="centered">
<div class="container">
<div class="component">
<div class="container_2">
<div class="imageContainer" style="left: 0;">
<div class="psuedoContainer">
<div class="psuedoHolder" style="text-align: left;">
<a href="https://stackoverflow.com/questions/55650192/html-add-link-to-overlapping-images-pic/55650729#55650729"> <img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" />
</a>
</div>
</div>
</div>
<div class="imageContainer" style="right: 0;">
<div class="psuedoContainer">
<div class="psuedoHolder" style="text-align: right;">
<a href="https://stackoverflow.com/"> <img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" />
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

How can I add responsive centered text to my HTML item in Bootstrap 4?

I would like to know how I can add a responsive centered text element to my <a> element? It shouldn't be affected by the overlay color. Please let me know if there is a clean alternative way of the extra overlay div. I am using Bootstrap 4.
http://jsfiddle.net/or74zoy0/1
HTML
<div class="container">
<div class="row">
<div class="col-md-3">
<a class="item" href="#">
<div class="image">
<img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
<div class="overlay"></div>
</div>
</a>
</div>
</div>
</div>
CSS
.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}
This is one way to do it:
HTML
<div class="container">
<div class="row">
<div class="col-md-3">
<a class="item" href="#">
<div class="image">
<img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
<div class="overlay"></div>
</div>
<span class="title">Centered Text</span>
</a>
</div>
</div>
</div>
CSS
.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}
.title {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Or solving it with bootstrap classes:
HTML
<div class="container">
<div class="row">
<div class="col-md-3">
<a class="item" href="#">
<div class="image">
<img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
<div class="overlay"></div>
</div>
<div class="title d-flex justify-content-center align-items-center">
Centered Text
</div>
</a>
</div>
</div>
</div>
CSS
.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}
.title { position: absolute; top: 0; left: 0; right:0; bottom:0;}

Image does not align to middle

I have a row of divs that I am trying to build a gallery row with.
Despite using align: auto; on the images, They still seem to align to the left.
I was wondering if I am missing something.
here is my fiddle: http://jsfiddle.net/82nrw19x/
here is my code:
CSS:
.galleryRow{
}
.galleryIconOuter{
background-color: gray;
width: 20%;
height: 70px;
border-radius: 5px;
}
#wrapper {
min-width:100%;
min-height:100%;
border: 4px gainsboro solid;
margin: 0auto;
position: relative;
}
#im {
margin: 0 auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url("https://s31.postimg.org/x8rd1797f/product1.png");
background-repeat: no-repeat;
background-size: contain;
}
HTML:
<section id="products" class="about">
<div class="container">
<div class="info col-lg-12 text-center">
<h2>Products</h2>
<div class="galleryRow w3-row">
<div class="galleryIconOuter w3-col">
<div id="wrapper">
<div id="im">
</div>
</div>
</div>
<div class="galleryIconOuter w3-col">
<div id="wrapper">
<div id="im">
</div>
</div>
</div>
<div class="galleryIconOuter w3-col">
<div id="wrapper">
<div id="im">
</div>
</div>
</div>
<div class="galleryIconOuter w3-col">
<div id="wrapper">
<div id="im">
</div>
</div>
</div>
<div class="galleryIconOuter w3-col">
<div id="wrapper">
<div id="im">
</div>
</div>
</div>
</div>
</div>
</div>
</section>
any help or advice is appreciated.
Thank you in advance.
You are including the img as backgrounds, then you just need to set
background-position to center:
Try this:
#im {
margin: 0 auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url("https://s31.postimg.org/x8rd1797f/product1.png");
background-repeat: no-repeat;
background-size: contain;
/* ADD THIS LINE*/
background-position:center;
}
Or the shorthand:
background: url("https://s31.postimg.org/x8rd1797f/product1.png") no-repeat center/contain;
Updated Fiddle
Do you mean like this?
.galleryRow{
position: relative;
width: 80%;
left: 10%;
right: 10%;
}
Is that what you mean by wanting them to "align to middle?"

Navigation arrow over image

I'm trying to add navigation (Left - Right) arrows on the center of image.
But somehow I don't have images of fixed sizes. They can be of any height and width.
Here is what I tried http://jsfiddle.net/vh1fp0k0/
#case-example-cover {
width: 100%;
height: 100%;
}
#nav-container {
position: relative;
border: 1px solid red;
}
#nav-container div {
position: absolute;
top: -webkit-calc(50% - 19px);
top: -moz-calc(50% - 19px);
top: -ms-calc(50% - 19px);
top: calc(50% - 19px);
width: 38px;
height: 38px;
background-image: url("http://s7.directupload.net/images/140625/lhmdzrfd.png");
border: 1px solid blue;
}
#case-left {
left: 0;
}
#case-right {
right: 0;
}
<div id="case-example-cover">
<div id="nav-container">
<div id="case-left"></div>
<div id="case-right"></div>
<img src="http://placehold.it/200x299" />
</div>
</div>
<br>
<div id="case-example-cover">
<div id="nav-container">
<div id="case-left"></div>
<div id="case-right"></div>
<img src="http://placehold.it/400x400" />
</div>
</div>
Desired result
How to do achieve this ?
If you make the #case-example-cover div inline-block it shrink wraps to the image size.
JSfiddle Demo
#case-example-cover {
display: inline-block;
}
#nav-container {
position: relative;
border: 1px solid red;
}
#nav-container img {
display: block;
}
#nav-container div {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 38px;
height: 38px;
background-image: url("http://s7.directupload.net/images/140625/lhmdzrfd.png");
border: 1px solid blue;
}
#case-left {
left: 0;
}
#case-right {
right: 0;
}
<div id="case-example-cover">
<div id="nav-container">
<div id="case-left"></div>
<div id="case-right"></div>
<img src="http://placehold.it/200x299" />
</div>
</div>
<div id="case-example-cover">
<div id="nav-container">
<div id="case-left"></div>
<div id="case-right"></div>
<img src="http://placehold.it/400x400" />
</div>
</div>