CSS cheat sheet - quick reference guide | Extraparse

CSS cheat sheet - quick reference guide

September 15, 20224 min read703 words

Access our comprehensive CSS Cheatsheet with practical examples to quickly reference CSS properties, selectors, and syntax for efficient web development.

Table of Contents

Introduction

Welcome to the CSS Cheat Sheet: Quick Reference Guide. This guide serves as an essential CSS reference for beginners and professionals in web development. It covers fundamental CSS selectors, properties, and syntax with practical examples to streamline your styling process.

Selectors

CSS selectors are patterns used to select the elements you want to style. Here are some commonly used selectors:

1/* Universal Selector */
2* {
3 margin: 0;
4 padding: 0;
5}
6/* Type Selector */
7p {
8 font-size: 16px;
9}
10/* Class Selector */
11.container {
12 width: 100%;
13 max-width: 1200px;
14}
15/* ID Selector */
16#header {
17 background-color: #333;
18}
19/* Descendant Selector */
20nav ul li {
21 list-style: none;
22}

Using selectors effectively allows you to target specific elements and apply styles efficiently.

Box Model

Understanding the CSS box model is crucial for controlling layout and spacing:

1.box {
2 width: 200px; /* Width of the content */
3 padding: 20px; /* Space inside the box */
4 border: 2px solid #000; /* Border around the box */
5 margin: 10px; /* Space outside the box */
6}

The box model consists of margin, border, padding, and content. Box Model Diagram

Flexbox

Flexbox provides a flexible way to arrange items within a container:

1.flex-container {
2 display: flex;
3 justify-content: center; /* Align items horizontally */
4 align-items: center; /* Align items vertically */
5 flex-direction: row; /* Arranges items in a row */
6}

Flexbox simplifies the process of designing responsive layouts.

Grid

CSS Grid offers a two-dimensional layout system for complex designs:

1.grid-container {
2 display: grid;
3 grid-template-columns: repeat(3, 1fr); /* Three equal columns */
4 gap: 20px; /* Space between grid items */
5}

Grid allows for precise placement of elements in rows and columns.

Text Styling

Control the appearance of text with these properties:

1h1 {
2 font-family: "Arial, sans-serif";
3 font-size: 2em;
4 font-weight: bold;
5 color: #333;
6}
7p {
8 line-height: 1.6;
9 text-align: justify;
10}

Proper text styling enhances readability and aesthetics.

Colors

Apply colors to elements using various methods:

1/* Named Colors */
2.button {
3 background-color: blue;
4}
5/* HEX Values */
6.header {
7 background-color: #4caf50;
8}
9/* RGB Values */
10.text {
11 color: rgb(255, 0, 0);
12}
13/* RGBA Values (with transparency) */
14.overlay {
15 background-color: rgba(0, 0, 0, 0.5);
16}

Using different color formats provides flexibility in design.

Backgrounds

Set background images and properties:

1body {
2 background-image: url("background.jpg");
3 background-size: cover; /* Scales the image to cover the element */
4 background-repeat: no-repeat; /* Prevents tiling */
5 background-position: center; /* Centers the background image */
6}

Background properties enhance visual appeal.

Borders and Outline

Define borders and outlines for elements:

1/* Borders */
2.box {
3 border: 1px solid #000;
4 border-radius: 5px; /* Rounded corners */
5}
6/* Outline */
7input:focus {
8 outline: 2px solid #00f;
9}

Borders and outlines help in emphasizing elements.

Positioning

Control the placement of elements on the page:

1/* Relative Positioning */
2.relative {
3 position: relative;
4 top: 10px;
5 left: 20px;
6}
7/* Absolute Positioning */
8.absolute {
9 position: absolute;
10 top: 50px;
11 right: 30px;
12}
13/* Fixed Positioning */
14.fixed {
15 position: fixed;
16 bottom: 0;
17 width: 100%;
18}

Positioning strategies are essential for layout design.

Transitions and Animations

Add interactivity with transitions and animations:

1/* Transition */
2.button {
3 transition: background-color 0.3s ease;
4}
5.button:hover {
6 background-color: #555;
7}
8/* Animation */
9@keyframes fadeIn {
10 from {
11 opacity: 0;
12 }
13 to {
14 opacity: 1;
15 }
16}
17.fade-in-element {
18 animation: fadeIn 2s forwards;
19}

Animations enhance user experience and engagement.

Responsive Design

Ensure your website looks good on all devices:

1/* Media Queries */
2@media (max-width: 768px) {
3 .flex-container {
4 flex-direction: column;
5 }
6 .grid-container {
7 grid-template-columns: 1fr;
8 }
9}

Responsive design promotes accessibility across different screen sizes.

Tips

  • Consistency: Maintain consistent naming conventions for classes and IDs.
  • Comments: Use comments to explain complex sections of your CSS.
  • Optimization: Minify CSS for faster load times in production.
  • Preprocessors: Consider using CSS preprocessors like SASS or LESS for enhanced features.