Add background with infinite width some elements in header

This is a live demo: http://baogiadientu.com/demo/fridgevan/
And the result that I want: http://baogiadientu.com/demo/fridgevan/images/demo.png

I want to add a background with an infinite width to two elements (.top-bar and .nav). I have tried to do the same with .bgwhite’s background but a scroll bar is appearing, which is not what I want. Does anyone have any advice on how to achieve this without a scroll bar?

Here is my HTML structure:


<div class="container">

        <header class="clearfix">
            <div class="bgwhite"></div>
            <div class="logo">
                <img alt="logo" src="images/logo.png">
            </div> <!-- end .logo -->

            <div class="beside-logo">
                <div class="white-spce"></div>
                <div class="top-bar">

                </div> <!-- end .top-bar -->

                <div class="nav">

                </div> <!-- end .nav -->
            </div> <!-- end .beside-logo -->
        </header>

 </div> <!-- end .container -->

Hi,

There are a lot of variables in that scenario and the way forward does depend on exactly how this is supposed to function.

I’ve just knocked up a rough example of what I think your are looking for but support is ie9+ (ie8 should work but have no transparency).

I’m not sure if that’s what you were after but may give you some ideas to work with. Note that your body image is over 50 times the file size it should be and as a rule of thumb you don’t really want images bigger than 200k at most for those large background images.

I see on this page http://fridgevan.kudoswebdesign.com/ and they can do it works like a charm in ie8. I try to do the same but not success, header layout is broken and font isn’t loaded :slight_smile: Could you give me some article help me layout html structure?

Actually that page is broken in all browsers as they have not understood the technique they are using. If the width of the page is smaller than the width of middle content then scrolling right leaves a black space over the content. If you have 100% wide backgrounds then you must set a minimum width equal to the largest fixed width element in that page or you will lose the background.

Here’s a breakdown of how to achieve that effect (although my original demo contains most of this techniques and is a miles better).


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
html,body{margin:0;padding:0}
body{background:#ccc;}
#masthead{
	width:100%;
	min-width:940px;/* this is important and matches the width of the fixed center element */
	background:url(images/transparent.png) repeat 0 0;	/* your transparent image goes here */
	background:red;/* just for testing - remove this !!! */
	background:rgba(255,255,255,0.3);/* just for testing - remove this !!! */
}
#site-header{
	width:940px;
	margin:auto;
	position:relative;
	min-height:132px;/* for testing only */
	/* no background on this element */	
}
.bgwhite{/* You could use :after instead of using this element for ie8+*/
	background: white;
	position: absolute;
	right: 100%;
	width: 1000%;
	height: 100%;
}
#logo {
	float: left;
	width: 211px;
	padding-top: 22px;
	padding-bottom: 10px;
	padding-right: 17px;
	background: #fff;
	height: 100px;
	border-right: 1px solid #DBEAF9;
	border-right: 1px solid rgba(98, 129, 160, 0.3);
}


</style>
</head>

<body>
<div id="masthead" class="header" role="banner">
		<div id="site-header">
				<div class="bgwhite"></div>
				<div id="logo">Logo here</div>
		</div>
</div>
</body>
</html>


#masthead holds a transparent image which allows the transparent effect and will work everywhere (at the expense of using an image). Just make a semi transparent image with a solid white portion at the top and use that to make the full width background. The site you linked to uses this image but make your own.

The white section to the left of the logo is created with this element.:

<div class=“bgwhite”></div>

It is then absolutely placed to the left and off the left side of the screen. #site-header is position:relative so the absolutel element stays track with it but still reaches all the way to the left because of the large width applied.


#site-header{
	width:940px;
	margin:auto;
	position:relative;
	min-height:132px;
}
.bgwhite{/* You could use :after instead of using this element for ie8+*/
	background: white;
	position: absolute;
	right: 100%;
	width: 1000%;
	height: 100%;
}


As I said above you don’t actually need bgwhite for ie8 and above as you could have used generated content instead.


#site-header:after{/* instead of using bgwhite element */
	content:" ";
	background: white;
	position: absolute;
	right: 100%;
	width: 1000%;
	height: 100%;
}

That’s the basics of it.:slight_smile:

However I would advise that you move to a max-width of 940px rather than a fixed width as these days you need to cater for a myriad of devices and if starting from scratch you should be looking to do this properly.

Thanks for your advise! I tested your above code #2, it has a problem. The header’s layout will be broken if I try to zoom (in, out) page :slight_smile:

I assume that you are referring to Paul’s code in post #2. I also assume that you are testing in Firefox and zooming text-only (an easy way to test for a range of font sizes).

Try these changes and see if it doesn’t give you a bit more zoom range before the header “breaks”. ( Add the blue, Delete the red.)


[color=blue].inner:before {
    width:2220px;
}[/color]

.logo {
    width:220px;
    float:left;
    text-align:center;
    [color=blue]position:relative;[/color]
    [color=red]background: none repeat scroll 0 0 #fff;[/color]
    [color=red]height: 133px;[/color]
    [color=red]line-height: 133px;[/color]
    [color=red]margin: 0;[/color]
}

IF you are NOT talking about the code in post #2, ignore this post.

Yes, I’m referring to post #2. Header is still broken. I tested on IE, Firefox, Chrome and result is the same. Maybe I’ll try other way to get it better :slight_smile:

Can you show us some code so we can see it break, too? A link to your page? A description of “broken”?

Here is my site: http://baogiadientu.com/demo/fridgevan/home.html. Please try to zoom header and see error :slight_smile:

Sorry but no one in the world is going to wait for that 5000k image to load. They will all go somewhere else. :slight_smile: Why have you used a 5000k image when the image on the site you first linked to is only 185k? There is not much point in addressing any issues until you rectify this as your site is unbearably slow.

Yes, I’m referring to post #2. Header is still broken

That demo scales very well with text zoom and its only the fixed height logo that doesn’t scale and as we know images don’t scale with text zoom so it should be no surprise. With a couple of tweaks you can place a white background under the image to keep the white background consistent but if you are going to use text zoom only[B] then expect only the text to scale and not images[/B]. However I have to take issue with you as [URL=“http://fridgevan.kudoswebdesign.com/”]the site that you keep holding up as a good example does not zoom even one click up or down without breaking very badly indeed. I am also slightly concerned that you seem to be copying another site which is not very professional unless you own that other site also. It’s ok to get ideas from other sites but to make exact copies including the image will break copyright laws. If the site is under your control then that’s ok but otherwise its not a good idea.

I’ve tweaked my original example which virtually scales forever now. In your page its a bit harder because you haven;t used all my code but if you add the following code it will scale much better.


.header .inner .nav .nav-top .phone{width:auto;}
.header .inner .nav .nav-top{
 height:3.3em;
}
.header .inner{height:auto}
.inner:before{margin:0 0 0 220px;}
.logo{position:relative;z-index:2;}
.header .inner .nav .nav-top .sociaty {
    margin-left: 140px;
}

You must remember that you need to make compromises with the design if you are going to be that picky with zoom and therefore should choose a layout that works more easily when zoomed. You would also need to use ems for dimensions rather than px so that the container scales with zoom at the same rate as the text. That’s why you can’t limit the design to the height of a fixed px logo.

Also be careful with your selectors.

e.g.


.header .inner .nav .nav-top .phone {}

That is too specific and a waste of code and resources and browser power. You are making the browser work many times harder than it needs to be. Why not just use:


.phone {}

Or if you have .phone in different contexts that are styled differently then just ues one extra selector.


.header .phone{width:auto;}

If you have .phone in many different contexts then it would be time to re-think your approach.

Thank you very much! The site is under my friend control, he gave me it’s design (psd) and said to me convert it to static site. I’ll find some tutorial help me make a good layout that works more easily when zoomed :slight_smile:

The demo I created for you scales very well now and I don’t think you can get much better than that for that type of design.

For layouts to zoom nicely avoid things like set heights and if you do need heights them use ems so that containers grow with the text.

I found the approach to resolve problem I gave in #1, it’s simple. I’m only put background (repeat) for body and image background for header. Don’t need to set properties for :after, :before. Thanks for helping me! You are nice man :slight_smile: