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"]
2014-01-30 11:30:28 +04:00
filename: learncss.css
2013-11-10 22:37:34 +04:00
---
2015-10-01 23:17:58 +03:00
In the early days of the web there were no visual elements, just pure text. But with the
further development of browsers, fully visual web pages also became common.
2013-11-11 01:09:31 +04:00
CSS is the standard language that exists to keep the separation between
the content (HTML) and the look-and-feel of web pages.
2013-11-10 22:37:34 +04:00
2013-11-11 01:09:31 +04:00
In short, what CSS does is to provide a syntax that enables you to target
different elements on an HTML page and assign different visual properties to them.
2013-11-10 22:37:34 +04:00
2015-10-01 23:17:58 +03:00
Like any other languages, CSS has many versions. Here we focus on CSS2.0,
which is not the most recent version, but is the most widely supported and compatible version.
2013-11-10 22:37:34 +04:00
2015-10-01 23:17:58 +03:00
**NOTE:** Because the outcome of CSS consists of visual effects, in order to
learn it, you need try everything in a
2013-11-19 02:39:13 +04:00
CSS playground like [dabblet ](http://dabblet.com/ ).
The main focus of this article is on the syntax and some general tips.
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
/* Generally, the primary statement in CSS is very simple */
selector { property: value; /* more properties...*/ }
/* the selector is used to target an element on page.
2015-02-16 12:44:33 +03:00
You can target all elements on the page using asterisk! */
2013-11-10 22:37:34 +04:00
* { color:red; }
/*
Given an element like this on the page:
2015-02-18 21:45:12 +03:00
< div class = 'some-class class2' id = 'someId' attr = 'value' otherAttr = 'en-us foo bar' / >
2013-11-10 22:37:34 +04:00
*/
2014-05-01 21:02:04 +04:00
/* you can target it by its name */
2013-11-10 22:37:34 +04:00
.some-class { }
2015-02-16 12:44:33 +03:00
/* or by both classes! */
2013-11-10 22:37:34 +04:00
.some-class.class2 { }
2014-05-01 21:02:04 +04:00
/* or by its element name */
2013-11-10 22:37:34 +04:00
div { }
2014-05-01 21:02:04 +04:00
/* or its id */
2013-11-10 22:37:34 +04:00
#someId { }
/* or by the fact that it has an attribute! */
[attr] { font-size:smaller; }
/* or that the attribute has a specific value */
[attr='value'] { font-size:smaller; }
2014-09-21 11:22:26 +04:00
/* start with a value (CSS3) */
2013-11-11 00:59:18 +04:00
[attr^='val'] { font-size:smaller; }
2013-11-10 22:37:34 +04:00
2014-09-21 11:22:26 +04:00
/* or ends with (CSS3) */
2013-11-10 22:37:34 +04:00
[attr$='ue'] { font-size:smaller; }
2015-02-18 21:45:12 +03:00
/* or select by one of the values from the whitespace separated list (CSS3) */
[otherAttr~='foo'] { font-size:smaller; }
/* or value can be exactly “value” or can begin with “value” immediately followed by “-” (U+002D) */
[otherAttr|='en'] { font-size:smaller; }
2013-11-10 22:37:34 +04:00
2013-11-11 01:09:31 +04:00
/* and more importantly you can combine these together -- there shouldn't be
2014-09-21 11:22:26 +04:00
any space between different parts because that makes it to have another
meaning. */
2013-11-10 22:37:34 +04:00
div.some-class[attr$='ue'] { }
2014-09-21 11:22:26 +04:00
/* you can also select an element based on its parent. */
2013-11-10 22:37:34 +04:00
2014-09-21 11:22:26 +04:00
/* an element which is direct child of an element (selected the same way) */
2013-11-10 22:37:34 +04:00
div.some-parent > .class-name {}
2014-09-21 11:22:26 +04:00
/* or any of its parents in the tree
the following basically means any element that has class "class-name"
and is child of a div with class name "some-parent" IN ANY DEPTH */
2013-11-10 22:37:34 +04:00
div.some-parent .class-name {}
2015-02-16 12:44:33 +03:00
/* warning: the same selector without space has another meaning.
2014-09-21 11:22:26 +04:00
can you say what? */
2013-11-10 22:37:34 +04:00
div.some-parent.class-name {}
2014-05-01 21:02:04 +04:00
/* you also might choose to select an element based on its direct
2014-09-21 11:22:26 +04:00
previous sibling */
2013-11-10 22:37:34 +04:00
.i-am-before + .this-element { }
2014-09-21 11:22:26 +04:00
/* or any sibling before this */
2013-11-10 22:37:34 +04:00
.i-am-any-before ~ .this-element {}
2013-11-11 01:09:31 +04:00
/* There are some pseudo classes that allows you to select an element
2014-09-21 11:22:26 +04:00
based on its page behaviour (rather than page structure) */
2013-11-10 22:37:34 +04:00
/* for example for when an element is hovered */
2014-09-21 11:22:26 +04:00
selector:hover {}
2013-11-10 22:37:34 +04:00
2014-09-21 11:22:26 +04:00
/* or a visited link */
selected:visited {}
2013-11-10 22:37:34 +04:00
2014-09-21 11:22:26 +04:00
/* or not visited link */
selected:link {}
2013-11-10 22:37:34 +04:00
/* or an input element which is focused */
2014-09-21 11:22:26 +04:00
selected:focus {}
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 {
2013-11-19 02:39:13 +04:00
/* Units */
width: 50%; /* in percent */
font-size: 2em; /* times current font-size */
width: 200px; /* in pixels */
font-size: 20pt; /* in points */
width: 5cm; /* in centimeters */
2014-09-21 11:22:26 +04:00
min-width: 50mm; /* in millimeters */
max-width: 5in; /* in inches. max-(width|height) */
height: 0.2vh; /* times vertical height of browser viewport (CSS3) */
width: 0.4vw; /* times horizontal width of browser viewport (CSS3) */
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) */
2013-11-19 02:39:13 +04:00
/* Colors */
2014-02-27 01:22:39 +04:00
background-color: #F6E ; /* in short hex */
background-color: #F262E2 ; /* in long hex format */
background-color: tomato; /* can be a named color */
background-color: rgb(255, 255, 255); /* in rgb */
background-color: rgb(10%, 20%, 50%); /* in rgb percent */
2014-09-21 11:22:26 +04:00
background-color: rgba(255, 0, 0, 0.3); /* in semi-transparent rgb (CSS3) */
background-color: transparent; /* see thru */
background-color: hsl(0, 100%, 50%); /* hsl format (CSS3). */
background-color: hsla(0, 100%, 50%, 0.3); /* Similar to RGBA, specify opacity at end (CSS3) */
2013-11-19 02:39:13 +04:00
/* Images */
2014-09-21 11:22:26 +04:00
background-image: url(/path-to-image/image.jpg); /* quotes inside url() optional */
2013-11-19 02:39:13 +04:00
/* Fonts */
font-family: Arial;
2015-02-16 12:44:33 +03:00
font-family: "Courier New"; /* if name has space it appears in single or double quotes */
2014-09-21 11:22:26 +04:00
font-family: "Courier New", Trebuchet, Arial, sans-serif; /* if first one was not found
browser uses the second font, and so forth */
2013-11-10 22:37:34 +04:00
}
```
## Usage
Save any CSS you want in a file with extension `.css` .
2013-11-19 02:39:13 +04:00
```xml
2013-11-10 22:37:34 +04:00
<!-- you need to include the css file in your page's <head>: -->
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
2013-11-11 01:09:31 +04:00
<!-- you can also include some CSS inline in your markup. However it is highly
recommended to avoid this. -->
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 >
2013-11-11 01:09:31 +04:00
<!-- or directly set CSS properties on the element.
This has to be avoided as much as you can. -->
2014-09-21 11:22:26 +04:00
< div style = "border: 1px solid red;" >
2013-11-10 22:37:34 +04:00
< / div >
```
## Precedence
2013-11-11 01:09:31 +04:00
As you noticed an element may be targetted by more than one selector.
and may have a property set on it in more than one.
In these cases, one of the rules takes precedence over others.
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
/*A*/
p.class1[attr='value']
/*B*/
p.class1 {}
/*C*/
p.class2 {}
/*D*/
p {}
/*E*/
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
2013-11-10 22:37:34 +04:00
< p style = '/*F*/ property:value;' class = 'class1 class2' attr = 'value' >
< / p >
```
The precedence of style is as followed:
2013-11-19 02:39:13 +04:00
Remember, the precedence is for each **property** , not for the entire block.
2013-11-10 22:37:34 +04:00
2013-11-11 01:09:31 +04:00
* `E` has the highest precedence because of the keyword `!important` .
2014-09-21 11:22:26 +04:00
It is recommended to avoid this unless it is strictly necessary to use.
2013-11-19 02:39:13 +04:00
* `F` is next, because it is inline style.
* `A` is next, because it is more "specific" than anything else.
2014-09-21 11:22:26 +04:00
more specific = more specifiers. here 3 specifiers: 1 tagname `p` +
class name `class1` + 1 attribute `attr='value'`
2013-11-19 02:39:13 +04:00
* `C` is next. although it has the same specificness as `B`
2014-09-21 11:22:26 +04:00
but it appears last.
2013-11-10 22:37:34 +04:00
* Then is `B`
* and lastly is `D` .
## Compatibility
2013-11-11 01:09:31 +04:00
Most of the features in CSS2 (and gradually in CSS3) are compatible across
2015-02-16 12:44:33 +03:00
all browsers and devices. But it's always vital to have in mind the compatibility
2013-11-11 01:09:31 +04:00
of what you use in CSS with your target browsers.
2013-11-10 22:37:34 +04:00
[QuirksMode CSS ](http://www.quirksmode.org/css/ ) is one of the best sources for this.
2014-09-21 11:22:26 +04:00
To run a quick compatibility check, [CanIUse ](http://caniuse.com ) is a great resource.
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/ )
* [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 )