WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

CSS Syntax and Selectors


CSS Syntax

A CSS must be pursue - set comprises of a selector and a statement block:

CSS selector

The selector focuses to the HTML component you need to style.

The revelation square contains at least one statements isolated by semicolons.

Each revelation incorporates a CSS property name and an esteem, isolated by a colon.

A CSS announcement dependably finishes with a semicolon, and assertion squares are encompassed by wavy braces.

In the accompanying model all <p> components will be correct adjusted, with a green content color:

Example

p {
    shading: green ;
    content adjust: right;
}
Try it yourself »

CSS Selectors

CSS selectors are utilized to choose HTML components dependent on their component name, class ,id, property, and more.


The component Selector

The component selector chooses components dependent on the component name.

You can choose all <p> components on a page this way (for this situation, all <p> components will be right-adjusted, with a green content color):

Example

p {
    content adjust: left;
    shading: green;
}
Try it yourself »

The id Selector

id selector used to choose one extraordinary component

id selector utilizes the id quality of a HTML component to choose a particular component.

To select a component with a particular id, compose a hash (#) character, trailed by the id of the element.

The style rule underneath will be connected to the HTML component with id="para1":

Example

#para1 {
    content adjust: right;
    shading: green;
}
Try it yourself »

The class Selector

The class selector chooses components with a particular class attribute.

To select components with a particular class, compose a period (.) character, trailed by the name of the class.

In the precedent beneath, all HTML components with class="right" will be green and right-aligned:

Example

.right {
    content adjust: right;
    shading: green;
}
Try it yourself »

You can likewise determine that just explicit HTML components ought to be influenced by a class.

In the precedent beneath, just <p> components with class="right" will be correct aligned:

Example

p.right {
    content adjust: right;
    shading: green;
}
Try it yourself »

HTML components can likewise allude to more than one class.

In the precedent beneath, the <p> component will be styled by class="right" what's more, to class="large":

Example

<p class="right large">This section alludes to two classes.</p>
Try it yourself »

Grouping Selectors

If you have components with a similar style definitions, as this:

h1 {
    content adjust: left;
    shading: green;
}

h2 {
    content adjust: right;
    shading: green;
}

p {
    content adjust: right;
    shading: green;
}

It is smart thought to amass selectors separate every selector with a comma.

It will be smarter to assemble the selectors, to limit the code.

In the precedent beneath we have gathered the selectors from the code above:

Example

h1, h2, p {
    content adjust: right;
    shading: green;
}
Try it yourself »

CSS Comments

Comment are valuable it help dishonorable by program.

Comment are utilized to clarify code and you can alter source code at a later date.

A CSS remark begins with/* and closes with */. Remarks can likewise range various lines:

Example

p {
    shading: green;
    /* This is a solitary line remark */
    content adjust: right;
}

/* This is
a multi-line
comment */
Try it yourself »