CSS Basics: Mastering Web Styling Techniques

css basics
css basics

Mastering the Art of CSS: The Comprehensive CSS Basics Guide for Beginners.


Cascading Style Sheets (CSS) is the unsung hero of web development, responsible for transforming the dull and lifeless HTML into visually appealing web pages. In this comprehensive guide, we’ll take you through CSS basics and equip you with the knowledge to make your web content stand out. So, let’s dive in and answer essential questions like how to change text colors, position elements, and decorate your web pages with background images and colors.

What is CSS?

Before we begin, it’s important to understand that CSS is not a programming language or markup language; it’s a style sheet language. Its primary purpose is to style HTML elements selectively. Let’s take an example to illustrate this:

p {
  color: red;

Try it yourself! Create a new file, paste these three lines of CSS, and save it as “style.css” in a folder named “styles.” To apply this CSS to your HTML document, insert the following line in the <head> section of your “index.html” file:

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

Open “index.html” in your browser, and you’ll see that your paragraph text turns red. Congratulations! Your CSS is up and running.

Anatomy of a CSS Ruleset:

Let’s dissect the CSS code we used for the red paragraph text to understand how it works:

  • Selector: This is the HTML element name at the start of the ruleset, defining which elements to style (e.g., <p> elements).
  • Declaration: A single rule (e.g., color: red;) specifying which property of the element you want to style.
  • Properties: Ways to style an HTML element (e.g., color is a property of <p> elements).
  • Property Value: The value to apply to the property (e.g., “red”).

Remember these key points of the CSS syntax:

  • Each ruleset must be enclosed in curly braces {}.
  • Use a colon : to separate the property from its value.
  • Use a semicolon ; to separate declarations in a ruleset.
  • For multiple property values in one ruleset, separate them by semicolons, like this:
p {
  color: red;
  width: 500px;
  border: 1px solid black;

You can also select multiple elements and apply a single ruleset to all of them by separating multiple selectors with commas:

h1 {
  color: red;

Different Types of Selectors:

There are various types of selectors available in CSS. The examples above used element selectors, which select all elements of a given type. But you can make more specific selections:

  • Element Selector: Selects all HTML elements of the specified type (e.g., p selects all paragraphs).
  • ID Selector: Selects the element with the specified ID (IDs should be unique on a page, e.g., #my-id).
  • Class Selector: Selects element(s) with a specific class (multiple elements can have the same class, e.g., .my-class).
  • Attribute Selector: Selects elements with a specified attribute (e.g., img[src] selects <img src="myimage.png">).
  • Pseudo-class Selector: Selects elements in a specified state (e.g., a:hover selects links when hovered over with a cursor).

These are just a few examples, and there are many more selectors to explore. To learn more, refer to the MDN Selectors guide.

Fonts and Text:

Now that we’ve covered some CSS fundamentals, let’s enhance the appearance of our example by adding more rules to the “style.css” file.

First, add the Google Fonts link to your “index.html” file to load the desired font family:

<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" />

Next, remove the existing red text rule and add the following lines, replacing the font-family with your selection from Google Fonts:

html {
  font-size: 10px; /* base font size is now 10 pixels high */
  font-family: "Open Sans", sans-serif; /* your choice from Google Fonts */

These lines set a base font size and family for the entire page, making your text consistent and more appealing. Additionally, set font sizes for elements within the HTML body:

h1 {
  font-size: 60px;
  text-align: center;

li {
  font-size: 16px;
  line-height: 2;
  letter-spacing: 1px;

Feel free to adjust the pixel values according to your preferences. This will give your webpage a more polished look.

CSS: All About Boxes:

A fundamental concept in CSS is the box model. HTML elements are essentially boxes that can be styled in terms of size, color, and position. Key properties related to boxes include:

  • padding: The space around the content.
  • border: The solid line outside the padding.
  • margin: The space around the outside of the border.

These properties, along with width, background-color, color, and text-shadow, play a crucial role in controlling the appearance of HTML elements. For instance, you can change the background color of the entire page with:

html {
  background-color: #00539f; /* Change this to your preferred color */

You can also style the body element with the following rules:

body {
  width: 600px;
  margin: 0 auto;
  background-color: #ff9500; /* Experiment with different colors */
  padding: 0 20px 20px 20px;
  border: 5px solid black;

These rules define the width, margins, background color, padding, and border of the <body> element. Feel free to adjust the values to achieve your desired layout.

Positioning and Styling:

To remove the gap at the top of the body caused by default browser styling, you can override the margin for the <h1> element:

h1 {
  margin: 0;
  padding: 20px 0;
  color: #00539f; /* Match it with the HTML background color */
  text-shadow: 3px 3px 1px black; /* Experiment with shadow values */

These rules set the margin, padding, text color, and text shadow for the main page title. Adjust the values to suit your design.

Centering the Image:

To center the image, use the following rule:

img {
  display: block;
  margin: 0 auto;

This ensures that the image is horizontally centered. Note that we use display: block; to make the auto margin trick work on inline elements like images. If your image overflows the body due to its size, you can resize it using a graphics editor or set the width property in the <img> element.


Congratulations! If you’ve followed the instructions in this guide, you now have

a beautifully styled web page. This is just the beginning of your journey into CSS. To delve deeper into the world of styling HTML using CSS, explore additional resources and tutorials.

Don’t hesitate to experiment, tweak values, and explore the endless possibilities of CSS to create unique and visually appealing web content. Remember, practice makes perfect, so keep honing your CSS skills to become a master web designer.


Please enter your comment!
Please enter your name here