Positioning text over layered images - html

I'm creating layered images right now but i need to somehow get text over each layered image. Every time I try adding it, it keeps just sitting next to the image itself. I can't get the positioning down.
#container {
position: relative;
width: 65%;
top: 0;
left: 0;
}
.imageOne {
position: relative;
z-index: 10;
top: 0;
left: 0;
}
.imageTwo {
position: absolute;
top: 66px;
left: 35%;
z-index: 20;
}
.imageThree {
position: absolute;
top: 190px;
left: 10%;
z-index: 30;
}
<div id="container">
<div>
<img class="imageOne" src="https://www.fillmurray.com/380/150/">
</div>
<div>
<img class="imageTwo" src="https://www.fillmurray.com/260/155/">
</div>
<div>
<img class="imageThree" src="https://www.fillmurray.com/270/100/">
</div>
</div>
I created a jsfiddle to see what it looks like with the images layered here:
https://jsfiddle.net/k1rpycq2/

I would use pseudo-selector to display text over the images, but the div element that contains the image must be of the same width as the image.
Do note that I had to switch the class attribute to the parent div element.
[edit] I don't like writing text in CSS so I used a data-attribute instead on the pseudo-element.
content: attr(data-caption);
#container {
position: relative;
/*width: 65%;*/
top: 0;
left: 0;
}
.imageOne {
display: inline-block;
position: relative;
z-index: 10;
top: 0;
left: 0;
}
.imageTwo {
position: absolute;
top: 66px;
left: 35%;
z-index: 20;
}
.imageThree {
position: absolute;
top: 190px;
left: 10%;
z-index: 30;
}
.imageOne::before,
.imageTwo::before,
.imageThree::before {
content: attr(data-caption);
position: absolute;
top: 50%;
left: 0px;
right: 0px;
display: block;
transform: translateY(-50%);
padding: 0.5rem;
background-color: rgba(255,255,255,0.7);
font-family: Verdana;
text-transform: uppercase;
letter-spacing: 9px;
text-align: center;
}
<div id="container">
<div class="imageOne" data-caption="These">
<img src="https://picsum.photos/200/200">
</div>
<div class="imageTwo" data-caption="Beautiful">
<img src="https://picsum.photos/200/80">
</div>
<div class="imageThree" data-caption="Images">
<img src="https://picsum.photos/200/100">
</div>
</div>

What you can do, is to put the images as background in the targeted div elements using background-image and then positon your text in the div:
HTML
<div id="container">
<div>
text # 1
</div>
<div>
text # 2
</div>
<div>
text # 3
</div>
</div>
CSS
#container {
position: relative;
width: 65%;
top: 0;
left: 0;
}
.imageOne {
position: relative;
z-index: 10;
top: 0;
left: 0;
background-image:url(/image1-380x150.jpg);
background-size:contain;
}
.imageTwo {
position: absolute;
top: 66px;
left: 35%;
z-index: 20;
background-image:url(/image2-260x155.jpg);
background-size:contain;
}
.imageThree {
position: absolute;
top: 190px;
left: 10%;
z-index: 30;
background-image:url(/image3-270x100.jpg);
background-size:contain;
}

What we need to do here is work with how position:relative and position:absolute interact. Anything which is absolute is positioned absolutely in relation to the nearest ancestor with postition:relative
Using this, create a bounding div to layer your figures. Then use the figure tag with position:absolute positioning the figcaption within that using position:absolute.
#container {
position: relative;
width: 65%;
top: 0;
left: 0;
}
.imageOne {
position: relative;
z-index: 10;
top: 0;
left: 0;
}
.imageTwo {
position: absolute;
top: 66px;
left: 35%;
z-index: 20;
}
.imageThree {
position: absolute;
top: 190px;
left: 10%;
z-index: 30;
}
[class^="image"] figure {
position: relative;
}
[class^="image"] figure>figcaption {
position: absolute;
top: 0;
background-color: rgba(255, 255, 255, 0.8)
}
<div id="container">
<div class="imageOne">
<figure>
<img src="https://www.fillmurray.com/g/380/150/">
<figcaption>Bill One</figcaption>
</figure>
</div>
<div class="imageTwo">
<figure>
<img src="https://www.fillmurray.com/260/155/">
<figcaption>Bill Two</figcaption>
</figure>
</div>
<div class="imageThree">
<figure>
<img src="https://www.fillmurray.com/270/100/">
<figcaption>Bill Three</figcaption>
</figure>
</div>
</div>

Related

CSS, Rotated div makes page horizontal scroll

I am trying to design a footer for my website like this:
but I've got this result, causing a horizontal scroll (red background is for better showing the problem)
.footer-container {
background-color: white;
width: 100%;
position: relative;
}
.rotate-container {
position: relative;
width: 100%;
}
.rotate-white {
position: absolute;
left: 100px;
top: -40px;
width: calc(100%);
height: 200px;
transform: rotate(-4deg);
background-color: red;
z-index: -1;
}
.rotate-grey {
top: -40px;
right: 100px;
position: absolute;
width: calc(100%);
height: 200px;
transform: rotate(5deg);
background-color: #e5e5e5;
z-index: -2;
}
.footer-top-img {
width: 290px;
position: absolute;
left: 20%;
right: auto;
top: -140px;
z-index: 1;
}
<footer style="margin-top: 150px;">
<div class="footer-container px-xl-5 w-100">
<div class="rotate-container">
<div class="rotate-white"></div>
<div class="rotate-grey"></div>
</div>
<img class="footer-top-img d-xl-block d-none" src="../global/imgs/footer/footertop.png" />
<div class="container-fluid footer-content px-xl-5">
. . .
</div>
</div>
<footer/>
How can I remove the overflow of the red div to prevent the page from scrolling?
Therefor, you need to hide the oferflow of the body itself with
body {
overflow-y: hidden;
}

CSS image overlap positioning

What I am specifically looking to do, is have the blue block and red block scale with the webpage, but also remain in place without shifting up and down like they are now.
Here is a gif demonstrating that it does what I want somewhat when scaling diagonally, but scaling either vertically or horizontally results in off positioning.
Imgur link to demo gif
Here is the HTML
<div class="blockDisplay">
<center><img src="greenBlock.png" class="greenBlock">
<img src="redBlock.png" class="redBlock">
<img src="blueBlock.png" class="blueBlock"></center>
</div>
Here is the CSS
.blockDisplay {
background-color: #444;
overflow: hidden;
}
.greenBlock {
position: relative;
width: 58%;
z-index: 2;
}
.redBlock {
position: absolute;
top: 6%;
left: 66%;
width: 8vw;
z-index: 4;
}
.blueBlock {
position: absolute;
top: 40vh;
left: 77%;
width: 23vw;
}
Try this hope it's helpful for you.
CSS
.blockDisplay {
background-color: #444;
overflow: hidden;
}
.greenBlock {
position: relative;
width: 58%;
z-index: 2;
}
.redBlock {
position: absolute;
top: 6%;
left: 66%;
width: 8vw;
z-index: 4;
}
.blueBlock {
position: absolute;
top: 39vh;
left: 76%;
width: 40vw;
height: 10vw;
}
#divOnTop { z-index: 1000;
<div class="blockDisplay">
<center><img src="greenBlock.png" class="greenBlock">
<img src="redBlock.png" class="redBlock">
<img id="divOnTop" src="blueBlock.png" class="blueBlock"></center>
</div>
Then here it as as an answer
.blockDisplay {
background-color: #444;
overflow: hidden;
position: relative;
}
.greenBlock {
width: 58%;
z-index: 2;
}
.redBlock {
position: absolute;
top: 6%;
left: 66%;
width: 8vw;
z-index: 4;
}
.blueBlock {
position: absolute;
top: 40vh;
left: 77%;
width: 23vw;
}
<div class="blockDisplay">
<center>
<img src="greenBlock.png" class="greenBlock">
<img src="redBlock.png" class="redBlock">
<img src="blueBlock.png" class="blueBlock">
</center>
</div>

Remove overflowing clickable area

I have a navbar with a certain height and a logo that overflows. This logo is, of course, clickable, but it means that the part that overflows, is also clickable.
Is it possible to make the logo overflow, but not the clickable area?
HTML
<nav>
<a href="#">
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
</div>
</a>
</nav>
CSS
body, html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
}
JSFIDDLE
Something like this?
body,
html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
a {
display: inline-block;
height: 100%;
width: 100%;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
pointer-events: none;
}
<nav>
<a href="#">
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
</div>
</a>
</nav>
Just move logo outside of your link and put that link on rest of header:
body,
html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
}
a {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 10;
display: inline-block;
}
<nav>
<a href="#">
</a>
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
</div>
</nav>
Change little bit of you structure.
Put <a> independent and pass link in it with following css.
HTML
<nav>
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
</div>
</nav>
CSS
body, html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
nav a{
position: relative;
display: block;
height: 100%;
z-index: 1;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
}
Example : https://jsfiddle.net/67s4ajqf/3/
Don't place the whole div inside the a.
Place the link after the image, give it absolute positioning and carefully set the position and size.
The other answers make the whole header clickable. If it is not desired, use this solution. You may have to adjust the width of the clickable area.
See the example below:
body, html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
}
a.clickable-logo {
position: absolute;
display: inline-block;
left: 36px;
top: 36px;
width: 600px;
height: 100px;
}
<nav>
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
<a href="#" class="clickable-logo">
</a>
</div>
</nav>
What about something like this?
HTML
<a href="#">
<div class="clear">
</div>
</a>
<nav>
<div class="logo">
<img src="http://i.imgur.com/h4bUdrZ.png" />
</div>
</nav>
CSS
body, html {
padding: 0;
margin: 0;
}
nav {
height: 100px;
background: blue;
position: relative;
}
.clear {
height: 100px;
background: 0;
position: absolute;
width: 100%;
z-index: 2;
}
.logo {
position: absolute;
top: -36px;
left: -39px;
}

CSS: Make an image come to the top of a div

My HTML:
<div id="why">
<div class="gallery clearfix">
<img src="images/why-bg.png" class="background-why">
<div class="gallery-inner">
...
</div>
<div class="gallery-inner">
...
</div>
<div class="gallery-inner">
...
</div>
</div>
</div>
MY CSS:
#why{
height: 100%;
}
#why .gallery{
height: 100%;
width: 100%;
position: relative;
}
#why .background-why{
position: absolute;
top: 0;
left: 0;
float: left;
}
I want the .background-why to come over the .gallery. But it stay under the .gallery with my code. I think this image will help you to understand what I am expecting
Use the z-index property for your .background-why class.
Your css should look like this:
#why{
height: 100%;
}
#why .gallery{
height: 100%;
width: 100%;
position: relative;
}
#why .background-why{
position: absolute;
top: 0;
left: 0;
float: left;
z-index: 1;
}
Z-index is your answer. Set the z-index of the top element higher than your background ones
#why{
height: 100%;
}
#why .gallery{
height: 100%;
width: 100%;
position: relative;
z-index:1;
}
#why .background-why{
position: absolute;
top: 0;
left: 0;
float: left;
z-index:2;
}
Use z-index:
#why .background-why{
position: absolute;
top: 0;
left: 0;
float: left;
z-index:1
}
Depending on your needs:
1) If you wish that .background-why to be permanently over the .gallery-inner (that whole area is .gallery-inner) - jsfiddle.net/bw6ecbuy/1/
2) If you want the .background-why to come over the .gallery-inner on hover than: jsfiddle.net/a8yszLsf/2/
Leave a comment if I got this wrong.

How to place in center if two images

I have created this website. And now, I would like to center text on those two images in header. Relevent code is here
<div class="header">
<img src="css/title578145459.png" class="headerImage left"/>
<img src="css/title756941752.png" class="headerImage right"/>
<span class="headerText">Ubytovna Stavařov Přerov</span>
</div>
and CSS
.headerImage {
width: 50%;
height: 100%;
}
.header {
position: relative;
height: 190px;
text-align: center;
padding-top: 5px;
opacity: 0.8;
}
.headerText {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: -1;
color: yellow;
font: normal 3em sling;
font-style: oblique;
}
I tried to set different values to top and bottom attributes, also I've tried to set padding and margin but neither of these have worked. Thanks for any tips.
Your z-index on .headerText should be positive. Using Chrome dev tools I was able to see the text using this:
.headerText {
position: absolute;
top: 120px;
left: 0px;
right: 0;
z-index: 1;
}
Try this
.headerText {
position: absolute;
top: 50%;
left: 25%;
right: 25%;
z-index: 1;
}