Suppose you create a concept widget called Awesome Widget and want to completely protect it from conflict with surrounding elements or child elements that are like content inside the widget.
What we do not want
div ul li {}
Solution 1: CSS Combinator for Kids
Use the selector for CSS child combinators to specify that only direct children be specified.
.awesomewidget > div > ul > li {}
Solution 2: class namespace
Using aw (Awesome Widget) as the namespace for each class, which reduces the likelihood of any other elements appearing on the page using the exact namespace + class name.
.awesomewidget .aw-container .aw-list .aw-listitem {}
There is also something like @namespace in CSS, but it is only for XML.
Besides solutions 1 and 2, are there any others that can be used? Which one would you prefer? Any best practices?
EDIT: An example of a problem that occurs without properly preventing name / style conflicts
Widget Style:
.awesomewidget > div ul li { background-color:red; }
Custom style:
ul li { background-color:blue; }
Markup:
<div class="awesomewidget"> <div> <ul> <li> <p>I am the user and I want to add some content here. Let add a list:</p> <ul> <li>Why is this list-item red and not blue?</li> </ul> </li> <li> </li> </ul> </div> </div>
html css namespaces widget
Dadu
source share