How to use CSS to stretch background to fit the content
This is a subject that I researched on the web countless times, and I had difficulty finding an answer that was 1) coherent and 2) helpful. I finally reached a level in my CSS expertise to know how to make sure the container or content div expands to the length of the content everytime. Here’s what I learned.
There are two ways make sure your background color or pattern stretches to fit your content. One is with absolute positioning, and one is with floats. If you choose to use absolute positioning, you will have to designate the height of every page that you have. This is the more annoying of the two methods, of course, but it is necessary for some sites, like, for example, sites that use z-index and absolute positioning for a specific purpose, like placing a transparent div directly under or on top of another div. I used absolute positioning in http://www.thechainstation.com, and because it is a small site with a limited number of pages, it wasn’t a big deal to specify the height for each page. However, this isn’t the ideal way to do it, because whenever you add or delete content, you need to change the height accordingly.
The better method to make sure your background pattern or color stretches to fit a content div is to use floating divs, and then make sure you clear them before the end of the main content or container div. (Clear your divs by using <div style=”clear:both;”></div> directly in the html file.) The trick I use to make sure the background contains my content is to set up both a container and a content div. The content div contains all the content that may change in height. In the container div, I specify height:100%, but in the content div, I do not specify a height at all. I found that this is the key to making sure the background color stretches to fit the content. Here’s how I set up my CSS.
html {
margin: 0px;
padding: 0px;
height: 100%;
}
body {
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
background-image: url(images/blueswirlybg.gif);
background-repeat: repeat-x repeat-y ;
background-position: top left;
background-color: #01091c;
}
#container {
width: 780px;
margin-right: auto;
margin-left: auto;
padding-bottom:20px;
margin-top: 0px;
position: relative;
}
#content {
padding:0px;
margin:0px;
width:780px;
background-color:#FFFFFF;
clear:both;
height:100%;
}
In my html, the container comes first, followed by the header or intro div (which is a specific height and doesn’t need to stretch), followed by the content div (in which all of the main content exists). You can always place the content div directly following the container div if you need the background to stretch behind the header section. You can see how I do my HTML by looking at my website, http://www.straightawaydesign.com/.
I hope this helps all of you out there who are looking for an answer for this frustrating issue! If you need more information, please feel free to contact me.
14 Responses to “How to use CSS to stretch background to fit the content”
Hi,
I’ve recently added a blog to my website. The blog is from a template from godaddy, and I’m having a really tough time modifying it. If you look at it, you’ll see that the background on the body is orange–this is fine. However, I want to change the white background on the outsides to a color. Would you be able to help me with this? I’d really appreciate it.
Here is the style sheet:
.sf_blog_wrapper {
background-position: 661px 0;
width: 944px;
background-image: url(images/Yalta___Copy2.jpg);
background-repeat: no-repeat;
}
.sf_blog_posttitle {
background-position: bottom;
color: #000000;
background-image: none;
background-repeat: repeat;
}
.sf_blog_outer_wrapper {
width: 944px;
margin: 0 auto;
background-position: top;
background-image: url(images/burnt_orange.jpg);
background-repeat: repeat;
}
.sidebarlist h2, .sidebarlist .calendartitle, h2.homelinkheader a, h2.homelinkheader a:visited {
background-position: bottom;
background-image: none;
background-repeat: no-repeat;
}
.calendar {
border-style: none !important;
margin: 0px auto;
width: 100%;
background-position: 0 22px;
background-image: none;
background-repeat: no-repeat;
}
body {
margin: 0px;
font-family: sans-serif;
font-size: 12px;
color: #000000;
}
.sf_blog_sidebar {
float: right;
width: 288px;
display: inline;
margin-top: 105px;
overflow: hidden;
font-family: sans-serif;
font-size: 12px;
}
.sf_blog_footer {
clear: both;
padding: 15px 0;
width: 944px;
background-position: center top;
font-family: sans-serif;
font-size: 12px;
background-image: none;
background-repeat: repeat;
}
.sf_blog_postmeta {
clear: left;
margin: 10px 0 0;
min-height: 22px;
height: auto;
_height: 22px;
padding: 0 10px;
width: 597px;
font-family: sans-serif;
font-size: 14px;
}
a:link {
text-decoration: underline;
color: #8B0520;
}
a:visited {
text-decoration: underline;
color: #05055E;
}
a:hover {
text-decoration: none;
color: #2A1F97;
}
Hi Jim,
The div in the CSS code that controls your background color is the “body” div. What you need to do is add “background-color:#(insert color code);” to this div. Here is a link to a color chart:http://www.creativeuplink.com/htmlbgcolors.png
body {
margin: 0px;
font-family: sans-serif;
font-size: 12px;
color: #000000;
background-color:#33CCEE;
}
(#33CCEE is a blue which complements orange-as an example)
Let me know if you need any more help! Thanks for your comment.
Thank you so much!!! This worked great. This was so nice of you!!!!!!!
Ugghh…
I have been trying to fix this problem for a week now. I’m ready to pull my hair out.
I have a gradient background image in the body and a logo background in a “container” div. The logo background wouldn’t stretch to the bottom of the page, and the way you have yours is exactly what I want.
Is it possible to have what I’m looking for? I have tried so many changes, I don’t remember anymore when I came closest to what I want. Now my design has a scroll bar on the bottom, which is unacceptable to me (but the logo background goes all the way).
If you could give it a look, I would appreciate it. I am fairly new to css- I was a master of nested tables.
I decided I would give it a go, but I am ready to go back to tables real soon.
Thanks!
Mary
html, body {
padding-top: 0px;
padding-bottom:
margin: 0px;
background-color: #bcab8e;
background-image:url(”../img/bg.jpg”);
background-repeat:repeat-x;
height: 100%;
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
color:#333333;
overflow-y: auto;
}
#logobg{
display:block;
clear:both;
background-image:url(”../img/logobg.png”);
height: 100%;
min-height: 100%;
margin-bottom: 0px;
position:relative;
z-index:1;
overflow-y: auto;
}
#main_container {
margin:0 0 auto 0;
}
#tpmbackground {
display:block;
clear:both;
background-image:url(”../img/logobg.png”);
visibility: visible;
position: absolute;
width: 100%;
height:inherit;
z-index: 1;
margin: 0px 0px 0px auto;
}
.tpmbackground {
background-image:url(”../img/logobg.png”);
width: 100%;
}
#topbg {
background-image:url(”../img/top.png”);
visibility: visible;
position: absolute;
left: 0px;
top: 0px;
z-index: 25;
width: 100%;
height: 211px;
}
* {
margin:0;
padding:0;
}
.clear {
clear:both;
}
blockquote {
background:#273322;
border:1px solid #202B1C;
}
#topcontainer {
width:780px;
height:206px;
margin:0 auto;
}
#bottomcontainer {
width:780px;
top:206px;
height:100%;
margin:0 auto;
position:relative;
z-index: 6;
}
#left {
width:315px;
float:left;
margin:0 auto;
}
#wrapper {
width:760px;
margin:0 auto;
}
#header {
font:Arial;
font-weight:bold;
font-style:italic;
font-size:12px;
padding:10px 0px 5px 245px;
color:#FFFFFF;
float:right;
background-color:#000000;
}
#bottomwrapper {
background-color:#ffffff;
border-bottom:solid 3px #bcab8e;
border-left:solid 3px #bcab8e;
border-right:solid 3px #bcab8e;
width:745px;
float:right;
z-index: -1;
position: relative;
}
#sidebar {
width:315px;
float:left;
}
#piccont {
width:315px;
height:160px;
float:left;
z-index: 25;
position:relative;
}
#content {
width:465px;
float:right;
background-color:#000000;
}
#nav {
padding-left: 20px;
padding-top: 80px;
}
#nav li {
list-style:none;
color:#bcab8e;
font-weight:bold;
display:inline;
padding-left: 0px;
padding-right: 0px;
padding-top: 80px;
}
#nav li a {
padding-left: 0px;
padding-right: 0px;
padding-top: 80px;
text-decoration:none;
font-weight:bold;
font-size:12px;
color:#990000;
}
#nav a:hover {
color:#bcab8e;
}
a em {
font-style:normal;
text-decoration:underline;
}
#content_main {
width: 457px;
float:right;
padding-top: 10px;
padding-right: 5px;
padding-bottom: 10px;
padding-left: 10px;
}
a {
color:#990000;
text-decoration:none;
}
a.hover {
color:#bcab8e;
text-decoration:underline;
}
p.details {
border-top:1px solid #202114;
border-bottom:1px solid #202114;
color:#959F91;
text-transform:uppercase;
padding:7px 0;
}
p.top {
color:#FFFFFF;
font-size: 10px;
font-weight:bolder;
}
#footer {
margin:30px 0 0 0;
padding:15px;
font-size:90%;
border-top:1px solid #333;
}
#topnav {
text-align:right;
width:465px;
height:109px;
width: auto;
}
#sidenav1 {
width:251px;
float:left;
background-color:#ffffff;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
padding-top: 60px;
}
#sidenav2 {
width:250px;
background-image:url(../img/dot.gif);
background-repeat:repeat-x;
}
#copyright {
font-size:9px;
text-align:center;
width: 745px;
padding: 10 px;
border-top:solid 3px #bcab8e;
}
.heading {
color:#990000;
font-size:11px;
font-weight:bold;
}
#contactleft {
padding-left:20px;
width:240px;
float:left;
color:#bcab8e;
}
#contactright {
width:210px;
float:right;
color:#bcab8e;
}
#contactbottom {
width:400 px;
text-align:center;
color:#bcab8e;
}
#spacer {
width:460;
height:10px;
}
/* expanding menu styling */
#udm ul {
margin-left:0 !important;
margin-top:0px !important;
width:230px !important;
width:230px !important;
}
#udm ul ul {
width:220px !important;
width:225px !important;
}
#udm ul ul ul {
width:230px !important;
width:230px !important;
}
/* give all anchors a hand cursor */
#udm a {
cursor:pointer !important;
cursor:hand !important;
}img {
border: 0px none;
}
.clearer {
clear: left;
line-height: 0;
height: 0;
width:100%;
}
Hi, Mary! Sure, I’d be happy to help. Are you trying to extend the gradient background (with the TPM) from the top to the bottom to the page, or did you want the black behind the logo to extend all the way to the edges? Or, did you want the black behind the logo in the header section to blend into the gradient background with all of the content isolated in the middle container? Right now, your header extends beyond what I call “the middle content section” (where your dropdowns are). What about the photo? Did you want it to overlap the content or stay in the header? If you can clarify what you’re trying to do, I’m sure I can help. Thanks for your response!
Wow- quick response.
Sorry- I guess all my confusion is translating into “blah blah blah”.
The client wants it exaclty the way it is, except without the left-right scroll bar in IE. (Firefox is displaying properly, so if you look at it in that browser, you will see what it’s supposed to look like-) I got the TPM logo in the background to go to the bottom of the page, but something I did made the scrollbar appear. I tried to “undo” everything up to that point, but it wasn’t helping.
Maybe a fresh set of eyes will help.
Thanks!
Mary
Okay, to fix this quickly with what you have already, try this. In your CSS, you’ll need to add an extra div, and also add it into your html. Instead of:
#topcontainer {
width:780px;
height:206px;
margin:0 auto;
}
use
#topcontainer {
width:100%;
background-color:#000000;
height:206px;
}
#topcontent {
width:780px; /*you can adjust this width as needed*/
margin:0 auto;
padding:0px;
}
The second div, #topcontent, is the one to add. Then add this div in your html under div id=”topcontainer”
Close the div where the other top divs are closed. There will now be four closed tags.
Also, get rid of the overflow-y: auto; in the #logobg div. This is causing the horizontal scroll.
Let me know if this works. By the way, have you looked at this in IE6 yet? FYI…PNGs don’t work in IE 6 without a special “fix.”
Ok, that didn’t work, but I found an answer that’s “good enough for now”. At this point, I just want to finish it. Like I said before, I was a master at nested tables… if I were to do this site that way, this job would have been a distant memory, and the paycheck already spent.
Now, because of cross-browser issues, my paycheck is only about half what it should have been.
I think for this site I will just change my .png’s to different file types and let it go at that.
I will revisit all these issues at a later date, when I have more time to spare.
Thanks for all your help!
Mary
Hi again, Mary. Are you sure that didn’t work? I put that exact coding in to your html and css and tested it in all browsers, and it was exactly the same. Did you want me to send you the new CSS and html files? I don’t mind.
That would be great- I will clean up my messy code after you send it. Sorry- I should have cleaned out all the unnecessary css before I gave it to you. What a mess- I kept adding stuff, trying to get it to work.
Thanks again for all your time… I really appreciate it.
Mary
Hi, you helped me out a while ago with my blog and I’m wondering if you could help me with two other issues. First of all, I have a spry menu bar and a series of photographs on every page of my website (www.jhmeyer.net) and I’d like to do the same for my blog (http://blog2.jhmeyer.net) so that everything is standard (I’d also have to lower the current “Occasional Postings” header so that the spry bar will fit). I can get rid of the current blog photo on my own.
Secondly, I’d want to narrow the maroon border on my blog by about half its current width in order to accommodate the spry bar.
Can you help me with this? Any suggestions you could provide me would be greatly appreciated.
Jim
Buy:Cialis Professional.Propecia.Tramadol.Viagra Professional.Viagra Soft Tabs.Cialis Soft Tabs.Cialis Super Active+.Zithromax.VPXL.Soma.Viagra Super Active+.Viagra.Maxaman.Super Active ED Pack.Cialis.Levitra.Viagra Super Force….
Buy:Nexium.100% Pure Okinawan Coral Calcium.Prevacid.Actos.Retin-A.Mega Hoodia.Zovirax.Prednisolone.Arimidex.Human Growth Hormone.Lumigan.Valtrex.Petcam (Metacam) Oral Suspension.Accutane.Zyban.Synthroid….
Seats http://borientalnhqtwfd.bestpartsstore.info/tag/Replacement+Seats+replacement/ : Seats…
Seats…
Leave a Reply