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.
- 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.
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 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.
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.
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:
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With