Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a transparent image over a div without affecting the layout of the page?

Tags:

html

css

How do I put a transparent png image over a DIV without it affect anything else in the HTML document? I would do position absolute because it takes it out of the "normal flow of the document" but I have the whole website centered using "margin-left: auto;" and "margin-right: auto;"

like image 976
funk-shun Avatar asked Feb 22 '11 05:02

funk-shun


People also ask

How to make the background of a Div transparent using CSS?

The opacity of background for transparency can be made by using the rgba colors of CSS. If you want to make the background of div transparent, you may use CSS opacity property. However, the opacity property may affect the inner element of the div also and make them transparent too.

How to place text in a transparent box in HTML?

This is some text that is placed in the transparent box. First, we create a <div> element (class="background") with a background image, and a border. Then we create another <div> (class="transbox") inside the first <div>. The <div class="transbox"> have a background color, and a border - the div is transparent.

What is transparency div element overlap over an image?

This is the transparency div element overlap over an image. The above example contains the image over which the div element overlapped. The overlapped div element contains a background with transparency using the RGBA color of CSS.

How to change opacity/transparency of an element using CSS?

The opacity property specifies the opacity/transparency of an element. The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent: The opacity property is often used together with the :hover selector to change the opacity on mouse-over: The first CSS block is similar to the code in Example 1.


3 Answers

if you apply position: relative to the div you want to cover then position: absolute on the image will be calculated relative to the covered div rather than the whole page, if it is a child element. i.e.

<div id="tobecoverd">
    <p>your content...</p>
    <img src="./img/transparent.png" class="cover" />
</div>

div#tobecovered{
    position: relative;
}
div#tobecovered img.cover{
    position: absolute;
    /* position in top left of #tobecovered */
    top: 0; /* top of #tobecovered */
    left: 0; /* left of #tobecovered */
}
like image 197
Chris Avatar answered Sep 19 '22 18:09

Chris


Here's how it works. The example shows a transparent image absolutely positioned over another image, creating a mask.

Check working example at http://jsfiddle.net/39VG9/1/

like image 44
Hussein Avatar answered Sep 21 '22 18:09

Hussein


Other solution is to use after like this:

.image:after{
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background: url(https://i.imgur.com/DDdKwlk.png) no-repeat 0 0;
    background-size: cover;
}

:)

like image 26
ismaestro Avatar answered Sep 21 '22 18:09

ismaestro