[css/en] Fixed grammar, increased clarity.

Also fixed extraneous whitespace, code mistakes, made sure line width was <80 characters.
This commit is contained in:
duci9y 2015-10-10 14:59:30 +05:30
parent d8a1c0cf6a
commit 689bb106f5

View File

@ -5,26 +5,21 @@ contributors:
- ["Marco Scannadinari", "https://github.com/marcoms"] - ["Marco Scannadinari", "https://github.com/marcoms"]
- ["Geoffrey Liu", "https://github.com/g-liu"] - ["Geoffrey Liu", "https://github.com/g-liu"]
- ["Connor Shea", "https://github.com/connorshea"] - ["Connor Shea", "https://github.com/connorshea"]
- ["Deepanshu Utkarsh", "https://github.com/duci9y"]
filename: learncss.css filename: learncss.css
--- ---
In the early days of the web there were no visual elements, just pure text. But with the In the early days of the web there were no visual elements, just pure text. But with further development of web browsers, fully visual web pages also became common.
further development of browsers, fully visual web pages also became common.
CSS is the standard language that exists to keep the separation between
the content (HTML) and the look-and-feel of web pages.
In short, what CSS does is to provide a syntax that enables you to target CSS helps maintain separation between the content (HTML) and the look-and-feel of a web page.
different elements on an HTML page and assign different visual properties to them.
Like any other languages, CSS has many versions. Here we focus on CSS2.0, CSS lets you target different elements on an HTML page and assign different visual properties to them.
which is not the most recent version, but is the most widely supported and compatible version.
**NOTE:** Because the outcome of CSS consists of visual effects, in order to This guide has been written for CSS 2, though CSS 3 is fast becoming popular.
learn it, you need try everything in a
CSS playground like [dabblet](http://dabblet.com/). **NOTE:** Because CSS produces visual results, in order to learn it, you need try everything in a CSS playground like [dabblet](http://dabblet.com/).
The main focus of this article is on the syntax and some general tips. The main focus of this article is on the syntax and some general tips.
```css ```css
/* comments appear inside slash-asterisk, just like this line! /* comments appear inside slash-asterisk, just like this line!
there are no "one-line comments"; this is the only comment style */ there are no "one-line comments"; this is the only comment style */
@ -33,219 +28,212 @@ The main focus of this article is on the syntax and some general tips.
## SELECTORS ## SELECTORS
#################### */ #################### */
/* Generally, the primary statement in CSS is very simple */ /* the selector is used to target an element on a page.
selector { property: value; /* more properties...*/ } selector { property: value; /* more properties...*/ }
/* the selector is used to target an element on page.
You can target all elements on the page using asterisk! */
* { color:red; }
/* /*
Given an element like this on the page: Here is an example element:
<div class='some-class class2' id='someId' attr='value' otherAttr='en-us foo bar' /> <div class='class1 class2' id='anID' attr='value' otherAttr='en-us foo bar' />
*/ */
/* you can target it by its name */ /* You can target it using one of its CSS classes */
.some-class { } .class1 { }
/* or by both classes! */ /* or both classes! */
.some-class.class2 { } .class1.class2 { }
/* or by its element name */ /* or its name */
div { } div { }
/* or its id */ /* or its id */
#someId { } #anID { }
/* or by the fact that it has an attribute! */ /* or using the fact that it has an attribute! */
[attr] { font-size:smaller; } [attr] { font-size:smaller; }
/* or that the attribute has a specific value */ /* or that the attribute has a specific value */
[attr='value'] { font-size:smaller; } [attr='value'] { font-size:smaller; }
/* start with a value (CSS3) */ /* starts with a value (CSS 3) */
[attr^='val'] { font-size:smaller; } [attr^='val'] { font-size:smaller; }
/* or ends with (CSS3) */ /* or ends with a value (CSS 3) */
[attr$='ue'] { font-size:smaller; } [attr$='ue'] { font-size:smaller; }
/* or select by one of the values from the whitespace separated list (CSS3) */ /* or contains a value in a space-separated list */
[otherAttr~='foo'] { font-size:smaller; } [otherAttr~='foo'] { }
[otherAttr~='bar'] { }
/* or value can be exactly “value” or can begin with “value” immediately followed by “-” (U+002D) */ /* or contains a value in a dash-separated list, ie, "-" (U+002D) */
[otherAttr|='en'] { font-size:smaller; } [otherAttr|='en'] { font-size:smaller; }
/* and more importantly you can combine these together -- there shouldn't be /* You can concatenate different selectors to create a narrower selector. Don't
any space between different parts because that makes it to have another put spaces between them. */
meaning. */
div.some-class[attr$='ue'] { } div.some-class[attr$='ue'] { }
/* you can also select an element based on its parent. */ /* You can select an element which is a child of another element */
div.some-parent > .class-name { }
/* an element which is direct child of an element (selected the same way) */ /* or a descendant of another element. Children are the direct descendants of
div.some-parent > .class-name {} their parent element, only one level down the tree. Descendants can be any
level down the tree. */
div.some-parent .class-name { }
/* or any of its parents in the tree /* Warning: the same selector without a space has another meaning.
the following basically means any element that has class "class-name" Can you guess what? */
and is child of a div with class name "some-parent" IN ANY DEPTH */ div.some-parent.class-name { }
div.some-parent .class-name {}
/* warning: the same selector without space has another meaning. /* You may also select an element based on its adjacent sibling */
can you say what? */ .i-am-just-before + .this-element { }
div.some-parent.class-name {}
/* you also might choose to select an element based on its direct /* or any sibling preceding it */
previous sibling */ .i-am-any-element-before ~ .this-element { }
.i-am-before + .this-element { }
/* or any sibling before this */ /* There are some selectors called pseudo classes that can be used to select an
.i-am-any-before ~ .this-element {} element when it is in a particular state */
/* There are some pseudo classes that allows you to select an element /* for example, when the cursor hovers over an element */
based on its page behaviour (rather than page structure) */ selector:hover { }
/* for example for when an element is hovered */ /* or a link has been visited */
selector:hover {} selector:visited { }
/* or a visited link */ /* or hasn't been visited */
selected:visited {} selected:link { }
/* or not visited link */ /* or an element in focus */
selected:link {} selected:focus { }
/* or an input element which is focused */
selected:focus {}
/* At appropriate places, an asterisk may be used as a wildcard to select every
element */
* { } /* all elements */
.parent * { } /* all descendants */
.parent > * { } /* all children */
/* #################### /* ####################
## PROPERTIES ## PROPERTIES
#################### */ #################### */
selector { selector {
/* Units */ /* Units of length can be absolute or relative. */
width: 50%; /* in percent */
font-size: 2em; /* times current font-size */ /* Relative units */
width: 200px; /* in pixels */ width: 50%; /* percentage of parent element width */
font-size: 20pt; /* in points */ font-size: 2em; /* multiples of element's original font-size */
width: 5cm; /* in centimeters */ font-size: 2rem; /* or the root element's font-size */
min-width: 50mm; /* in millimeters */ font-size: 2vw; /* multiples of 1% of the viewport's width (CSS 3) */
max-width: 5in; /* in inches. max-(width|height) */ font-size: 2vh; /* or its height */
height: 0.2vh; /* times vertical height of browser viewport (CSS3) */ font-size: 2vmin; /* whichever of a vh or a vw is smaller */
width: 0.4vw; /* times horizontal width of browser viewport (CSS3) */ font-size: 2vmax; /* or greater */
min-height: 0.1vmin; /* the lesser of vertical, horizontal dimensions of browser viewport (CSS3) */
max-width: 0.3vmax; /* same as above, except the greater of the dimensions (CSS3) */ /* Absolute units */
width: 200px; /* pixels */
font-size: 20pt; /* points */
width: 5cm; /* centimeters */
min-width: 50mm; /* millimeters */
max-width: 5in; /* inches */
/* Colors */ /* Colors */
background-color: #F6E; /* in short hex */ color: #F6E; /* short hex format */
background-color: #F262E2; /* in long hex format */ color: #FF66EE; /* long hex format */
background-color: tomato; /* can be a named color */ color: tomato; /* a named color */
background-color: rgb(255, 255, 255); /* in rgb */ color: rgb(255, 255, 255); /* as rgb values */
background-color: rgb(10%, 20%, 50%); /* in rgb percent */ color: rgb(10%, 20%, 50%); /* as rgb percentages */
background-color: rgba(255, 0, 0, 0.3); /* in semi-transparent rgb (CSS3) */ color: rgba(255, 0, 0, 0.3); /* as rgba values (CSS 3) Note: 0 < a < 1 */
background-color: transparent; /* see thru */ color: transparent; /* equivalent to setting the alpha to 0 */
background-color: hsl(0, 100%, 50%); /* hsl format (CSS3). */ color: hsl(0, 100%, 50%); /* as hsl percentages (CSS 3) */
background-color: hsla(0, 100%, 50%, 0.3); /* Similar to RGBA, specify opacity at end (CSS3) */ color: hsla(0, 100%, 50%, 0.3); /* as hsla percentages with alpha */
/* Images as backgrounds of elements */
/* Images */ background-image: url(/img-path/img.jpg); /* quotes inside url() optional */
background-image: url(/path-to-image/image.jpg); /* quotes inside url() optional */
/* Fonts */ /* Fonts */
font-family: Arial; font-family: Arial;
font-family: "Courier New"; /* if name has space it appears in single or double quotes */ /* if the font family name has a space, it must be quoted */
font-family: "Courier New", Trebuchet, Arial, sans-serif; /* if first one was not found font-family: "Courier New";
browser uses the second font, and so forth */ /* if the first one is not found, the browser uses the next, and so on */
font-family: "Courier New", Trebuchet, Arial, sans-serif;
} }
``` ```
## Usage ## Usage
Save any CSS you want in a file with extension `.css`. Save a CSS stylesheet with the extension `.css`.
```xml ```xml
<!-- you need to include the css file in your page's <head>: --> <!-- You need to include the css file in your page's <head>. This is the
recommended method. Refer to http://stackoverflow.com/questions/8284365 -->
<link rel='stylesheet' type='text/css' href='path/to/style.css' /> <link rel='stylesheet' type='text/css' href='path/to/style.css' />
<!-- you can also include some CSS inline in your markup. However it is highly <!-- You can also include some CSS inline in your markup. -->
recommended to avoid this. -->
<style> <style>
a { color: purple; } a { color: purple; }
</style> </style>
<!-- or directly set CSS properties on the element. <!-- Or directly set CSS properties on the element. -->
This has to be avoided as much as you can. -->
<div style="border: 1px solid red;"> <div style="border: 1px solid red;">
</div> </div>
``` ```
## Precedence ## Precedence or Cascade
As you noticed an element may be targetted by more than one selector. An element may be targeted by multiple selectors and may have a property set on it in more than once. In these cases, one of the rules takes precedence over others. Generally, a rule in a more specific selector take precedence over a less specific one, and a rule occuring later in the stylesheet overwrites a previous one.
and may have a property set on it in more than one.
In these cases, one of the rules takes precedence over others. This process is called cascading, hence the name Cascading Style Sheets.
Given the following CSS: Given the following CSS:
```css ```css
/*A*/ /* A */
p.class1[attr='value'] p.class1[attr='value']
/*B*/ /* B */
p.class1 {} p.class1 { }
/*C*/ /* C */
p.class2 {} p.class2 { }
/*D*/ /* D */
p {} p { }
/*E*/ /* E */
p { property: value !important; } p { property: value !important; }
``` ```
and the following markup: and the following markup:
```xml ```xml
<p style='/*F*/ property:value;' class='class1 class2' attr='value'> <p style='/*F*/ property:value;' class='class1 class2' attr='value' />
</p>
``` ```
The precedence of style is as followed: The precedence of style is as follows. Remember, the precedence is for each **property**, not for the entire block.
Remember, the precedence is for each **property**, not for the entire block.
* `E` has the highest precedence because of the keyword `!important`. * `E` has the highest precedence because of the keyword `!important`. It is recommended that you avoid its usage.
It is recommended to avoid this unless it is strictly necessary to use. * `F` is next, because it is an inline style.
* `F` is next, because it is inline style. * `A` is next, because it is more "specific" than anything else. It has 3 specifiers: The name of the element `p`, its class `class1`, an attribute `attr='value'`.
* `A` is next, because it is more "specific" than anything else. * `C` is next, even though it has the same specificity as `B`. This is because it appears after `B`.
more specific = more specifiers. here 3 specifiers: 1 tagname `p` + * `B` is next.
class name `class1` + 1 attribute `attr='value'` * `D` is the last one.
* `C` is next. although it has the same specificness as `B`
but it appears last.
* Then is `B`
* and lastly is `D`.
## Compatibility ## Compatibility
Most of the features in CSS2 (and gradually in CSS3) are compatible across Most of the features in CSS 2 (and many in CSS 3) are available across all browsers and devices. But it's always good practice to check before using a new feature.
all browsers and devices. But it's always vital to have in mind the compatibility
of what you use in CSS with your target browsers.
[QuirksMode CSS](http://www.quirksmode.org/css/) is one of the best sources for this. ## Resources
To run a quick compatibility check, [Can I Use...](http://caniuse.com) is a great resource. * To run a quick compatibility check, [CanIUse](http://caniuse.com).
* CSS Playground [Dabblet](http://dabblet.com/).
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/)
## Further Reading ## Further Reading
* [Mozilla Developer Network's CSS documentation](https://developer.mozilla.org/en-US/docs/Web/CSS)
* [Codrops' CSS Reference](http://tympanus.net/codrops/css_reference/)
* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/) * [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/)
* [Selecting elements using attributes](https://css-tricks.com/almanac/selectors/a/attribute/)
* [QuirksMode CSS](http://www.quirksmode.org/css/) * [QuirksMode CSS](http://www.quirksmode.org/css/)
* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context) * [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context)
* [SCSS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing * [SASS](http://sass-lang.com/) and [LESS](http://lesscss.org/) for CSS pre-processing
* [CSS-Tricks](https://css-tricks.com)