Tuesday, May 21, 2024

CSS Selectors

 selectors.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Selectors</title>
    <link rel="stylesheet" href="selectors.css">
</head>
<body>
        <ul>
                <li id="select">The Great Barrier Reef</li>
                <li class="underline">Pyramids of Giza</li>
                <li class="underline">Stonehenge</li>
        </ul>
</body>
</html>

selectors.css
li{
  border: 4px solid blue;
}
#select{
   background-color: red;
}
.underline{
 text-decoration: underline;
}

Output




Basic Selectors

  1. Universal Selector (*):

    • Selects all elements.
    • Example: * { margin: 0; padding: 0; }
  2. Type Selector (Element Selector):

    • Selects all elements of a given type.
    • Example: p { font-size: 16px; }
  3. Class Selector (.):

    • Selects all elements with a specific class.
    • Example: .highlight { background-color: yellow; }
  4. ID Selector (#):

    • Selects a single element with a specific ID.
    • Example: #header { background-color: blue; }

Attribute Selectors

  1. [attribute] Selector:

    • Selects elements with a specific attribute.
    • Example: [type="text"] { width: 200px; }
  2. [attribute=value] Selector:

    • Selects elements with a specific attribute value.
    • Example: [href="example.com"] { color: red; }
  3. [attribute~=value] Selector:

    • Selects elements with an attribute containing a specific word.
    • Example: [class~=button] { padding: 10px; }
  4. [attribute|=value] Selector:

    • Selects elements with an attribute value that is exactly equal to a given value or starts with a given value followed by a hyphen.
    • Example: [lang|=en] { font-family: Arial; }
  5. [attribute^=value] Selector:

    • Selects elements with an attribute value that starts with a specific value.
    • Example: [title^="Hello"] { color: green; }
  6. [attribute$=value] Selector:

    • Selects elements with an attribute value that ends with a specific value.
    • Example: [src$=".jpg"] { border: 1px solid black; }
  7. [attribute=value] Selector*:

    • Selects elements with an attribute value that contains a specific value.
    • Example: [title*="important"] { font-weight: bold; }

Combinator Selectors

  1. Descendant Selector (space):

    • Selects all elements that are descendants of a specified element.
    • Example: div p { color: blue; }
  2. Child Selector (>):

    • Selects all elements that are direct children of a specified element.
    • Example: ul > li { list-style-type: none; }
  3. Adjacent Sibling Selector (+):

    • Selects an element that is the next sibling immediately following another element.
    • Example: h1 + p { margin-top: 0; }
  4. General Sibling Selector (~):

    • Selects all elements that are siblings of a specified element.
    • Example: h2 ~ p { color: gray; }

Pseudo-class Selectors

  1. :hover:

    • Selects elements when the user hovers over them.
    • Example: a:hover { color: red; }
  2. :focus:

    • Selects elements that have focus.
    • Example: input:focus { border-color: blue; }
  3. :nth-child(n):

    • Selects the nth child of a parent.
    • Example: li:nth-child(2) { color: green; }
  4. :nth-of-type(n):

    • Selects the nth child of a specific type of its parent.
    • Example: p:nth-of-type(2) { font-size: 18px; }
  5. :first-child:

    • Selects the first child of a parent.
    • Example: p:first-child { font-weight: bold; }
  6. :last-child:

    • Selects the last child of a parent.
    • Example: p:last-child { font-style: italic; }
  7. :only-child:

    • Selects an element that is the only child of its parent.
    • Example: p:only-child { color: purple; }
  8. :nth-last-child(n):

    • Selects the nth child from the end of its parent.
    • Example: li:nth-last-child(2) { color: orange; }

Pseudo-element Selectors

  1. ::before:

    • Inserts content before the content of an element.
    • Example: p::before { content: "Note: "; font-weight: bold; }
  2. ::after:

    • Inserts content after the content of an element.
    • Example: p::after { content: " End"; }
  3. ::first-line:

    • Selects the first line of a block-level element.
    • Example: p::first-line { font-size: 1.2em; }
  4. ::first-letter:

    • Selects the first letter of a block-level element.
    • Example: p::first-letter { font-size: 2em; color: red; }

Grouping Selectors

  • Grouping Selector (,):
    • Selects all elements matched by the selectors separated by a comma.
    • Example: h1, h2, h3 { margin-bottom: 10px; }

Other Useful Selectors

  1. :not(selector):

    • Selects all elements that do not match the given selector.
    • Example: :not(p) { background-color: lightgray; }
  2. :empty:

    • Selects elements that have no children.
    • Example: div:empty { display: none; }
  3. :checked:

    • Selects checked elements (checkboxes or radio buttons).
    • Example: input:checked { background-color: yellow; }
  4. :disabled:

    • Selects disabled form elements.
    • Example: input:disabled { background-color: lightgray; }

No comments:

Post a Comment

HTML Semantics sheet

<article> : Defines independent, self-contained content. <aside>: Contains content that is tangentially related to the content...