Tutorials

Latest Word » Tutorials » Achieving Liquid Backgrounds with Fixed Content
Achieving Liquid Backgrounds with Fixed Content

Achieving Liquid Backgrounds with Fixed Content

Tags:

I’ve been asked a few times by readers on how to create fluid backgrounds while maintaining a fixed width. When I was just getting my feet wet with CSS layouts, I too was a bit stumped by this situation as well. It’s really not that complicated as it seems, let’s look into it now.

HTML

What you can do is simply stretch the parent div 100% wide with the repeating background or color, and nest a fixed container class inside.

<div id="section_name" class="fluid">
	<div class="container">
	  <!--Content Here-->
	</div>
</div>

Let’s break this down so you can easily understand the logic here.

id=”section_name”

I used “section_name” as an example, but basically label each section with an ID so you can specify the unique properties (backgrounds, fonts, etc).

#section_name {
	background: #333 url(background_image.jpg) repeat-x;
	padding: 20px 0;
}

class=”fluid”

This will basically have the properties to stretch 100% and will act as the wire-frame of this section.

.fluid {
	width: 100%;
	float: left;
}

class=”container”

We will use this class to keep the contents fixed and placed in the center.

.container {
	width: 970px;
	margin: 0 auto;
	overflow: hidden; /*--We add the overflow hidden to keep floating elements from breaking out of this container--*/
}

Last Note!

CSS Bug - Liquid Background
Since the background is stretching 100% across the monitor, what if the user has their browser at a size that is smaller than the width of the content? You will find that the background only stretches to the browser window size and not covering the entire content width. You can fix this issue by adding a min-width to your body tag in the style sheet.
To read further, checkout this great article by scriptandstyle.com.

body {
	min-width:970px;
}

Conclusion

As you can see, this technique is quite simple! I’m sure there are various ways in achieving this effect, so if someone has a better or different solution, please do let us know. Below are some nice examples of designers using this effect.

Related Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment19 Peeps Have Spoken Their Minds...

  1. Adriano

    As a complete beginner to CSS, I found Blueprint CSS made this kind of fixed-content/liquid background setup a breeze. I used it for my homepage.

    Given your blog title, I suspect you’d frown upon this?

  2. Lao

    How did you know…I just came to this problem was searching and found. Great timing! Thank you

  3. Soh

    Hey Adriano,

    Although I am a big fan and believer in learning/creating everything by hand, just as long as you learned why/how it worked by your blueprint, I think it’s ok.

    Everyone learns in different ways, and if that is your method I don’t think there is anything wrong with that. Just be sure that you don’t rely on it and do understand what it’s doing for you. Don’t let it guide you blindly and I think your all good!

  4. Jamie Coulter

    Short and sweet, always a great read. Thanks!!

  5. Raymond Selda

    Your article will be a good resource for this kind of approach. Each layout technique whether fixed, fluid, elastic or what have you will depend entirely on the the project.

    I’m leaning more towards using fixed width layouts. Thank you for this information.

  6. Adriano

    That’s a good policy, Soh.

    I’ll bookmark this blog for reference, when I need some clean code to learn from.

    Thanks for the posts!

  7. Steven Bradley

    Nice approach Soh. I hadn’t realized it till reading your post, but I’ve used a similar approach on occasion. The place I differ is where you have the main content area I probably would have skipped the fluid div and just let the background of the body show through. I like the way you set things up here so I may have to change my ways a little.

    By the way the link you have at the bottom of the demo winds up in 404 land. I’m guessing it’s meant to point back here to the post and maybe you changed the post URL after coding the link.

  8. lee

    hey guy its very cool

  9. Ron

    Thanks for the quick reply and tutorial. Much needed. Glad I stumbled onto your site. Your awesome!!!

    Ron R.

  10. Soh

    No problem, your very welcome :-)

  11. john

    koo stuff

  12. Toussaint

    Great article . . .
    Rare resource for web designers.
    I do have a question: How can I proceed to get the footer sticky at the bottom of the page and specify the height of my choice?
    Thanx

  13. JR

    Absolutely awesome! I couldn’t find this fix anywhere on the web until I stumbled upon your site. Thanks so much Soh!

  14. Timmy

    Is there a way to keep this as is and yet have a “sticky footer” and ensure that the footer remains at the bottom of the browser window so that when the user scrolls down the large page, there is no blank space between the footer background image and the bottom of the window?

  15. Timmy

    sorry, just to clarify the previous request, this is assuming the content is insufficient to fill a full length of a browser window, so that it appears as though the footer is just hanging in the middle of the browser and the body background continues below it. Thanks.

  16. Kidel Kachui

    Nice, i have been learning all tutorials for all from CSS to PHP (+++++++ alot more!!!)

    And this was a nice Tut, but you never explains tags, thats a problem =/

  17. g.lo

    @Toussaint & @Timmy:

    http://www.cssstickyfooter.com/

    just stick the footer outside of the wrap.

    it’ll give you 100% width on it’s own. then, if you still want, you can employ the above technique.

    cheers. and thanks for the all tuts. you’re is one of my fave sites.

  18. Ronit

    Hi,
    I like your site. I found the articles very helpful.
    I am trying to implement the fixed content/fluid bkg method.
    I need to use image repeat-x to create like a banner, in the feature section. It works fine in Mozilla and Chrome, but not in IE.
    If you get a chance to look at it (in IE vs Mozilla) I’ll appreciate it.
    http://www.oolaladesign.com/FLwindows/FixedContentFluidBkg.html

    Thanks in advance,
    Ronit

  19. JJKrol

    @Ronit
    I’m not sure if
    “margin: 0 auto;”
    works well in IE. If it doesn’t the soulition is to add
    “text-align: center;”
    to the parent container (.fluid in this case) and add
    “text-align: left” in the text container (.container in this case)

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery