4

I'm starting to work on a website. I would like to have two background images. One would be a banner image at the top similar to the gray strip across the top of stack overflow's website. Then the other image is a image that I would have to create a textured background. This would be like the white space on this website but instead of a color it would be a repeatable image. Websites with the look i'm going for are pinterest.com, subtlepatterns.com, Facebook.com, etc...

I have tried many different things. I tried putting a background in the html tag in the css. That didn't work. I also tried putting two different background images in the body tag. That didn't work because it would just show the first one. I also tried creating the background in it's own class. But when I did that it wouldn't show up at all. Maybe I left something out of the html?

Currently I have Following code:

body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td {
margin: 0;
padding: 0;
background-image:url(../images/absurdidad.png);
background-repeat:repeat;
}

This has created the main background image that takes up the whole screen like I want it to. Know if I could add the second one just at the top to only repeat horizontally then it'll be great!

Thanks in advance for the help.

arttronics
  • 9,957
  • 2
  • 26
  • 62
user1493824
  • 41
  • 1
  • 1
  • 2
  • possible duplicate of [Can I have multiple background images using CSS?](http://stackoverflow.com/questions/423172/can-i-have-multiple-background-images-using-css) – Donal Fellows Dec 01 '12 at 08:28

5 Answers5

5

using commas, you can have multiple background in the same element:

background-image: url('first'), url('second');

and the same rule apply for other styling:

background-position: top, bottom;

And so on.

More informations @ css3.info

If you want widespread browser support, multiple DIVs is the only way to do it:

<div class="one">
  <div class="two">
  </div>
</div>

Good luck!

Edouard Reinach
  • 614
  • 2
  • 6
  • 10
  • I never knew that you could do the background-position: top, bottom; that's great to know. Thanks so much for the help! – user1493824 Jul 03 '12 at 02:52
2

The only way I know of to do this is to have multiple wrapper tags with background associated with them. For example:

<div style="background-image:url(../images/image1.png);">
    <div style="background-image:url(../images/image2.png);">
        <!-- div content -->
    </div>
</div>
King Skippus
  • 3,801
  • 1
  • 24
  • 24
  • I had put it in the div tag and it didn't work but it's because I was missing the fact that it had to be a wrapper tag with "style" in it. This is great! Thank you so much! – user1493824 Jul 03 '12 at 02:50
  • Well, it doesn't really have to be, my main point by illustration was to put an outer div and an inner div, each with images. I used the style tag here to hard-code the images, but you can also use css classes or ids, which is actually a better practice if you'll be inserting a lot of images in your code. – King Skippus Jul 04 '12 at 04:41
2

Here's a jsFiddle I just made: jsFiddle Banner and Repeatable Background

CSS:

body{
    background-image:url("http://silviahartmann.com/background-tile-art/images/grunge-background-seamless-repeating.jpg");
    background-repeat: repeat;
    color: white;
}

#headerBackground{
   position: fixed;
   width: 100%;
   height: 50px;
   background-color: gray;
}

#headerContent {
    background-image: url(http://lorempixel.com/700/50/city/5);
    background-position: center center;
    background-repeat: no-repeat;
    width: 100%;
    height: 100%;
    text-align: center;
}

HTML:

<div id="headerBackground">
    <div id="headerContent">Other Header Content</div>
</div>

Update: Revised jsFiddle with Hotlinkable repeatable background image.

arttronics
  • 9,957
  • 2
  • 26
  • 62
  • You are a genius!!! Thank You! Thank You! It worked great! The only thing that I changed was the #headerContent background-repeat to repeat-x. This was wonderful! I could not have done it on my own! :) – user1493824 Jul 03 '12 at 02:48
  • Thank you. Please complete the Stackoverflow Process by accepting this Answer since it resolves your Question. Once you get to 15rep points, then you can also perform a Upvote (This answer is useful) if that applies too. – arttronics Jul 03 '12 at 03:22
1

This will help you Can I have multiple background images using CSS?

You can use CSS3 to serve multiple backgrounds or some tag trickery to get it to work.

Community
  • 1
  • 1
leopic
  • 2,958
  • 2
  • 27
  • 42
1

size and position you can adjust.Here image2 will repeat.

background:url(image1.jpg),url(image2.jpg);
background-size:80px 60px;
-moz-background-size:80px 60px; /* Firefox 3.6 */
background-repeat:no-repeat,repeat;
background-position:top left,center center;
Prashobh
  • 9,216
  • 15
  • 61
  • 91