CSS controls how a web page looks. HTML is the content and the structure. CSS is the color, size, and placement. Without CSS, a page is just plain black text on a white background. This tutorial uses the bio page from the basic HTML tutorial as its example. If you have not read that tutorial yet, read it first so the examples make sense.

How to add CSS

There are three ways to add CSS. First: write it right in a tag with the style attribute. This is called inline CSS:

<h1 style="color: blue;">Briyan Febri</h1>

Second: write it inside a style tag in the head. All the rules for that page sit in one place:

<head>
  <style>
    h1 { color: blue; }
  </style>
</head>

Third: write it in a separate .css file, then link it from the HTML. This is the recommended way:

<link rel="stylesheet" href="style.css">

A separate file is tidier and easier to maintain. One CSS file can serve many pages. Warning: check the file name and its location. If even one letter is wrong, the CSS will not load and the page stays plain.

CSS rules and selectors

One CSS rule has two parts: a selector and a declaration. The selector picks which part of the page to change. The declaration holds a property (the trait to change) and a value (the result). A declaration sits inside curly braces and ends with a semicolon.

Anatomy of a CSS rule

There are three basic selectors. A tag selector picks every tag of that kind. For example, h1 picks all h1 headings. A class selector picks tags that have that class. A class is written with a dot in front, for example .nama. An id selector picks one tag that has that id. An id is written with a hash in front, for example #judul. One page may have only one tag with the same id. A class can be reused many times.

h1 { color: blue; }
.nama { font-size: 28px; }
#judul { text-align: center; }

Colors

Colors are written as names, hex codes, or rgb codes. Color names are the easiest, for example blue and red. A hex code starts with a hash and uses six digits, for example #55557f. An rgb code uses three numbers, for example rgb(85, 85, 127). The two most used color properties are color for text color and background-color for background color.

h1 {
  color: #55557f;
  background-color: #f4f4f4;
}

Fonts

The font-family property sets the typeface. List several font names at once, separated by commas. The browser uses the first font found on the computer. If it is missing, the browser tries the next one. This matters because every computer has different fonts.

body {
  font-family: Arial, sans-serif;
  font-size: 16px;
}

The font-size property sets the text size. The px unit (pixel) is the easiest for beginners. The font-weight: bold property makes text bold. The text-align: center property centers text.

The box model

Every element on a page is a box. The box has four layers, from inside out: content, padding (inner space), border (edge line), and margin (outer space). Padding adds space between the content and the border. Margin adds space between this box and other boxes.

The box model: four layers of a box

.kartu {
  padding: 30px;
  border: 1px solid #dddddd;
  margin: 40px auto;
}

The value margin: 40px auto means 40px of space on the top and bottom, and automatic space on the left and right. The auto value on the sides centers the box. This only works when the box has a set width, for example with max-width. Warning: the total width of a box is the content plus padding plus border. A box 500px wide with 30px of padding becomes 560px wide. To keep the width fixed, add box-sizing: border-box.

Basic layout with flexbox

Flexbox arranges a row of items inside one container. The way to do it: give the container the display: flex property. The items inside it line up side by side at once. Without flexbox, the items stack downwards.

Flexbox: items in a row

.keahlian {
  display: flex;
  justify-content: center;
  gap: 10px;
}

The justify-content: center property centers the items. The gap: 10px property adds 10px of space between items. These two properties are enough for a simple row like the skills list on a bio page.

Final example program: complete bio page

This is a complete example you can copy right away. There are two files: index.html and style.css. Save both in the same folder. Then open index.html in a browser. If the page still looks plain, check the CSS file name and the link line in the HTML again.

First file, index.html:

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Biodata Briyan Febri</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="kartu">
    <div class="foto">BF</div>
    <h1 class="nama">Briyan Febri</h1>
    <p class="peran">Pelajar dan pembuat website</p>
    <p class="tentang">Halo! Saya Briyan. Saya sedang belajar membuat website dengan HTML dan CSS.</p>
    <h2>Keahlian</h2>
    <div class="keahlian">
      <span class="item">HTML</span>
      <span class="item">CSS</span>
      <span class="item">Linux</span>
    </div>
    <p class="kontak">Hubungi saya: briyan@contoh.id</p>
  </div>
</body>
</html>

Second file, style.css:

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
  color: #2c2c2c;
  margin: 0;
}

.kartu {
  max-width: 500px;
  margin: 40px auto;
  padding: 30px;
  background-color: #ffffff;
  border: 1px solid #dddddd;
  border-radius: 12px;
  text-align: center;
}

.foto {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background-color: #55557f;
  color: #ffffff;
  font-size: 36px;
  font-weight: bold;
  line-height: 100px;
  border-radius: 50%;
}

.nama {
  color: #55557f;
  margin-bottom: 5px;
}

.peran {
  color: #777777;
  margin-top: 0;
}

.keahlian {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.item {
  background-color: #55557f;
  color: #ffffff;
  padding: 8px 14px;
  border-radius: 20px;
}

Replace the name, role, and email address with your own data. Add a new skill by copying one span line. If you want to ask Muse about CSS, mention the broken code and the message you saw.