Learn to Code

Introduction to CSS

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 describes how elements should be rendered on screen, on paper, in speech, or on other media.

CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript. It describes how semantic HTML content is presented on screen, on page, in speech, etc. CSS can be used to style websites and other documents to be displayed on the screen. CSS can be used to style all the elements on a page, from the text to the layout, colors, and even animations.

It's all about making things look good!

CSS Selectors

Selectors are used to "find" (or select) the HTML elements you want to style. You can select elements based on their:

Common Selector Types:

Element Selector: Selects all elements of a specific type.

p { color: blue; }

Class Selector: Selects elements with a specific class attribute.

.important { font-weight: bold; }

ID Selector: Selects an element with a specific ID attribute. IDs must be unique!

#main-header { font-size: 2em; }

Attribute Selector: Selects elements based on their attributes and values.

input[type="submit"] { background-color: green; }

Properties and Values

CSS rules consist of a selector and a declaration block. The declaration block contains one or more declarations, separated by semicolons. Each declaration consists of a CSS property name and a value, separated by a colon.

Property: The style attribute you want to change (e.g., color, font-size, margin).

Value: The specific setting for the property (e.g., red, 16px, 20px).

Example:


selector {
  property: value;
}

p {
  color: navy;
  font-size: 16px;
  margin-bottom: 10px;
}
            

The CSS Box Model

The CSS Box Model is essentially a box that wraps around every HTML element. It consists of:

Understanding the box model is crucial for layout and sizing.


.element {
  width: 200px; /* Content width */
  height: 100px; /* Content height */
  padding: 20px; /* Space inside the border */
  border: 5px solid black; /* Border around padding */
  margin: 15px; /* Space outside the border */
}
            

With the default box-sizing: content-box;, the total width would be 200px (content) + 20px (left padding) + 20px (right padding) + 5px (left border) + 5px (right border) = 250px.

Using box-sizing: border-box; makes the width and height properties include the padding and border, which is often more intuitive:


.element {
  box-sizing: border-box;
  width: 200px; /* Includes content, padding, and border */
  padding: 20px;
  border: 5px solid black;
  margin: 15px;
}
            

The `display` Property

The `display` property specifies how an element should be displayed. Key values include:

Example:


.block-element {
  display: block;
  width: 50%;
}

.inline-element {
  display: inline;
  color: purple;
}

.hidden-element {
  display: none;
}
            

CSS Positioning

The position property specifies the type of positioning method used for an element. It affects how elements are placed in relation to other elements or the viewport.

Remember that top, right, bottom, and left properties only work when the position is not static.

Colors and Backgrounds

Styling elements often involves setting their colors and backgrounds.

Color values can be specified using:

Example:


.styled-text {
  color: #3366cc; /* A nice blue */
  background-color: rgba(255, 255, 255, 0.8);
  padding: 15px;
  border-radius: 5px;
}
            

Fonts and Text Styling

Control the appearance of text with CSS properties related to fonts and text formatting.

Example:


h1 {
  font-family: 'Georgia', serif;
  font-size: 2.5em;
  font-weight: bold;
  text-align: center;
  color: #0056b3;
}

p.intro {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-size: 1.1em;
  line-height: 1.7;
}
            

Keep practicing and experimenting to master CSS! Each property offers a world of possibilities for designing beautiful and functional web pages.