Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend the background of an element beyond its container?

Tags:

html

css

I have a div that contains an h3 tag, and i want the h3's background to extend beyond the containing div tag to the edge of the page. Is there a way to do this in CSS?
I considered absolutely positioning the h3, but I don't know the size from the edge of the screen to the edge of the div (where I want it to be) since the div is centered.

like image 584
chustar Avatar asked Jan 29 '10 17:01

chustar


People also ask

How do I make a div extend beyond container?

- First set the width of the extended-content-container div to 500%. This will make the div you are trying to extend 5 time wider than the center column it lives inside. This can be adjusted up or down depending on the size of the center column you are working with.

How would you set a background for an element?

We can set background color by selecting the element by its class name of id name and then apply the background-color property on it to set the background color. Syntax: background-color: color_name; Below examples illustrates the approach.

How do you add a background color to a whole website?

How to Add Background Color in HTML. To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

How do you extend an image in CSS?

You can use the CSS background-size: cover; to stretch and scale an image in the background with CSS only. This scales the image as large as possible in such a way that the background area is completely covered by the background image, while preserving its intrinsic aspect ratio.


1 Answers

On this page, I used Firebug to change the question title's css.

#question-header h2 {
    background:lime;
    margin-bottom:-1000px;
    padding-bottom:1000px;
}

This is what happened: alt text
(source: zastica.com)

There is a #question-header div that only extends to the top of the question ("I have a div..."). The key is having a large padding-bottom and a large (negative) margin-bottom.

This also works for side-to-side backgrounds. You just have to make sure the container doesn't have overflow: hidden set.

Edit: To get this effect left and right, set:

html {
    overflow-x: hidden;
}

#question-header h2 {
    padding: 0 1500px;
    margin: 0 -1500px;
}

The html { overflow-x: hidden; } prevents the page width from getting really big due to the padding.

like image 76
davethegr8 Avatar answered Oct 12 '22 03:10

davethegr8