Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elements won't show, appear to be hidden behind background image?

Tags:

html

css

xhtml

Can soneone give me a hint as to why my element(id="stuff") won't show?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
 <title>blah</title>
<style type="text/css" media="screen, print, projection">

body{     
    width: 100%;     
    height: 100%;     
    margin: 0;     
    padding: 0;      
}
img#background {     
    position: absolute;     
    top: 0;     
    left: 0;     
    width: 100%;    
    height: 100%; 
    margin:0;     
    padding:0;
} 
#stuff{
background: black;
height: 50px;
width: 100px;
z-index: 2;
}


</style> 
</head>
<body>
<img id="background" src="greenbackground.png" alt="Background Image" />
<div id="stuff"><p>stuff</p></div>



</body>
</html>
like image 909
javi Avatar asked Jul 08 '11 23:07

javi


People also ask

Why is my CSS background image not working?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Why is my Div background not showing?

I recommend moving your css from the inline scope. Assuming that your . png file actually exists, try setting the background size and repeat tags. If that doesn't work, try checking in your browser's developer tools for the response codes and making sure that the url is correct.

How do I make my background image no-repeat the background image is only shown once?

You have to set the background-repeat property to no-repeat . This will ensure that the background-image is not repeated. The image will only be shown once. To fit the image into each cell you can use background-size: cover and background-position: center .

What feature is used to set the background image of an element?

The background-image CSS property sets one or more background images on an element.


2 Answers

z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). Adding the position line to #stuff will fix this.

#stuff{
 position:relative;
background: black;
height: 50px;
width: 100px;
z-index: 2;
}
like image 157
JBrooks Avatar answered Sep 24 '22 16:09

JBrooks


Give it a position other than static:

#stuff{
    background: black;
    height: 50px;
    width: 100px;
    z-index: 2;
    position:relative;
}
like image 43
AlienWebguy Avatar answered Sep 22 '22 16:09

AlienWebguy