Description

CSS Tutorials with Example Explanation

UI Design Evolution

CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.

  • HTML adds Structure to a web page.
  • JavaScript adds logic to it and CSS makes it visually appealing or stylish. It controls the layout of a web page i.e. how HTML elements will be displayed on a webpage.

CSS was released (in 1996), 3 years after HTML (in 1993). The main idea behind its use is that it allows the separation of content (HTML) from presentation (CSS). This makes websites easier to maintain and more flexible.

How to Add CSS to HTML?

There are three different ways to add CSS styles to an HTML document:

1. Inline CSS

Use the style attribute on the HTML elements to add styles to the web page. It is used for small projects.

<!-- File name: index.html -->
<html>
<body>  
    <!-- Using Inline CSS -->
    <h3 style="text-align: center;">
          Welcome To GFG
     </h3>
  
    <p>CSS Tutorial - GeeksforGeeks</p>
</body>
</html>

2. Internal CSS

Place the CSS styles within a <style> tag inside the HTML file, usually inside the <head> Section.

<!-- File name: index.html -->
<html>
<head>    
      <!-- Using Internal CSS -->
    <style>
        h3 {
            color: green;
        }
    </style>
</head>
<body>  
    <!-- CSS is applied here -->
    <h3>Welcome To GFG</h3>
  
    <p>CSS Tutorial - GeeksforGeeks</p>
</body>
</html>

3. External CSS

Create a separate CSS file with a .css extension and link this file to your HTML file using the <link> tag in header section. It consider the best practice to add CSS into HTML File

<!-- File name: index.html -->
<html>
<head>
  	<!-- Importing External CSS -->
    <link rel="stylesheet" href="style.css" /> 
</head>
<body>
    <p>CSS Tutorial - GeeksforGeeks</p>
</body>
</html>
/* Write CSS Here *//* External CSS */
/* File name: style.css */
p {
  	text-align: center;
}

Adding Animations in CSS

CSS allows you to animate HTML elements using the @keyframes rule. Let’s see how you can create a simple animation for a <p> tag. Let's understand this with the help of example:

<html>
    <head>
    <style>
        /* Applying the animation to the <p> tag */
        p {
            animation-duration: 3s; /* Time for one cycle */
            animation-name: slide-in; /* Name of the animation */
            animation-iteration-count: infinite; /* Animation repeats indefinitely */
            animation-direction: alternate; /* Alternate the direction of the animation */
        }

        /* Keyframes for slide-in effect */
        @keyframes slide-in {
            0% {
                transform: translateX(-100%); /* Start from the left */
            }
            100% {
                transform: translateX(100%); /* Move to the right */
            }
        }
    </style>
</head>
<body>
    <h3 style="text-align: center;">Welcome To GFG</h3>
    <p>CSS Tutorial - GeeksforGeeks</p>
</body>
</html>

In this example

  • animation-duration: 3s;: The animation will last 3 seconds for each cycle.
  • animation-name: slide-in;: The name of the animation defined by @keyframes (which is "slide-in").
  • animation-iteration-count: infinite;: The animation repeats infinitely.
  • animation-direction: alternate;: The animation will alternate between moving left and right.

CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their IDclass, type attributes, and more. There are mainly 5 types of selectors.

  • Basic CSS Selectors: These are used to target elements by tag.class, or # ID for fundamental styling needs.
  • Combinators: Ideal for styling elements based on their DOM relationships (e.g., parent-child or sibling relationships).
  • Group Selectors: These are used to apply the same styles to multiple unrelated elements simultaneously.
  • Attribute Selectors: Perfect for styling elements based on specific attributes or values, such as form inputs or links with certain prefixes or states.
  • Pseudo-Classes: Best for styling elements dynamically or interactively, like: hover for user interaction, or:nth-child() for structural styling.

Types of CSS Selectors

Basic Selectors

Basic selectors in CSS are simple tools used to target specific HTML elements for styling. These include selecting by element name (e.g., h1), class (.class Name), ID (#idName), or universally (* for all elements).

1. Universal Selector (*): Selects all elements on the page and applies the same style universally. For example, setting the font color for every element.

Example:   * {
            color: red;
        }

2.Elements Selectors: Targets all elements of a specific type, such as paragraphs or headers. For example, setting a common font size for all paragraphs

Example:   <style>
        p {
            font-size: 16px;
        }
    </style>

 

3. class: Applies styles to elements with a specific class attribute For instance, making all buttons have a blue background.

Example:

<style>
        .button {
            background-color: blue;
            color: white;
        }
    </style>

4. ID Selector(#): Styles a single element identified by its unique idFor example, changing the background color of a header.

<style>
        #header {
            background-color: gray;
        }
    </style>

Combinator Selectors

ond in CSS are used to define relationships between selectors, allowing you to style elements based on their hierarchy or positioning in the document. Common combinators include descendant ( ), child (>), adjacent sibling (+), and general sibling (~).

1. Descendent Selector: Targets an element inside another, such as paragraphs inside div .For example, styling paragraphs inside a div.

Example:

<style>
        div p {
            color: red;
        }
    </style>

 

2. Chield SelectorThey only affects the directchield elements of parents of a parent. For example, styling direct children paragraphs of a div.

Example:

 <style>
        div>p {
            margin-left: 20px;
        }
    </style>

Your browser does not support PDFs. Download PDF.