2013-11-10 22:37:34 +04:00
---
language: css
contributors:
- ["Mohammad Valipour", "https://github.com/mvalipour"]
2014-02-27 01:22:39 +04:00
- ["Marco Scannadinari", "https://github.com/marcoms"]
2014-09-21 11:22:26 +04:00
- ["Geoffrey Liu", "https://github.com/g-liu"]
2015-10-05 03:53:55 +03:00
- ["Connor Shea", "https://github.com/connorshea"]
2015-10-10 12:29:30 +03:00
- ["Deepanshu Utkarsh", "https://github.com/duci9y"]
2015-10-26 02:15:23 +03:00
- ["Tyler Mumford", "https://tylermumford.com"]
2014-01-30 11:30:28 +04:00
filename: learncss.css
2013-11-10 22:37:34 +04:00
---
2015-10-26 02:15:23 +03:00
Web pages are built with HTML, which specifies the content of a page. CSS (Cascading Style Sheets) is a separate language which specifies a page's **appearance** .
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
CSS code is made of static *rules* . Each rule takes one or more *selectors* and gives specific *values* to a number of visual *properties* . Those properties are then applied to the page elements indicated by the selectors.
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
This guide has been written with CSS 2 in mind, which is extended by the new features of CSS 3.
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
**NOTE:** Because CSS produces visual results, in order to learn it, you need to try everything in a CSS playground like [dabblet ](http://dabblet.com/ ).
2015-10-10 12:29:30 +03:00
The main focus of this article is on the syntax and some general tips.
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
## Syntax
2013-11-10 22:37:34 +04:00
```css
2014-09-21 11:22:26 +04:00
/* comments appear inside slash-asterisk, just like this line!
there are no "one-line comments"; this is the only comment style */
2013-11-19 02:39:13 +04:00
/* ####################
## SELECTORS
2014-09-21 11:22:26 +04:00
#################### */
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
/* the selector is used to target an element on a page. */
2013-11-10 22:37:34 +04:00
selector { property: value; /* more properties...*/ }
/*
2015-10-10 12:29:30 +03:00
Here is an example element:
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
< div class = 'class1 class2' id = 'anID' attr = 'value' otherAttr = 'en-us foo bar' / >
2013-11-10 22:37:34 +04:00
*/
2015-10-10 12:29:30 +03:00
/* You can target it using one of its CSS classes */
.class1 { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or both classes! */
.class1.class2 { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or its name */
2013-11-10 22:37:34 +04:00
div { }
2014-05-01 21:02:04 +04:00
/* or its id */
2015-10-10 12:29:30 +03:00
#anID { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or using the fact that it has an attribute! */
2013-11-10 22:37:34 +04:00
[attr] { font-size:smaller; }
/* or that the attribute has a specific value */
[attr='value'] { font-size:smaller; }
2015-10-10 12:29:30 +03:00
/* starts with a value (CSS 3) */
2013-11-11 00:59:18 +04:00
[attr^='val'] { font-size:smaller; }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or ends with a value (CSS 3) */
2013-11-10 22:37:34 +04:00
[attr$='ue'] { font-size:smaller; }
2015-10-10 12:29:30 +03:00
/* or contains a value in a space-separated list */
[otherAttr~='foo'] { }
[otherAttr~='bar'] { }
2015-02-18 21:45:12 +03:00
2015-10-10 12:29:30 +03:00
/* or contains a value in a dash-separated list, ie, "-" (U+002D) */
2015-02-18 21:45:12 +03:00
[otherAttr|='en'] { font-size:smaller; }
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
/* You can combine different selectors to create a more focused selector. Don't
2015-10-10 12:29:30 +03:00
put spaces between them. */
2013-11-10 22:37:34 +04:00
div.some-class[attr$='ue'] { }
2015-10-10 12:29:30 +03:00
/* You can select an element which is a child of another element */
div.some-parent > .class-name { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or a descendant of another element. Children are the direct descendants of
their parent element, only one level down the tree. Descendants can be any
level down the tree. */
div.some-parent .class-name { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* Warning: the same selector without a space has another meaning.
Can you guess what? */
div.some-parent.class-name { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* You may also select an element based on its adjacent sibling */
.i-am-just-before + .this-element { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or any sibling preceding it */
.i-am-any-element-before ~ .this-element { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* There are some selectors called pseudo classes that can be used to select an
2015-10-26 02:15:23 +03:00
element only when it is in a particular state */
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* for example, when the cursor hovers over an element */
selector:hover { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or a link has been visited */
selector:visited { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* or hasn't been visited */
selected:link { }
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
/* or an element is in focus */
2015-10-10 12:29:30 +03:00
selected:focus { }
2013-11-10 22:37:34 +04:00
2015-10-10 15:20:03 +03:00
/* any element that is the first child of its parent */
selector:first-child {}
/* any element that is the last child of its parent */
selector:last-child {}
/* Just like pseudo classes, pseudo elements allow you to style certain parts of a document */
/* matches a virtual first child of the selected element */
selector::before {}
/* matches a virtual last child of the selected element */
selector::after {}
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* At appropriate places, an asterisk may be used as a wildcard to select every
element */
* { } /* all elements */
.parent * { } /* all descendants */
.parent > * { } /* all children */
2013-11-10 22:37:34 +04:00
2013-11-19 02:39:13 +04:00
/* ####################
## PROPERTIES
2014-09-21 11:22:26 +04:00
#################### */
2013-11-10 22:37:34 +04:00
selector {
2015-10-10 12:29:30 +03:00
/* Units of length can be absolute or relative. */
/* Relative units */
width: 50%; /* percentage of parent element width */
font-size: 2em; /* multiples of element's original font-size */
font-size: 2rem; /* or the root element's font-size */
font-size: 2vw; /* multiples of 1% of the viewport's width (CSS 3) */
font-size: 2vh; /* or its height */
font-size: 2vmin; /* whichever of a vh or a vw is smaller */
font-size: 2vmax; /* or greater */
/* Absolute units */
width: 200px; /* pixels */
font-size: 20pt; /* points */
width: 5cm; /* centimeters */
min-width: 50mm; /* millimeters */
max-width: 5in; /* inches */
2013-11-19 02:39:13 +04:00
/* Colors */
2015-10-10 12:29:30 +03:00
color: #F6E ; /* short hex format */
color: #FF66EE ; /* long hex format */
color: tomato; /* a named color */
color: rgb(255, 255, 255); /* as rgb values */
color: rgb(10%, 20%, 50%); /* as rgb percentages */
2015-10-26 02:15:23 +03:00
color: rgba(255, 0, 0, 0.3); /* as rgba values (CSS 3) Note: 0 < = a < = 1 */
2015-10-10 12:29:30 +03:00
color: transparent; /* equivalent to setting the alpha to 0 */
color: hsl(0, 100%, 50%); /* as hsl percentages (CSS 3) */
2015-10-26 02:15:23 +03:00
color: hsla(0, 100%, 50%, 0.3); /* as hsl percentages with alpha */
2015-10-10 12:29:30 +03:00
/* Images as backgrounds of elements */
background-image: url(/img-path/img.jpg); /* quotes inside url() optional */
2013-11-19 02:39:13 +04:00
/* Fonts */
font-family: Arial;
2015-10-10 12:29:30 +03:00
/* if the font family name has a space, it must be quoted */
font-family: "Courier New";
/* if the first one is not found, the browser uses the next, and so on */
font-family: "Courier New", Trebuchet, Arial, sans-serif;
2013-11-10 22:37:34 +04:00
}
```
## Usage
2015-10-10 12:29:30 +03:00
Save a CSS stylesheet with the extension `.css` .
2013-11-10 22:37:34 +04:00
2013-11-19 02:39:13 +04:00
```xml
2015-10-10 12:29:30 +03:00
<!-- You need to include the css file in your page's <head>. This is the
recommended method. Refer to http://stackoverflow.com/questions/8284365 -->
2014-09-21 11:22:26 +04:00
< link rel = 'stylesheet' type = 'text/css' href = 'path/to/style.css' / >
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
<!-- You can also include some CSS inline in your markup. -->
2013-11-10 22:37:34 +04:00
< style >
2014-09-21 11:22:26 +04:00
a { color: purple; }
2013-11-10 22:37:34 +04:00
< / style >
2015-10-10 12:29:30 +03:00
<!-- Or directly set CSS properties on the element. -->
2014-09-21 11:22:26 +04:00
< div style = "border: 1px solid red;" >
2013-11-10 22:37:34 +04:00
< / div >
```
2015-10-10 12:29:30 +03:00
## Precedence or Cascade
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
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. Rules with a more specific selector take precedence over a less specific one, and a rule occuring later in the stylesheet overwrites a previous one.
2015-10-10 12:29:30 +03:00
This process is called cascading, hence the name Cascading Style Sheets.
2013-11-10 22:37:34 +04:00
Given the following CSS:
2013-11-26 02:55:07 +04:00
2013-11-10 22:37:34 +04:00
```css
2015-10-10 12:29:30 +03:00
/* A */
2013-11-10 22:37:34 +04:00
p.class1[attr='value']
2015-10-10 12:29:30 +03:00
/* B */
p.class1 { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* C */
p.class2 { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* D */
p { }
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
/* E */
2013-11-10 22:37:34 +04:00
p { property: value !important; }
```
2013-11-26 02:55:07 +04:00
2013-11-19 02:39:13 +04:00
and the following markup:
2013-11-26 02:55:07 +04:00
2013-11-19 02:39:13 +04:00
```xml
2015-10-10 12:29:30 +03:00
< p style = '/*F*/ property:value;' class = 'class1 class2' attr = 'value' / >
2013-11-10 22:37:34 +04:00
```
2015-10-10 12:29:30 +03:00
The precedence of style is as follows. Remember, the precedence is for each **property** , not for the entire block.
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
* `E` has the highest precedence because of the keyword `!important` . It is recommended that you avoid its usage.
* `F` is next, because it is an 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'` .
* `C` is next, even though it has the same specificity as `B` . This is because it appears after `B` .
* `B` is next.
* `D` is the last one.
2013-11-10 22:37:34 +04:00
## Compatibility
2015-10-10 12:29:30 +03:00
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.
2013-11-10 22:37:34 +04:00
2015-10-10 12:29:30 +03:00
## Resources
2013-11-10 22:37:34 +04:00
2015-10-26 02:15:23 +03:00
* [CanIUse ](http://caniuse.com ) (Detailed compatibility info)
* [Dabblet ](http://dabblet.com/ ) (CSS playground)
* [Mozilla Developer Network's CSS documentation ](https://developer.mozilla.org/en-US/docs/Web/CSS ) (Tutorials and reference)
* [Codrops' CSS Reference ](http://tympanus.net/codrops/css_reference/ ) (Reference)
2014-09-21 11:22:26 +04:00
2013-11-10 22:37:34 +04:00
## Further Reading
* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade ](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/ )
2015-10-10 12:29:30 +03:00
* [Selecting elements using attributes ](https://css-tricks.com/almanac/selectors/a/attribute/ )
2013-11-10 22:37:34 +04:00
* [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 )
2015-10-10 12:29:30 +03:00
* [SASS ](http://sass-lang.com/ ) and [LESS ](http://lesscss.org/ ) for CSS pre-processing
* [CSS-Tricks ](https://css-tricks.com )