× Boo Learning HTML CSS Cheatsheet


Learn to write CSS

☰ Menu
Navigation
Jump to Syntax and Select
Jump to Backgrounds and Colors
Jump to Position Property
Jump to Border Property
Jump to Other Resources

Introducting CSS

What is CSS?

1. CSS stands for Cascading Style Sheets
2. CSS describes how HTML elements are to be displayed on screen, paper, or in other media
3. CSS saves a lot of work. It can control the layout of multiple web pages all at once
4. External stylesheets are stored in CSS files

Simple CSS Code

1. Syntax and Select

A CSS rule consists of a selector and a declaration block.
In this example all <p> elements will be center-aligned, with a red text color:

p {
text-align: center;
color: red;
}

If you have a selector in your .html file, you can use these formats to find the specific class

p.center {
text-align: center;
color: red;
}
or
.center {
text-align: center;
color: red;
}

If you want to add .css to .html, put this line in the <head> of your .html

<link rel="stylesheet" type="text/css" href="mystyle.css">

2. Backgrounds and Colors

We can try the set a body Background color by using this command.
body { background-color: lightblue; }

You can set color by entering color number
body { background-color: #ff0000; }

Or an RGB value
body { background-color: rgb(255, 0, 0); }

Or an HSLA vaule
body { background-color: hsla(0, 100%, 50%); }

Tip: You can add styles to all the other variables including <h1>, <div>, and <span>.

3. The Position Property

The position property specifies the type of positioning method used for an element.
There are five different position values:

• static
• relative
• fixed
• absolute
• sticky

Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the position value.

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.

div.static {
position: static;
border: 3px solid #73AD21;
}

position: relative;

An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relative 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.

div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}

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.

div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}

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.

div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
border: 3px solid #73AD21;
}

position: sticky;

An element with position: sticky; is positioned based on the user's scroll position.
A sticky element toggles between relative and fixed, depending on the scroll position.
It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
padding: 5px;
border: 3px solid #73AD21;
background-color: #eee;
font-size: 20px;
}

4. Border Property

dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
none - Defines no border
hidden - Defines a hidden border

There is an example of them being used:

p.dotted { border-style: dotted; }
p.dashed { border-style: dashed; }
p.solid { border-style: solid; }
p.double { border-style: double; }
p.groove { border-style: groove; }
p.ridge { border-style: ridge; }
p.inset { border-style: inset; }
p.outset { border-style: outset; }
p.none { border-style: none; }
p.hidden { border-style: hidden; }

Here is the result:

dotted

dashed

solid

double

groove

ridge

inset

outset

none

5. Other Helpful Links

Tutorialspoints for css
w3schools
csstutorialnet