John
John

Reputation: 7339

CSS: gradient header 100% width of screen fine unless page scrolls

I have looked everywhere for this one and can only find solutions for background gradients.

This one has had me stumped, so...here goes.

I have a gradient header. I want its width to be the width of the screen. Which all works fine when resizing the window, except when scrolling to the side. My gradient then turns to white space.

The content needs to stay at a large fixed width. In the HTML of my actual page the header is contained within a wrapper as well as another div. It be great if the solution was in HTML in CSS, but javascript and jquery will do if the former is impossible.

I have made a js fiddle showing the problem. http://jsfiddle.net/WvHYM/18/

here's the html

<body>
    <div id="wrapper">
        <div id="header">&nbsp</div>
        <div id="content">This is the content</div>
    </div>
</body>

and css it includes the gradient so it's a beast

html, body{
    width:100%;
}

#wrapper{
    width:100%;
}
#header {
    height: 75px;
    width: 100%;
    background: #7d7e7d; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPHJhZGlhbEdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iNzUlIj4KICAgIDxzdG9wIG9mZnNldD0iMCUiIHN0b3AtY29sb3I9IiM3ZDdlN2QiIHN0b3Atb3BhY2l0eT0iMSIvPgogICAgPHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjMGUwZTBlIiBzdG9wLW9wYWNpdHk9IjEiLz4KICA8L3JhZGlhbEdyYWRpZW50PgogIDxyZWN0IHg9Ii01MCIgeT0iLTUwIiB3aWR0aD0iMTAxIiBoZWlnaHQ9IjEwMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, #7d7e7d), color-stop(100%, #0e0e0e)); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%); /* IE10+ */
    background: radial-gradient(ellipse at center, #7d7e7d 0%, #0e0e0e 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#7d7e7d', endColorstr='#0e0e0e', GradientType=1); /* IE6-8 fallback on horizontal gradient */
}
#content {
    width: 600px;
    height: 200px;
    background-color: #ccc;
    padding: 20px;
    font-family: Lucida Sans Unicode;
}

Here are some images of the problem.

This is fine

Not cool!

The answer would be greatly appreciated. :)

Upvotes: 2

Views: 1104

Answers (3)

John
John

Reputation: 7339

I found the answer here: How to make div not larger than its contents?

basically if you want a 100% width parent with dynamic fixed width children you need to set the parent element to display inline block and a width of auto (which is the default, meaning width can also just be left off completley.)

Important CSS

Parent

#wrapper{
    display: inline-block;
}

Child

#content {
    width:600px;
    height:200px;
    background-color:#ccc;
    padding:20px;
    font-family:Lucida Sans Unicode;
}

DEMO

Upvotes: 0

jhunlio
jhunlio

Reputation: 2660

html

<body>
    <div class="wrapper"><div>&nbsp</div></div>
    <div>This is the content</div>
</body>

css

body{
    width:100%;
}
.wrapper {
    width:100%;
}
div:first-of-type {
    height:75px;
    width:100%;
    background: #7d7e7d;
    /* Old browsers */
    /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;
    base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPHJhZGlhbEdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iNzUlIj4KICAgIDxzdG9wIG9mZnNldD0iMCUiIHN0b3AtY29sb3I9IiM3ZDdlN2QiIHN0b3Atb3BhY2l0eT0iMSIvPgogICAgPHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjMGUwZTBlIiBzdG9wLW9wYWNpdHk9IjEiLz4KICA8L3JhZGlhbEdyYWRpZW50PgogIDxyZWN0IHg9Ii01MCIgeT0iLTUwIiB3aWR0aD0iMTAxIiBoZWlnaHQ9IjEwMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%);
    /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, #7d7e7d), color-stop(100%, #0e0e0e));
    /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%);
    /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%);
    /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover, #7d7e7d 0%, #0e0e0e 100%);
    /* IE10+ */
    background: radial-gradient(ellipse at center, #7d7e7d 0%, #0e0e0e 100%);
    /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#7d7e7d', endColorstr='#0e0e0e', GradientType=1);
    /* IE6-8 fallback on horizontal gradient */
}
div:last-of-type {
    width:600px;
    height:200px;
    background-color:#ccc;
    padding:20px;
    font-family:Lucida Sans Unicode;
}
}

DEMO

Upvotes: 0

user1781710
user1781710

Reputation:

Your first-of-type and last-of-type only correspond to <p> tags. To resolve the issue, you'll need a <div> inside another <div>.

Try this:

<div id="header">

   <div id="header-gradient">

   //your content

   </div>

</div>

For the CSS:

#header {
   width: 100%;
}

#header-gradient:first-of-type {
   //style here
}

#header-gradient:last-of-type {
   //style here
}

Upvotes: 0

Related Questions