HatriBytesPRO to go ad free is coming soon till then Experiment with HTML code in real-time with HatriBytes Playground Code Now!
Advertisement

CSS Basics: A Beginner's Guide

Welcome back to Hatribytes! Today, we're going to explore the basics of CSS, a powerful tool that brings style and visual appeal to your web pages. Whether you're a beginner or need a refresher, this guide will help you understand the foundational elements of CSS.

What is CSS?

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. CSS controls the layout, colors, fonts, and overall appearance of your web pages.

How to Add CSS to Your HTML

There are three ways to add CSS to your HTML:

  • Inline CSS: Uses the style attribute inside HTML tags.
  • 
    <h1 style="color: blue;">This is a heading</h1>
            
  • Internal CSS: Uses a <style> tag inside the <head> section of the HTML document.
  • 
    <head>
        <style>
            h1 {
                color: blue;
            }
        </style>
    </head>
            
  • External CSS: Links an external CSS file using the <link> tag inside the <head> section.
  • 
    <head>
        <link rel="stylesheet" href="styles.css">
    </head>
            

CSS Selectors

CSS selectors are used to select HTML elements you want to style. Here are some basic types:

  • Element Selector: Selects elements by their tag name.
  • 
    h1 {
        color: blue;
    }
            
  • Class Selector: Selects elements by their class attribute. Use a dot (.) before the class name.
  • 
    <p class="intro">This is an introduction.</p>
            
    
    .intro {
        font-size: 20px;
    }
            
  • ID Selector: Selects an element by its id attribute. Use a hash (#) before the id name.
  • 
    <h1 id="main-heading">Main Heading</h1>
            
    
    #main-heading {
        text-align: center;
    }
            
  • Universal Selector: Selects all elements on the page.
  • 
    * {
        margin: 0;
        padding: 0;
    }
            

CSS Properties

CSS properties are used to style HTML elements. Here are some commonly used properties:

  • Color: Sets the text color.
  • 
    p {
        color: green;
    }
            
  • Background: Sets the background color or image.
  • 
    body {
        background-color: #f0f0f0;
    }
            
  • Font: Sets the font size, family, and style.
  • 
    p {
        font-size: 16px;
        font-family: Arial, sans-serif;
        font-weight: bold;
    }
            
  • Margin: Sets the space outside the element's border.
  • 
    h1 {
        margin: 20px;
    }
            
  • Padding: Sets the space inside the element's border.
  • 
    div {
        padding: 10px;
    }
            
  • Border: Sets the border around an element.
  • 
    p {
        border: 1px solid black;
    }
            

Box Model

The CSS box model describes the rectangular boxes generated for elements in the document tree and consists of: margins, borders, padding, and the actual content. Here's an illustration:


div {
    width: 300px;
    padding: 10px;
    border: 5px solid gray;
    margin: 20px;
}
        

CSS Layout

CSS provides several properties to control the layout of elements:

  • Display: Specifies how an element is displayed.
  • 
    p {
        display: none; /* Hides the element */
    }
            
  • Position: Specifies the positioning method of an element (static, relative, absolute, fixed, or sticky).
  • 
    div {
        position: relative;
        top: 10px;
        left: 20px;
    }
            
  • Flexbox: Provides a more efficient way to lay out, align, and distribute space among items in a container.
  • 
    .container {
        display: flex;
        justify-content: center;
        align-items: center;
    }
            
  • Grid: Provides a grid-based layout system, with rows and columns.
  • 
    .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        gap: 10px;
    }
            

Responsive Design

Responsive design ensures that your web pages look good on all devices. You can achieve this by using:

  • Media Queries: Apply different styles for different screen sizes.
  • 
    @media (max-width: 600px) {
        .container {
            flex-direction: column;
        }
    }
            

Conclusion

CSS is a powerful tool that can greatly enhance the visual appeal of your web pages. With the basics covered in this guide, you're well on your way to creating beautiful and responsive websites.

Stay tuned to Hatribytes for more tutorials on JavaScript and Python to further advance your web development skills. Happy styling!

Feel free to ask any questions or leave comments below. Let's learn and grow together!

About the Author

The founder of Xtaverse Global and Hatri Technologies.

Post a Comment

Advertisement Why Ads?
Advertisement Why Ads?
Advertisement Why Ads?
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.