Placing a translucent layer over an image

Let's discuss a situation where we have a <div> element with relative positioning, and inside it, there is an absolutely positioned <img>.

<div style="position: relative;">
  <img src="image.png" style="position: absolute;" />
  <span id="overlay_text">OVERLAY</span>
</div>

The issue at hand is that the element should be positioned at the top of the screen (higher on the Y-axis), but its distance seems to be calculated from the bottom instead.

Answer №1

Utilize the CSS properties z-index and top to create layered elements. Place the div at the bottom, followed by the image, and then the span acting as an overlay on top. Adjust the positioning from the top using top, allowing for negative values to position the element higher on the Y axis than its parent. In the example provided, the span is positioned 10px above the top of its parent div.

<div style="position: relative; z-index: 1;">
  <img src="image.png" style="position: absolute; z-index: 2;" />
  <span id="overlay_text" style="position: relative; top: -10px; z-index: 3;">OVERLAY</span>
</div>

Answer №2

For certain scenarios, this alternative approach proves to be more effective as it allows the parent div to adopt the height of the image:

<div style="position: relative; z-index: 1;">
  <img src="picture.png" />
  <span style="position: absolute; top: 0px; z-index: 5;">SUPERIMPOSE</span>
</div>

Answer №3

While I realize this question is not recent, I have observed multiple instances where z-index has been utilized, which might not be essential for this particular scenario and could present challenges when dealing with numerous stacked elements.

Note: The order of stacking goes from the bottom to the top. In this example:

<span>OVERLAY</span> is positioned below <img />. Hence, span appears on top of img.

<div style="position: relative;">
  <img src="https://upload.wikimedia.org/wikipedia/commons/6/67/Delta-shield.png" style="position: absolute;" />
  <span id="overlay_text" style="position: relative; top: 8rem; margin-left: 80px">OVERLAY</span>
</div>

For more insights on stacking, refer to this resource.

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

Extract the URL from the query parameters and inject it into a div element

I am currently working on a project to create an ASPX page with two separate div's. The first div will have static content, while I want the content in the second div to be dynamic. Users should be able to input a URL as a query string, and that URL s ...

The text size in Google Chrome always stays consistent across the page

Encountered a puzzling issue with a website today involving unusual behavior in Google Chrome. Despite using the "CTRL +" and "CTRL -" functions to adjust text size, the images enlarge while the text size remains the same. I have thoroughly checked the HTM ...

Utilize jQuery to toggle the visibility of subrows on demand

I am currently working on a problem and utilizing jQuery for the solution. Unfortunately, I do not have any code to share at the moment. Initially, only the Master Rows (with classes term1_master and term2_master) should be visible. When the "Show" link ...

Variety of color palettes within a single style sheet

My theme features various styles, including different color schemes. Currently, I load the default CSS first and then an additional stylesheet with specific colors based on the chosen color scheme. However, this process is causing a delay in my site' ...

What is the reason behind Flask's choice to return <embed> files for download rather than simply displaying them

When I try to render a template using Flask that includes images, the files are being downloaded instead of displayed. For example, <embed src="static/yes.svg" type="image/svg+xml"> If I place this code inside test.html and view it in Google Chrom ...

Aligning the text in the middle of a div

I could really use your assistance. How can I align the text in my customized alert box vertically? I assumed that by utilizing the following CSS properties: #alertBox_text { width: 100%; height: auto; text-align: center; margin-top: 10px; } Th ...

A full-width CSS menu featuring dropdowns beneath each entry for easy navigation

Check out the JSFiddle here I've created a full-width CSS menu that spans the entire screen, but I'm struggling to figure out how to make the corresponding subnav items appear below their parent items. Is it even possible to achieve this design ...

Arrange two text elements vertically

I'm having trouble aligning these two text elements (h1 & p) vertically. It seems like the header is preventing the paragraph from moving to the left side. https://i.sstatic.net/hmdip.png .container .links h1 { font-size: 20px; float: right; ...

Identifying the pressing of the enter key in an input field

Currently, I am developing an innovative IFrame browser that features an omnibox (identified as "address") and a "GO" button (identified as "submit"). One of the key challenges I'm facing is detecting when the user inputs information and hits the Ente ...

There are two borders in place, with the second border present only on the right and bottom

Can someone help me achieve borders like the ones in this image? https://i.sstatic.net/qZHbK.png I tried using the after pseudo-element as shown below, but after learning from this discussion: Why can't an element with a z-index value cover its child ...

Enhance Chart JS by adding scroll functionality for handling large amounts of data

I'm encountering an issue with chart js. When displaying thousands of entries in a single chart, the density makes it difficult to read. I am looking for a way to implement a scrollbar when the data surpasses a certain threshold (or when there is a sp ...

The cropper fails to load within the image element inside a modal pop-up

Utilizing fengyuanchen's cropper library, I am cropping an uploaded image and then submitting it to a form. <div id="change-dp" uk-modal> <div class="uk-modal-dialog uk-modal-body"> <button class="uk ...

Why is it that the z-index doesn't seem to affect the stacking order of my background image?

I am facing an issue with my Bootstrap carousel where the z-index is not working as expected. The decoration I have in the background is overlapping the text instead of appearing behind it. I want the illustration to be positioned under the text. .carou ...

Customize the color of arrows within an ng-bootstrap carousel

I am currently working on a carousel using ng-bootstrap, and I need to change the color or image of the arrows from white to black because my background images are white and not visible. The issue is that I am having trouble accessing span, and I am unsure ...

Retrieve information from the table and display it in a reverse order

Below is a link to view the table in my database: click here I want to retrieve and display the names of users whose user_group is "member". I plan to put their ratings in an array and then print their names in descending order based on their rating. For ...

Leveraging createMuiTheme to customize default styles for divs, paragraphs, and the body element

Seeking guidance on customizing a Material UI Theme I aim to modify the default styles for elements like <body>. Currently, at the root of my React tree: import theme from './mui-theme' ReactDOM.render( <Router> <ThemePr ...

Passing a selected option in a select box to another website is achievable using JavaScript

I'm still learning JavaScript and I would like to have a user be directed to another page when they select an option from a dropdown menu. Any suggestions on how to accomplish this? ...

What could be causing the issue with the padding-top not being applied to my section?

I'm having trouble getting the desired 150px padding to appear on the top and bottom of a section in my project. Here's an example: Below is the code for the section: .wp-block-mkl-section-block .section-bg { position: absolute; top: ...

What is the reason behind JavaScript being able to alter pre-designed shiny UI components but not ones that are inserted through insert

Is it possible to use JavaScript to control attributes of a class of UI elements, including those added dynamically using insertUI? In this scenario, a colored box is inserted each time a button is pressed. The color is meant to change based on user input ...

The background image and svgs are not displayed on laravel localhost/public/index.php, but they appear when using "php artisan serve"

I've created a beautiful Laravel project on my PC. The welcome.blade.php file in the project has a background image located in the: public/images/ directory. To display the images, I've used the following CSS: html, body { color: #f4f6f7; ...