You are currently viewing Beginner’s Guide to Learning CSS: Cascading Style Sheets Tutorial

Beginner’s Guide to Learning CSS: Cascading Style Sheets Tutorial

  • Post author:
  • Post category:CSS
  • Post comments:0 Comments
  • Post last modified:May 16, 2024

Introduction to CSS

Cascading Style Sheets (CSS) is a fundamental technology used to style web pages. In this tutorial, you’ll learn the basics of CSS, including syntax, selectors, properties, and how to apply styles to HTML elements.

Getting Started with CSS

To get started with CSS, you’ll need a basic understanding of HTML. If you’re new to HTML, it’s recommended to learn HTML first before diving into CSS. Once you’re comfortable with HTML, you can begin adding styles with CSS.

CSS Syntax

CSS consists of a selector and one or more declarations enclosed in curly braces { }. Each declaration includes a property and a value separated by a colon :. Here’s an example of CSS syntax:

selector {
    property: value;
}

CSS Selectors

Selectors are used to target HTML elements that you want to style. There are various types of selectors in CSS, including element selectors, class selectors, ID selectors, and more. Here are some examples:

  • Element Selector:
p {
    color: blue;
}
  • Class Selector:
.my-class {
    font-size: 16px;
}
  • ID Selector:
#my-id {
    background-color: yellow;
}

CSS Properties

CSS properties define the visual appearance of HTML elements. There are numerous CSS properties available to control things like colors, fonts, margins, padding, and more. Here are a few examples:

  • color: Sets the text color.
  • font-size: Sets the font size.
  • background-color: Sets the background color.
  • margin: Sets the margin around an element.
  • padding: Sets the padding inside an element.

Applying CSS Styles

You can apply CSS styles to HTML elements using three different methods: inline styles, internal styles, and external stylesheets.

  • Inline Styles:
<p style="color: red;">This is a paragraph with inline style.</p>
  • Internal Styles:
<head>
    <style>
        p {
            color: blue;
        }
    </style>
</head>
  • External Stylesheets:
    Create a separate CSS file (e.g., style.css) and link it to your HTML file:
<head>
    <link rel="stylesheet" href="style.css">
</head>

CSS Code Examples

Let’s take a look at some CSS code examples to illustrate how styles can be applied:

/* Style all paragraphs */
p {
    font-size: 18px;
    color: #333;
}

/* Style a specific class */
.my-class {
    background-color: lightblue;
    padding: 10px;
}

/* Style an element with a specific ID */
#my-id {
    border: 1px solid black;
    margin: 20px;
}

Conclusion

Congratulations! You’ve now learned the basics of CSS. With these fundamentals, you can start styling your web pages and creating visually appealing designs. Practice writing CSS code, experiment with different styles, and continue to expand your knowledge. Happy coding!

Leave a Reply