CSS Rule

CSS stylesheets are created to set value of the HTML elements attributes and apply them throughout a web document to implement consistency in look. The technique of doing this is by creating CSS rules. A CSS rule sets the value for defining the appearance of elements.

Syntax- CSS rule

A CSS rule includes the element on which the CSS style is to be applied with a CSS Rule. The syntax is

SELECTOR {property:value;….}

SELECTOR is an HTML element on which you need to apply the style. This can be the tag to which the style is applied, a class or an ID. Class and ID can be used to stylize content in DIV or SPAN tag.

Multiple property and value pairs can be given with in curly parenthesis. The property represents the attribute of the selector. Value is how the element should look with respect to that attribute. Property and value are separated by a colon. Property is the formatting and value is the physical characteristic of that property.

Each of such pair of a property and a value for a selector is also called a declaration.

Pointers to remember

  • You can define as many as CSS rules as you need in a style sheet.
  • Property in a rule and is the attribute of the selector element.
  • Value is the physical feature of the attribute that will tell the browser how to present the element in a web page.
  • Property and value is separated by a colon.
  • Multiple property and value pairs are separated by semicolons with in curly parenthesis.  It lets the browser know about the end of a rule and start of the next one.
  • Each selector can have multiple CSS rule definitions.
  • An inner selector CSS rule overrides the style of an outer selector CSS rule when applied in an HTML page.

Examples

Paragraph tag is modified with text color green and style of font as italics

P{COLOR:green; FONT-STYLE:italic;}

Body tag is stylized with text color white, background color as black and size of text as 14 pixels

BODY{COLOR:#FFFFFF; BACKGROUND-COLOR:#000000; FONT-SIZE:14px;}

Header level 1 tag is stylized with text color red, size of text as 16 pixels and bold.

H1{COLOR:red; FONT-SIZE:16px;FONT-STYLE:BOLD;}

Header level 2 tag is stylized with text color red, size of text as 14 pixels and bold.

H2{COLOR:red; FONT-SIZE:14px;FONT-STYLE:BOLD;}

When HTML document contains the above tags to represent the content, the content will be displayed with the defined style.

<STYLE TYPE="text/css">
P{COLOR:green; FONT-STYLE:italic;}
BODY{COLOR:#FFFFFF; BACKGROUND-COLOR:#000000; FONT-SIZE:14px;}
H1{COLOR:red; FONT-SIZE:22px;FONT-STYLE:BOLD;}
H2{COLOR:red; FONT-SIZE:14px;FONT-STYLE:BOLD;}
</STYLE>
<BODY>
<H1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </H1>Integer nec odio. Praesent libero. 
Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. 
Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.
Vestibulum lacinia arcu eget nulla. <H2>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. 
Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor.</H2> 
<P>Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. 
Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. 
Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. 
Mauris ipsum. Nulla metus metus, ullamcorper vel,</P> tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. 
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, 
urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. 
<P>Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor,
sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci 
luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. </P>
In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc,
 viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit 
 amet mauris. <P>Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. 
Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, 
ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. </P>Aenean laoreet. Vestibulum nisi lectus, commodo ac, 
facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. 
</BODY>