Javatpoint Logo
Javatpoint Logo

CSS Syntax

The CSS provides the style to the HTML element, which the browser interprets. After being interpreted by the browser, the CSS style property will be applied to all the elements of the HTML. We can provide style property to the HTML element in three parts. These three parts are as follows.

1. Selector

It is an HTML tag. All the style properties of the CSS will be applied to the selector. The selector tag like <h1> or <table> etc.

2. Property

It is a type of attribute that is present in HTML tags. All the attributes of the HTML will be converted to the CSS properties. The CSS properties like color, border, etc.

3. Value

In HTML, these are assigned to the properties. For example, the color property can have a value of either red or #F1F1F1, etc.

Syntax:

We must provide the CSS property to the HTML element in a proper way. We must follow the syntax below to implement the CSS property.


CSS syntax

Example:

Let's implement the CSS property by seeing an example.

Code:

Output:

CSS syntax

Explanation:

In the above example, we have provided some CSS styles. Let's explain what we do in the above example.

  • In the above example, there is a body tag. In the body tag, we set the entire page's font family, margin, padding, and background color.
  • Also, there is a #header selector. Inside the #header selector, we have set the header section's background color, text color, text alignment, and padding.
  • Inside the h1 selector, we have set the font size and margin for the main heading.
  • Inside the p selector, we have set font size and margin for paragraphs.
  • We have set the button with a red background, white text color, padding, border, and other properties inside the button selector.
  • Inside the .button:hover selector, we have set the background color to change when the button hovers, giving it a darker shade of red.

The Type Selectors

This type of selector is seen in the above example. Let's take one more example, which provides the red color for all the heading tags.

Example:

The Universal Selectors

With the help of a universal selector, we can provide the style property for the whole web page. Let's understand this by taking an example.

Example:

Output:

CSS syntax

Explanation:

In the above code, we have implemented the universal selector. It is represented as "*". With the help of a universal selector, we set the margin, padding, and box-sizing.

The Descendant Selectors

When there is a need to provide the style property to an element present inside another tag, we must take the help of a descendant selector.

Syntax:

Example:

Output:

CSS syntax

Explanation:

In the above code, we have created a horizontal nav bar that contains some nested menus. When we hover on any menu, then it will change the color.

The Class Selectors

In this selector, we must provide the style properties based on the class name. All elements with the same class name will be formatted per the CSS style properties.

Syntax

Example:

Output:

CSS syntax

Explanation:

In the above code, we have created a beautiful card that contains information regarding the Indian flag. The class .card styles the overall card container while. The card img applies styles to the image inside the card. .card-content targets the container for the card's content, and .card-title, .card-text, and .card-link style the individual elements inside the card content.

The ID Selectors

With the help of the ID selector, we can provide the CSS style based on the ID attribute. All elements with the same ID name will be formatted per the CSS style properties.

Syntax:

Example:

Output:

CSS syntax

Explanation:

We created a pink button with a click-and-hover effect in the above code. This button is situated inside a center tag. When we hover over the button, the background color of the div tag slightly changes to a darker pink color.

The Child Selectors

Previously, we have seen the descendant selectors. Another type of selector is available in the CSS, i.e., child selector. It has different functionality in comparison to descendant selectors. The child selector uses the">" symbol to target elements that are immediate descendants of another element.

Syntax:

Example:

Output:

CSS syntax

Explanation:

In the above code, we created the navigation menu containing the service sub-menu. With the help of the child selector, sub-menu items are styled differently from the main menu items. When the user hovers over the "Services" menu item, the sub-menu with "Web Design," "Graphic Design," and "SEO" options will be displayed on a beautiful pink background.

The Attribute Selectors

With the help of this attribute, we can apply the style property to the attribute.

Syntax:

We must follow some rules during the implementation of the attribute selector. These are as follows.

  • p[lang]: It can select all the paragraphs with the help of the lang attribute.
  • p[lang= "fr"]: It can select all the paragraphs with the help of the lang attribute, and the lang attribute should have a value of exactly "fr."
  • p[lang~= "fr"]: It can select all the paragraphs with the help of the lang attribute, and the lang attribute should have the word "fr."
  • p[lang|= "en"]: It can select all the paragraphs with the help of the lang attribute, and the lang attribute should have "en" or begin with "en-"

Example:

Output:

CSS syntax

Explanation:

In the above code, we have created an image gallery. Then, using attribute selectors, we add different colored borders to images based on their "data-category" attributes. Images with "data-category=" nature" will have a green border, "data-category=" cityscape" will have a blue border, "data-category=" beach" will have an orange border, and "data-category=" mountains" will have a purple border.

Multiple Style Rules

With the help of this, we have to define the multiple styles for a single element. We must define all the corresponding rules and values inside the single unit. Here, we have to separate all the properties, which are separated by the semicolon (;).

Example:

Output:

CSS syntax
Next TopicCSS Selector





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