Introduction to Software Systems
  • About
  • Introduction
  • Software Engineering
    • Software System
    • Software Product Development
    • Computer Networks
  • Terminal Programming
    • BASH - Basic Commands
    • BASH - Conditions and Loops
    • Worked-out Examples
    • Practice Questions
  • Databases
    • Structured Databases
      • SQL Queries
      • Worked-out Example
    • Unstructured Databases
      • NoSQL Queries
      • Worked-out Example
  • Object Oriented Programming
    • Python - Introduction
    • Python - Basic Concepts
    • Python - Inbuilt Datastructures
    • Python - Conditions and Loops
    • Python - Lambda, Functions, Class/Objects
    • Worked-out Examples
  • WEB TECHNOLOGIES
    • HTML
    • CSS
    • Native JavaScript - Basics
    • Native JavaScript - Conditional Statements and Loops
    • Native JavaScript - Data Structures
    • JavaScript - Scope, Functions, Type Conversion
Powered by GitBook
On this page
  • Importance of CSS in Web Development
  • CSS Syntax
  • Basic Syntax Structure:
  • Types of CSS
  • Advantages of Different CSS Types
  • CSS Syntax Rules
  • CSS Selectors
  • 1. Simple Selectors
  • 2. Combinator Selectors
  • 3. Pseudo-Class Selectors
  • 4. Pseudo-Element Selectors
  • 5. Attribute Selectors
  • 6. Grouping Selectors
  • Example of Each Type:
  • CSS Selectors Examples
  • 1. Simple Selectors
  • 2. Combinator Selectors
  • 3. Pseudo-Class Selectors
  • 4. Pseudo-Element Selectors
  • 5. Attribute Selectors
  • 6. Tag with Class Selectors
  1. WEB TECHNOLOGIES

CSS

CSS, or Cascading Style Sheets, is a stylesheet language used in web development to control the layout and presentation of web pages. It is essential for enhancing the visual appearance and user experience of websites by adding design elements such as colors, fonts, and spacing to HTML content

Importance of CSS in Web Development

CSS is crucial in web development for several reasons:

  1. Separation of Content and Presentation:

    • CSS allows developers to separate the content (HTML) from the presentation (CSS), making it easier to maintain and update websites without affecting the underlying structure

  2. Consistent Styling:

    • CSS enables consistent styling across multiple web pages by using external style sheets, which can be applied globally with minimal effort

  3. Responsive Design:

    • CSS facilitates responsive web design, allowing websites to adapt seamlessly to different screen sizes and devices, ensuring optimal user experience across various platforms

  4. Cross-Browser Compatibility:

    • CSS helps ensure that web pages display consistently across different browsers, reducing the need for browser-specific modifications

  5. Enhanced User Experience:

    • CSS allows for the creation of interactive elements like animations and transitions, enhancing user engagement and overall website aesthetics

  6. Faster Page Loading:

    • By using external CSS files, browsers can cache styles, leading to faster page loading times, especially for returning visitors

  7. Accessibility:

    • Properly structured CSS can improve web accessibility by controlling the presentation of content, making it more inclusive for users with disabilities

In summary, CSS is vital for creating visually appealing, user-friendly, and responsive websites that are easy to maintain and update. It plays a central role in enhancing the overall user experience and optimizing website performance.

CSS Syntax

CSS syntax is composed of a selector and a declaration block. The selector identifies the HTML elements to be styled, while the declaration block contains one or more declarations that specify the styles to be applied.

Basic Syntax Structure:

selector {
  property1: value1;
  property2: value2;
}
  • Selector: Identifies the HTML element(s) to style (e.g., h1, .class, #id).

  • Declaration Block: Contains the styles enclosed in curly braces {}.

  • Property: Specifies the attribute of the HTML element to change (e.g., color, background-color).

  • Value: Specifies the new value for the property (e.g., red, blue).

Types of CSS

  1. Internal CSS:

    • Defined within the HTML document using the <style> tag.

    • Example:

      <head>
        <style>
          p { color: blue; }
        </style>
      </head>
  2. External CSS:

    • Stored in a separate file with a .css extension.

    • Linked to an HTML document using the <link> tag.

    • Example:

      <head>
        <link rel="stylesheet" type="text/css" href="style.css">
      </head>
  3. Inline CSS:

    • Applied directly to an HTML element using the style attribute.

    • Example:

      <p style="color: blue;">This text will be blue.</p>
  4. Inline-Block CSS:

    • Not a standard type but can refer to using inline styles with block-level elements.

Advantages of Different CSS Types

  • Internal CSS: Easy to implement for small projects or quick styling.

  • External CSS: Ideal for large projects, as it separates content from presentation and facilitates easier maintenance.

  • Inline CSS: Useful for applying styles to a single element, but generally discouraged due to its specificity and difficulty in maintaining.

CSS Syntax Rules

  • Case Insensitivity: CSS is case-insensitive by default.

  • Semicolons: Declarations must end with a semicolon.

  • Colon: Separates properties from values.

  • Curly Braces: Enclose the declaration block.

  • White Space: Ignored between properties and values, but not inside them.

Understanding CSS syntax and types is crucial for effective web development, allowing developers to control the layout and visual styling of web pages efficiently.

CSS Selectors

CSS selectors are used to target specific HTML elements to apply styles. There are several types of CSS selectors, each serving a distinct purpose. Here are the main categories:

1. Simple Selectors

  • Element Type Selectors: Select elements based on their HTML tag name (e.g., p, h1, div).

  • ID Selectors: Use the # symbol followed by the element's ID (e.g., #header).

  • Class Selectors: Use the . symbol followed by the element's class (e.g., .container).

  • Universal Selectors: Use the * symbol to select all elements on the page.

2. Combinator Selectors

  • Descendant Selectors: Select elements that are descendants of another element (e.g., div p).

  • Child Selectors: Select elements that are direct children of another element (e.g., div > p).

  • Adjacent Sibling Selectors: Select elements that are adjacent siblings of another element (e.g., p + p).

  • General Sibling Selectors: Select elements that are siblings of another element (e.g., p ~ p).

3. Pseudo-Class Selectors

  • These select elements based on their state or position (e.g., :hover, :first-child, :checked).

  • Examples include :active, :focus, :visited, :nth-child(), etc.

4. Pseudo-Element Selectors

  • These select parts of elements rather than the elements themselves (e.g., ::before, ::after, ::first-letter).

5. Attribute Selectors

  • Select elements based on their attributes or attribute values (e.g., [hreflang], [hreflang="en"], [hreflang|="en"]).

6. Grouping Selectors

  • Allow multiple selectors to share the same styles by separating them with commas (e.g., h1, h2, h3 { color: blue; }).

These categories provide a comprehensive way to target and style HTML elements using CSS.

Example of Each Type:

  • Element Type: p { color: green; }

  • ID: #header { background-color: blue; }

  • Class: .container { width: 80%; }

  • Universal: * { margin: 0; }

  • Descendant: div p { font-size: 18px; }

  • Child: div > p { font-weight: bold; }

  • Adjacent Sibling: p + p { margin-top: 10px; }

  • Pseudo-Class: a:hover { color: red; }

  • Pseudo-Element: p::first-letter { font-size: 24px; }

  • Attribute: [hreflang="en"] { color: blue; }

  • Grouping: h1, h2, h3 { color: blue; }

CSS Selectors Examples

CSS selectors are used to target specific HTML elements to apply styles. Here are examples of various CSS selectors categorized by type:

1. Simple Selectors

  • Element Type Selectors: Select elements based on their HTML tag name.

    p { color: green; }
  • ID Selectors: Use the # symbol followed by the element's ID.

    #header { background-color: blue; }
  • Class Selectors: Use the . symbol followed by the element's class.

    .container { width: 80%; }
  • Universal Selectors: Use the * symbol to select all elements.

    * { margin: 0; }
  • Grouping Selectors: Group multiple selectors to apply the same styles.

    h1, h2, h3 { color: blue; }

2. Combinator Selectors

  • Descendant Selectors: Select elements that are descendants of another element.

    div p { font-size: 18px; }
  • Child Selectors: Select elements that are direct children of another element.

    div > p { font-weight: bold; }
  • Adjacent Sibling Selectors: Select elements that are adjacent siblings of another element.

    p + p { margin-top: 10px; }
  • General Sibling Selectors: Select elements that are siblings of another element.

    p ~ p { color: grey; }

3. Pseudo-Class Selectors

  • These select elements based on their state or position.

  • Examples include :hover, :first-child, :checked.

    a:hover { color: red; }

4. Pseudo-Element Selectors

  • These select parts of elements rather than the elements themselves.

  • Examples include ::before, ::after, ::first-letter.

    p::first-letter { font-size: 24px; }

5. Attribute Selectors

  • Select elements based on their attributes or attribute values.

  • Examples include [hreflang], [hreflang="en"], [hreflang|="en"].

    [hreflang|="en"] { color: blue; }
  • Attribute Starts With: [href^="https"].

    [href^="https"] { color: green; }
  • Attribute Ends With: [href$=".pdf"].

    [href$=".pdf"] { color: red; }
  • Attribute Contains: [href*="w3schools"].

    [href*="w3schools"] { color: purple; }

6. Tag with Class Selectors

  • Select elements that match both the tag and class.

    p.para { color: white; background: blue; }

These examples cover the main categories of CSS selectors, demonstrating how to target and style HTML elements effectively.

PreviousHTMLNextNative JavaScript - Basics

Last updated 2 months ago