Html CSS Syntax Rules And Selector Examples

CSS styles consist of a series of rules that are parsed by a web browser and then applied to the corresponding elements of an HTML document. This article will tell you what the CSS syntax rules are and how to use a CSS selector to select Html elements.

1. CSS Syntax Rules.

  1. CSS syntax rules consist of three parts: selectors, properties, and values.
  2. Selector: it is composed of HTML element ID, class attribute, or element name with some special symbols. It is used to specify which HTML element to define the style for. For example, the selector P defines the style for all Html <p> tags on the web page. Please Note, the CSS selector is case sensitive.
  3. Property Name: the CSS property name that you want to set for HTML elements, which is composed of a series of keywords, such as color, border, font, etc.
  4. Property Value: the property value is composed of numerical value and unit or keyword, which is used to control the display effect of a certain attribute. For example, the value of the color attribute can be green.
  5. The property name and value are separated by a colon (:), each name-value pair are separated by a semicolon (;), and all the name-value pairs belonging to the same selector will be wrapped by braces ( { } ).
  6. Below is a CSS example. The selector is h1, then the CSS style will be applied to all the <h1> tags on the Html web page. All the text that is wrapped by the <h1> tag has a blue color, and the text will be aligned in the center.
    h1 {
        color: blue;
        text-align: center;
    }

2. CSS Selectors.

  1. Selectors are composed of HTML element ID, class attribute, or element name itself with some special symbols. It is used to specify the target HTML elements that apply the style.
  2. Selectors are an important part of CSS style rules. We can regard selectors as the matching pattern between CSS styles and HTML elements.
  3. The style rules associated with selectors will be applied to the HTML elements specified by selectors.

2.1 Universal Selector.

  1. The universal selector is indicated by an asterisk *, which does not match a specific HTML element, but matches every element in the HTML document.
  2. In development, we usually use universal selectors to clear the default inner and outer margins in HTML elements, below is an example.
    * {
        margin: 0 auto;
        padding: 0;
    }
  3. It is recommended not to use universal selectors too frequently in production environments. Because generic selectors match every Html element on the page, frequent use can put too much unnecessary stress on the browser.

2.2 Html Tag Selector.

  1. A complete HTML document is composed of various tags, and tag selectors can match all tags with the same name in the document through specific tag names.
  2. Below is a CSS style definition that uses an Html tag selector.
    p {
        color: green;
    }
  3. The above P selector will apply the style to all the Html <P> tags on the Html web page.

2.3 ID Selector.

  1. The ID selector is used to match tags with the specified ID attribute in the HTML document.
  2. Defining an ID selector requires the hash sign ( # ) followed by the value of the ID property, below is an example.
    #nav {
        color: blue;
    }
  3. The above #nav selector can match tags with the id=”nav” attribute in the Html document.

2.4 Class Selector.

2.4.1 Define CSS Style With Class Selector.
  1. The class selector can match specific HTML tags according to the class attribute of the tag, and all qualified tags will be formatted according to the CSS style defined in the selector.
  2. The definition of a class selector requires an English period ( . ), followed by the class name, as shown below.
    .red {
        color: red;
    }
  3. The above red selector matches all tags in the document with the class=”red” attribute.
2.4.2 Define Class Selector To Special Html Tag.
  1. Since the class attribute can be applied to different Html tags, you can also specify specific tags when defining the class selector as follows.
    p.red {
        color: red;
    }
  2. The p.red selector only applies the styles in it to all Html <p> tags with the attribute class=”red” and has no effect on other tags with the class=”red” attribute.
2.4.3 Define Multiple Class Selectors.
  1. In addition, there may be more than one class attribute in an HTML tag, for example.
    <div class="info selected"></div>
    <div class="info"></div>
  2. Suppose we want to set bold for all elements whose class attribute contains info, we can define the below CSS style.
    .info {
        font-weight:bold;
    }
  3. Set red font for all elements whose class attribute contains selected, we can define the below CSS style.
    .selected {
        color: red;
    }
  4. To set a blue background for an element that contains both info and selected in its class attribute, it can be written as follows.
    .info.selected {
        background: blue;
    }
  5. This kind of class selector composed of multiple class attribute values can be called “multi-class selector”, and the class selector defined by a single class attribute value described earlier can be called “single-class selector”.
  6. In a multi-class selector, multiple class attributes are next to each other (such as .info.selected) and do not need to be separated by spaces.

2.5 Descendant Selector.

  1. When a <p> tag is nested inside a <div> tag, the <p> tag can be regarded as a descendant of the <div> tag.
  2. When we need to select the descendant tag of a tag, we can use the descendant selector.
  3. The definition of the descendant selector is to list the tag name, class attribute, or ID attribute from outside to inside according to the nesting relationship of tags, and separate them with spaces in the middle.
  4. The below example selector will only match all descendants <a> tags nested in the unordered list <ul><li> tag.
    ul li a {
        text-decoration: none;
    }

2.6 Child Selector.

  1. Child selector is similar to the descendant selector, except that child selector only matches the direct descendant of an element (there is only one nesting level between an element and its child elements).
  2. A child selector consists of two or more selectors separated by a greater than sign( > ).
  3. In the below example, The p > strong selector will set the text in the first child tag <strong> of the <p> tag to red, but it has no effect on the <strong> tag in the second <p> tag.
    p > strong {
        color:red;
    }
    
    <p>welcome to<strong>dev2qa.com</strong>!</p>
    <p><em><strong>dev2qa.com</strong></em>, a dedicated coding example website</p>

2.7 Neighbor Sibling Selector.

  1. The neighbor sibling selector is used to match another element immediately after an element. These two elements have the same parent element and there is no nesting relationship.
  2. To define the neighbor sibling selector, you need to use the plus sign +. There are two neighbor elements on both sides of the plus sign, and the selector will match the elements after the plus sign.
  3. In the below example, the h1 + p selector matches the <p> tag immediately after the <h1> tag under the same parent element. The ul.task + p selector will match the <p> tag next to and after the <ul> tag under the same parent element, and the <ul> tag must have the class = “task” attribute.
    h1 + p {
        color: blue;
        font-size: 18px;
    }
    ul.task + p {
        color: #f0f;
        text-indent: 30px;
    }

2.8 Universal Sibling Selector.

  1. Universal sibling selector is very similar to neighboring sibling selectors, but not so strict.
  2. Universal sibling selectors also match sibling elements under the same parent element, but sibling elements do not need to be adjacent to each other.
  3. Defining a universal sibling selector requires a tilde ( ~ ). Both sides of the tilde are elements under the same parent element. The selector will match the element after the tilde.
  4. In the below example, The h1 ∼ p selector matches all <p> tags after the <h1> tag under the same parent element.
    h1 ∼ p {
        color: blue;
        font-size: 18px;
    }

2.9 Group Selector.

  1. Group selectors can apply the same style rules to multiple selectors, separated by a comma.
  2. Doing so can avoid defining duplicate style rules and minimize the code in CSS style sheets. For example, in a CSS style sheet, different selectors may contain the same style rules.
  3. In the below example CSS code, the selectors h1, h2, and h3 contain the same CSS style code font-weight: normal;.
    h1 {
        font-size: 36px;
        font-weight: normal;
    }
    h2 {
        font-size: 28px;
        font-weight: normal;
    }
    h3 {
        font-size: 22px;
        font-weight: normal;
    }
  4. At this time, you can use the group selector to define the same style rules for h1, h2, and h3, and then define different style rules between them, as shown below.
    h1, h2, h3 {
        font-weight: normal;
    }
    h1 {
        font-size: 36px;
    }
    h2 {
        font-size: 28px;
    }
    h3 {
        font-size: 22px;
    }

2.10 Attribute Selector.

  1. Attribute selector is used to matching elements with specific attributes.
  2. The definition of an attribute selector is similar to that of an Html tag selector, except that you need to add the square brackets [] after the tag to specify the attribute information of the Html tag.
  3. In the below example, the input [type = “text”] selector matches all <input> tags with the typ =”text” attribute.
    input[type="text"] {
       color: red;
    }
  4. The attribute information in square brackets [] of the attribute selector also supports the following writing methods.
  5. [target]: select all elements with target attribute.
  6. [target = _blank]: select all elements with target = “_blank” attribute.
  7. [title ~ = flower]: select all elements that the title attribute contains the word “flower”.
  8. [lang| = en]: select all elements that have the lang attribute is exactly “en” or start with “en”.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky