CSS box model
The CSS box model is essentially a box that wraps around every HTML element. |
Content
Padding
Border
Margin
CSS length units
cm - centimeters
em - elements (i.e., relative to the font-size of the element; e.g., 2 em means 2 times the current font size)
in - inches
mm - millimeters
pc - picas (1 pc = 12 pt = 1/6th of an inch)
pt - points (1 pt = 1/72nd of an inch)
px - pixels (1 px = 1/96th of an inch) |
CSS position Property
The position property specifies the type of positioning method used for an element (static, relative, fixed or absolute)
position: static; - HTML elements are positioned static by default. Static positioned elements are NOT affected by the top, bottom, left, and right properties. An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:
position: relative; - An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
position: fixed; - An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
position: absolute; - An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling. A fixed element does not leave a gap in the page where it would normally have been located. |
CSS3 Features
Media Queries
Box Sizing
Webfonts @font-face
Animations and Transitions
Gradients |
|
|
3 main ways to apply CSS styles
Inline - HTML elements may have CSS applied to them via the STYLE attribute.
Embedded - By placing the code in a STYLE element within the HEAD element.
Linked/ Imported - Place the CSS in an external file and link it via a link element. |
CSS Display property
The display CSS property specifies the type of rendering box used for an element.
A block element is an element that takes up the full width available, and has a line break before and after it. <h1>, <p>, <li>, and <div> are all examples of block elements.
An inline element only takes up as much width as necessary, cannot accept width and height values, and does not force line breaks. <a> and <span> are examples of inline elements.
display: inline;
display: block;
display: inline-block;
display: list-item;
display: table; |
CSS Cascade
There are three main concepts that control the order in which CSS declarations are applied:
1. Importance - !important keyword
2. Specificity
- Inline styles
- IDs
- Classes attributes and pseudo-classes
- Elements and pseudo-elements
3. Source order
All other things being equal, the styles that are defined latest, i.e. written nearest to the actual HTML elements and read by the browser last, will over-rule earlier definitions. |
|
|
CSS Selectors
Type selector - selects which elements in the DOM the rule applies to. - eg h1, p
In addition to tag names, you can use attribute values in selectors. This allows your rules to be more specific.
Class selectors - Multiple elements in a document can have the same class value. - eg .class
ID selectors - The ID name must be unique in the document. - eg #id
Attribute Selectors - You can specify other attributes by using square brackets. Inside the brackets you put the attribute name, optionally followed by a matching operator and a value. eg [type='button']
Pseudo-classes selectors - A CSS pseudo-class is a keyword added to selectors that specifies a special state of the element to be selected. For example :hover will apply a style when the user hovers over the element specified by the selector.
:link
:visited
:active
:hover
:focus
:first-child
:last-child
:nth-child
:nth-last-child
:nth-of-type
:first-of-type
:last-of-type
:empty
:target
:checked
:enabled
:disabled
Pseudo-elements - Added to selectors but instead of describing a special state, they allow you to style certain parts of a document. For example, the ::first-line pseudo-element targets only the first line of an element specified by the selector. Double colon notation
::after
::before
::first-letter
::first-line
::selection
::backdrop
::placeholder
::marker
::spelling-error
::grammar-error |
|