Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In my sass I'm keen for it not to get out of hand, i've a simple question/example what I'd like to know is what'd be the most efficient way to target the button in the example below?
Personally I like option 2 but have i done it correctly?
Call it picky but my problems are as listed below,
I'm not a fan of adding class/id to everything through html
Using Sass's ability to nest/target child elements within a parent is too overly specific
Option 1:
Give button class name in html making it easy to target in css
<div id = "box">
<!-- Give button class/id -->
<button class = "button1"></button>
</div>
Option 2:
Have _buttons.scss partial containing a .button1 class
On my main.scss target the parent container #box without button a class name in html, and then target nested button
buttons.scss
.button1 {
width: 100px;
height: 100px;
background-color: grey;
}
main.scss
#import 'components/buttons.scss';
#box {
button {
// Extend class from buttons.scss
#extend .button1;
}
}
I think it depends on how often you are going to use the styling for different buttons. Having an id of 'box' seems very specific to me though. Are you not going to have any other boxes? If so, this needs to be a class and not an id.
Instead of targeting a parent and styling the child (what happens when u want to style an element without that parent), I would just style the class on the button itself.
1st option - only one class needed in the HTML
.button1{
/* button css */
}
2nd option - id attribute needed, specific HTML hierarchy, more CSS output
#box button, .button1{
/* button css */
}
A lot of the answer as to what's "most efficient" when it comes to SASS and partials depends on what your complete SASS configuration will look like, so it's hard to say based on a specific example.
Are you going to need to use the style for this button in multiple stylesheets?
For instance, if you only have one style sheet (main.scss), I'd say there's no reason to use a partial at all. Partials should be used when you need to include the items in the partial in multiple stylesheets. Many people, for example, will write all variables and mixins in a partial and include them in every stylesheet.
Given this one specific example, the more efficient thing to do is not use a partial.
Adding a class to the button and targeting that class in the css, or targeting the container and child element (without class) is equally efficient:
#box button {}
button.button1 {}
Also, there's no reason to nest here if you don't have other rules inside of the #box parent:
#box {
button {
}
}
Just do this:
#box button {}
(And be careful with extends. If used in areas where they're not really needed, you can end up with a TON of unnecessary style rules. See this great article.)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Let we have a html like this;
<div class="mainElement">
<div class="subElement1">...</div>
<div class="subElement2">...</div>
<div class="subElement3">...</div>
</div>
So if want to style that 'subElement1' which one is more standart or faster.
.mainElement > .subElement1{
/*some CSS here..*/
}
.mainElement .subElement1{
/*some CSS here..*/
}
.mainElement > div:first-child{
/*some CSS here..*/
}
From ones you have, best is:
.mainElement > .subElement1{
/*some CSS here..*/
}
because it targets direct child from parent class.
But if you really want performance, you want to target class directly:
.subElement1{
/*some CSS here..*/
}
Or if you want even more faster code, use IDs:
#subElement1{
/*some CSS here..*/
}
IDs are generally faster for browser to target, since they are supposed to be used only once per element.
Browsers read CSS from right to left, so adding a parent class/id unless you really need it, is only slowing down your code.
Why not just .subelement1?
If there is no reason to nest your selectors it is better off not to nest/couple them together.
But for your question it really depends on what you are trying to do the options you have posted all do different things.
The first one is fast and specific but selects only direct descendants of .mainElement
The second option selects all .subelement1 classes that are within .mainElement which right now is only one element but you can keep nesting them inside of each other and they would all get the same styling not just the direct descendants.
As for the last one it is the most specific and could get you into trouble it finds the a div inside of .mainElement that is a first-child this could lead to trouble if you were to change the markup at all and say uses spans instead of divs also only the first child will always get that styling no matter what class you gave it.
.subElement is enough, you shouldn't access the parent class first, cause it's affect to file size (althaough very small size) and make the css load file slower. Hope this link help you Writing efficient CSS
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Occasionally, css adds dynamic to pure html pages. For example, changing the background colour on hover. I want to understand, how does the css work?
CSS:
div {
position: absolute;
width: 100px;
height: 100px;
background: black;
}
div:hover {
background: red;
}
HTML:
<html>
<body>
<div></div>
</body>
</html>
plnkr code
When mouse enters "div" does browser just override background to black (i.e browser leaves height: 100px, width: 100px and position: absolute) or recalculates it ?
In usual case, when some event happens and changes css of an element, does it just override properties which are present in css style ? In other words, after some several events, does style of an element might end up arbitrary?
In usual case, when some event happens and changes css of an element, does it just override properties which are present in css style ?
The element begins to match a new selector. A new rule-set is applied to the element. The properties of the element are recalculated according to the standard rules for the cascade.
In other words, after some several events, does style of an element might end up arbitrary?
No. The rules for the cascade (which defines which order rules are applied in) are (very) clearly defined in the specification. There is nothing arbitrary about them.
In a nutshell: When you hover over an element, the element goes into the :hover state. It now matches two selectors in the CSS file: div and div:hover. All the properties of both rule sets are applied to the element. Where there are conflicts, e.g. background defined on both, the more specific selector wins (*read the spec, the rules aren't easily summarised).
When you leave the element, it goes out of its :hover state. The div:hover selector does not apply anymore, only one rule set applies now. All the element's style properties are recalculated from scratch, resulting in the background turning black again (because it's the only rule that applies).
Everything is in a very defined state at all times, the changes are not incremental and will never result in weird states because something didn't get unset or such.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to display two kinds of styles in one webpage. I have a.css for the first style and b.css for the second style. The order is b overrides a. The problem is I want the css priority to be reversed in a particular div tag. Is this possible?
What is the reason for this? You can always override using an "!important" declaration. For example:
.style { font-size: 12px !important;}
Also, refer to this guide here: http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade (Specificity Calculations, Inheritance, The Cascade)
There are very few cases where you need to have 2 different CSS files to do the same thing. However, there are many methods that can fix this, one of which is just creating an class/ID of its own in what ever CSS file you want to override with and then attach it to the HTML Element.
If that doesn't work, my next suggestion would be is to try inline styling.
<div id="blabla" style="whatyouwantforthisinparticular">
You can't just "override" another script through HTML. Code works in a linear format. Once it sees a new line of code referring to that, it will take precedence based on what you did with it. For example, in CSS, you can have 2 different body stylings, but the top one's attributes will only be used unless the second has something new to add. For example:
body{ background-color:black; width: 500px;
}
body{ background-color:white; height: 300px;
}
In this example, background-color: black will changed to "white" and then it will add 500px to the height on top of the width of the previous styling. However, if you must have black, then adding !important will make it take precedence over white.
Yes you can do it using the !important attribute.
.your-class{
property: value !important;
}
Also you can do that being more specific in your class
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have some CSS classes as:
.span9 {
width: 870px;
}
.span4 {
width: 370px;
}
In another custom CSS file I want to make another CSS class .span9? How do I do this?
The reason is because a DOM element uses .span4 and I want to override this but it must be overridden in a custom external CSS file.
Is this possible?
you could use the !important feature to override any settings in another file so in your new file you could do something like this:
.span9 {
width: 800px !important;
}
Yes it is, CSS is cascading (style sheet) so simply load the changes after the initial styles and it will work as you want. If you want to override width just re declare, if you want to add styles simple declare them.
If you have different parent of .span you can do this :
HTML
<div id="parent">
<span class="span9"> Hello world!</span>
</div>
CSS
.span9{width:870px;}
#parent .span9{width:100px;}
Or in you're HTML directly (but it's not a very good method)
<span class="span9" style="width:100px!important;"> Hello world</span>
Yes, just use !important. This will override other CSS rules that come after it, unless they're also marked as !important.
So your second stylesheet could contain something like
.span9 {
width: 1020px !important;
}
and that's what the CSS will default to. You can use this to override inbuilt stylesheets in CMSs etc, without having to mess with their actual stylesheet.
You can override it by concatenation of tag name and class name. For example , this : div.span4 will override simple .span4 , have a look: http://jsfiddle.net/5dYHG/1/
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What is the 'best practice' in terms of including hierarchy within css names? EG:, suppose we have the following html:
<div class="foo">
<div class="$className2">
<div class="$className3">123
</div>
</div>
</div>
Should $className2 include 'foo-' as a prefix, and $className3 include both 'foo-' and $className2 as a prefix? Or should the parent class names not be included within child class names?
There is no need to include a parent class's name. CSS selectors will handle any parent formatting you need.
A class, should be describing the type of content you are using. For example
<div class="sidebar">
<div class="error">
</div>
</div>
It is likely that you are going to want to have formatting specific to the error class, that is irrespective of the sidebar class. In the event that you want formatting for the error class that is specific to the sidebar, a css selector could handle that for you
.sidebar > .error
or
.sidebar .error
Very simply, are you reusing those class names or will you be reusing those class names anywhere other than in the same kind of structure? If not then it is not necessary to limit it by parent. If you are, however, then it would be a good idea to explicitly indicate which child element is which by including their parent node selectors.
One thing that I do most of the time is to prepend an underscore to child classes - I would use the following css for the html from your example:
.foo { ... }
.foo ._$className2 { .. }
.foo ._$className3 { .. }
In this case, ".foo" would be wrapping something like a "foo component". And every child class that is only used inside this wrapper (meaning it wouldn't make sense on its own) is prepended with an underscore. This keeps your css clean and makes it easy for you to see whether a class is intended to be applied to elements everywhere on the page or whether it should appear in a special context.
There aren't hard best practices when it comes to CSS class naming. Relevant factors like how many rules you have, how modular your components are, and the scale of your website are important considerations.
For small sites, I prefer to keep the CSS classes very simple.
In your CSS, you can use a nested selector to target elements that are structured in the way you presented like this:
.foo > .bar > .baz { ... }
HTML:
<div class="foo">
<div class="bar">
<div class="baz">
...
</div>
</div>
</div>
For larger sites, or if you're looking for a prescriptive naming pattern, one popular methodology is block-element modifier (BEM).