CSS Syntax

CSS Syntax

CSS Syntax

A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts −

·      Selector − A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table> etc.

·      Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties. They could be colorborder etc.

·      Value − Values are assigned to properties. For example, color property can have value either red or #F1F1F1 etc.

CSS Style Rule Syntax as follows –

        selector {property : value}

The Type Selectors

This is the same selector we have seen above. Again, one more example to give a color to all level 1 headings −

h1 {
   color: #36CFFF; 
}

The Universal Selectors

Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type −

* { 
   color: #000000; 
}

This rule renders the content of every element in our document in black.

The Descendant Selectors

Suppose you want to apply a style rule to a particular element only when it lies inside a particular element. As given in the following example, style rule will apply to <em> element only when it lies inside <ul> tag.

ul em {
   color: #000000; 
}

The Class Selectors

You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.

.black {
   color: #000000; 
}

The ID Selectors

You can define style rules based on the id attribute of the elements. All the elements having that id will be formatted according to the defined rule.

#black {
   color: #000000; 
}

This rule renders the content in black for every element with id attribute set to black in our document. You can make it a bit more particular. For example −

h1#black {
   color: #000000; 
}

This rule renders the content in black for only <h1> elements with id attribute set to black.

The true power of id selectors is when they are used as the foundation for descendant selectors, For example −

#black h2 {
   color: #000000; 
}

The Child Selectors

You have seen the descendant selectors. There is one more type of selector, which is very similar to descendants but have different functionality. Consider the following example −

body > p {
   color: #000000; 
}

The Attribute Selectors

You can also apply styles to HTML elements with particular attributes. The style rule below will match all the input elements having a type attribute with a value of text −

input[type = "text"] {
   color: #000000; 
}

The advantage to this method is that the <input type = "submit" /> element is unaffected, and the color applied only to the desired text fields.

There are following rules applied to attribute selector.

·      p[lang] − Selects all paragraph elements with a lang attribute.

·      p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of exactly "fr".

·      p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the word "fr".

·      p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values that are exactly "en", or begin with "en-".

Multiple Style Rules

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example −

h1 {
   color: #36C;
   font-weight: normal;
   letter-spacing: .4em;
   margin-bottom: 1em;
   text-transform: lowercase;
}

Grouping Selectors

You can apply a style to many selectors if you like. Just separate the selectors with a comma, as given in the following example −

h1, h2, h3 {
   color: #36C;
   font-weight: normal;
   letter-spacing: .4em;
   margin-bottom: 1em;
   text-transform: lowercase;
}

Embedded CSS - The <style> Element

You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside the <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax −

<!DOCTYPE html>
<html>
   <head>
      <style type = "text/css" media = "all">
         body {
            background-color: linen;
         }
         h1 {
            color: maroon;
            margin-left: 40px;
         }
      </style>
   </head>   
   <body>
      <h1>This is a heading</h1>
      <p>This is a paragraph.</p>
   </body>
</html>

Inline CSS - The style Attribute

You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. Here is the generic syntax −

<element style = "...style rules....">

Example

<html>
   <head>
   </head>

   <body>
      <h1 style = "color:#36C;"> 
         This is inline CSS 
      </h1>
   </body>

</External CSS - The <link> Element

The <link> element can be used to include an external stylesheet file in your HTML document.

An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element.

Here is the generic syntax of including external CSS file −

<head>

<link type = "text/css" href = "..." media = "..." />

</head>

Example

Consider a simple style sheet file with a name mystyle.css having the following rules −

h1, h2, h3 {
   color: #36C;
   font-weight: normal;
   letter-spacing: .4em;
   margin-bottom: 1em;
   text-transform: lowercase;
}

Now you can include this file mystyle.css in any HTML document as follows −

<head>
   <link type = "text/css" href = "mystyle.css" media = " all" />
</head>

Imported CSS - @import Rule

@import is used to import an external stylesheet in a manner similar to the <link> element. Here is the generic syntax of @import rule.

<head>
   @import "URL";
</head>

Here URL is the URL of the style sheet file having style rules. You can use another syntax as well −

<head>
   @import url("URL");
</head>

Example

Following is the example showing you how to import a style sheet file into HTML document −

<head>
   @import "mystyle.css";
</head>