Results 1 to 10 of 10

Thread: [RESOLVED] CSS Stretch Background image?

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Resolved [RESOLVED] CSS Stretch Background image?

    is there a way to have a centered Background image stretch according to screen size?
    this doesnt work

    Code:
    body {
    background-image: url('bg_main.jpg');
    background-repeat: no-repeat;
    background-position: center;
    background-width: 100%;
    background-height: 100%;
    background-color: black;
    }
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: CSS Stretch Background image?

    nevermind.. found out u cant.... have to create a layer and stretch that.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    New Member
    Join Date
    Jun 2006
    Posts
    2

    Re: [RESOLVED] CSS Stretch Background image?

    HOw do you do it with a layer?
    I'm gueesing
    Code:
    <layer background="MemDayFlag.jpg" height="100%" width="100%">
    and am trying this as I ask.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] CSS Stretch Background image?

    There is no such thing as a layer in HTML. There's no way to stretch a background image.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] CSS Stretch Background image?

    It's usually done by faking the background. You place the image in a div (which is what he probably meant by layer :cringe: ), and then stretch the div.
    Code:
    #divbg {
    width: 100%;
    height: 100%;
    left: 0px;
    top: 0px;
    position: absolute;
    z-index: 0;   <------------Notice!
    }
    
    #maincontent
    {
    z-index:1; <--------------Notice!
    position:absolute;
    }
    Then in your HTML,

    Code:
    <div id="divbg"><img src="abc.jpg" width="100%" height="100%"/></div>
    <div id="maincontent">all text here</div>

  6. #6
    New Member
    Join Date
    Jun 2006
    Posts
    2

    Re: [RESOLVED] CSS Stretch Background image?

    Quote Originally Posted by penagate
    There is no such thing as a layer in HTML. There's no way to stretch a background image.


    Yeah that's old HTML.

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] CSS Stretch Background image?

    Nothing new really, but just to show how a CSS expert has done it:
    http://www.cssplay.co.uk/layouts/background.html

  8. #8

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] CSS Stretch Background image?

    Layer was the wrong choice of words... sorry

    basically I need an image to cover the screen... and always center so if u were 800x600 the logo would still be in the middle....

    Code:
    <style type="text/css">
    body {
    background-image: url('bg_main.jpg');
    background-repeat: no-repeat;
    background-position: center;
    background-color: black;
    cursor: hand;
    scrollbar-arrow-color: black;
    scrollbar-base-color: black;
    scrollbar-dark-shadow-color: black;
    scrollbar-track-color: black;
    scrollbar-face-color: black;
    scrollbar-shadow-color: black;
    scrollbar-highlight-color: black;
    scrollbar-3d-light-color: black;
    }
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    New Member
    Join Date
    Dec 2012
    Posts
    1

    Re: [RESOLVED] CSS Stretch Background image?

    In modern browsers you can now use the CSS3 property background-size

    Code:
    .selector {
        /* will stretch the image so that it covers all of the element */
        background-size: cover; 
        /* will stretch the image so that it fits perfectly inside the element  */
        background-size: contain;
    }
    This allows to achieve background stretching while preserving the aspect ratio of the background-image: https://developer.mozilla.org/en-US/...ackground-size

    I’ve personally developed a polyfill that implements those properties in IE8: https://github.com/louisremi/background-size-polyfill

  10. #10

    Re: [RESOLVED] CSS Stretch Background image?

    Thanks for the useful info, its really helped me out too

    Quote Originally Posted by mendhak View Post
    It's usually done by faking the background. You place the image in a div (which is what he probably meant by layer :cringe: ), and then stretch the div.
    Code:
    #divbg {
    width: 100%;
    height: 100%;
    left: 0px;
    top: 0px;
    position: absolute;
    z-index: 0;   <------------Notice!
    }
    
    #maincontent
    {
    z-index:1; <--------------Notice!
    position:absolute;
    }
    Then in your HTML,

    Code:
    <div id="divbg"><img src="abc.jpg" width="100%" height="100%"/></div>
    <div id="maincontent">all text here</div>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width