This question already has answers here:
Is there a specific order for CSS properties?
(6 answers)
Closed 2 years ago.
Does the order of properties in a CSS declaration have an effect on the rendering of the HTML?
This is really a trickier question than I first thought. My first reaction was:
Within a CSS rule (also called “rule set”), the order of declarations (of the form property: value) is immaterial. So foo { color: red; background: white; } is equivalent to foo { background: white; color: red; }. There is no separate statement about this in specifications (I suppose); it follows from the lack of any statement that would make the order significant.
On second thought, we could also have
* { font: 100% Calibri; font-weight: bold; }
What happens if you switch the order? In browsers, it does have an effect. I’m not sure how this should be interpreted in terms of CSS specification, but browsers apparently, and naturally, process the declarations sequentially. So the rule makes the text bold, but if you change the order,
* { font-weight: bold; font: 100% Calibri; }
then things change. The font shorthand sets font-weight to its initial value normal, so the text is not bold.
On third thought, there are many methods and tricks based on the order. So yes, the order of declarations is significant.
The impact of the order of rules is a completely different issue.
Apparently the order does not have any direct impact on the result.
The subject has been mentioned here: http://css-tricks.com/new-poll-how-order-css-properties/
It does have an impact according to here: http://css-tricks.com/ordering-css3-properties/
And here is another trend: http://perishablepress.com/obsessive-css-code-formatting-patterns-and-trends/
Final verdict: Arrange the way you judge best, it will work.
Mainly 5 Types Of CSS Property Orde:
Randomly
Grouped By
Alphabetical
By Line Length
CSS Properties by Importance
1. Randomly
.module {
border: 1px solid #ccc;
width: 25%;
padding: 20px;
position: relative;
min-height: 100px;
z-index: 1;
border-radius: 20px;
}
2. Grouped By
.module {
width: 25%;
min-height: 100px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 20px;
position: relative;
z-index: 1;
}
3. Alphabetical
.module {
border-radius: 20px;
border: 1px solid #ccc;
min-height: 100px;
padding: 20px;
position: relative;
width: 25%;
z-index: 1;
}
4. By Line Length
.module {
border: 1px solid #ccc;
border-radius: 20px;
position: relative;
min-height: 100px;
padding: 20px;
z-index: 1;
width: 25%;
}
Here is Refrence URL: https://css-tricks.com/new-poll-how-order-css-properties/
5. Ordering CSS Properties by Importance
But Best Way To Ordering CSS Properties by Importance
Layout Properties (position, float, clear, display)
Box Model Properties (width, height, margin, padding)
Visual Properties (color, background, border, box-shadow)
Typography Properties (font-size, font-family, text-align,
text-transform)
Misc Properties (cursor, overflow, z-index)
Example:
.button {
display:inline-block;
margin:1em 0;
padding:1em 4em;
color:#fff;
background:#196e76;
border:0.25em solid #196e76;
box-shadow:inset 0.25em 0.25em 0.5em rgba(0,0,0,0.3),
0.5em 0.5em 0 #444;
font-size:3em;
font-family:Avenir, Helvetica, Arial, sans-serif;
text-align:center;
text-transform:uppercase;
text-decoration:none;
}
In normally no need or rule to order of properties in a CSS declaration. I wrote order like my requirements of design and browser response, but in firebug tool it arrange sin alphabet order.
The best method of order is
1. box model = width ->height->float
2. font related = font-size -> text-decoration, font-family
3. background images = width, height, border, image
Then only browser allocate space for each element faster in my experience.
Some persons order like below types:
Random
Alphabetical
Grouped by Type
By properties Length
There are some guidelines. But, at the end of the day it comes down to browser implementation. Try not to rely on a certain order always working, because it won't. Inside a declaration, a statement tends to override a previous statement.
Related
Let's say I have two different stylesheets that are setting values to unique properties on the div element with class="example".
Stylesheet A:
.example {
background-color: #000;
border-radius: 50%;
color: #fff;
width: 25px;
height: 25px;
line-height: 25px;
display: inline-block;
text-align: center;
}
Stylesheet B:
div{
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
vertical-align: baseline;
background: transparent;
}
Is there a shorthand property and value similar to all: revert; that I could add to the Stylesheet A to set all undeclared properties to the browser default and essentially overwrite/erase the Stylesheet B? In my mind, it would be something like remaining: revert;.
If there is not such a shorthand property, is there some other trick I could do without explicitly having to list all the properties that haven't been declared in Stylesheet A and applying 'revert'? Bonus points if I could apply this trick recursively to the elements within the div with > and/or * selectors.
Thanks in advance.
There isn't a one-size-fits-all way to do this due to how cascade resolution for the all property works (so the bonus question doesn't have a universal answer, it depends entirely on the project). The CSS cascade does not compartmentalize rules and declarations by the stylesheet they appear in — all author-origin styles are consolidated to a single logical "stylesheet" in the order they appear in, and cascade resolution is performed accordingly. This means equally specific styles in later stylesheets will take precedence, whereas more specific styles in earlier stylesheets will take precedence.
In this case, because .example happens to be more specific than div, placing an all: reset declaration at the top of the .example rule will give you the desired effect (assuming there are no other style declarations elsewhere you would like to preserve, or more specific declarations elsewhere you would like removed as those would continue to apply even if you do this):
.example {
all: revert;
background-color: #000;
border-radius: 50%;
/* ... */
Everything in .example appearing after the all: revert declaration will be unaffected by it.
In the following code snippet you can compare the results of applying all: revert this way, against an element without the declaration:
.example:where(:first-of-type) {
all: revert;
}
.example {
background-color: #000;
border-radius: 50%;
color: #fff;
width: 25px;
height: 25px;
line-height: 25px;
display: inline-block;
text-align: center;
}
div{
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
vertical-align: baseline;
background: transparent;
}
<p>Inspect each element's styles to see the effects of <code>all: revert</code>
<div class="example">1</div>
<div class="example">2</div>
Or, see the following screenshots of Firefox's Inspector:
Element 1:
Element 2:
I'm trying to create a label that has a right arrow. It looks like this:
The code I'm using is the following (LESS):
#font-size: 14px;
#padding-top: 4px;
#arrow-bheight: #font-size;
.label {
position: relative;
font-size: #font-size;
font-weight: normal;
font-style: italic;
text-transform: uppercase;
padding: #padding-top 8px;
line-height: 1;
color: #fff;
background: #000;
&:after {
content: "";
width: 0;
height: 0;
border-top: #arrow-bheight solid transparent;
border-bottom: #arrow-bheight solid transparent;
border-left: #arrow-bheight solid #000;
position: absolute;
right: -#arrow-bheight;
top:0;
}
}
My problem is that depending on the font we use, the arrow doesn't fit perfectly into the text font.
I've been trying to set a specific value for the line-height, e.g. line-height: #font-size, but it's not working.
Here's a CodePen of the problem.
How can I make the right arrow have the proper height regardless of the font used?
There's no need to define #padding-top variable separately. Instead you could use em unit which is relative to the element's font-size.
Also you should change the display type of the labels to inline-block:
Updated Example
.label {
display: inline-block;
font-size: #font-size; /* This has been set to 14px */
padding: 0.5em 8px; /* The computed value of 0.5em would be 14px/2 = 7px */
line-height: 1;
/* other declarations... omitted due to brevity */
}
Two things that may help...
The background-position edge offsets and the CSS calc unit.
I'm not familiar with the first though CSS calc will let you do things such as...
height: calc(100% - 24px);
I like it to separate different concerns of a class in css.
Example:
// Layout
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
}
// Chrome
.myElement {
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
}
// Content
.myElement {
color: white;
font-size: 120%;
}
// Interaction
.myElement:hover {
background: black;
border: 10px dotted red;
}
Pretty fine so far. Personally I find that approach readable, nice to maintain and nice for development.
But is there any mechanism/tool available which would merge all rules of a class into one single class declaration before deployment automatically?
Desired result:
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
color: white;
font-size: 120%;
}
.myElement:hover {
background: black;
border: 10px dotted red;
}
EDIT:
After reading the comments I felt my "artificial" example wasn't comprehensive enough. Below is a real world example written in the Stylus syntax but the results are the same as described above. I left the original example from above in place.
The next example exists as a single file which describes the appereance of a certain area and its contents which appears in various places on a site. It's like a microcosm for that area. As a consequence you can imagine there are more of these microcosms.
// Layout
.events
padding 2em
.eventItem
margin 0 0 4em
.eventCal
float left
padding 2.5em 0 0
.eventArticle
margin 0 0 0 6em
.eventHead
margin 0 0 1em
time
margin 0 2em 0 0
// Chrome
.eventCal
background url("../../assets/img/icon-events.png") no-repeat
.eventHead
border-bottom .5em solid $chimney
// Content
.eventItem
list-style-type none
a
color $beige
text-decoration none
h2
font-family $elsie
.eventCal
color $beige
font-family $elsie
font-weight bold
font-size 150%
.eventHead
font-size 75%
.eventDesc
font-size 90%
// Interaction
.eventItem
a:hover
color $chimney
text-decoration underline
My computer is sorta nagging me to reboot after a few straight days of coding , but basically I do stuff like that fairly regularly in my libraries. Basically what you can do to quickly mimmick this functionality is to grab one of the zillion coding examples for enumerating the CSSrules from the sheets.
Then in the enumeration function simply take the selectorText property and drop it into document.querySelector/querySelectorAll which will give you the elements matching the selectorText toss these , the selectorText and related cssText into a little {}/[] or whatever suits your fancy.
After the enumeration finishes do a document.createElement('style') and use the insertRule() to glue the styles together as you please.
Then simply grab the finished product from style.textContent and you have your merged stylesheet.
Being as the commonly available code for enumeration does the actual hard part , this is a nice lazy evenings coding task for most folks and it will do the job EXACTLY the way YOU want.
Is there a standard or best practice for the order of grouping of styles CSS element styles? I know that this isn't a major concern, but I want to be sure I'm always producing readable code, especially for elements with many styles.
Take for example:
#element {
font-family: Arial;
font-size: 8pt;
color: #666666;
border: 1px solid #ccc;
position: relative;
top: 5px;
left: 5px;
}
#element groups the styles in the order of text styles, then border, then position. Is there a standard for a css-type hierarchy that places some type of priority or importance on this order? For example, should you group in order of: position, text styles, border?
Quite frankly, it boils down to personal preference, but here's my convention:
Group things that look like they're related to each other. Then, use white space to separate each "group". I just hit "enter" after each block. For other styles, like "top", "left", etc., I put them all in one line, after their main style (like "position"). I also tend to put CSS3 properties as the last style in any given block.
Sometimes, when I'm in a good mood, I also tend to loosely alphabetize the properties (by block). But again, it's really just preference.
Example of what I do:
#element {
color:black;
font-family:Arial;
font-size:1.2em;
font-weight:bold;
text-transform:capitalize;
text-shadow:0 1px 1px black;
background-color:white;
border-bottom:1px dotted gray;
box-shadow:1px 1px 2px black;
position:fixed;
top:0; right:0;
height:30px;
width:245px;
}
Just my two pennies!
Different people have different opinion, I would prefer it to be alphabetically sorted.
Then CSS itself can be re-factored to separate out structural CSS pages and ui element specific pages.
The structural CSS are the ones that control the structure of the pages.
Good question, not sure there is a standard but my preferred method is.
#element {
layout,
positioning,
text style
appearance
}
For example
#element {
display: block;
width: 15px;
height: 15px;
float: left;
position: relative;
font-size: 12px;
line-height: 16px;
text-decoration: none;
color: #333;
text-indent: -9999px;
white-space: nowrap;
background: #fff;
border: 1px solid #ccc;
border-radius: 3px;
}
Like with all styles of coding, it's not that important. What does matter is consistency. If you work on a team, you should all agree on what suits you all best rather than find a convention that is thought to be best that you'd need to adapt to... That's really a waste of time. Likewise, if you are working on projects that will be inherited, stick with the same convention, but do what suits you best.
Anyone know why CSS provides color for text, but does not have font-color or text-color?
Seems very counter-intuitive, kind of like text-decoration: underline rather than font-style or something related to fonts.
Does anyone know why/how the W3C came up with such a wide array of CSS names like this?
The same way Boston came up with its street plan. They followed the cow paths already there, and built houses where the streets weren't, and after a while it was too much trouble to change.
I would think that one reason could be that the color is applied to things other than font. For example:
div {
border: 1px solid;
color: red;
}
Yields both a red font color and a red border.
Alternatively, it could just be that the W3C's CSS standards are completely backwards and nonsensical as evidenced elsewhere.
I know this is an old post but as MisterZimbu stated, the color property is defining the values of other properties, as the border-color and, with CSS3, of currentColor.
currentColor is very handy if you want to use the font color for other elements (as the background or custom checkboxes and radios of inner elements for example).
Example:
.element {
color: green;
background: red;
display: block;
width: 200px;
height: 200px;
padding: 0;
margin: 0;
}
.innerElement1 {
border: solid 10px;
display: inline-block;
width: 60px;
height: 100px;
margin: 10px;
}
.innerElement2 {
background: currentColor;
display: inline-block;
width: 60px;
height: 100px;
margin: 10px;
}
<div class="element">
<div class="innerElement1"></div>
<div class="innerElement2"></div>
</div>