codegurukool.com · Free coding tutorials & online compiler

Home › CSS Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

color

Definition: Sets the text color.

Syntax:

color: value;

Example:

color: crimson;

padding

Definition: Adds space inside an element, between content and border.

Syntax:

padding: size;

Example:

padding: 12px;
background: skyblue;

margin

Definition: Adds space outside an element.

Syntax:

margin: size;

Example:

margin: 24px;
background: skyblue;

text-align

Definition: Aligns text horizontally.

Syntax:

text-align: left | center | right;

Example:

text-align: center;

display

Definition: Sets how an element is laid out: block, inline, flex, grid or none.

Syntax:

display: value;

Example:

display: flex;
justify-content: center;

calc

Definition: Calculates a value, mixing different units.

Syntax:

width: calc(a + b);

Example:

width: calc(100% - 40px);
background: skyblue;

p

Definition: A rule is selector { property: value; } .

Syntax:

p

Example:

p { color: teal; }
<p>Teal paragraph</p>
<p class="big">Big paragraph</p>

↗ CSS selectors

#top

Definition: You can select by tag ( p ), class ( .card ) or id ( #top ).

Syntax:

#top

Example:

#top { background: gold; }

↗ CSS selectors

hsl()

Definition: Colors can be names, hex ( #ff0 ), rgb() or hsl() .

Syntax:

hsl()

Example:

h1 { color: hsl(210, 80%, 45%); letter-spacing: 1px; }

↗ Colors and text

line-height

Definition: Set line-height around 1.5 for readable text.

Syntax:

line-height

Example:

body { font-family: Georgia, serif; line-height: 1.6; }

↗ Colors and text

width

Definition: box-sizing: border-box makes width include padding and border, which is easier to reason about.

Syntax:

width

Example:

div { box-sizing: border-box; width: 240px; padding: 20px;

↗ The box model

justify-content

Definition: Then use justify-content (main axis), align-items (cross axis) and gap .

Syntax:

justify-content

Example:

.row { display: flex; justify-content: space-between;

↗ Flexbox

align-items

Definition: Then use justify-content (main axis), align-items (cross axis) and gap .

Syntax:

align-items

Example:

align-items: center; gap: 10px; height: 120px; background: #eee; }

↗ Flexbox

gap

Definition: Then use justify-content (main axis), align-items (cross axis) and gap .

Syntax:

gap

Example:

align-items: center; gap: 10px; height: 120px; background: #eee; }

↗ Flexbox

background

Definition: Use background for colors and gradients, border-radius for curves and box-shadow for depth.

Syntax:

background

Example:

background:linear-gradient(135deg,#2e86de,#8e44ad);

↗ Backgrounds and borders

border-radius

Definition: Use background for colors and gradients, border-radius for curves and box-shadow for depth.

Syntax:

border-radius

Example:

.c{width:220px;padding:24px;color:#fff;border-radius:14px;

↗ Backgrounds and borders

box-shadow

Definition: Use background for colors and gradients, border-radius for curves and box-shadow for depth.

Syntax:

box-shadow

Example:

box-shadow:0 6px 16px rgba(0,0,0,.3);border:3px dashed #fff}

↗ Backgrounds and borders

relative

Definition: relative offsets from the normal spot and creates a reference.

Syntax:

relative

Example:

.box{position:relative;height:140px;background:#eee}

↗ Position

absolute

Definition: absolute positions inside the nearest positioned parent.

Syntax:

absolute

Example:

.tag{position:absolute;top:10px;right:10px;background:crimson;color:#fff;padding:4px 8px}

↗ Position

grid-template-columns

Definition: Use grid-template-columns , gap and grid-column to span cells.

Syntax:

grid-template-columns

Example:

.g{display:grid;grid-template-columns:1fr 2fr 1fr;gap:8px}

↗ CSS Grid

grid-column

Definition: Use grid-template-columns , gap and grid-column to span cells.

Syntax:

grid-column

Example:

.wide{grid-column:1 / 4;background:#14213d!important}

↗ CSS Grid

transition

Definition: transition animates changes between states such as hover.

Syntax:

transition

Example:

.b{display:inline-block;padding:12px 20px;background:#2e86de;color:#fff;transition:transform .2s}

↗ Transitions and animations

link

Definition: External files linked with link are best.

Syntax:

link

Example:

<!-- <link rel='stylesheet' href='style.css'> -->

↗ CSS how to

style

Definition: Internal uses style in the head.

Syntax:

style

Example:

<style>p{color:teal}</style>
<p style='color:purple'>Inline</p>
<!-- <link rel='stylesheet' href='style.css'> -->

↗ CSS how to

border

Definition: Set border-width , border-style and border-color , or the shorthand border .

Syntax:

border

Example:

.a{border:3px solid navy;padding:8px}

↗ CSS borders

border-left

Definition: Style one side with border-left etc.

Syntax:

border-left

Example:

.b{border-left:8px double tomato;padding:8px;margin-top:10px}

↗ CSS borders

height

Definition: Use width , height , min- and max- variants.

Syntax:

height

Example:

div{width:50%;height:80px;background:#1b998b;color:#fff;min-width:150px}

↗ Height and width

text-decoration

Definition: Key properties: text-align , text-decoration , text-transform , letter-spacing , line-height , text-indent .

Syntax:

text-decoration

Example:

p{text-indent:30px;line-height:1.8;text-decoration:underline wavy teal}

↗ CSS text

text-transform

Definition: Key properties: text-align , text-decoration , text-transform , letter-spacing , line-height , text-indent .

Syntax:

text-transform

Example:

h2{text-align:center;text-transform:uppercase;letter-spacing:3px}

↗ CSS text

letter-spacing

Definition: Key properties: text-align , text-decoration , text-transform , letter-spacing , line-height , text-indent .

Syntax:

letter-spacing

Example:

h2{text-align:center;text-transform:uppercase;letter-spacing:3px}

↗ CSS text

text-indent

Definition: Key properties: text-align , text-decoration , text-transform , letter-spacing , line-height , text-indent .

Syntax:

text-indent

Example:

p{text-indent:30px;line-height:1.8;text-decoration:underline wavy teal}

↗ CSS text

font-family

Definition: List fallbacks in font-family .

Syntax:

font-family

Example:

.s{font-family:Arial,sans-serif;font-weight:300}

↗ CSS fonts

font-weight

Definition: Use font-size , font-weight , font-style and the font shorthand.

Syntax:

font-weight

Example:

.s{font-family:Arial,sans-serif;font-weight:300}

↗ CSS fonts

font

Definition: List fallbacks in font-family .

Syntax:

font

Example:

p{font:italic 700 22px Georgia,serif}

↗ CSS fonts

a:link

Definition: Order matters: a:link , a:visited , a:hover , a:active .

Syntax:

a:link

Example:

a:link{color:#2e86de}a:visited{color:purple}

↗ CSS links

a:visited

Definition: Order matters: a:link , a:visited , a:hover , a:active .

Syntax:

a:visited

Example:

a:link{color:#2e86de}a:visited{color:purple}

↗ CSS links

a:hover

Definition: Order matters: a:link , a:visited , a:hover , a:active .

Syntax:

a:hover

Example:

a:hover{color:tomato;text-decoration:none}a:active{color:black}

↗ CSS links

a:active

Definition: Order matters: a:link , a:visited , a:hover , a:active .

Syntax:

a:active

Example:

a:hover{color:tomato;text-decoration:none}a:active{color:black}

↗ CSS links

list-style-type

Definition: list-style-type picks the marker, list-style-position places it, and list-style:none removes it.

Syntax:

list-style-type

Example:

ul{list-style-type:square}ol{list-style-type:upper-roman}

↗ CSS lists

list-style:none

Definition: list-style-type picks the marker, list-style-position places it, and list-style:none removes it.

Syntax:

list-style:none

Example:

.n{list-style:none;padding:0}

↗ CSS lists

border-collapse

Definition: Use border-collapse , padding, and :nth-child(even) for striped rows.

Syntax:

border-collapse

Example:

table{border-collapse:collapse;width:100%}

↗ CSS tables

none

Definition: none removes it; visibility:hidden hides it but keeps its space.

Syntax:

none

Example:

p.h{display:none}

↗ CSS display

max-width

Definition: max-width lets an element shrink on small screens yet never exceed a limit, which beats a fixed width.

Syntax:

max-width

Example:

<style>div{max-width:300px;background:#fde;padding:10px;margin:auto}</style>

↗ CSS max-width

z-index

Definition: Higher z-index sits on top.

Syntax:

z-index

Example:

.a{background:tomato;z-index:2;left:20px;top:20px}
.b{background:#2e86de;z-index:1;left:60px;top:50px}

↗ CSS z-index

auto

Definition: Values: visible , hidden , scroll , auto .

Syntax:

auto

Example:

<style>div{width:180px;height:70px;overflow:auto;border:2px solid #888}</style>

↗ CSS overflow

inline-block

Definition: An inline-block sits in a line yet accepts width, height and vertical margins.

Syntax:

inline-block

Example:

<style>.k{display:inline-block;width:80px;height:50px;background:#1b998b;margin:4px;color:#fff}</style>

↗ CSS inline-block

place-items:center

Definition: Vertical and both: flexbox align-items and justify-content , or place-items:center in grid.

Syntax:

place-items:center

Example:

<style>.c{display:grid;place-items:center;height:140px;background:#eee}</style>

↗ CSS align

opacity

Definition: opacity goes from 0 to 1 and fades the element and its children.

Syntax:

opacity

Example:

<div class='o' style='opacity:.4'>opacity .4</div>

↗ CSS opacity

rgba()

Definition: rgba() fades only a background.

Syntax:

rgba()

Example:

<div class='o' style='background:rgba(228,87,46,.4)'>rgba background</div>

↗ CSS opacity

box-sizing:border-box

Definition: Set box-sizing:border-box so widths behave.

Syntax:

box-sizing:border-box

Example:

input,textarea{width:100%;padding:10px;border:2px solid #aaa;border-radius:6px;box-sizing:border-box;margin:4px 0}

↗ CSS forms

counter-reset

Definition: Create with counter-reset , increase with counter-increment , and show with counter() in content .

Syntax:

counter-reset

Example:

body{counter-reset:s}h3::before{counter-increment:s;content:'Section ' counter(s) ': '}

↗ CSS counters

counter-increment

Definition: Create with counter-reset , increase with counter-increment , and show with counter() in content .

Syntax:

counter-increment

Example:

body{counter-reset:s}h3::before{counter-increment:s;content:'Section ' counter(s) ': '}

↗ CSS counters

counter()

Definition: Create with counter-reset , increase with counter-increment , and show with counter() in content .

Syntax:

counter()

Example:

body{counter-reset:s}h3::before{counter-increment:s;content:'Section ' counter(s) ': '}

↗ CSS counters

content

Definition: Create with counter-reset , increase with counter-increment , and show with counter() in content .

Syntax:

content

Example:

body{counter-reset:s}h3::before{counter-increment:s;content:'Section ' counter(s) ': '}

↗ CSS counters

inherit

Definition: Text properties such as color and font-family inherit.

Syntax:

inherit

Example:

section p{border:inherit}

↗ CSS inheritance

clamp(min, ideal, max)

Definition: min() , max() and clamp(min, ideal, max) make fluid sizes.

Syntax:

clamp(min, ideal, max)

Example:

h2{font-size:clamp(1rem,4vw,2.4rem)}

↗ CSS math functions

linear-gradient

Definition: linear-gradient , radial-gradient and conic-gradient create images from colors.

Syntax:

linear-gradient

Example:

<div style='background:linear-gradient(90deg,#e4572e,#ffc857)'>Linear</div>

↗ CSS gradients

radial-gradient

Definition: linear-gradient , radial-gradient and conic-gradient create images from colors.

Syntax:

radial-gradient

Example:

<div style='background:radial-gradient(circle,#2e86de,#14213d)'>Radial</div>

↗ CSS gradients

conic-gradient

Definition: linear-gradient , radial-gradient and conic-gradient create images from colors.

Syntax:

conic-gradient

Example:

<div style='background:conic-gradient(#e4572e,#ffc857,#1b998b,#e4572e)'>Conic</div>

↗ CSS gradients

text-overflow:ellipsis

Definition: text-overflow:ellipsis needs overflow:hidden and white-space:nowrap .

Syntax:

text-overflow:ellipsis

Example:

<style>.e{width:150px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;border:1px solid #888}</style>

↗ CSS text effects

overflow:hidden

Definition: text-overflow:ellipsis needs overflow:hidden and white-space:nowrap .

Syntax:

overflow:hidden

Example:

<style>.e{width:150px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;border:1px solid #888}</style>

↗ CSS text effects

white-space:nowrap

Definition: text-overflow:ellipsis needs overflow:hidden and white-space:nowrap .

Syntax:

white-space:nowrap

Example:

<style>.e{width:150px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;border:1px solid #888}</style>

↗ CSS text effects

rotate()

Definition: translate() , rotate() , scale() and skew() change how an element is drawn without moving others.

Syntax:

rotate()

Example:

<div style='transform:rotate(20deg)'>Rotate</div><div style='transform:scale(1.4)'>Scale</div><div style='transform:skew(20deg)'>Skew</div>

↗ CSS 2D transforms

scale()

Definition: translate() , rotate() , scale() and skew() change how an element is drawn without moving others.

Syntax:

scale()

Example:

<div style='transform:rotate(20deg)'>Rotate</div><div style='transform:scale(1.4)'>Scale</div><div style='transform:skew(20deg)'>Skew</div>

↗ CSS 2D transforms

skew()

Definition: translate() , rotate() , scale() and skew() change how an element is drawn without moving others.

Syntax:

skew()

Example:

<div style='transform:rotate(20deg)'>Rotate</div><div style='transform:scale(1.4)'>Scale</div><div style='transform:skew(20deg)'>Skew</div>

↗ CSS 2D transforms

perspective

Definition: Set perspective on the parent, then use rotateX , rotateY or translateZ on the child.

Syntax:

perspective

Example:

.s{perspective:400px;padding:30px}

↗ CSS 3D transforms

rotateY

Definition: Set perspective on the parent, then use rotateX , rotateY or translateZ on the child.

Syntax:

rotateY

Example:

.c{width:120px;height:80px;background:#e4572e;color:#fff;transform:rotateY(45deg);transition:transform .6s}
.c:hover{transform:rotateY(0)}

↗ CSS 3D transforms

filter

Definition: filter accepts blur() , grayscale() , brightness() , contrast() , hue-rotate() and drop-shadow() .

Syntax:

filter

Example:

<div></div><div style='filter:grayscale(1)'></div><div style='filter:blur(3px)'></div><div style='filter:hue-rotate(120deg)'></div>

↗ CSS image filters

blur()

Definition: filter accepts blur() , grayscale() , brightness() , contrast() , hue-rotate() and drop-shadow() .

Syntax:

blur()

Example:

<div></div><div style='filter:grayscale(1)'></div><div style='filter:blur(3px)'></div><div style='filter:hue-rotate(120deg)'></div>

↗ CSS image filters

grayscale()

Definition: filter accepts blur() , grayscale() , brightness() , contrast() , hue-rotate() and drop-shadow() .

Syntax:

grayscale()

Example:

<div></div><div style='filter:grayscale(1)'></div><div style='filter:blur(3px)'></div><div style='filter:hue-rotate(120deg)'></div>

↗ CSS image filters

hue-rotate()

Definition: filter accepts blur() , grayscale() , brightness() , contrast() , hue-rotate() and drop-shadow() .

Syntax:

hue-rotate()

Example:

<div></div><div style='filter:grayscale(1)'></div><div style='filter:blur(3px)'></div><div style='filter:hue-rotate(120deg)'></div>

↗ CSS image filters

object-fit

Definition: object-fit ( cover , contain , fill ) controls cropping.

Syntax:

object-fit

Example:

<style>img{width:160px;height:90px;object-fit:cover;object-position:left;border:1px solid #888}</style>

↗ object-fit and position

cover

Definition: object-fit ( cover , contain , fill ) controls cropping.

Syntax:

cover

Example:

<style>img{width:160px;height:90px;object-fit:cover;object-position:left;border:1px solid #888}</style>

↗ object-fit and position

fill

Definition: object-fit ( cover , contain , fill ) controls cropping.

Syntax:

fill

Example:

<img alt='Demo' src='data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 width=%27300%27 height=%27300%27%3E%3Crect width=%27150%27 height=%27300%27 fill=%27%23e4572e%27/%3E%3Crect x=%27150%27 width=%27150%27 height=%27300%27 fill=%27%232e86de%27/%3E%3C/svg%3E'>

↗ object-fit and position

object-position

Definition: object-position chooses which part stays visible.

Syntax:

object-position

Example:

<style>img{width:160px;height:90px;object-fit:cover;object-position:left;border:1px solid #888}</style>

↗ object-fit and position

column-count

Definition: Use column-count or column-width , plus column-gap and column-rule .

Syntax:

column-count

Example:

<style>div{column-count:3;column-gap:20px;column-rule:1px solid #888}</style>

↗ Multiple columns

column-gap

Definition: Use column-count or column-width , plus column-gap and column-rule .

Syntax:

column-gap

Example:

<style>div{column-count:3;column-gap:20px;column-rule:1px solid #888}</style>

↗ Multiple columns

column-rule

Definition: Use column-count or column-width , plus column-gap and column-rule .

Syntax:

column-rule

Example:

<style>div{column-count:3;column-gap:20px;column-rule:1px solid #888}</style>

↗ Multiple columns

var(--name,fallback)

Definition: Define with --name (usually on :root ) and read with var(--name,fallback) .

Syntax:

var(--name,fallback)

Example:

.a{background:var(--main);padding:var(--pad);color:#fff}

↗ CSS variables

flex-direction

Definition: flex-direction , flex-wrap , justify-content , align-items , align-content , gap .

Syntax:

flex-direction

Example:

.f{display:flex;flex-direction:row;flex-wrap:wrap;justify-content:center;align-items:flex-start;gap:8px;height:130px;background:#eee}

↗ Flex container

flex-wrap

Definition: flex-direction , flex-wrap , justify-content , align-items , align-content , gap .

Syntax:

flex-wrap

Example:

.f{display:flex;flex-direction:row;flex-wrap:wrap;justify-content:center;align-items:flex-start;gap:8px;height:130px;background:#eee}

↗ Flex container

flex-grow

Definition: flex-grow , flex-shrink , flex-basis , order and align-self .

Syntax:

flex-grow

Example:

.g{flex-grow:1}.l{order:-1;align-self:flex-end}

↗ Flex items

order

Definition: flex-grow , flex-shrink , flex-basis , order and align-self .

Syntax:

order

Example:

.g{flex-grow:1}.l{order:-1;align-self:flex-end}

↗ Flex items

align-self

Definition: flex-grow , flex-shrink , flex-basis , order and align-self .

Syntax:

align-self

Example:

.g{flex-grow:1}.l{order:-1;align-self:flex-end}

↗ Flex items

repeat()

Definition: Use grid-template-columns , grid-template-areas , repeat() , minmax() and auto-fit for responsive grids.

Syntax:

repeat()

Example:

.g{display:grid;grid-template-columns:repeat(auto-fit,minmax(90px,1fr));gap:8px}

↗ Grid container and items

minmax()

Definition: Use grid-template-columns , grid-template-areas , repeat() , minmax() and auto-fit for responsive grids.

Syntax:

minmax()

Example:

.g{display:grid;grid-template-columns:repeat(auto-fit,minmax(90px,1fr));gap:8px}

↗ Grid container and items

auto-fit

Definition: Use grid-template-columns , grid-template-areas , repeat() , minmax() and auto-fit for responsive grids.

Syntax:

auto-fit

Example:

.g{display:grid;grid-template-columns:repeat(auto-fit,minmax(90px,1fr));gap:8px}

↗ Grid container and items

aspect-ratio

Definition: Use max-width:100%;height:auto for images and aspect-ratio for video frames so they scale.

Syntax:

aspect-ratio

Example:

<style>.v{width:100%;max-width:320px;aspect-ratio:16/9;background:#14213d;color:#fff;display:grid;place-items:center}</style>

↗ Responsive: viewport and images

Example code

<style>
  p { color: teal; }
  .big { font-size: 28px; }
  #top { background: gold; }
</style>
<p>Teal paragraph</p>
<p class="big">Big paragraph</p>
<p id="top">Gold paragraph</p>

Run this code in the online editor

← Welcome to the CSS courseCSS selectors →