CSS – Basics, Properties, Selectors, and Practical Examples

CSS (Cascading Style Sheets) is a style sheet language used to define the appearance and layout of elements on web pages. With CSS, you can control colors, fonts, sizes, spacing, positioning, backgrounds, animations, and the way content adapts to different screen sizes and devices.

While HTML defines the structure and meaning of content, CSS determines how that structure is presented in a web browser. For example, HTML can define a heading, text, image, and button, while CSS controls their colors, sizes, spacing, positioning, and overall visual style.

CSS is one of the core technologies of frontend development and is essential for building modern, well-organized, and responsive websites. Understanding CSS allows you to transform a simple HTML document into a visually appealing business website, blog, online store, or complex web application.

In this guide, you will learn the fundamentals of CSS, how it connects to HTML, how selectors work, and the most important CSS properties. You will also discover how modern layouts are created using Flexbox and CSS Grid, along with practical examples that demonstrate how CSS is used in real-world web development projects.

What Is CSS and What Is It Used For?

CSS (Cascading Style Sheets) is a style sheet language used to describe the presentation and visual appearance of HTML documents. Its primary purpose is to separate a webpage’s content and structure from its design and styling.

Without CSS, web browsers display HTML elements using their default styles. Headings, paragraphs, links, and lists still exist and function correctly, but the page lacks a customized design and visual identity.

By applying CSS rules, we can modify nearly every visual aspect of a webpage, including:

  • text and background colors;
  • font family, size, and weight;
  • element width and height;
  • internal and external spacing;
  • layouts with rows and columns;
  • the appearance of buttons, navigation menus, and cards;
  • content presentation on smartphones and tablets;
  • animations and visual effects.

For example, HTML defines a heading:

HTML

 

 

<h1>Welcome to Our Website</h1>

 

CSS can then assign a specific visual style to it:

CSS

 

 

h1 {

color: #183b70;

font-size: 36px;

font-weight: 700;

text-align: center;

}

The result is a dark blue heading, displayed at 36 pixels, bold, and horizontally centered.

This separation of structure and presentation allows the same HTML content to be used with different designs without modifying the document itself.

CSS Box Model – How Do HTML Element Dimensions Work?

One of the most important concepts in CSS is the Box Model. From a layout perspective, every HTML element is treated as a rectangular box composed of several layers.

The Box Model consists of:

  1. Content – the actual content of the element, such as text or an image.
  2. Padding – the inner space between the content and the element's border.
  3. Border – the outline that surrounds the content and padding.
  4. Margin – the outer space between the element and other elements.

Example:

CSS

 

 

.card {

width: 300px;

padding: 20px;

border: 2px solid #ddd;

margin: 30px;

}

With the default content-box model, the total width of an element includes the content width plus its padding and border, while the margin remains outside the element's calculated dimensions.

To make element dimensions more predictable, developers commonly use the following rule:

CSS

 

 

*,

*::before,

*::after {

box-sizing: border-box;

}

When box-sizing: border-box is applied, the specified width and height already include the element's padding and border. This makes sizing calculations much easier and simplifies the creation of consistent layouts.

For example, with:

CSS

 

 

.card {

width: 300px;

padding: 20px;

border: 2px solid #ddd;

box-sizing: border-box;

}

the element's total width remains 300 pixels, including both the padding and border. Without border-box, the final rendered width would be larger than 300 pixels because the padding and border would be added on top of the content width.

Understanding the Box Model is essential for creating accurate layouts, controlling spacing, and building responsive web designs that behave consistently across different browsers and screen sizes.

How Is CSS Connected to an HTML Document?

There are three primary ways to add CSS styles to an HTML document: external CSS, internal CSS, and inline CSS.

1. External CSS – Linking a Separate Stylesheet

This is the most common and recommended method for organizing styles in web projects. CSS rules are stored in a separate file, typically named style.css, which is linked to the HTML document using the <link> element.

HTML document:

HTML

 

 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

 

<title>My Website</title>

 

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

</head>

<body>

 

<h1>Welcome</h1>

<p>This is an example of linking a CSS file.</p>

 

</body>

</html>

 

style.css file:

CSS

 

 

body {

font-family: Arial, sans-serif;

background-color: #f4f6f9;

color: #333;

}

 

h1 {

color: #183b70;

}

The main advantage of external CSS is that the same styles can be reused across multiple HTML pages. It also makes code cleaner, easier to maintain, and more suitable for larger projects.

2. Internal CSS – Styles Inside the HTML Document

Internal CSS is defined within a <style> element, which is usually placed inside the <head> section of the document.

HTML

 

 

<head>

<style>

body {

background-color: #f4f6f9;

font-family: Arial, sans-serif;

}

 

h1 {

color: #183b70;

}

</style>

</head>

 

This approach can be useful for small examples, demonstrations, or pages with unique styling requirements, but it is generally less practical for large projects.

3. Inline CSS – Styles Applied Directly to an Element

Inline CSS is added using the style attribute directly within an HTML element.

HTML

 

 

<h1 style="color: blue; font-size: 32px;">

My Heading

</h1>

 

Although this is a quick way to test or apply a specific style, it is not recommended as a standard practice for larger projects. Excessive use of inline styles makes code harder to maintain and reduces the reusability of CSS rules.

Basic CSS Syntax – How Are CSS Rules Written?

CSS consists of rules that determine how specific HTML elements will be displayed.

Every CSS rule has three main parts:

  1. Selector – specifies which HTML elements the rule applies to.
  2. Property – defines what aspect of the element you want to change.
  3. Value – specifies how that property should be set.

Example:

CSS

 

 

p {

color: #333333;

font-size: 18px;

line-height: 1.6;

}

In this example:

  1. p is the selector that targets all HTML paragraph elements.
  2. color is the property that defines the text color.
  3. #333333 is the color value.
  4. font-size defines the font size.
  5. line-height defines the spacing between lines of text.

Each property and its value are separated by a colon (:), while each declaration ends with a semicolon (;). A single CSS rule can contain multiple declarations.

CSS Selectors – How to Target HTML Elements

Selectors are the foundation of working with CSS. They allow you to choose which HTML elements should receive specific styles.

The most commonly used selectors are element selectors, class selectors, and ID selectors.

Element Selector

An element selector applies styles to all elements of a specific HTML type.

CSS

 

p {

color: #444;

font-size: 16px;

}

 

h2 {

color: #183b70;

font-size: 28px;

}

In this example, all paragraphs receive the same style, and all level-two headings receive another.

Class Selector

Classes make it possible to apply the same style to multiple elements. In CSS, a class is prefixed with a period (.), while in HTML it is defined with the class attribute.

HTML:

HTML

 

 

<h2 class="title">Our Services</h2>

 

<p class="highlighted-text">

We provide professional web solutions.

</p>

 

CSS:

CSS

 

 

.title {

color: #183b70;

text-transform: uppercase;

}

 

.highlighted-text {

font-size: 20px;

color: #555;

}

Classes are especially useful when the same visual style is used across different parts of a website.

ID Selector

An ID is used to target a specific HTML element. In CSS, it is prefixed with the hash symbol (#).

HTML

 

 

<section id="contact">

<h2>Contact Us</h2>

</section>

 

CSS

 

 

#contact {

background-color: #edf2f8;

padding: 40px;

}

Unlike a class, an ID should be unique within a single HTML document.

Grouping Selectors

If multiple elements should share the same styles, selectors can be grouped using commas.

CSS

 

 

h1,

h2,

h3 {

font-family: Arial, sans-serif;

color: #183b70;

}

Descendant Selectors

Combined selectors allow you to target elements more precisely.

CSS

 

 

article p {

color: #555;

line-height: 1.7;

}

This rule applies to all paragraphs located inside an <article> element.

Attribute Selectors

CSS can also target elements based on their attributes.

CSS

 

 

input[type="email"] {

border: 1px solid #ccc;

padding: 10px;

}

This selector applies styles only to <input> elements whose type attribute is set to email.

CSS Colors – Defining Text, Background, and Element Colors

Colors have a major impact on the appearance and visual identity of a website. CSS allows colors to be defined in several different ways.

The most commonly used methods are:

  1. Named colors, such as red, blue, and black.
  2. HEX values, such as #183b70.
  3. RGB values, such as rgb(24, 59, 112).
  4. RGBA values, which allow you to define transparency.
  5. HSL and HSLA values, based on hue, saturation, and lightness.

Example:

CSS

 

 

body {

color: #333333;

background-color: #f5f7fa;

}

 

h1 {

color: rgb(24, 59, 112);

}

 

.card {

background-color: rgba(24, 59, 112, 0.08);

}

CSS also allows transparency to be controlled using the opacity property:

CSS

 

 

.image {

opacity: 0.7;

}

A value of 1 represents full visibility, while 0 makes the element completely transparent.

When choosing colors, it is important to consider the contrast between text and background colors. Good contrast improves readability and accessibility, especially for users with visual impairments.

CSS Fonts and Typography – How to Style Text

Typography is one of the most important aspects of quality web design. The choice of font, font size, weight, and line spacing directly affects readability and the overall impression of a webpage.

The most important CSS properties for styling text are:

  1. font-family – defines the font family.
  2. font-size – sets the size of the text.
  3. font-weight – controls the font weight (thickness).
  4. font-style – allows normal or italic text styling.
  5. line-height – defines the spacing between lines of text.
  6. text-align – controls horizontal text alignment.
  7. text-decoration – manages text decorations.
  8. letter-spacing – determines the spacing between characters.

Example:

CSS

 

 

body {

font-family: Arial, Helvetica, sans-serif;

font-size: 16px;

line-height: 1.7;

color: #333;

}

 

h1 {

font-size: 36px;

font-weight: 700;

line-height: 1.2;

text-align: center;

}

 

p {

font-size: 17px;

line-height: 1.8;

}

For modern web projects, relative units such as rem are often used because they make it easier to manage font sizes consistently across an entire website.

Example:

CSS

 

 

html {

font-size: 16px;

}

 

h1 {

font-size: 2.5rem;

}

 

p {

font-size: 1rem;

}

In this example, the heading has a font size that is 2.5 times the base font size, resulting in 40 pixels when the root font size is 16 pixels.

CSS Units of Measurement – px, %, em, rem, vw, and vh

CSS uses various units to define dimensions, spacing, and element sizes. Choosing the right unit is important for building flexible and responsive web pages.

The most commonly used units are:

UnitMeaning and Usage
pxPixels – a fixed unit of measurement
%Percentage relative to a reference value
emRelative unit based on the font size of the current element
remRelative unit based on the root element's font size
vwPercentage of the viewport width
vhPercentage of the viewport height
frFraction of the available space in a CSS Grid layout

Example:

CSS

 

 

.container {

width: 80%;

max-width: 1200px;

margin: 0 auto;

}

 

.heading {

font-size: 2rem;

}

 

.hero {

min-height: 70vh;

}

Percentage-based and relative units allow element dimensions to adapt to their surrounding context. This is especially useful when building websites that need to function properly across a variety of screen sizes and devices.

CSS Margin and Padding – Properly Defining Spacing

margin and padding are two commonly used CSS properties, but they serve different purposes.

margin defines the outer spacing around an element, while padding defines the inner spacing between the content and the element's border.

Example:

CSS

 

 

.card {

margin: 30px;

padding: 20px;

background-color: #f2f5f9;

}

Spacing can also be defined individually for each side:

CSS

 

 

.element {

margin-top: 20px;

margin-right: 15px;

margin-bottom: 30px;

margin-left: 15px;

}

The same applies to padding:

CSS

 

 

.element {

padding-top: 20px;

padding-right: 15px;

padding-bottom: 20px;

padding-left: 15px;

}

CSS also supports shorthand notation:

CSS

 

 

.element {

margin: 10px 20px;

padding: 15px 25px;

}

When two values are specified, the first value applies to the top and bottom sides, while the second value applies to the left and right sides.

You can also use four values to control each side individually:

CSS

 

.element {

margin: 10px 20px 30px 40px;

}

In this case, the values are applied in the following order:

  • Top: 10px
  • Right: 20px
  • Bottom: 30px
  • Left: 40px

Proper use of spacing helps keep content readable, prevents elements from feeling crowded, and creates a balanced, visually appealing layout.

CSS Borders, Border Radius, and Box Shadow – Styling Elements

Borders, rounded corners, and shadows are commonly used to visually distinguish elements such as cards, buttons, images, and forms.

Example of a border:

CSS

 

 

.card {

border: 1px solid #ddd;

}

The border-radius property is used to create rounded corners:

CSS

 

 

.button {

border-radius: 8px;

}

To add a shadow, use the box-shadow property:

CSS

 

 

.card {

background-color: #fff;

border-radius: 12px;

box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);

}

The values used in box-shadow typically represent:

  1. Horizontal offset.
  2. Vertical offset.
  3. Blur radius.
  4. Shadow color.

For example:

CSS

 

 

box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);

means the shadow has:

  • 0 pixels horizontal offset,
  • 4px vertical offset,
  • 16px blur radius,
  • a black color with 8% opacity.

You can combine multiple properties to create modern-looking cards, buttons, and interface components without the need for additional images or graphic elements. Rounded corners and subtle shadows are widely used in contemporary web design to add depth, improve visual hierarchy, and enhance the overall user experience.

CSS Display – How Elements Are Displayed and Positioned

The display property determines how an HTML element behaves within a page layout and how it is rendered by the browser.

The most commonly used values are:

  1. block – the element is displayed as a block and typically occupies the full available width of its parent container.
  2. inline – the element is displayed within a line of text and does not accept width and height in the same way as block elements.
  3. inline-block – the element behaves like an inline element but supports custom width and height.
  4. none – the element is hidden and does not occupy any space in the layout.
  5. flex – enables the Flexbox layout system.
  6. grid – enables the CSS Grid layout system.

Example:

CSS

 

 

.block {

display: block;

}

 

.inline-element {

display: inline;

}

 

.button {

display: inline-block;

}

 

.hidden-element {

display: none;

}

Understanding the display property is essential for organizing content correctly and controlling how elements are positioned within a webpage.

For example:

  • Elements such as <div>, <section>, and <h1> are typically block-level elements by default.
  • Elements such as <span>, <a>, and <strong> are typically inline elements by default.
  • display: none completely removes an element from the layout, making it invisible and preventing it from taking up any space.
  • display: flex and display: grid are widely used in modern web development to create responsive and flexible layouts.

The display property is one of the most important CSS layout tools and serves as the foundation for positioning and organizing content on modern websites.

Ask Copilot

 

 

CSS Flexbox – Flexible Layout for Arranging Elements

Flexbox is a CSS layout system used to arrange elements along a single primary axis, either horizontally or vertically. It is particularly useful for navigation menus, card rows, content alignment, and organizing elements within smaller user interface components.

To enable Flexbox, assign display: flex to the parent element.

Example:

HTML

 

 

<div class="menu">

<a href="/">Home</a>

<a href="/services">Services</a>

<a href="/contact">Contact</a>

</div>

 

CSS

 

 

.menu {

display: flex;

gap: 20px;

justify-content: center;

align-items: center;

}

The most important Flexbox properties are:

  1. display: flex – activates the Flexbox layout.
  2. flex-direction – defines the direction in which items are arranged.
  3. justify-content – controls the distribution of items along the main axis.
  4. align-items – aligns items along the cross axis.
  5. flex-wrap – allows items to wrap onto a new line when necessary.
  6. gap – defines the spacing between items.

Example of a card layout:

HTML

 

 

<div class="cards">

<div class="card">HTML</div>

<div class="card">CSS</div>

<div class="card">JavaScript</div>

</div>

 

CSS

 

 

.cards {

display: flex;

gap: 20px;

flex-wrap: wrap;

}

 

.card {

flex: 1 1 200px;

padding: 25px;

background-color: #edf2f8;

border-radius: 10px;

}

In this example, the cards are arranged in rows and automatically wrap onto a new line when there is not enough available space. This makes Flexbox an excellent choice for creating flexible and responsive layouts that adapt to different screen sizes.

CSS Grid – Arranging Elements in Rows and Columns

CSS Grid is a two-dimensional layout system for arranging elements in rows and columns. Unlike Flexbox, which is primarily designed for one-dimensional layouts, Grid gives you simultaneous control over both rows and columns.

This makes it particularly useful for page layouts, image galleries, product grids, and more complex sections of modern websites.

Example:

HTML

 

 

<div class="gallery">

<div>Image 1</div>

<div>Image 2</div>

<div>Image 3</div>

<div>Image 4</div>

</div>

 

CSS

 

 

.gallery {

display: grid;

grid-template-columns: repeat(2, 1fr);

gap: 20px;

}

 

.gallery div {

padding: 40px;

background-color: #e9eef5;

text-align: center;

}

In this example, two equal-width columns are created, and the elements are automatically arranged within the grid.

For a more flexible layout, you can use:

CSS

 

 

.gallery {

display: grid;

grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));

gap: 20px;

}

This approach allows the number of columns to automatically adjust according to the available width, eliminating the need to write separate CSS rules for different column counts.

Some of the most important CSS Grid properties include:

  1. display: grid – activates the Grid layout system.
  2. grid-template-columns – defines the number and size of columns.
  3. grid-template-rows – defines the number and size of rows.
  4. gap – sets the spacing between rows and columns.
  5. grid-column – controls how an item spans across columns.
  6. grid-row – controls how an item spans across rows.
  7. justify-items – aligns items horizontally within their grid cells.
  8. align-items – aligns items vertically within their grid cells.

CSS Grid is one of the most powerful layout tools available in modern web development and is ideal for creating responsive, organized, and complex page structures.

CSS Positioning – position, top, right, bottom, and left

CSS allows precise positioning of elements through the position property.

The main values are:

  1. static – the default positioning behavior of elements.
  2. relative – the element remains in the normal document flow but can be moved relative to its original position.
  3. absolute – the element is positioned relative to its nearest positioned ancestor.
  4. fixed – the element is positioned relative to the viewport and remains in place while scrolling.
  5. sticky – the element behaves like a relatively positioned element until it reaches a specified threshold, after which it remains fixed in that position while scrolling within its container's limits.

Example of relative and absolute positioning:

HTML

 

 

<div class="parent">

<span class="badge">New</span>

<p>Product or Service</p>

</div>

 

CSS

 

 

.parent {

position: relative;

padding: 30px;

background-color: #edf2f8;

}

 

.badge {

position: absolute;

top: 10px;

right: 10px;

background-color: #183b70;

color: white;

padding: 5px 10px;

}

In this example, the parent element serves as the reference point for positioning the badge. This technique is commonly used for badges, labels, icons, notifications, and other small interface elements placed within cards or containers.

You can also use left and bottom to control positioning:

CSS

 

 

.element {

position: absolute;

bottom: 20px;

left: 20px;

}

Example of fixed positioning:

CSS

 

 

.back-to-top {

position: fixed;

bottom: 20px;

right: 20px;

}

This keeps the element in the same location on the screen even when the user scrolls the page.

Example of sticky positioning:

CSS

 

 

.navigation {

position: sticky;

top: 0;

background-color: white;

}

This allows the navigation bar to scroll normally at first and then remain visible at the top of the screen once it reaches the specified position.

Understanding CSS positioning is important for creating navigation menus, badges, banners, overlays, floating buttons, and many other interface components used in modern web design.

CSS Pseudo-Classes and Pseudo-Elements

CSS pseudo-classes and pseudo-elements provide additional ways to style elements based on their state or specific parts of their content.

Pseudo-Classes

Pseudo-classes represent a special state of an element. One of the most commonly used pseudo-classes is :hover, which becomes active when a user moves the mouse pointer over an element.

Example:

CSS

 

 

.button {

background-color: #183b70;

color: white;

padding: 12px 24px;

border-radius: 6px;

}

 

.button:hover {

background-color: #28589a;

}

Other useful pseudo-classes include:

  1. :focus – when an element receives focus.
  2. :active – while an element is being activated.
  3. :first-child – when an element is the first child of its parent.
  4. :last-child – when an element is the last child of its parent.
  5. :nth-child() – selects elements according to their position.
  6. :checked – targets checked checkbox and radio inputs.

Example:

CSS

 

 

li:nth-child(even) {

background-color: #f2f5f9;

}

This rule applies a style to every even item in a list.

Pseudo-Elements

Pseudo-elements make it possible to style a specific part of an element or add decorative content without modifying the HTML markup.

The most commonly used pseudo-elements are ::before, ::after, ::first-letter, and ::first-line.

Example:

CSS

 

 

.heading::after {

content: "";

display: block;

width: 60px;

height: 3px;

background-color: #183b70;

margin-top: 10px;

}

This rule adds a decorative line after the heading without requiring an additional HTML element.

Another example:

CSS

 

 

p::first-letter {

font-size: 2rem;

font-weight: bold;

}

This styles only the first letter of a paragraph, a technique often used in articles and editorial layouts.

Pseudo-classes and pseudo-elements are powerful CSS features that help create more interactive, visually appealing, and maintainable user interfaces while keeping HTML markup clean and semantic.

CSS Responsive Design – Adapting Websites for Mobile Phones and Tablets

Responsive web design is the practice of building websites that display correctly on devices with different screen sizes. The same content should remain clear and usable on large desktop monitors, laptops, tablets, and smartphones.

CSS allows layouts to adapt through the use of media queries.

Example:

CSS

 

 

.container {

width: 90%;

max-width: 1200px;

margin: 0 auto;

}

 

h1 {

font-size: 36px;

}

 

@media (max-width: 768px) {

h1 {

font-size: 28px;

}

 

.container {

width: 95%;

}

}

In this example, the heading size is reduced when the viewport width reaches 768 pixels or less.

You can also adapt card layouts:

CSS

 

 

.cards {

display: grid;

grid-template-columns: repeat(3, 1fr);

gap: 20px;

}

 

@media (max-width: 900px) {

.cards {

grid-template-columns: repeat(2, 1fr);

}

}

 

@media (max-width: 600px) {

.cards {

grid-template-columns: 1fr;

}

}

On wide screens, three columns are displayed. On medium-sized screens, the layout changes to two columns, and on smaller screens, it switches to a single-column layout.

In addition to media queries, responsive design commonly relies on:

  • flexible units such as %, em, rem, vw, and vh;
  • percentage-based widths;
  • Flexbox layouts;
  • CSS Grid layouts;
  • properties such as min-width, max-width, and clamp().

By combining these techniques, developers can create websites that provide a consistent and user-friendly experience across a wide range of devices and screen sizes.

CSS Variables – Easier Style Management

CSS variables, also known as custom properties, allow you to define reusable values that can be referenced throughout your CSS code.

They are especially useful when a website has a predefined color palette, typography system, spacing scale, or other shared design values.

Example:

CSS

 

 

:root {

--primary-color: #183b70;

--secondary-color: #f2f5f9;

--text-color: #333333;

--spacing: 20px;

--border-radius: 8px;

}

 

h1 {

color: var(--primary-color);

}

 

.card {

background-color: var(--secondary-color);

padding: var(--spacing);

border-radius: var(--border-radius);

}

 

p {

color: var(--text-color);

}

If you later decide to change the primary color across the entire website, you only need to update the value in the :root block. Every declaration that uses that variable will automatically reflect the new color.

Variables can also be used with fallback values:

CSS

 

 

.button {

background-color: var(--primary-color, #0044cc);

}

In this example, if --primary-color is not defined, the browser will use #0044cc as the fallback value.

CSS variables improve code consistency, simplify maintenance, reduce repetition, and make it easier to implement design systems, themes, and dark mode functionality.

CSS Transitions and Animations – Adding Motion and Interactivity to Elements

CSS makes it possible to create visual effects and motion without relying on JavaScript. Transitions are used for smooth changes between states, while animations allow you to define more complex sequences of visual changes.

Transitions

Transitions create a gradual change when a CSS property is modified.

Example of changing a button's background color when the user hovers over it:

CSS

 

 

.button {

background-color: #183b70;

color: white;

transition: background-color 0.3s ease;

}

 

.button:hover {

background-color: #28589a;

}

 

In this example, the color change does not happen instantly. Instead, it smoothly transitions over 0.3 seconds.

The transition property typically consists of:

  1. The property being animated.
  2. The animation duration.
  3. The timing function.
  4. An optional delay.

Example:

CSS

 

 

.card {

transition: transform 0.4s ease, box-shadow 0.4s ease;

}

 

.card:hover {

transform: translateY(-5px);

box-shadow: 0 10px 24px rgba(0, 0, 0, 0.12);

}

CSS Animations

For more advanced effects, CSS provides the @keyframes rule, which defines the key stages of an animation.

Example:

CSS

 

 

@keyframes fadeIn {

from {

opacity: 0;

transform: translateY(20px);

}

 

to {

opacity: 1;

transform: translateY(0);

}

}

 

.card {

animation: fadeIn 0.6s ease;

}

In this example, the card gradually appears while moving upward into its final position.

The animation shorthand property can define:

  1. Animation name.
  2. Duration.
  3. Timing function.
  4. Delay.
  5. Iteration count.
  6. Direction.
  7. Fill mode.

Example:

CSS

 

 

.spinner {

animation: rotate 2s linear infinite;

}

 

@keyframes rotate {

from {

transform: rotate(0deg);

}

 

to {

transform: rotate(360deg);

}

}

This creates a continuous rotation effect.

When to Use Animations

Animations can improve user experience by:

  • highlighting important actions;
  • providing visual feedback;
  • drawing attention to interactive elements;
  • making interfaces feel smoother and more polished.

However, animations should be used sparingly. Excessive motion can distract users and interfere with reading and navigation.

For accessibility, it is recommended to respect users who prefer reduced motion by using the prefers-reduced-motion media query:

CSS

 

 

@media (prefers-reduced-motion: reduce) {

* {

animation: none;

transition: none;

}

}

This helps create a more comfortable experience for users who are sensitive to motion effects.

CSS Cascade, Inheritance, and Specificity – Why Aren't Some Styles Applied?

One of the defining characteristics of CSS is its cascading mechanism for determining final styles. When multiple CSS rules target the same element and define the same property, the browser must decide which rule takes precedence.

The result is influenced by the origin and importance of declarations, selector specificity, source order, and special rules related to inheritance.

Example:

CSS

 

 

p {

color: blue;

}

 

.text {

color: green;

}

 

#intro {

color: red;

}

HTML:

HTML

 

 

<p id="intro" class="text">

This is an example of CSS specificity.

</p>

 

In this case, the text will be red because the ID selector has a higher specificity than both the class selector and the element selector.

Inheritance

Inheritance means that certain CSS properties can be passed from a parent element to its child elements.

Example:

CSS

 

 

body {

font-family: Arial, sans-serif;

color: #333;

}

Text-based elements inside the <body> element inherit the defined font family and text color unless a different rule has been explicitly applied to them.

Specificity Priority

When multiple selectors target the same element, CSS generally follows this order of specificity:

  1. Inline styles (style="")
  2. ID selectors (#id)
  3. Class selectors (.class), attribute selectors, and pseudo-classes
  4. Element selectors (p, h1, div)
  5. Universal selector (*)

Example:

CSS

 

 

p {

color: blue;

}

 

.article p {

color: green;

}

 

#intro {

color: red;

}

The element with the ID intro will appear red because the ID selector has the highest specificity.

Source Order

When selectors have the same specificity, the rule that appears later in the stylesheet wins.

Example:

CSS

 

 

p {

color: blue;

}

 

p {

color: green;

}

The text will be green because the second declaration overrides the first.

The !important Rule

CSS also provides the !important keyword, which can override normal specificity rules.

CSS

 

 

p {

color: blue !important;

}

 

#intro {

color: red;

}

In this example, the paragraph will be blue because the declaration marked with !important takes precedence over regular rules.

However, !important should be used sparingly, as excessive use can make stylesheets more difficult to maintain and debug.

Understanding the cascade, inheritance, and specificity makes it much easier to diagnose styling issues and determine why a particular style is not being applied as expected.

CSS Practical Project – Building a Modern Service Card

The best way to test how well you understand CSS is by creating a small project. In the following example, we will build a service card that can be used to showcase a service, product, or featured piece of information on a website.

HTML:

HTML

 

 

<div class="service-card">

 

<span class="badge">Popular</span>

 

<h2>Website Development</h2>

 

<p>

Professional development of modern websites

optimized for different devices.

</p>

 

<a href="/services/website-development" class="button">

Learn More

</a>

 

</div>

 

CSS:

CSS

 

 

*,

*::before,

*::after {

box-sizing: border-box;

}

 

body {

font-family: Arial, sans-serif;

background-color: #f3f5f8;

color: #333;

padding: 40px;

}

 

.service-card {

position: relative;

max-width: 380px;

margin: 0 auto;

padding: 35px;

background-color: #fff;

border: 1px solid #e5e8ed;

border-radius: 14px;

box-shadow: 0 8px 25px rgba(0, 0, 0, 0.06);

}

 

.service-card h2 {

color: #183b70;

font-size: 25px;

margin-bottom: 15px;

}

 

.service-card p {

font-size: 16px;

line-height: 1.7;

margin-bottom: 25px;

}

 

.badge {

display: inline-block;

position: absolute;

top: -12px;

right: 20px;

padding: 6px 12px;

background-color: #183b70;

color: #fff;

border-radius: 20px;

font-size: 12px;

}

 

.button {

display: inline-block;

padding: 12px 20px;

background-color: #183b70;

color: #fff;

text-decoration: none;

border-radius: 6px;

transition: background-color 0.3s ease;

}

 

.button:hover {

background-color: #28589a;

}

 

This project combines several CSS concepts you have learned, including selectors, colors, typography, padding, margin, borders, border-radius, box-shadow, positioning, and transitions.

You can further enhance it by adding an image, changing the color palette, creating multiple cards, and arranging them using Flexbox or CSS Grid.

Common Mistakes When Writing CSS Code

When learning CSS, beginners often encounter issues that are not caused by complex programming concepts, but rather by incorrect syntax, improper selector usage, or a misunderstanding of how CSS processes rules.

Pay attention to the following examples:

1. Missing Semicolons Between Declarations

Incorrect:

CSS

 

 

p {

color: #333

font-size: 18px;

}

Correct:

CSS

 

 

p {

color: #333;

font-size: 18px;

}

2. Incorrect Use of Class and ID Selectors

If HTML contains class="title", the CSS selector must begin with a period (.):

CSS

 

 

.title {

color: blue;

}

For id="title", use the hash symbol (#):

CSS

 

 

#title {

color: blue;

}

3. Incorrectly Defined Dimensions

Example of an invalid rule:

CSS

 

 

.card {

width: 300;

}

Correct:

CSS

 

 

.card {

width: 300px;

}

For most numeric dimensions, you need to specify an appropriate unit of measurement. Common exceptions include the value 0 and certain properties that accept unitless values, such as line-height and font-weight.

4. Overusing !important

CSS

 

 

.title {

color: red !important;

}

The !important keyword overrides the normal CSS cascade and can make future maintenance more difficult. Instead of relying on it frequently, it is usually better to organize selectors properly and understand CSS specificity.

5. Not Optimizing for Mobile Devices

Elements with fixed widths can cause horizontal scrolling on smaller screens.

Instead of:

CSS

 

 

.container {

width: 1200px;

}

It is often better to use:

CSS

 

 

.container {

width: 90%;

max-width: 1200px;

margin: 0 auto;

}

6. Poor CSS Organization

A large number of unrelated rules, repeated declarations, and overly complex selectors can make a project difficult to maintain.

To keep stylesheets manageable:

  • Organize styles into logical sections.
  • Use consistent class naming conventions.
  • Avoid unnecessary selector complexity.
  • Reuse common values through CSS variables.
  • Group related styles together whenever possible.

Writing clean, organized CSS from the beginning makes development faster, debugging easier, and long-term maintenance significantly more manageable.

How to Write Clean, Organized, and High-Quality CSS

Good CSS is not just code that produces the desired visual result. It should also be organized, readable, consistent, and easy to maintain and update as a project grows.

When working on a web project, it is helpful to follow these best practices:

  1. Use an external CSS file for shared styles.
  2. Organize your styles into logical sections and components.
  3. Use classes for reusable styling.
  4. Avoid unnecessarily complex selectors.
  5. Limit the use of !important.
  6. Use CSS variables for colors, spacing, and other shared values.
  7. Apply box-sizing: border-box for more predictable sizing and layout calculations.
  8. Use Flexbox and CSS Grid instead of outdated layout techniques.
  9. Test your design on different screen sizes and devices.
  10. Pay attention to contrast, readability, and accessibility.

In larger projects, it is often beneficial to organize CSS by components, pages, or functional sections. This approach helps ensure that changes made to one part of the design do not unintentionally affect other areas of the website.

A simple example of organized CSS might look like this:

CSS

 

 

/* Variables */

:root {

--primary-color: #183b70;

--text-color: #333;

--spacing: 20px;

}

 

/* Global styles */

body {

font-family: Arial, sans-serif;

color: var(--text-color);

}

 

/* Buttons */

.button {

padding: 12px 20px;

background-color: var(--primary-color);

color: white;

}

 

/* Cards */

.card {

padding: var(--spacing);

border-radius: 10px;

}

By keeping CSS structured and consistent from the beginning, you make development faster, maintenance easier, and collaboration with other developers much more efficient.

CSS and SEO – How Does High-Quality CSS Affect a Website?

CSS is not a direct ranking factor in the same way as content quality, page relevance, or backlinks. However, well-written CSS can indirectly contribute to a website's overall quality and user experience.

Well-organized CSS helps ensure that a website is visually clear, easy to read, and functional across different devices. This is especially important because a growing number of users access the web through mobile phones and tablets.

Particular attention should be paid to the following areas:

Responsive Design

A website should be easy to use on mobile devices, without unnecessary horizontal scrolling or overlapping elements.

Performance and Page Loading Speed

Large CSS files, unnecessary rules, and unoptimized resources can negatively affect page loading times. Well-structured CSS, removal of unused styles, and optimized resource delivery can contribute to better performance.

Readability and Accessibility

Appropriate font sizes, sufficient color contrast, clearly distinguishable links, and visible focus states make a website easier to use for a wide range of users, including those with accessibility needs.

Stable Layouts

Properly defined dimensions for images, containers, and interface components can help prevent unexpected layout shifts while the page is loading.

High-quality CSS is therefore important for much more than appearance alone. It is a key part of the technical foundation of a well-built website, alongside proper HTML structure, optimized resources, and valuable content.

How to Continue Learning CSS?

Once you have mastered the basic CSS properties and selectors, the next step is to gain a deeper understanding of layout systems, responsive design, and more advanced CSS features.

A recommended learning path is:

  1. Basic CSS syntax and selectors.
  2. Colors, fonts, and text-related properties.
  3. CSS units of measurement and the Box Model.
  4. Margin, padding, borders, and element dimensions.
  5. Display and basic layout behaviors.
  6. Flexbox for flexible layouts.
  7. CSS Grid for row-and-column layouts.
  8. Positioning and layout control with position.
  9. Pseudo-classes and pseudo-elements.
  10. Responsive design and media queries.
  11. CSS variables, transitions, and animations.
  12. CSS optimization, organization, and maintainability.

After CSS, it is recommended to continue learning JavaScript, which will enable you to add interactivity, process user input, and build dynamic functionality into your web projects.

CSS is much more than changing colors and fonts. Once you understand its core principles, you can create complex layouts, responsive components, and a consistent visual identity across an entire website or application. That is why CSS remains one of the most important skills for every frontend developer.