Note that there are some explanatory texts on larger screens.

plurals
  1. POIn CSS, how to avoid accidentally getting a style from other classes, especially for widgets?
    primarykey
    data
    text
    <p>When we write our widgets using HTML, JavaScript, and CSS and package the widgets together as a library, we may have</p> <pre><code>.foobarwidget-sidebar-section .sub-section { border: 1px solid #000 } </code></pre> <p>so we will implement the JavaScript to work with the behavior of this widget that we defined, that can be used in any page on our website or by other people.</p> <p>But then, if there is a user of this widget, and he has HTML in his own webpage for a "sub-section":</p> <pre><code>.sub-section { color: green } </code></pre> <p>Then pretty much his <code>.sub-section</code> will add to our widget's CSS style.</p> <p>It is true that he can use</p> <pre><code>#the-main-content .sub-section { color: green } </code></pre> <p>or</p> <pre><code>.a-main-content-box .sub-section { color: green } </code></pre> <p>so that it doesn't affect our widget, but what if his design is that the <code>#main-content</code> box <strong>contains</strong> our <code>sidebar</code> widget (or a search box widget, or a social link button widget)? In that case, his <code>.sub-section</code> will again affect our CSS for the widget.</p> <p>Of course, we can define all the possible CSS styles for our widget, such as</p> <pre><code>.foobarwidget-sidebar-section .sub-section { color: blue !important; font-weight: bold !important; /* ... and all possible CSS styles ... */ } </code></pre> <p>but that doesn't look like a good feasible solution. We also might use</p> <pre><code>.foobarwidget-sidebar-section &gt; .sub-section { ... } </code></pre> <p>so that the <code>.sub-section</code> must be the immediate child, but it doesn't affect a standalone <code>.sub-section</code> affect our widget's CSS. In such situation, what is a good solution to handle it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload