Javatpoint Logo
Javatpoint Logo

Style Specification Format in CSS

We will discuss the style specification format in CSS. Let us understand the meaning of style specification first.

Style Specification

The set of rules and declarations in CSS that are specified using <style> tag or "style" attribute is called style specification.

Style makes the HTML elements look beautiful on a webpage and attracts more users. We can provide style to the elements utilizing CSS.

Style Specification Format

The style specification format is the structure that is utilized to define the rules for styling the HTML elements.

Syntax:

The "selector" is utilized to select the HTML element on which we want to apply the style. The "property1" and "property2" …. property_n are the properties that are utilized to give style to the selected element. We can put many styles on a single element utilizing different CSS properties.

There are distinct selectors in CSS, which are given below:

1. Element selector:

The element selector is simply the name of the HTML elements like p, h1, div, etc.

Syntax:

The "p" is the element selector in the above syntax. The name "p" is utilized for applying style to <p> element.

Example:

We will utilize an element selector to give the style to HTML elements. We will give style to <h2>, <div> and <p> elements in this demonstration.

Code:

Output:

Here, in the output, we can witness the style provided on the HTML elements utilizing the element selector.

Style Specification Format in CSS

2. Id selector:

The id selector selects the element with a specific id. The "id" attribute provides the id name for the element. The ID name should be unique for each element on a single web page. It is selected by adding a hash (#) character in front of the id name.

Syntax:

The "#id" is the id selector in the above-given syntax.

Example:

We will utilize the id selector to give the style to the HTML element. We will give style <h1> and <h2> elements in this demonstration.

Code:

Output:

Here is the output where we can witness the style provided on the <h1> and <h2> elements utilizing the id selector.

Style Specification Format in CSS

3. Class selector:

The class selector is utilized to select the element which has a specific class. The "class" attribute is utilized to give the class name to the element. We can give same class name to different HTML elements on a single web page. It is selected by adding an element-name and dot (.) character in front of the class name.

Syntax:

The "element-name.class" is the class selector in the above syntax.

Example:

We will apply the style to HTML elements with the help of a class selector in this demonstration. We will apply the style to <h1> element, <h2> element, and <h3> element utilizing the same class name.

Code:

Output:

Here is the output where we can witness that the style has been applied to the HTML elements utilizing the class selector.

Style Specification Format in CSS

4. Generic class selector:

The generic class selector is utilized to provide the same style to different elements. The "class" attribute is utilized to give the generic class name to the element. It is selected by adding a dot (.) character in front of the class name.

Syntax:

The ".class" is the generic class selector in the above syntax.

Example:

We will apply the style to HTML elements with the help of a generic class selector in this demonstration. We will apply the same style to <h1>, <h2> and <h3> elements.

Code:

Output:

We can witness that the same style has been applied to <h1>, <h2>, and <h3> elements utilizing a generic class selector.

Style Specification Format in CSS

5. Universal selector:

The universal selector is utilized to select all the HTML elements. The symbol asterisk (*) is used to select all HTML elements.

Syntax:

In the above syntax, the "*" is the universal selector. The properties are written inside the curly braces.

Example:

We will apply the style to HTML elements with the help of a universal selector in this demonstration.

Code:

Output:

We can witness that the style has been applied to the HTML elements utilizing the universal selector.

Style Specification Format in CSS

6. Group selector:

The group selector is utilized to select many HTML elements at a time. It allows the application of the same style to many HTML elements.

Syntax:

The "element1, element2, element3, …" is the group selector in the above syntax.

Example:

We will apply the style to HTML elements with the help of a group selector in this demonstration.

Code:

Output:

We can witness that the style has been applied to the HTML elements utilizing the group selector.

Style Specification Format in CSS

7. Pseudo-class:

The pseudo-class is a selector used to select a specific element and change its state. The symbol colon (:) is utilized before the pseudo-class.

Syntax:

The selector name is written in place of the selector, and the name of the pseudo-class is written in place of the pseudo-class. The symbol colon (:) is used between selector and pseudo-class. The properties are written inside the curly braces.

The selector can be element, id, class, etc. There are various pseudo-classes, which are described below:

  • :hover: It is used to change the appearance of the element when the user hovers over the element.
  • :active: It is used to change the appearance of an element when the element is active.
  • :link: It is used to give style to the unvisited link.
  • :visited: It is used to give style to the visited link.
  • :focus: It is used to give style to the element when it comes to focus.
  • :first-child: It is used to give style to the first child of an element.
  • :second-child: It is used to give style to the second child of an element.

Example:

We will apply the style to HTML elements with the help of pseudo-classes in this demonstration. We will be utilizing the element selector and ":hover" pseudo-class to change the link color when the user hovers over the link. We will be utilizing the id selector and ":focus" pseudo-class to change the background color of the text area when it comes to focus.

Code:

Output:

We can witness the output below:

Style Specification Format in CSS

When the user hovers over the link and the text area comes to focus then we can witness the appearance of both link and text area has been changed utilizing pseudo-classes.

Style Specification Format in CSS

We can provide style to the HTML elements in three methods:

1. Inline:

It is the first method of adding style to HTML elements by utilizing inline CSS. In this method, the "style" attribute is used inside each HTML element on which we want to put style.

Illustration of inline CSS:

Let us understand inline CSS with the help of an illustration.

Code:

Output:

Here is the output where we can witness that the style has been applied on <p> elements utilizing inline CSS.

Style Specification Format in CSS

2. Internal:

The second method is to provide style to the HTML elements by utilizing internal CSS for which the <style> tag is utilized inside the head section of the HTML code.

Illustration of internal CSS:

Let us comprehend internal CSS with the help of this illustration.

Code:

Output:

We can witness the style applied on <h2> and <li> elements utilizing internal CSS.

Style Specification Format in CSS

3. External:

The third way to add style to the HTML elements is using external CSS. The external style sheet is created, and then the external file is linked to the HTML file utilizing the <link> tag.

Illustration of external CSS:

Let us comprehend external CSS with the help of this illustration.

External CSS Code:

HTML Code:

Output:

We can witness the style applied on <h2>, <table>, <tr>, <th>, and <td> elements utilizing external CSS.

Style Specification Format in CSS

Conclusion

We have understood the style specification format in CSS in this article. We have understood many demonstrations to comprehend the style specification format properly. The following are the points to remember:

1. Style specifications are the set of rules and declarations in CSS.

2. Various selectors are utilized to apply style to HTML elements, which are given below:

  • Element selector: This function selects an element by its name, such as h1, p, etc.
  • Id selector: It is utilized to select the element by the id name. Example: #id-name
  • Class selector: It selects the element by the class name. Example: element-name.class-name
  • Generic class selector: It is utilized to select the element by the generic class name. Example: .generic-class-name
  • Universal selector: It selects all the HTML elements. Example: *
  • Pseudo-classes: It selects the HTML element and changes the state of the element. Example: element-name:pseudo-class-name

3. There are three ways to apply style to the elements:

  • Inline CSS: It is utilized to add style to HTML elements utilizing the "style" attribute.
  • Internal CSS: It is utilized to add style to HTML elements utilizing the <style> tag under the head section.
  • External CSS: It is utilized to add style to HTML elements utilizing an external style sheet and linking it to the HTML document using the <link> tag under the head section.

Next TopicTextarea CSS





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA