ids-in-cascading-style-sheets-css-

CSS allow us to redefine properties of HTML elements. By redefining properties we can create attractive look and feel for web pages. More importantly its usage is simple and straight forward. For example consider following rule.

P
{
color: #F00 ;
}

In above rule “P” is a basic CSS selector which can be used to change any property of Paragraph.

CSS also allow us to make custom selector called ID and Class. ID and Class are strong mechanism to control the layout of web page. IDs is one the popular and frequently used CSS selector .

An ID can only be used once in a page and it uniquely identifies and HTML element. ID can be applied to an element by using id=”name” attribute just after the opening tag within the element.

Example:

<p id=”red”> This paragraph is red</p>
<p id=”grey”>This paragraph is grey</p>
In CSS we use hash (#) character to identify an ID

#red{
color:#F00;
}
#grey{
color:#333;
}
IDs can be combined with Selector to add further control.

Example:

<p id=”red”> This paragraph is red</p>
<p>This paragraph is plain</p>
In CSS rule

p#red{
color:#F00;
}
Above CSS rule states to turn a paragraph to red if it has and ID “RED”.

Comments are closed.