I am trying to apply the style to the .lvl-2 to apply an indent to a particular div, but it is getting overriden by something in materialize.
Chrome inspector scrennshots below
I have some HTML (below is excerpt it is in a list)
<li key={elements.index}>
<div className="row">
<div className="col red s4">f</div>
<div className="col blue s8">f</div>
</div>
<div className="row">
<div className="col red s4 lvl-2">indented</div>
<div className="col green s8">f</div>
</div>
I have some CSS
li> .lvl-2 {
padding: 0 0 0 10px;
}
I have also tried being more specific but when I do inspector doesn't see the style at all (which is wierd )
li div> div .lvl-2 {
padding: 0 0 0 10px;
}
The problem is that when I view in the inspector it say that the style is being overwritten by the style below from materialize
ul:not(.browser-default) {
padding-left: 0;
list-style-type: none;
}
Unfortunately if I just override it it won't work because I want different syles on different li's
ul:not(.browser-default) {
padding: 0 0 0 10px;
list-style-type: none;
}
Your CSS was incorrect defined, that's the reason why you don't see your style applied.
With your HTML markup:
<li key={elements.index}>
<div className="row">
<div className="col red s4">f</div>
<div className="col blue s8">f</div>
</div>
<div className="row">
<div className="col red s4 lvl-2">indented</div>
<div className="col green s8">f</div>
</div>
</li>
And you want to style .lvl-2, the proper one should be : li .row .lvl-2 {} ,
Of course the CSS specificity might not be stronger than the one defined in Material UI, in that case you should add more specificity, something like:
ul li > .row > .col.lvl-2
Please, never ever using "!important" unless you're forced to. In this scenario, it's still quite easy to fix and this will save your future-self.
=========
Updated answer on Nov 18th:
Let's go through each of your attempt one by one and see what went wrong:
1)
li> .lvl-2 {
padding: 0 0 0 10px;
}
This means "get all the direct child elements with .lvl-2 class from li", this won't apply because your HTML markup is different.
To be more specific, your lvl-2 element has a wrapper div with class row, as of now the direct children from li are divs with .row class. The > stands for "get direct child`.
If you want to use direct selector, the selector should be like li .row > .lvl-2.
2)
li div> div .lvl-2 {
padding: 0 0 0 10px;
}
Once again, the CSS won't run because your HTML markup is different from what your CSS defines.
In this case, it means "from any li element, select all divs children, from each selected div, get all direct div children, then get all element with .lvl-2 class from each direct selected div.".
That might be hard to get the first time, but let me show you the HTML markup which will work in your second attempt.
<li>
<div class="wrapper">
<div class="child">
<div class="lvl-2">
</div>
</div>
</div>
</li>
or even another markup
li
div
div
div
div.lvl-2
To answer for your question, I think you should read on this article, it will help you easier than my explanation "https://dev.to/emmawedekind/css-specificity-1kca"
In your scenario, and also from my experience, you should define CSS selectors using class, not with type selectors (e.g like div, li, button).
Type selector is the lowest priority in CSS specificity calculation. Use with caution.
In term of project scalability, using type selector means you're forcing the element to be that specific type. E.g: li span means only the <span> tag, what happens if another developer or an upcoming request change, that tag needs to be replaced with a div. So saving your future-self, use with classes like li .item, it will both apply either to <li> <span class="item"> or <li> <div class="item">.
It reduces confusion as much as possible, and increase readability CSS code. Reading something .list .item always helps a clearer vision than a li span.
Most of the time, when your Frontend architect depends on a 3rd party like Bootstrap or Material-UI, they all use class selectors. To override them, the only way is to use class selectors.
Hope this helps,
Use !important which is an exception to the specificity calculation rule.
li > .lvl-2 {
padding: 0 0 0 10px !important;
}
MDN excerpt:
The !important exception
When an important rule is used on a style declaration, this
declaration overrides any other declarations. Although technically
!important has nothing to do with specificity, it interacts directly
with it. Using !important, however, is bad practice and should be
avoided because it makes debugging more difficult by breaking the
natural cascading in your stylesheets. When two conflicting
declarations with the !important rule are applied to the same element,
the declaration with a greater specificity will be applied.
Some rules of thumb:
Always look for a way to use specificity before even considering
!important
Only use !important on page-specific CSS that overrides
foreign CSS (from external libraries, like Bootstrap or
normalize.css).
Never use !important when you're writing a
plugin/mashup.
Never use !important on site-wide CSS.
try be more specific like:
ul:not(.browser-default) li .lvl-2 {
padding: 0 0 0 10px;
}
avoid using !important if you can, this will bring pain later
also i think the culprit is not the padding on ul:not(.browser-default) since this should apply to the ul element, not to it's children
use !important keyword
Example : width :100px into width :100px !important
Related
I'm going over Frontify and I want to inspect an element in Firebug. The element is <div class="mod mod-header fixed open">.
When selecting that element in Firebug's HTML panel, usually you expect to see the styles in the Styles side panel. I see .mod-header listed there but not .fixed or .open. I want to see what these classes do. Why can't I see them?
EDIT: You have to scroll down or click the menu to see those classes.
.fixed is being used as what I would refer to as a scoping selector. A scoping selector may have it's own styles but it's also used to control where it and related CSS selectors can affect other elements. You'll often see modules/components use this approach.
If you look at the first child element of <div class="mod mod-header fixed"> you'll see <div class="row header">. Select that element in your inspector. You'll now notice how .fixed is being used. You'll see the following CSS selector in the inspector window.
.mod-header.fixed .header {
z-index: 10;
padding: 15px 0;
max-width: 100%;
box-shadow: 0 1px 5px rgba(0,0,0,.1);
}
So .fixed and .open are being used to control child elements rather than the element that they're applied to.
It can often be easier to add a single class to the outer most element and setup your CSS selectors accordingly to re-style child elements instead of finding a handful of elements and applying a class to each.
I am trying to apply a hover effect on a div. Why isn't this working at all?
My Html looks like this:
<a href="#panel-866" id="panel-866">
<div class="application-icon" style="background-image: url('/custom-icon-off.png')">
</div>
</a>
CSS
.tab-title > #panel-866 .application-icon:hover {
background-image:url(/custom-icon-hover.png);
}
You need to override the inline styles, which have higher specificity than external / embedded styles.
Try this:
#panel-866 > .application-icon:hover {
background-image:url('/custom-icon-hover.png') !important;
}
Here's a demo: https://jsfiddle.net/0aghvn3u/
The '>' - selector gets direct descendants, maybe just remove
.tab-title >
and it will work. Difficult to say without knowing your markup since its a simple task and your solution seems to be correct.
Make it important so it overrides the anchor tag's default hover styles.
.tab-title > #panel-866 .application-icon:hover {
background-image:url('/custom-icon-hover.png') !important;
}
There are a few problems with your code, so it's hard to say what specifically is causing the problem. You have a div element in an a tag, which you should avoid because block level elements don't work well within inline elements. This is likely not the problem, though.
I've added some markup and removed some CSS that included a selector not in the code you presented here that might have caused the effect not to work:
<a href="#panel-866" id="panel-866">
<span class="application-icon" style="background-image: url('http://lorempixel.com/400/400')">
</span>
</a>
and
#panel-866 .application-icon {
height: 400px;
width: 400px;
display: block;
}
#panel-866 .application-icon:hover {
background-image: url(http://lorempixel.com/200/400) !important;
}
Notice I made the inline span element display:block (this is technically "allowed") so I could give it a width and height. Even when on a div element, background images need a width and height to display.
Secondly, as the other posters mentioned, adding an !important declaration to your :hover style rule is needed because browsers will always override internal or external style rules with inline ones.
https://jsfiddle.net/3b2ywp5b/
I am making a theme for a website, but I ran into a problem. I can't change their HTML or use javascript, only CSS.
This is what the site's HTML looks like:
<div class="container">
<div style="margin:a ridiculously massive number">
<p id="title"> Title of page </p>
<p> Words that cannot be read because of the ridiculous margin </p>
</div>
<div id="otherContent"> There a lot of divs without ridiculous margin all with different ids </div>
</div>
I want to remove the ridculous margin without affecting the other divs margins. Is this possible?
yes you can target the div that is the first-child inside of .container as to not effect other divs.
.container div:first-child{
//code
}
EXAMPLE 1
Example 1 is specifically for the example you posted where the div you would like to target is the first child of it's parent. Also note if the margin is inline like your example you're going to have to over-ride it with !important like so:
.container div:first-child{
margin: 0 !important;
}
OR
You could also use the :not selector if the other's have a similar class
.container div:not(.classname) {
//code
}
EXAMPLE 2
The point of example 2 is if your div isn't the first child and the only without a class (it would probably be unlikely you would have multiple divs with the same classname except one but it's possible). in your example you could also use :not() to target that other div with id #otherContent like so:
.container div:not(#otherContent) {
//code
}
OR
The last option you can use if the others don't apply would be nth-of-type() to target specifically which one you want to effect:
.container div:nth-of-type(2) {
//code
}
EXAMPLE 3
In this case you will have to use first-child selector with !important keyword, as this is the only way to make rule more specific than style="margin" rule:
.container > div:first-child {
margin: 0 !important;
}
If all the other divs have ID you can use the following:
div>div:not([id]) {
margin: 0 !important;
}
I'm trying to place a link in Wordpress quickly and we have a pretty complex style being applied to all a href links in the section. Here's a small sample of the selector and the styles within (there's about 40 lines of styles which I held back)
div.content-rotator li.featured-content a {
margin: 0px;
border: 1px solid rgb(34,56,19);
}
Is there anyway I can place a link in this li and override the parent style? It has to appear within the li with class featured-content.
I don't want to touch the existing CSS at this stage so I'd prefer to implement inline styles on the a element.
Thanks
EDIT: Just in case it wasn't clear, the CSS above is coming from the style sheet and I'd like to zero it out.
There's > 50 lines of styles in this though, I've only shown two for brevity so inline replacing them all isn't really an option.
Just use inline styles or/and add !important to overriden CSS definition, like:
<div class="content-rotator">
<ul>
<li class="featured-content">
...
</li>
</ul>
</div>
or
div.content-rotator li.featured-content a.other {
margin: 3px !important;
border: none !important;
}
Give the selected link an ID and just add !important to the styles. I don't think there is a better alternative unless you plan to go through the entire stylesheet.
I write a html element as below ::
<div class="box"> Foo box </div>
and write css like
.box {
width: 400px;
height: 40px;
color: red;
text-align: center;
}
or
div.box {
width: 400px;
height: 40px;
color: red;
text-align: center;
}
I want to ask that how the both css for box class is different than each other.
The difference is that in the first class you tell that all element (div, p, span ...) with class box have that attribute.
Like this:
<span class="box">test</span>
<div class="box">test</div>
<p class="box">test</p>
The second class means that only div with class box has that attribute
Only this elements get second class:
<div class="box">test</div>
The selector before the class specify which type of elements can take this class
One very important difference between div.box and simply .box is in something called selector specificity. It is a set of rules which defines which selector gets more weight once the browser starts going through all the selectors that potentially have influence on a particular element.
What this means is easily demonstrated in the following example (DEMO)
We have a simple div containing some text.
<div class="box">
Zarro boogs found!
</div>
Now we add some CSS selectors to the example.
div.box {
padding:0.8em;
background: #bd0000;
color: #fff;
}
.box {
color: #bd0000;
}
One of the most basic rules of CSS is that selectors can be redefined in a way that whatever definition comes last and has influence on a particular element its the one that is going to be used (the sole exception being when using !important which always takes precedence).
Now in the above example redefining the .box class selector should actually hide the text but instead its still visible. How is that possible if we said that latter rules always take precedence? Its because the div.box rule has a higher specificity that .box since it actually gets points for containing both an element (div) and a class selector (.box) in its selector declaration (div.box).
Of course the div.box rule will be applied only on a div element but since class selectors are often reusable pieces of code there is plenty of situations when they are used on divs.
Although the rules in the official W3 specification are not that hard to understand they are sometimes pretty hard to remember. That's why I would like to recommend an excellent article on CSS selector specificity which can be found here.
In my opinion selector specificity is by far the most important thing to master when it comes to tracing inheritance problems with CSS stylesheets.
.box means any element having class box.
Example:
<div class="box">...</div>
<section class="box">...</section>
<span class="box">...</span>
div.box means only div element having class box.
Example:
<div class="box">...</div>