Domain Search Form
£11.98
£33.98
£24.98
£49.98

A Beginner's Guide to CSS

Written by Giraffe Hosting Limited
Published 30 January 2024
A Beginner's Guide to CSS
Published: 30 January 2024
Category: 
Written by: Giraffe Hosting Limited
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language like HTML. CSS is one of the cornerstone technologies of the World Wide Web, alongside HTML and JavaScript.

Table of Contents

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language like HTML. CSS is one of the cornerstone technologies of the World Wide Web, alongside HTML and JavaScript.

What is CSS?

CSS is used to control the style and layout of web pages. It allows you to apply styles to HTML elements, such as setting colors, fonts, spacing, and positioning. CSS helps in keeping content and presentation separate, which improves accessibility and maintainability.

How to Use CSS

CSS can be applied in three ways:

  1. Inline - by using the style attribute in HTML elements.
  2. Internal - by using a <style> tag in the <head> section of the HTML.
  3. External - by linking to an external CSS file using the <link> tag in the HTML <head> section.

External styling is generally preferred for maintaining larger websites.

Example of Inline CSS

<p style="color: blue; font-size: 20px;">This is a blue paragraph.</p>

Example of Internal CSS

<style>
  p {
    color: red;
    font-size: 16px;
  }
</style>

Example of External CSS

In your HTML file:

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

In styles.css file:

p {
  color: green;
  font-size: 14px;
}

CSS Syntax

CSS is composed of selectors and declarations. A selector points to the HTML element you want to style, while a declaration block contains one or more declarations separated by semicolons. Each declaration includes a property and a value, separated by a colon.

h1 {
  color: navy;
  margin-left: 20px;
}

Basic Selectors

Type Selector: Selects elements by their type.

h1 {
  color: orange;
}

Class Selector: Selects elements by the class attribute. Prefixed with a dot.

.myclass {
  font-weight: bold;
}

ID Selector: Selects a single element by its id attribute. Prefixed with a hash.

#myid {
  background-color: yellow;
}

Basic Properties

Color and Background

  • color: Sets the text color.
  • background-color: Sets the background color.
p {
  color: red;
}

div {
  background-color: lightblue;
}

Text Formatting

  • font-size: Sets the size of the font.
  • text-align: Sets the horizontal alignment of text.
p {
  font-size: 16px;
}
h1 {
  text-align: center;
}

Box Model

  • margin: Space around elements.
  • padding: Space inside the element between the content and the border.
  • border: Border around the element.
.box {
  margin: 10px;
}
.box {
  padding: 20px;
}
.box {
  border: 1px solid black;
}

Positioning

  • position: Specifies the type of positioning (e.g., absolute, relative).
.element {
  position: absolute;
  top: 10px;
  left: 20px;
}

Combining Selectors

You can combine multiple selectors to target elements more specifically.

/* Selects all <p> elements inside <div> elements */
div p {
  color: blue;
}

Flexbox

Flexbox is a layout model that allows for responsive design elements to be aligned and distributed within a container.

.container {
  display: flex;
  justify-content: center; /* Aligns items horizontally */
  align-items: center;     /* Aligns items vertically */
}

.item {
  flex: 1; /* Allows items to grow to fill available space */
}

Grid Layout

CSS Grid Layout is a two-dimensional layout system for the web, enabling more complex layouts and alignments.

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto; /* Creates a three-column layout */
  gap: 10px; /* Space between grid items */
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px;
}

Pseudo-Classes and Pseudo-Elements

Pseudo-classes and pseudo-elements allow you to style specific parts of your elements.

a:hover {
  color: green; /* Changes color when the mouse hovers over a link */
}

p::first-letter {
  font-size: 200%;
  color: red; /* Styles the first letter of a paragraph */
}

Responsive Design

Media queries are a key feature of responsive design, allowing you to apply styles based on the size of the device.

@media screen and (max-width: 600px) {
  .container {
    background-color: lightblue;
  }
}

CSS Variables

CSS Variables (Custom Properties) let you store values to reuse in your stylesheet.

:root {
  --main-bg-color: coral;
}

body {
  background-color: var(--main-bg-color); /* Use the stored color */
}

Transitions and Animations

Transitions allow for smooth changes between states, while animations enable more complex sequences of style changes.

Example of Transition:

.button {
  background-color: blue;
  transition: background-color 0.5s ease; /* Smooth transition for background color */
}

.button:hover {
  background-color: red;
}

Example of Animation:

@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}

.div {
  animation-name: example;
  animation-duration: 4s;
}

Combining Selectors for Specificity

You can increase the specificity of your selectors by combining multiple types.

#nav .item:hover {
  color: purple; /* This applies only to elements with class 'item' inside the element with id 'nav' when hovered */
}

Final Thoughts

This guide introduces the basics of CSS, from how to apply CSS to HTML elements to understanding the syntax, selectors, and some common properties. As you practice and explore more complex aspects of CSS, you'll be able to create more visually appealing and functionally rich web pages. Remember, the best way to learn CSS is through hands-on experience and experimentation.

Articles You Might Like
Choose Giraffe Hosting Limited for your Domain Registration, Cloud Hosting, WordPress Hosting, and Virtual Private Server Hosting; Powered by renewable energy, contributed to sustainable growth.
Copyright © Giraffe Hosting Limited. 2007 – 2025 All rights reserved.
Explore
Services
Support