There’s a wide range of CSS commands that are essential for creating a website. We’ll provide you with an overview of the most important and fre­quently used CSS commands. We’ve also included some short, cus­tom­is­able examples to help you with designing your website.

What can I do with CSS commands?

Initially, CSS commands were used to define fonts and font colours and to make tables more at­tract­ive or have text flow around elements. But the pos­sib­il­it­ies of CSS commands have grown ex­po­nen­tially since then. In addition to cropping images, you can also create colour gradients, shadows, curves and an­im­a­tions with CSS commands.

Tip

Nowadays, building a website wouldn’t be possible without Cascading Style Sheets (CSS). You can find out more about what CSS is and how to integrate CSS into HTML in the Digital Guide articles linked here. Ad­di­tion­ally, we recommend reading our CSS tutorial for beginners and taking a look at our article on CSS tricks for more CSS code snippets.

A CSS command, also known as a CSS rule, is con­struc­ted like this:

Selector Opening bracket Property Property value Closing bracket
p { color: navy; }

Together, the property and property value form what’s known as the de­clar­a­tion.

Anyone writing CSS should always keep in mind that despite many im­prove­ments and ad­just­ments, not all browsers ‘un­der­stand’ everything. That’s why CSS has browser prefixes, which are used to address browser engines spe­cific­ally. These prefixes are:

  • -moz-: for Firefox
  • -ms-: for Internet Explorer
  • -khtml-: for Conqueror
  • -o-: for old Opera Versions
  • -webkit-: for Safari, Chrome, new Opera Versions

Here’s a simple example:

.box {
	-moz-border-radius: 10px; / *Instruction for Firefox* /
border-radius: 10px;
}

You can test the com­pat­ib­il­ity of CSS commands with all common browsers for free on the website CANIUSE.

Tip

In CSS, any text between / / is not executed by the browser. This allows users to add comments and hide CSS commands for testing purposes without having to delete and rewrite them.

Some tools that have been added to CSS are CSS Flexbox, CSS Grid and media queries, all of which help to implement mobile-first prin­ciples with new CSS commands. These are just a few of the tools available that help websites to be re­spons­ive.

How to specify colours in CSS

The world of colours on the internet is based on the RGB color model. In CSS, there are commands to define colours for in­di­vidu­al com­pon­ents of a website. Almost every element can be assigned a colour or a colour gradient.

Tip

The class and ID names in CSS commands are case sensitive. This means that CSS dif­fer­en­ti­ates, for example, between the ID #YELLOW { ... } and #yellow { ... }. It’s essential to use IDs in exactly the same way they are used in the HTML element.

Colour values

  • Hexcode: #63f0ac
  • Colour names: Red, green, blue, coral, white, black …
  • RGB: rgb(238,130,238) and RGB trans­par­ency with rgb(238,130,238, 0.7)
  • HSL with trans­par­ency: hsla(140,20%,50%,0.5) -Hue: 0–360 degrees on the colour wheel -Sat­ur­a­tion: From 0 % col­our­less to 100 % highest colour intensity -Lightness: From 0 % (black) to 100 % (white) -Numerical trans­par­ency value up to 1.0 (full colour coverage)

Spe­cify­ing colours for HTML elements

The color property is used to designate font colours:

p { color: red; } / *a paragraph in red* /
div { color: #ffc3dd; } / *DIV container with font in light pink* /
h1 { color: rgba(220,0,218,0.85); } / *Heading in strong violet with transparency 85 %* /

Spe­cify­ing colour gradients

In the past, de­velopers had to use image files to create colour gradients. Now, it can easily be done using a CSS command. You can use the command background-image: gradient, or its short form background: gradient, to create colour gradients. gradient rep­res­ents a value for an image without di­men­sions and creates a colour gradient that can be changed at par­tic­u­lar points.

CSS command De­scrip­tion Possible values
linear-gradient Linear gradient from one colour to another or with several colours to-top, to-right, 45deg (any angle), colours with % as stop/trans­ition points
radial-gradient Circular gradient from one colour to another circle, ellipse with pixel values and colours
conic-gradient Conically arranged change from one colour to another colours with % values and start angle as number

The table only shows some of the possible at­trib­utes. All common values can be used as colour spe­cific­a­tions, including the colour values for trans­par­ency.

The complete CSS command could be as follows:

.box { background-image: linear-gradient(to top, white 0%, black 50%); }

CSS commands for fonts

With the CSS command font, the six prop­er­ties of a font can be changed in one go.

p { font: italic small-caps 700 1.2em/1.5em Arial, Helvetica, sans-serif; }

This CSS command can be resolved in­di­vidu­ally, in the order specified:

CSS command De­scrip­tion Possible Values
font-style Style of the font normal, italic, oblique
font-variant Variant of the font normal, small-caps
font-weight Boldness of the font normal (= 400), bold, bolder, lighter, 100 to 900 (in steps of 100)
font-size/line-height Size of the font Spe­cific­a­tions in px, %, em, rem, vw, vh
font-family Fonts Fonts depend on operating system, browser or ad­di­tion­ally installed ex­ten­sions

The setting of the font-family for the body of a website is applied to all child elements. A font statement is not used to define the font colour. Instead color: is used followed by a specific colour value.

Tip

Fonts with spaces should be enclosed in the CSS command with single or double quotation marks, e.g., font-family: "Lobster Two".

You can find more in­form­a­tion on the units of meas­ure­ment that can be used for font-size in our article ‘Ty­po­graphy in re­spons­ive web design – Part 3 : Technical Im­ple­ment­a­tion with CSS’.

CSS commands for text design

In addition to font type, colour and size, other ty­po­graph­ic sub­tleties are important for the read­ab­il­ity of a website. These include:

CSS command De­scrip­tion Possible values
text-align Text alignment left, right, centre, justify (justified)
text-dec­or­a­tion Text dec­or­a­tion underline, overline, line-through
word-spacing Spacing between words numerical value in pt (point) mm, cm, px, em, rem
letter-spacing Spacing between char­ac­ters numeric value in pt (point) mm, cm, px, em, rem
text-indent Indention of 1st line numeric value in pt (point) mm, cm, px, em, rem
text-transform Text case cap­it­al­ise, uppercase, lowercase, none

CSS commands for lines and borders

To design borders, the CSS element border is used: Similar to font, several prop­er­ties can be combined here as well.

Example: An image (with the HTML tag img) is sur­roun­ded by a 5-pixel thick line in navy blue. The ab­bre­vi­ation is:

img { border: 5px solid #000080; }

Here’s an overview of this CSS command:

CSS command De­scrip­tion Possible values
border-width Line thickness px, mm, in, em, rem
border-style Line style none, hidden, dotted, dashed, double, groove, ridge, inset, outset
border-color Line colour See section on colours

You can place lines along the in­di­vidu­al edges of an image or rectangle in­di­vidu­ally with border-top, border-right, border-bottom and border-left using the same at­trib­utes (width, style and colour) as you would with the shorthand border.

CSS commands for spacing

There are two types of spacing:

  • padding, which refers to the space between the content and border of an element
  • margin, which refers to the space between two elements

Inner spacing

With padding, you can define the distance between a text block or image and the sur­round­ing frame. You can specify padding using the shorthand notation or define spe­cific­a­tions for all sides of a content block.

CSS command De­scrip­tion Possible values
padding: 1px General inner spacing px, em, mm, in, pt, pc, %
padding: 1em 2.5em; Internal spacing top & bottom (1st value), left & right (2nd value) px, em, mm, in, pt, pc, %
padding: 5px 2px 4px; Internal spacing top (1st value), left & right (2nd value), bottom (3rd value) px, em, mm, in, pt, pc, %
padding-top: 1mm; Internal spacing to top px, em, mm, in, pt, pc, %
padding-right: 1pc; Internal spacing to the right px, em, mm, in, pt, pc, %
padding-bottom: 3pt; Internal spacing to the bottom px, em, mm, in, pt, pc, %
padding-left: 2in; Internal spacing to the left px, em, mm, in, pt, pc, %

If the short form padding includes values for all four sides, the values will be processed starting from the top and moving in a clockwise manner:

p { padding: 5px 0 5px 0; } / *top and bottom 5 pixels, right and left no inner spacing* /

Outer margin

The CSS command margin defines how far away a layout element is from the following one. The spelling and enu­mer­a­tion of in­di­vidu­al values is identical to padding.

CSS command De­scrip­tion Possible CSS command values
margin: 5px; General outside distance px, em, mm, in, pt, pc, %
margin: 2em 1.5em; Outer margin top & bottom (1st value), left & right (2nd value) px, em, mm, in, pt, pc, %
margin: 2px 10px 15px; Outer spacing top (1st value), left & right (2nd value), bottom (3rd value) px, em, mm, in, pt, pc, %
margin-top: 2pc; Outer spacing to top px, em, mm, in, pt, pc, %
margin-right: 5mm; Outer margin to the right px, em, mm, in, pt, pc, %
margin-bottom: 7pt; Outer spacing to the bottom px, em, mm, in, pt, pc, %
margin-left: 1in; Outer spacing to the left px, em, mm, in, pt, pc, %
Tip

When cal­cu­lat­ing the elements of a layout, all com­pon­ents must be included. This includes the width of the text or image (width), inner spacing (padding), line thickness (border-width) and outer spacing (margin).

When defining the design of links in CSS, you can design each status of a link sep­ar­ately. On top of the normal ap­pear­ance, you can also specify how a link should be displayed when the mouse hovers over it, or when you click or select it (for example, with the tab key).

a:link   { background-color: black; color: white;}

As a rule, changes are made to these pseudo-classes in order to change the colour of links. In principle, however, any CSS command can be in­teg­rated into these classes.

CSS command De­scrip­tion Possible values
a:link Link in initial state Can be combined with CSS commands
a:hover Link when hovering with the mouse pointer Can be combined with CSS commands
a:active Link when clicked Can be combined with CSS commands
a:focus Selected link (e.g., with the tab key) Can be combined with CSS commands
a:visited Link after it has been clicked Can be combined with CSS commands

CSS commands for back­grounds

The back­ground of a website can also be changed using CSS. This can be done by setting the colour or inserting an image. If you select an image, you can also define the ad­di­tion­al features.

CSS command De­scrip­tion Possible values
back­ground-color Back­ground colour See section on colours
back­ground-image Back­ground image URL
back­ground-at­tach­ment De­term­ines whether the back­ground moves when scrolling or remains fixed scroll, fixed
back­ground-clip De­term­ines the area to which the back­ground prop­er­ties apply padding-box, corner-box, content-box
back­ground-position Alignment of the back­ground image top, centre, bottom / left, centre, right
back­ground-repeat If the image is smaller than the back­ground, it can be repeated. repeat, repeat-x (ho­ri­zont­al), r(vertical), space (repeat without cropping), round (repeat with scaling)

These are detail commands. These can also be nested under the top-level command background:

background: white url("http://example.org/image.png")
    repeat-x

The commands background-color, background-image and background-repeat are in­teg­rated here.

Tip

In our Digital Guide, you can find out how to pin a website to the taskbar in Windows 11. By pinning this article to your taskbar, you’ll have quick access to an overview of CSS commands at all times.

Go to Main Menu