Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive CSS background image [duplicate]

Simple concept, I'd like to have a responsive CSS background image that scales at a 1:1 width/height ratio within a browser window, this is the easy part

body{
    height:100%;
}

.banner{
    height:100%;
    background: url('path/to/img') center center no-repeat;
    background-size:contain;
}

<body>
    <div class="banner"></div>
</body>

the problem is I also want to place text inside the background container with word wrapping, and have the height of the background image flow height wise with the text as a browser window resizes.

<body>
    <div class="banner">
        <p>
            Lorem ipsum dolor sit amet, in eos idque epicuri...
        </p>
    </div>
</body>

Is this possible with pure CSS/HTML?

like image 678
dcd0181 Avatar asked Nov 29 '22 07:11

dcd0181


2 Answers

Background Image

One way to do the background is to use background-size: cover as per this post:

html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

Text

To do the text, you could look at using @media queries, but as you've selected the answer, I'll leave the rest up to you guys :)

like image 162
Richard Peck Avatar answered Dec 05 '22 10:12

Richard Peck


Try this:

HTML:

<div class="banner">
    <img src="https://www.google.es/images/srpr/logo11w.png" />
    <div class="content">
        Content
    </div>
</div>

CSS:

.banner {
    position: relative;
}
.banner > img{
    width: 100%;
    height: auto;
}
.banner > .content{
    position: absolute;
    top: 0;
    bottom: 0;
    overflow: auto;
}

Demo

like image 21
Oriol Avatar answered Dec 05 '22 08:12

Oriol