overwrite css items depening on className - css

I'm having problems with my CSS markup in my code.
I'm building a control and my plan is to add a standard class to it so it has a fixed layout and add any userdefined css classes behind it, to personalise the control. but during my tests I noticed a problem which I can't resolve.
when I have an element like this
<div class="test1 test2"></div>
and underlaying code in another stylesheet file.
.test1
{
width: 200px;
height: 200px;
background-color: red;
}
.test2
{
background-color: yellow;
}
then it doesn't matter if I put test1 first or test2. the div will always be yellow only because test1 is written last on the css file.
if I replace test2 with test1 in the css file itself then the div will always be red.
how can I make the background-color overwrite incase its added a second time depending on the order its written in the className itself?
I also want to take notice I don't want to force users to use the !important tag. I already know about this and yes that works fine but I need it without. Any ideas on how to resolve this issues is welcome. I'm open for alternatives

You could make it so .test2 when combined with .test1 becomes yellow
.test1.test2{
background-color: yellow;
}
a better way tough is not to work like this at all. have a read of this article instead. It explains a technique for CSS called BEM (Block, Element, Modifier) which is pretty awesome. When trying to modify a existing style it will look like:
.test{
width: 200px;
height: 200px;
background-color: red;
}
.test--warning{
background-color: yellow;
}
and your div will look like <div class="test test--warning">

You can twiddle the precedence of the class's selectors like this:
.test2[class*=test2]
{
...
}
This should make class test2 override other classes that have only class name selectors.
(sorry, this part is not correct)
If you want to lower test1's precedence, you could do it like this:
[class*=test1]
{
...
}
(I haven't tested this, you might need to name it *[class*=test1] instead)

I been researching for a long time and posted here because I couldn't find a good answer. thx to the answers and responses here I was able to find an article over the problem I'm facing here CSS howto
What I'm trying to do is not possible because of the order css in generated. What I wanted is my css to work between browser default and external or internal stylesheets. I will look for an alternative solution to my project.

Related

Css for specific page is not working

i got a little css problem, when i try to change something for just specific page or post. Like in this case i would like to change the background color of the entry content of page number 2458. And its working
#post-2458 .entry-content {
background-color: #fff;
}
But right here i use same logic and i try to change the col 8 background color for page 2458, but it does not workss. Sure if i do it without #post-2458 it changes, but it changes for all pages and i would like to do that only for page of that id.
#post-2458 .col-md-8 {
background-color: #fff;
}
Am i missing something out? could you advice how to adjust that code pls? BTW im using custom css plugin to put in that codes.
Thank you
Try overriding it with this:
#post-2458 .col-md-8 {
background-color: #fff!important;
}
Try add some id to div which you want to change and operate directly on id. Don't use col-md-8 to avoid troubles. When you want to make changes only for one element id is the best option and I has a bigger importance than class and don't use !important if you can fix something smarter.
If I were you I would do few things:
Add !important to see if it's all about the style weights.
#post-2458 .col-md-8 {
background-color: #fff !important;
}
Go to inspect tab of your browser and check which styles overrides yours or which styles you just overridden. In most cases you can override other styles just by correct reference.
For example this might NOT work:
#post-2458 .col-md-8 {
background-color: #fff;
}
But this could work, since maybe you are trying to override background-color given by the reference .col-md-8.sidebar-right
#post-2458 .col-md-8.sidebar-right {
background-color: #fff;
}
There are lots of maybe's, since it is hard to give you proper solution without a link to the website in which your problem occurs. Check the weights and make sure to override correct styles. I hope it helps!

CSS rules conflict : CSS disabled as a result of conflicts with another one

I have a web page, which I need to change its CSS. At the moment, I need a quick fix to an annoying issue. There are some HTML elements that use several CSS classes like the one below.
<ul class="core nested-level">
The problem is that "core" is defined in many places with different rules; hover, ul, *, etc. One of these rules for some reason cause "nested-level" to be disabled as chrome developer tool annoyingly keep showing up.
Any idea how to quick fix this issue or to force this style to override the already defined one (if it exists) ? I tried out the style below, but it didn't show up properly:
.nested-level {
padding-left: 62px;
}
It seems that you defined a rule in your "core" css class for a specific HMTL element. For instance:
ul.core{
padding-left: 0px;
}
Then in your "nested-level", assumingly, you tried to define a rule for the same property.
The way to fix it is either to avoid defining your css rule based on an HTML element, or to use the "important" keyword when defining your css rule, as this
.nested-level {
padding-left: 62px !important;
}
This will fix your issue.
better is dont use !important.
Read More: https://j11y.io/css/dont-use-important/
add ID in Element tag . id Selector have Higher priority than class Selector
<ul id="myId" class="core nested-level">
and use css Like :
#myId {
padding-left: 62px;
}

How to change classes while object oriented css with bem?

Well I'm watching videos about oop css with bem. I didn't understand one thing. I have a media object and I use it everywhere like in navbar and content and footer etc. So how shall I change the media object and insiders. I guess 3 ways there are.
1 - I can catch inside other blocks grandchild chooser
it will like ".navbar .media".
This way makes me worrying because of grandchildren is making slow and complicated I think. Don't think about only .media. I have to select media-item etc etc...
2 - I can give another class to .media like .navbar together
it will like ".navbar.media".
This way need more classes to html so it makes me thinking.
3 - I guess there is no third option if there is please let me know :) Which way I shall do.
Thank you already.
You should add an extra class, navbar__media (that's a double underscore for descendant), and add that to the media elements inside of the navbar.
A rule of BEM/OOP CSS is that an element should always have their style defined by the classes they have, and not based on where they are in the DOM.
For reference: http://getbem.com/naming/
Example:
<div class="navbar">
<div class="media navbar__media"></div>
</div>
<div class="media"></div>
With this css:
.navbar {
background: #00f;
}
.media {
background: #f00;
}
.navbar__media {
background: #0f0;
}

IE Conditional in Sass

Question
I use Paul Irish's IE Conditional Comments trick to target different versions of IE. In sass how do I apply one of these classes when nested in another class.
.container{
// I need to put a ie8 specific style here which is a .ie8 class applied to the body
}
I'd like to avoid doing this if possible (it's nice to be able to keep the properties together):
.container{}
.ie8{
.container{}
}
Is this possible?
I believe this is what you're looking for - using SASS's parent selector "&", you can do this:
.container {
.ie8 & {
// ie8 specific styles here
}
}
I am a little unclear as to what you are trying to do specifically. But If you are trying to do what I think you are trying to, then you could use a variable.
For example, lets say you only want to have your font color red in IE. You could do something like this.. Although I don't see how using SASS would be all that beneficial in this use case, unless of course you are trying to do something different.
SASS:
You can test this at: http://sassmeister.com/
$ieEightAndNine: "\0/";
.container {
color:unquote(red + $ieEightAndNine);
width:100px;
height:100px;
}
CSS:
.container {
color: red\0/;
width: 100px;
height: 100px;
}
TEST IN CHROME VS IE:
http://jsfiddle.net/3G48v/

Sass Multiple Selector Refactoring

I remember reading about a Sass feature that allowed you to specify a list of elements and then a child and it would compile to a list of multiple selectors. I ahve searched around but can't find it.
I want it to compile down to this:
header .container,
footer .container
background: yellow
But i'm sure there is a feature of Sass that allows writing that in a much nicer way.
Any ideas?
That style could be refactored as this:
.container {
header &, footer & {
background: yellow;
}
}
But maybe you are thinking of a Compass feature? http://compass-style.org/reference/compass/helpers/selectors/#nest

Resources