Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable inherited css styles?

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?

like image 841
Jack B Nimble Avatar asked Jun 26 '09 00:06

Jack B Nimble


People also ask

How do you stop inheriting in CSS?

Using the wildcard * selector in CSS to override inheritance for all attributes of an element (by setting these back to their initial state).

How do you override inherited CSS styles?

Identify the element with inherited style by right-clicking the element and select Inspect Element within your browser. A console appears, and the element is highlighted on the page. Right-click the element and select Copy > Copy selector. You will paste this selector into your variation code.

How do you stop inheritance?

To prevent inheritance, use the keyword "final" when creating the class. The designers of the String class realized that it was not a candidate for inheritance and have prevented it from being extended.

Are CSS styles inherited?

What is CSS inheritance? CSS rulesets cascade down the CSS hierarchy from parent selectors to their children selectors. These CSS rulesets are inherited from their parent selectors. The child element will naturally inherit a CSS property with its value from the parent element if the CSS property is not specified.


1 Answers

The simple answer is to change

div.rounded div div div {     padding: 10px; } 

to

div.rounded div div div {     background-image: none;     padding: 10px; } 

The reason is because when you make a rule for div.rounded div div it means every div element nested inside a div inside a div with a class of rounded, regardless of nesting.

If you want to only target a div that's the direct descendent, you can use the syntax div.rounded div > div (though this is only supported by more recent browsers).

Incidentally, you can usually simplify this method to use only two divs (one each for either top and bottom or left and right), by using a technique called Sliding Doors.

like image 148
Aupajo Avatar answered Sep 28 '22 22:09

Aupajo