Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Background Repeat

Is there any way to make a background image stretch rather than repeat?

like image 200
Ben Shelock Avatar asked Dec 19 '25 00:12

Ben Shelock


1 Answers

Not using any kind of cross-browser compatible CSS (there is the background-size property however.)

If this is directed at any browser in particular, it might be possible. Otherwise, you'll need to use an <img> and stretch that.

Here's how you do it in recent browsers:

body {
    background-image: url(bg.jpg);
    -moz-background-size: 100% 100%;     /* Gecko 1.9.2 (Firefox 3.6) */
    -o-background-size: 100% 100%;       /* Opera 9.5 */
    -webkit-background-size: 100% 100%;  /* Safari 3.0 */
    -khtml-background-size: 100% 100%;   /* Konqueror 3.5.4 */
}

Otherwise, using an <img>:

img#background {
    /* height: 100%; Note: to keep ratio, don't use height */
    left: 0;
    position: fixed; /* or absolute, if you like */
    top: 0;
    z-index: -1;
    width: 100%;
}
like image 102
Blixt Avatar answered Dec 21 '25 17:12

Blixt