DEV Community

Cover image for CSS Tutorial for Beginners: Learn How to Style Your Web Pages
Rishabh parmar
Rishabh parmar

Posted on

CSS Tutorial for Beginners: Learn How to Style Your Web Pages

After mastering HTML, the next essential step in your web development journey is learning CSS. While HTML creates the structure of a web page, CSS (Cascading Style Sheets) brings that structure to life with colors, layouts, fonts, and animations. In this CSS tutorial, you’ll learn how to make your website visually appealing and user-friendly.

Whether you’re building a personal blog or aspiring to become a front-end developer, this complete CSS tutorial will guide you through the basics and set a strong foundation for more advanced design techniques.

What is CSS?
CSS, short for Cascading Style Sheets, is a styling language used to control the appearance of HTML elements on a web page. It defines how elements like headings, paragraphs, buttons, and layouts look across different devices.

Here’s a simple example:

css
Copy
Edit
h1 {
color: blue;
font-size: 36px;
}
This code makes all

headings blue and sets the font size to 36 pixels.

Why Learn CSS?
Before diving deeper into this CSS tutorial, let’s understand why CSS is so important:

Visual Design: CSS controls color schemes, typography, spacing, and layout.

Responsive Web Design: Make your site look good on mobile, tablet, and desktop.

Efficiency: Apply styles to multiple elements or pages with one stylesheet.

Separation of Concerns: Keep content (HTML) and style (CSS) separate for cleaner code.

How to Add CSS to Your HTML
There are three ways to apply CSS in your HTML file:

  1. Inline CSS Styles are written inside HTML elements using the style attribute.

html
Copy
Edit

This is a green paragraph.

  1. Internal CSS CSS is placed within the tag in the HTML <head> section.</li> </ol> <p>html<br> Copy<br> Edit<br> <head><br> <style><br> p {<br> color: green;<br> }<br>
    1. External CSS The best and most scalable way. CSS is written in a separate file (e.g., style.css) and linked using:

    html
    Copy
    Edit

    CSS Syntax Explained
    CSS follows a straightforward syntax:

    css
    Copy
    Edit
    selector {
    property: value;
    }
    For example:

    css
    Copy
    Edit
    body {
    background-color: #f5f5f5;
    font-family: Arial, sans-serif;
    }
    Selector: The HTML element you want to style (e.g., body, p, h1).

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

    Value: The setting you give to the property (e.g., blue, 16px, auto).

    Common CSS Properties
    Here are some essential CSS properties you’ll often use:

    Property Description
    color Text color
    background-color Background color
    font-size Size of the text
    font-family Typeface used
    margin Space outside the element
    padding Space inside the element
    border Border around the element
    width / height Set element size
    display Layout behavior (block, inline, flex)
    text-align Alignment of text

    Selectors in CSS
    CSS allows you to target specific elements using various selectors:

    1. Element Selector Targets all elements of a specific type.

    css
    Copy
    Edit
    p {
    color: gray;
    }

    1. Class Selector Targets elements with a specific class.

    css
    Copy
    Edit
    .intro {
    font-style: italic;
    }
    HTML:

    html
    Copy
    Edit

    This is an introduction.

    1. ID Selector Targets a unique element by its ID.

    css
    Copy
    Edit

    header {

    background-color: navy;
    color: white;
    }
    HTML:

    html
    Copy
    Edit

    Welcome!

    1. Group Selector Apply the same style to multiple elements.

    css
    Copy
    Edit
    h1, h2, h3 {
    font-family: 'Verdana';
    }
    CSS Box Model
    Understanding the box model is key to mastering layout in this CSS tutorial.

    Every HTML element is a box made up of:

    Content – The text or image.

    Padding – Space between content and border.

    Border – Surrounds the padding.

    Margin – Space outside the border.

    Example:

    css
    Copy
    Edit
    div {
    padding: 20px;
    border: 1px solid black;
    margin: 10px;
    }
    CSS Layout Techniques

    1. Flexbox Flexbox allows responsive, flexible layouts with minimal code.

    css
    Copy
    Edit
    .container {
    display: flex;
    justify-content: space-between;
    }

    1. Grid CSS Grid is perfect for 2D layouts (rows and columns).

    css
    Copy
    Edit
    .grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    }

    1. Positioning Control where elements appear:

    static (default)

    relative

    absolute

    fixed

    sticky

    Example: A Styled Web Page
    Here’s a quick example combining HTML and external CSS:

    HTML (index.html):

    html
    Copy
    Edit
    <!DOCTYPE html>





    My Profile







    Jane Doe

    Web developer with a passion for clean design.

    CSS (style.css):

    css
    Copy
    Edit
    body {
    background-color: #e6f7ff;
    font-family: 'Arial';
    padding: 20px;
    }

    .profile {
    background-color: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }

    .bio {
    color: #555;
    }
    Best Practices for Writing CSS
    Keep styles in external files for reusability.

    Use meaningful class and ID names.

    Avoid overusing !important.

    Use shorthand properties (e.g., margin: 10px 20px;).

    Comment your code for clarity.

    What’s Next After This CSS Tutorial?
    After finishing this beginner-friendly CSS tutorial, here’s where you can go next:

    Responsive Design: Use media queries to make your site mobile-friendly.

    CSS Frameworks: Learn Bootstrap or Tailwind CSS for faster development.

    CSS Preprocessors: Try SASS or LESS to write cleaner, modular CSS.

    CSS Animations: Create transitions and keyframe animations.

    Advanced Layouts: Master Flexbox and Grid deeply.

    Final Thoughts
    This CSS tutorial has introduced you to the world of web styling, from basic syntax to layouts and best practices. With HTML and CSS together, you're now capable of creating beautiful, functional websites that work across devices.

    As with any skill, mastering CSS takes practice. Don’t be afraid to experiment, break things, and build small projects. With time, you’ll develop the eye and intuition for great web design.

    So open your text editor, create your stylesheet, and start styling your first website today!

Top comments (1)

Collapse
 
aman_kureshi_ profile image
Aman Kureshi

Good information !