I have a div like this:
<div class="mobile">Some mobile content</div>
This content is hidden by default (core.css):
.mobile{display: none;}
Now, I want to show this div, when browser resolution is lower than 1024px and it doesn't work:
#media (max-width: 1024px) {
.mobile{display: block;}
}
How can I show this div? Opposite way works fine - showing and then hiding when resolution changes.
Since you use the same selector, it will always use the last called selector.
See this fiddle : http://jsfiddle.net/9KtHg/
It is working perfectly since the media query is called last (so it override the CSS when the condition are met).
But here : http://jsfiddle.net/9KtHg/1/
It is not working since the display:none is last and will override the other CSS.
To avoid that, you need to use greater specificity selector in the media query like :
div.mobile <-the tag name containing class='mobile'
[.][#][tag]parent .mobile <- use the parent in the selector
.mobile{display:block!important}<- using important is a bad pratice, avoid it.
You could also include the core.css before your CSS files containing your mediaqueries.
In conclusion, welcome to the fabulous world of CSS override!
By the way, CSS mean "cascading style sheets". As it said in its name, it work as a cascade where the last declared CSS will be used!
Related
IN order to keep the CSS rule I want for a div I have to add: "padding-left:70px!important" to apply generally.
but for the mobile, i would like padding-left:0px.
So I simply add to the media query of the mobile size "padding-left:0px!Important"
So I thought that automatically when switching to mobile size it will take the CSS style inside the media query as the one to use as both have!Important.
But does not happen, it still keeps the 75px padding.
Thanks
Order matters, later one with !important will overwrite previous (!important) one, see example below, whatever media query works for your mobile, make sure you order them correctly
(move your global CSS to top, and media query to bottom)
.test {
color: red !important;
}
.test {
color: green !important;
}
<span class="test">TEST TEST TEST</span>
CSS styles will take the most specific rule you supply, so make sure that your padding-left:0px!important rule is still more specific than the one declaring padding-left:70px!important.
If the specificity is identical, then CSS will use the last rule defined, so also ensure that your mobile override appears after the initial padding-left rule.
This is where you can check and understand specificity:
https://www.w3.org/TR/css3-selectors/#specificity
Bonus: calculate selector specificity: https://specificity.keegan.st/
I'm using media queries to make my site resposnive. In my CSS doc, the media queries are below all other styles. I'm am using diplay: none; which works perfectly but on another div the original width is taking priority even when I reduce the browser size.
Image of dev console:
Do I really have to add !important to every media rule?
CSS:
#media screen and (max-width: 930px) {
/* INDEX */
nav ul {
display: none;
}
#sliderContainer {
width: 80%;
height: auto;
}
}
The rule at line #112 in index.css is also applied by #sliderContainer and not by nav li, as you state in your question (it can be seen in the image you posted). Because it is met later and has same specificity, it applies.
If you place !important on a rule, you'll probably need to use !important when trying to override it, and before you know it, half your rules will be !important and fixing responsiveness is going to be a nightmare. Either slightly increase specificity of your rule or change their order.
Very important note: #media queries do not add any specificity to CSS rules. They just make them apply (when conditions are true) or not (when not true).
Useful note: A very good technique to always keep specificity of your selectors as low as possible is to place your custom stylesheets last inside <head>, after any theme/libraries/plugins stylesheets. Whenever you need to override anything, you just copy-paste the selector from where it is currently defined, and only placing it in your custom stylesheet will make it have priority without higher specificity.
Adding !important tags to your media queries may be necessary, should you need to override styles provided by a pre-set template or development platform. For example I work with Squarespace, and have to override their default styles from time to time in this way - however, as with myself, I can understand your aversion towards doing so.
I know I'm not supposed to "ask for clarification" here, but my lack of rep prevents me from simply making a comment: are you working on a web develop platform similar to Squarespace, Weebly, etc., and does applying the !important tag in fact achieve the desired result?
Best,
Tyler
I have a weird one that I can't seem to be able to figure out. I am new to CSS and decided to use bootstrap to assist with styles etc.
the problem I have is when I try to assign two classes to a div element, 1 being the bootstrap column and another from my own stylesheet.
the code from my stylesheet seems to be ignored in some cases. now i have taken that one bit of code and css out and put it into the jsfiddle but it works fine. its only when combined with the rest of the html does it seem to have issues. also note that if i use inline styles it works...
I copied the entire code to js fiddle now so that you guys can replicate the issue. the section I am having issues with is the 4 images that are side by side
class="services-boxes"
anyway any assistance will be appreciated, as well as general feedback as I am new to this all! :)
https://jsfiddle.net/d9bv0grx/1/
Due to the way cascading style sheets work it (styles are be applied in order AND by specificity). It is most likely that styles you are expecting to see are being overridden by specificity.
Give this guide a read.
An example is that for <div id="selector">
#selector {background-color:red;}
div {background-color:green;}
You can expect to see a div with a red background, even though the green background is set afterwards, the id selector has greater specificity.
Then try and alter the specificity of your selectors in your css so that they will take precedence over in bootstrap.
Also just going to add, you have casing issues - you declare the class with lowercase in css, capitalised in your html.
You also have syntax issues in your css. Your css should look like:
.services-boxes {
padding:0;
max-height:500px;
width:100%;
}
Sort all this and you should be golden! jsfiddle
Looks like a combination of syntax errors. Your style should be declared like this:
.services-boxes {
padding:0px;
max-height: 500PX;
width:100%;
}
Note that the class is all lowercase (which should match style where declared which is currently Services-Boxes), a colon separating property and value (you has used = in some instances) and one set of curly braces per declaration (the above class .logo-image has 2 closing braces). Just a bit of formatting should see your code recognised
When you don't have total control over your HTML, you can use the !important property in css to give a priority to your styles.
.services-boxes {
color: red !important;
}
However keep in mind that you have to avoid the !important property as much as possible and do not use it unless you can't do it any other way.
Let I have 2 classes named class1 and class2. Also I have an element with id="responsive_element". What I want is to assign class1 to this element when screen size is below 768px and class2 otherwise.
I can do this in Less like:
#media screen and (max-width:768px){
#responsive_element{
.class1()
}
}
#media screen and (min-width:769px){
#responsive_element{
.class2()
}
}
Is there any "CSS only" way to achieve this?
Edit: I think I couldn't explain my question clear enough. I am already able to do this by compiling less, but the size of css file grows for the long class definitions and using them too much. I want to handle it with simply changing class of the element .
Except for the .class1() and .class2() calls, your Less code already is CSS. Specifically, the #media queries that apply styling based on screen size aren't Less-specific. So, for example, the following is pure CSS:
#media screen and (max-width:768px){
#responsive_element{
color: blue;
}
}
#media screen and (min-width:769px){
#responsive_element{
color: red;
}
}
If you want to convert your Less into CSS, all you need to do is copy the CSS code from .class1 and .class2 into the place of the .class1() and .class2() calls. In fact, since Less is implemented as a converter to CSS, you can just use the online converter at LESS2CSS to do the conversion for you.
If you're asking if there's a way in plain CSS to write these queries so that they use an existing .class1 and .class2 style definition without copying, then I believe the answer is no. The main reason Less was invented was because CSS doesn't support this kind of reuse of styling information.
If you mean, is there a way to use CSS alone to change the class of an element in response to changing screen sizes (literally, adding or removing new classnames to the element's HTML "class" attribute so CSS for different classes will take effect on that element), then the answer is simply "no, you cannot do this with CSS alone".
(Less can't do this either. Your original Less code doesn't change the class attribute of the element, as you can plainly see by looking at the generated CSS. It just uses classes as a handy trick to name sections of shareable CSS.)
The only way to change the element's class in the manner you seem to want is to make changes to the DOM, which you can obviously do via JavaScript but not with CSS alone.
I have a situation where a multiple select is somehow getting it's width via javascript dynamically but I can't seem to find where. I have nothing in my CSS that specifies a width of 411 px.
Website of Problem here
The width of the sidebar is the same always so I tried the following CSS and it is ignored? Am I missing some syntax? I've tried both of these and they do not work:
#widget-wrap .chosen-container-multi {width:200px !important;}
and
.widget-wrap .chosen-container-multi {width:200px !important;}
the control misbehaving is ul.chosen-choice so please set css as follows:
ul.chosen-choice {width:200px !important;}
There seems to be some inline styles declared in the widget template itself, rather than generated with JS.
Ideally you should remove the inline style from the template; otherwise this CSS will work just fine:
.chosen-container {
width:123px !important;
}
I can't see why your existing CSS wouldn't work other than it simply not being uploaded.