So I am creating a container with rounded corners using the following method:
div.rounded {
background: #CFFEB6 url('tr.gif') no-repeat top right;
}
div.rounded div {
background: url('br.gif') no-repeat bottom right;
}
div.rounded div div {
background: url('bl.gif') no-repeat bottom left;
}
div.rounded div div div {
padding: 10px;
}
Now I want to use a div inside my container:
.button {
border: 1px solid #999;
background:#eeeeee url('');
text-align:center;
}
.button:hover {
background-color:#c4e2f2;
}
<div class='round'><div><div><div>
<div class='button'><a href='#'>Test</a></div>
</div></div></div></div>
However, with I put a div inside my nested divs, the button has the bl image in the corner.
How do I remove the inherited background image?
This question is related to
html
css
stylesheet
The cleanest solution is probably to specify your divs as exact children.
Try changing this:
div.rounded div div {
background: url('bl.gif') no-repeat bottom left;
}
To this:
div.rounded > div > div {
background: url('bl.gif') no-repeat bottom left;
}
Cascading Style Sheet are designed for inheritance. Inheritance is intrinsic to their existence. If it wasn't built to be cascading, they would only be called "Style Sheets".
That said, if an inherited style doesn't fit your needs, you'll have to override it with another style closer to the object. Forget about the notion of "blocking inheritance".
You can also choose the more granular solution by giving styles to every individual objects, and not giving styles to the general tags like div, p, pre, etc.
For example, you can use styles that start with # for objects with a specific ID:
<style>
#dividstyle{
font-family:MS Trebuchet;
}
</style>
<div id="dividstyle">Hello world</div>
You can define classes for objects:
<style>
.divclassstyle{
font-family: Calibri;
}
</style>
<div class="divclassstyle">Hello world</div>
Hope it helps.
You can use the unset
keyword to reset a property.
div.rounded div div div {
background-image: unset; /* reset background */
padding: unset; /* reset padding */
}
More info on developer.mozilla.org
Simplest is to class-ify all of the divs:
div.rounded {
background: #CFFEB6 url('tr.gif') no-repeat top right;
}
div.rounded div.br {
background: url('br.gif') no-repeat bottom right;
}
div.rounded div.br div.bl {
background: url('bl.gif') no-repeat bottom left;
}
div.rounded div.br div.bl div.inner {
padding: 10px;
}
.button {
border: 1px solid #999;
background:#eeeeee url('');
text-align:center;
}
.button:hover {
background-color:#c4e2f2;
}
And then use:
<div class='round'><div class='br'><div class='bl'><div class='inner'>
<div class='button'><a href='#'>Test</a></div>
</div></div></div></div>
If you control both the HTML and CSS, I'd suggest switching to using ID's on all the divs needed for the rounded corner.
CSS
#d1 {
background: #CFFEB6 url('tr.gif') no-repeat top right;
}
#d2 {
background: url('br.gif') no-repeat bottom right;
}
#d3 {
background: url('bl.gif') no-repeat bottom left;
}
#d4 {
padding: 10px;
}
HTML
<div id="d1"><div id="d2"><div id="d3"><div id="d4">
<div class='button'><a href='#'>Test</a></div>
</div></div></div></div>
Give the div you don't want him inheriting the property background too.
Source: Stackoverflow.com