Is there a way to ensure that the text stays positioned behind the initial image even as the window size is adjusted

<html>
<head>
<title> Example </title>
<style>
img:hover {opacity : 0.3;}
img {z-index : 1;
height : 33%;
width : 33%;

}
.first { position : absolute;
 top: 0%;
 left: 0%;}
 .second {position : absolute;
 top: 0%;
 left: 33%;}
 .third {position : absolute;
 top: 0%;
 left: 66%;}
</style>
</head>
<body>
This content will stay behind the first image when resizing the window.
<img class = "first" src = "http://www.dubcen.com/uploads/ex.jpg">
<img class = "second" src = "http://www.dubcen.com/uploads/ex.jpg">
<img class = "third" src = "http://www.dubcen.com/uploads/ex.jpg">

</body>
</html>​

Is there a way to make this text appear only behind the first image? I've tried using div elements but haven't found a working solution yet.

Answer №1

To create a clean layout, consider using position: absolute for your first image and organizing content into three separate boxes. Place text only in the first box and adjust the positioning accordingly. Take a look at the fiddle link provided for reference.

position: absolute;
top: 0;
left: 0;

For a potential solution, visit: https://jsfiddle.net/c4oz41nu/

If you have any questions or need further assistance, feel free to reach out!

Answer №2

Your HTML and CSS need some adjustments.

Avoid using position absolute in your code like you did in the example.

Check out the updated code below:

New HTML

<div class="imgHolder">
    <p>This text should stay behind the 1st image when resizing the window.</p>
    <img src="http://www.dubcen.com/uploads/ex.jpg">
</div>
<div class="imgHolder"><img src="http://www.dubcen.com/uploads/ex.jpg"></div>
<div class="imgHolder"><img src="http://www.dubcen.com/uploads/ex.jpg"></div>

Updated CSS

 img:hover {opacity : 0.3}
.imgHolder{float:left;width:33%;white-space:normal;position:relative}
.imgHolder p{position:absolute;top:0;display:none}
.imgHolder:hover p{display:block}

This should resolve the issue.

Answer №3

Here is the code snippet you can use:

<!DOCTYPE html>
<html>
<head>
<title> Example </title>
<style>
img:hover {
    opacity : 0.3;
}
img {
    height : 33%;
    width : 33%;
}
.first {
    z-index: 1;
    position : fixed;
    top: 0%;
    left: 0%;}
 .second {
    z-index: 0;
    position : absolute;
    top: 0%;
    left: 33%;
 }
 .third {
    z-index: 0;
    position : absolute;
    top: 0%;
    left: 66%;
 }
 p{
    top: 0;
    left: 0;
    position: absolute;
    z-index: 0;
 }
</style>
</head>
<body>
<p class="text">This text will stay behind the first image when the window is resized.</p>
<img class = "first" src = "http://www.dubcen.com/uploads/ex.jpg">
<img class = "second" src = "http://www.dubcen.com/uploads/ex.jpg">
<img class = "third" src = "http://www.dubcen.com/uploads/ex.jpg">
</body>
</html>​

To achieve this layout, I added a <p> tag for the text and set its z-index to 0. Each image, except the first one, also has a z-index of 0. The first image has a z-index of 1 to ensure it stays on top. The text is positioned absolutely and remains behind the first image even after resizing, but not behind the other images. I hope this explanation clarifies things for you :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Difficulty encountered while using CSS to customize a vue template

I'm currently working on a login page and experiencing difficulty styling Vue templates using CSS. Here is the code that works without Vue: HTML <body class="text-center"> <form class="form-signin"> <h1 class="h3 mb-3 fon ...

What is the reason for the website allowing me to scroll towards the right?

I'm encountering a strange issue with my code that I can't seem to figure out. The website keeps letting me scroll to the right as if there's an image or something off-screen, even though there isn't anything there. Any idea why this is ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Menu with full-width navigation and dropdown options

I am trying to create a navigation menu with a black background that spans the full width of the window. To achieve this, I have placed the ul element inside a div with overflow: hidden; set. However, I am facing issues where hovering over submenus causes ...

A method for merging the values of three text fields and submitting them to a hidden text field

<label class="textRight label95 select205">Cost Center: </label> <input type="hidden" name="label_0" value="Cost Center" /> <input type="text" name="value_0" class="input64 inputTxtGray" value="" maxlength="10" /> <input type=" ...

Updating the background image of a React app according to each component

After researching extensively and attempting various solutions, I am still struggling to make my app function correctly. My goal is to display a different background image depending on which component is being rendered on the screen. The app is built using ...

Unable to store the outcomes from [ngbTypeahead] in [resultTemplate]

I'm trying to integrate ngbTypeahead into my HTML using the code snippet below <ng-template #rt let-r="result" let-t="term"> <ngb-highlight [result]="r.FirstName" [term]="t"></ngb-highlight> </ng-template> <input name ...

In the Twitter Bootstrap documentation, which element is utilized for the sidebar?

I'm curious about the most efficient method for creating a sidebar like this using Bootstrap 4. https://i.sstatic.net/3eKwN.png From what I can see, it appears to be a custom component designed specifically for the documentation page, rather than ...

I am curious about when the CSS of my website will start impacting its search engine ranking

Initially, my perception of CSS was that it was only used for styling the appearance of a document when viewed in a browser. However, I soon came to realize that search engines also utilize CSS for indexing web pages. It is said that search engines do not ...

Increase visibility, decrease visibility by utilizing ng-hide/ng-show and functions

As I implement the show more/show less feature, I am uncertain if achieving a smooth effect is feasible. However, I believe it's worth reaching out to this community for some creative ideas on how to make it possible. I have a list of dynamic links w ...

The validation process did not pass because of a manually inputted value

When a user selects a file, the filename value is automatically inserted into the fileName field. However, the validation fails because the field is still considered empty until at least one more character is added. How can I fix this issue? This is how t ...

sidebar that appears upon the initial page load

I'm currently working on implementing a sidebar navigation panel for my website using JavaScript, HTML, and CSS. However, I am facing an issue where the sidebar automatically opens when the page is first loaded, even before clicking on the icon to ope ...

Refreshing an iframe located on disparate domains

There is a webpage called "main.jsp" within the domain "domain1". This page contains an iframe that loads content from another domain known as "domain2". Essentially, "main.jsp" serves as a common content platform, with the iframe displaying content from v ...

Encountering an issue where an error message indicates that a variable previously declared is now undefined

Currently, I am working on developing a small test application to enhance my coding skills. However, I have encountered a roadblock while attempting to display dummy information from a mongodb database that I have set up. I have tried various solutions bu ...

Increased space between the sidebar and the top bar

Currently, my code is a bit messy as I am still in the learning stages of bootstrap. I need some assistance on how to resolve the empty space that exists between the top and side bar elements. <!DOCTYPE html> <html lang="en"> <sty ...

Struggling to make jQuery apply .css("display", "block") upon clicking a link

I am having trouble with getting jQuery to display CSS properties when a link is clicked on. After testing the CSS properties, I found that setting the display to "block" in the style sheet works as expected. However, if I set the initial CSS display to " ...

Tips for utilizing jQuery or AJAX to automatically update a div every 10 seconds

Seeking assistance from fellow developers. Working on a PHP web app utilizing the Yii MVC framework and its array of tools. Specifically, I am looking to dynamically refresh a div every 10 seconds. Currently, my Ajax function looks like this: <script t ...

Guide to launching a web browser within a Phonegap app with a button click event

We are developing a PhoneGap application using HTML and JavaScript. Our goal is to integrate PayPal's website so that users can make payments with a simple button click event within the app, and then seamlessly return back to the application afterward ...

Progress Bar Rating System in Bootstrap 4

Trying to achieve a specific layout using Bootstrap 4: https://i.sstatic.net/Ym6Ov.jpg Disregarding "See All" and "4,327 Ratings" Progress so far has led to: https://i.sstatic.net/hPSRn.png The numerical ratings can be replaced with font icons. Provi ...

Creating a Pop-Up on Website Load with HTML, CSS, and Jquery

<script> // utilizing only jquery $(document).ready(function(){ $('#overlay-back').fadeIn(500,function(){ $('#popup').show(); }); $(".close-image").on('click', function() { $('#po ...