Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding background image to div using CSS

I have been trying to add background image to a div class using CSS, but I didn't have any success.

HTML code:

<header id="masthead" class="site-header" role="banner">     <div class="header-shadow"></div>         <hgroup></hgroup>         <nav role="navigation" class="site-navigation main-navigation">          </nav><!-- .site-navigation .main-navigation --> </header><!-- #masthead .site-header --> 

CSS:

.header-shadow{     background-image: url('../images/header-shade.jpg'); } 

Additional information:

This is an image with a shadow and I am using it at the top of the website, so it mustn't have a particular width.

like image 934
Orahmax Avatar asked Nov 19 '13 06:11

Orahmax


People also ask

How do you add an image to a div in HTML?

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file. The HTML image element is an “empty element,” meaning it does not have a closing tag.


2 Answers

You need to add a width and a height of the background image for it to display properly.

For instance,

.header-shadow{     background-image: url('../images/header-shade.jpg');     width: XXpx;     height: XXpx; } 

As you mentioned that you are using it as a shadow, you can remove the width and add a background-repeat (either vertically or horizontally if required).

For instance,

.header-shadow{     background-image: url('../images/header-shade.jpg');     background-repeat: repeat-y; /* for vertical repeat */     background-repeat: repeat-x; /* for horizontal repeat */     height: XXpx; } 

PS: XX is a dummy value. You need to replace it with your actual values of your image.

like image 114
Nitesh Avatar answered Sep 20 '22 09:09

Nitesh


Specify a height and a width:

.header-shadow{     background-image: url('../images/header-shade.jpg');     height: 10px;     width: 10px; } 
like image 37
Ramesh Avatar answered Sep 23 '22 09:09

Ramesh