CSS Selectors
Selector |
Target |
Example |
Element Selector |
Select a HTML element. |
|
Id Selector |
Select HTML element by ID |
|
Class Selector |
Select HTML element by Class |
|
Element + Class Selector |
Select elements marked with a specific Class |
|
Universal Selector |
Select every element in a HTML file |
|
Grouping Selector |
Select multiple elements at once |
h1,p,tr { CSS Code Here }
|
Adding CSS Into HTML
Option |
Example Code |
Use Case |
External CSS |
<link rel="stylesheet" href="mystyle.css">
|
Link to an external CSS file |
Internal CSS |
<style>{ CSS Code Here}</style>
|
Add CSS to the <head>
section of your HTML file |
Inline CSS |
<p style="color:red;">This is a paragraph.</p>
|
Add CSS directly into a HTML element |
Note that the order of preference for selecting which CSS to run is: Inline Style has highest priority, then External and Internal Styles, and finally browser default styles.
CSS Borders
Property |
Example Values |
Effects |
|
dotted
, dashed
, solid
, double
, groove
, ridge
, inset
, outset
, none
, hidden
|
Kind of border to display. Values can be combined |
|
medium
, thick
, 2px
, 2in
... |
Specify width of the four borders |
|
RGB, names, HSL, HEX... |
Color of the borders |
border-{side}-{property}
|
|
Specify a border-property for a specific side |
|
|
How much the border corners should be rounded |
|
20px, 30px
(horizontal then vertical space) ... |
Set space between cells of table |
|
|
Whether borders of table cells should be joint or not |
Note that none of the other border properties will work without specifying a border-style
first.
If border-color
is not set, it inherits the color of the parent.
To set border-{property} for specific sides do: border-{property}: {top} {right} {bottom} {left}
or border-{property}: {top&bottom} {right&left}
or border-{property}: {top} {right&left} {bottom}
Border shorthand is border: {width} {style} {color}
.
border-collapse
can be used with border-spacing
to style tables.
CSS Margins
Property |
Use Case |
margin-{individual-side}
|
Which border sides do you want to add margin to |
Margin is the space around the border of an element. Available sides are top, bottom, left, right.
Margin values should be in px. margin: auto
means horizontally center the element within its container, and margin: inherit
means inherit margin from parent element.
Shorthand margin
applies margin values to all four sides of a border.
When two margins border each other, they collapse into one, the largest one. (Only for top and bottom margins)
CSS Padding
Property |
Use Case |
padding-{individual-side}
|
Which border side do you want to add padding to |
Padding is the distance between an element and its border. Values can be in length e.g 2px, % (percentage of the width of the containing element), or inherit (from parent element).
You can use the shorthand padding
to set padding on all sides.
If an element has a specified width, adding padding will add to the initial width.
|
|
CSS Color Options
Option |
? |
Example |
Color names |
Use the exact color name |
red, green, blue etc. |
RGB (Red Green Blue) |
Use RGB values to produce a color |
rgb(255, 255, 255)
produces white |
RGBA (Red Green Blue Aplha) |
Use RGB with an opacity (alpha) |
rgba(123, 45, 67 0.4)
produces a color that 40%
transparent |
HEX |
Use hexadecimal values to produce colors |
|
HSL (Hue Saturation Light) |
Use HSL to produce colors |
hsl(120, 50%, 100%)
produces a 50% green color |
HSLA(Hue Saturation Light Alpha) |
Use HSL with opacity (alpha) |
hsla(120, 50%, 100%, 0.4)
produces a 50% green color that's 40% opaque |
CSS Width, Max-Width, and Height
Property |
Example Values |
Use Case |
|
auto
, length
, initial
, inherit
, 2%
, |
Set the height of an element |
|
auto
, length
, initial
, inherit
, 2%
, |
Set the width of an element |
|
|
Set the maximum width of an element |
auto means the computer will calculate the heights and widths for you. Initial means default value. Inherit means get values from parent element.
For width
, use the %
so that it can be scaled as a percentage of the parent width. max-width allows the browser to better handle horizontal scrollbars when the available view port width becomes smaller than, say, the size of a div
.
When using max-width
, don't give an element a width
. The latter overrides the former.
CSS Box Model
Every HTML element has layers around it, which we call a box. Content is what's in the layers e.g text. Padding is transparent area around the content. Border is area enclosing content and padding. Margin is transparent area around the border.
Setting the width
and height
of an element with CSS, just sets the width and height of the content area. To calculate the full size of an element, you must also add padding
, borders
and margins
.
CSS Backgrounds
Property |
Example Values |
Effects |
|
red
, rgba(0, 123, 45, 0.6)
, hsl(65, 34%, 100%) |
Color for the background |
|
0.0
(0% opaque), 1.0
(100% opaque) |
Opacity for the background color |
|
url("{ link to image }") |
Background image for your page |
|
repeat-x
(horizontally), repeat-y
(vertically), no-repeat
|
Image should be repeated/ no image repetition |
|
top
, bottom
, left
, right
, center bottom 10px
(offset from position) |
Position of the background image |
background-attachment
|
|
Image should scroll with the page or not |
|
100%
(100% of available screen width) |
Size of the background image |
|
border-box
, padding-box
, content-box
|
How far the background (image or color) should extend within an element |
|
border-box
, padding-box
, content-box
|
Origin position of a background image |
NOTE that the background-{property} properties can be combined into one shorthand like this:
background:
{-color}
{-image}
{-repeat}
{-attachment}
{-position} ;
For background-clip
, border-box makes background to extend beyond the border, padding-box, background extends to the inside edge of the border, content-box, background extends to the edge of the content box e.g around a paragraph. With background-origin
, it's the same dimensions.
|