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

Get rid of the drop-down shadow effect

Currently working on a website project for a client and looking to remove the blue "shadow" effect from the dropdown menus. I believe the code that needs editing is as shown below: .main_nav ul.sub-menu { position: absolute; top: 65px; width: ...

Create dynamic and interactive content by embedding text within SVG shapes using the powerful D

How can I write text into an SVG shape created with d3.js and limit it to stay inside the shape similar to this example http://bl.ocks.org/mbostock/4063582? I attempted to use a clipPath following the example provided. However, when inspecting with firebu ...

Tips for concealing a tab upon selecting an option from a dropdown menu

<ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#song">Song</a></li> <li id="image-tab"><a href="#image" data-toggle="tab">Image</a></li> </ul> <div class="tab-cont ...

Elements in Motion

Working on a parallax effect, I've managed to assign unique scroll speeds to (almost) every element. Moreover, these elements are programmed not to activate their scroll speed until they enter the viewport. Below is the JavaScript code for trigger co ...

Tips for creating a mobile-friendly Bootstrap carousel that is both scalable and zoomable while maintaining the default slide functionality

I have a website using Bootstrap 4.x carousel. The layout is user-scalable with the following viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2, shrink-to-fit=no">. Currently, users can pinch to z ...

Tips for adjusting the wrapper css rule of a Tabs component to left-align text in a vertical tab within Material-UI

Typically, when text is placed inside the wrapper, it is aligned in the center. Is there a way to adjust the wrapper rule in <span class="MuiTab-wrapper">Item One</span> so that the tab text is aligned to the left (similar to what w ...

What is the best way to position an image in the center over a video using HTML and CSS?

I am currently developing a website with a background video and I want to overlay a company logo on top of it while keeping it centered. My coding language of choice for this project is using HTML and CSS. Here's the HTML code snippet I have for this ...

Generate a hyperlink within a paragraph

Can anyone provide tips on how to turn a string from Json into a paragraph with a hyperlink included? <p>Dumy Dumy Dumy Dumy Dumy Dumy Dumy DumyDumyDumyDumy abc.com </p> Currently, the paragraph displays as is, but I would like to make abc.c ...

What is the importance of chaining selectors in order to utilize flexbox effectively?

Check out this code snippet: .flex-container { border: solid 1px purple; display: flex; } .flex-container div { border: solid 1px red; padding: 20px; margin: 20px; height: 100px; flex: 1 1 0%; } .flex-container ...

Getting a specific element from an Ajax response may involve using JavaScript methods to target the desired

Within my PHP file, there are multiple HTML elements that I am fetching in an AJAX response. Currently, all these HTML elements are being returned in my drop area. How can I adjust the code to only retrieve a specific element, such as an image, like so: P ...

The React sidebar expanded to cover the entire screen width

As I work on creating a dashboard page that will display a sidebar after the user logs in, I am facing an issue where the sidebar is taking up the full width of the page, causing my Dashboard component to drop to the bottom. You can view the image link of ...

Customize your webpage's style to reflect your unique identity with user

Is there a way to allow users to customize the style of a webpage? I know I need multiple CSS files, but how can I implement the code that enables this customization based on user preferences? ...

Hide all elements in jQuery that do not have a class assigned

I've implemented a straightforward jQuery script that toggles the "active" class on an li element when it is clicked: $('li').click(function() { $(this).toggleClass('active'); }); My goal is to hide all other li elements in t ...

Prevent users from selecting elements on the mobile site

Hey there! I'm currently working on preventing users from selecting items on my mobile site. While I've been successful in doing so on a regular website using the CSS class below .unselectable { -moz-user-select: -moz-none; -khtml-user-s ...

Crafting an iframe that dynamically adjusts with Tailwind CSS

I'm having trouble ensuring that an iframe fits properly within a div while using tailwind CSS. I'm struggling to make the iframe responsive, no matter what class I try. Can anyone suggest a class that would help me achieve responsiveness for an ...

Retrieve the value of a variable from a JSON decoded response

My json response has been decoded into an array $data: stdClass Object ( [outboundSMSMessageRequest] => stdClass Object ( [deliveryInfoList] => stdClass Object ( [deliveryInfo] => stdClass Object ( [address] => 8606142527 [deliveryStatus] => ...

Looking for a way to customize it using @emotion/styled? Dealing with the deprecated makeStyles from MUI problem?

I have been working on setting up my styling using @emotion since makestyles were removed in React 18. Despite making changes as per my research, I am not seeing any updates reflected on my page. The expected result looks like this: https://i.stack.imgur. ...

Pressing the 'Enter' key within a textarea in a JQuery

This question seems fairly straightforward. I have a text area where hitting "enter" submits the content. Even though I reset the text to "Say something..." after submission, the cursor continues to blink. Is there a way to require the user to click on ...

Setting the parent's height to match one of its children

I'm struggling to align the height of the pink '#images-wrap' with the main image. When there are too many small rollover images on the right, it causes the pink div to extend beyond the main image's height. If I could make them match i ...

Is there a way to display two cards side by side in mobile view?

On a desktop view, 2 cards are displayed in a row. I would like them to also appear in a row on mobile view. I tried using col-sm-4 but it's not working. How can I get the cards to display in a row on mobile view so that I can see both the product pho ...