Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overlay a text over another using CSS?

Tags:

html

css

I have this html file where I want to overlay some text over another. I tried to use the z-index property but can't get it to work. What is missing in my code?

thanks a lot

Here is the code

<html>
<head>
<style>
#overlay {
    z-index:100;
}
</style>

</head>
<body>
<div id='overlay'>overlayed text</div>

This is some dummy text

</body>
</html>
like image 388
eric01 Avatar asked Mar 19 '12 18:03

eric01


3 Answers

You need to set position: absolute. The z-index applies to elements that aren't statically positioned (see BoltClock's comment).

<html>
<head>
<style>
#overlay {
    background-color: #EEEEEE;
    position: absolute;
    z-index:100;
}
</style>

</head>
<body>
<div id='overlay'>overlayed text</div>

This is some dummy text

</body>
</html>
like image 81
Mario Aguilera Avatar answered Oct 17 '22 12:10

Mario Aguilera


You can do this with CSS alone by making use of :before

img.image:before {
    left: 0;
    content: "Coming Soon";
    background: rgba(255, 255, 255, 0.59);
    color: #000;
    width: 100%;
    height: 100%;
    line-height: 2.5;
    font-weight: 600;
   position: absolute; 

}
like image 43
raison Avatar answered Oct 17 '22 14:10

raison


z-index only applies to elements with position:relative or position:absolute. In your case you'll be wanted to use position:absolute to place the overlayed text.

like image 1
bhamlin Avatar answered Oct 17 '22 12:10

bhamlin