What is CSS or Cascading Style Sheets?

Jeff Ross
05/16/2023 1:48:00 AM Comment(s)

What is CSS?

Let's start with a basic introduction to CSS (Cascading Style Sheets), which is a language used to style HTML documents. 

What is CSS?

CSS stands for Cascading Style Sheets. It describes how HTML elements are to be displayed on the screen, paper, or in other media. CSS can save a lot of work as it can control the layout of multiple web pages all at once.

CSS Syntax
A CSS rule-set consists of a selector and a declaration block:

selector {
  property: value;
}

The `selector` points to the HTML element you want to style. The `declaration block` contains one or more declarations separated by semicolons. Each `declaration` includes a CSS `property` name and a `value`, separated by a colon. A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by curly braces.


Basic Steps to Write CSS

Set Up Your HTML
First, you'll need an HTML document to style. Here's a simple example:

<!DOCTYPE html>
<html>
<head>
  <title>My First CSS Example</title>
</head>
<body>
  <h1>Welcome to My Website</h1>
  <p>This is a paragraph.</p>
</body>
</html>


2. Create a CSS File
Create a new text file with a `.css` extension. For example, you might call it `styles.css`. 

In this CSS file, you'll write your styles. Let's start by styling the `h1` and `p` elements from the HTML document above:

h1 {
  color: red;
  text-align: center;
}

p {
  color: blue;
  font-size: 20px;
}

In this example, the `h1` selector will select all `<h1>` elements in your HTML document and apply the styles within the curly braces. Here, it changes the text color to red and centers the text. 

Similarly, the `p` selector selects all `<p>` (paragraph) elements, changing the text color to blue and setting the font size to 20 pixels.


3. Link the CSS File to Your HTML Document
Now that you've written some CSS, you need to link it to your HTML document. This is typically done within the `<head>` element of the HTML document using the `<link>` element:

<head>
  <title>My First CSS Example</title>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>

In this example, the `href` attribute specifies the path to the CSS file.

Now, when you open your HTML document in a web browser, it should display the styles specified in your CSS file.

Practice 
Play around with different CSS properties and different HTML elements. Here are some properties you might find useful:

- `color`: changes the text color
- `background-color`: changes the background color
- `font-size`: changes the text size
- `text-align`: aligns the text (e.g., left, right, center)
- `margin`: sets the space around an element, outside of any defined borders
- `padding`: sets the space around an element's content, inside of any defined borders

You can find a comprehensive list of CSS properties on many websites like [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference).

Learning More
This is just the very basics of CSS. There's much more to learn, including more complex selectors, how to use classes and ids, how to style specific states of elements (like when a button is hovered over), and much more. Here are some additional concepts and techniques you may want to explore as you continue your learning journey in CSS.

CSS Classes and IDs
CSS Classes and IDs are ways to select and style more specific elements or groups of elements. 

- Classes are specified in HTML using the `class` attribute, and in CSS by a period `.` before the class name.
- IDs are unique identifiers specified in HTML using the `id` attribute, and in CSS by a hash `#` before the id name.

For example:

<p>This is a highlighted paragraph.</p>
<div>This is a special div.</div>

.highlight {
  background-color: yellow;
}

specialDiv {
  border: 2px solid red;
}


Pseudo-Classes
Pseudo-classes are used to define a special state of an element. For example, you can style an element when it's being hovered over, or an input when it's focused (clicked but not yet typed into).

button:hover {
  background-color: green;
}

input:focus {
  border: 2px solid blue;
}


The Box Model
Every element in HTML is a rectangular box, and can have margins, borders, padding, and content. You have control over all these aspects in CSS.

- **Content** - The content of the box, where text and images appear
- **Padding** - Clears an area around the content. The padding is transparent
- **Border** - A border that goes around the padding and content
- **Margin** - Clears an area outside the border. The margin is transparent


Flexbox and Grid
CSS Flexbox and Grid are powerful layout systems that make creating complex layouts much easier.

- Flexbox is one-dimensional (either in a row or a column).
- Grid is two-dimensional (both rows and columns).

These concepts cover just the tip of the iceberg of what's possible with CSS. As you get more comfortable with these basics, you can start to explore more advanced CSS topics, such as animations and transitions, transformations, advanced selectors and pseudo-elements, media queries for responsive design, and more.

There are many online resources available to learn CSS, including tutorials, video courses, and interactive coding exercises. Some reputable resources are [MDN Web Docs] (https://developer.mozilla.org/en-US/docs/Web/CSS), [W3Schools] (https://www.w3schools.com/css/), and [CSS-Tricks] (https://css-tricks.com).

Jeff Ross