HTML Styles

Style rules describe how documents are presented on the web browsers.

Styling HTML Elements

HTML is quite limited when it comes to the presentation of document. It was originally designed as a simple way of presenting information. CSS (Cascading Style Sheets) was introduced in December 1996 by the W3C, to provide a better way to style HTML elements.

With CSS, it becomes easy to specify the colors used for the text and the backgrounds, the size and the style for the fonts, the amount of space between elements, adding border and outlines to the elements and a host of other styling.

Adding Styles to HTML Elements

Style information can be either attached as a separate document or embedded in the HTML document. These are the three methods of implementing styling information to the HTML document (from highest to lowest priority).

  • Inline styles — Using the style attribute in the HTML start tag.
  • Embedded style — Using the <style> element in the head section of the document.
  • External style sheet — Using the <link> element, pointing to an external CSS files.

In this tutorial we will cover all these different types of style sheet one by one:

Inline Styles

Inline styles are used to apply the unique style rules to an element by putting the CSS rules directly into the start tag. It can be attached to an element using the style attribute.

The style attribute includes a series of CSS property and value pairs. Each property: value pair is separated by a semicolon (;), just as you would write into an embedded or external style sheet. But it needs to be all in one line i.e. no line break after the semicolon.

The following example will show you how to change the color and font-size of the text:

  • <h1 style="color:red; font-size:30px;">This is a heading</h1>
  • <p style="color:green; font-size:18px;">This is a paragraph.</p>
  • <div style="color:green; font-size:18px;">This is some text.</div>

Using the inline styles are generally considered as a bad practice. As style rules are embedded directly inside the html tag, it causes the presentation to become mixed with the content of the document; which negates the purpose of using CSS.

Note: It's become impossible to style pseudo-elements and -classes with inline styles. You should, therefore, avoid the use of style attributes in your markup. Using external style sheet is the preferred way to add style to HTML document.

Embedded Style Sheets

Embedded or internal style sheets only affect the document they are embedded in.

Embedded style sheets are defined in the <head> section of an HTML document using the <style> tag. You can define any number of <style> elements inside the <head> section of an HTML document. See the example below:

  • <head>
  •     <style type="text/css">
  •         body {background-color: YellowGreen;}
  •         p {color: Black;}
  •     </style>
  • </head>

External Style Sheets

An external style sheet is ideal when the style is applied to many pages.

An external style sheet holds all the style rules in a separate document that you can link from any HTML file on your site. External style sheets are the most flexible because with an external style sheet, you can change the look of an entire website by changing just one file.

You can attach external style sheets in two ways — linking and importing:

Linking External Style Sheets

An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag goes inside the <head> section:

  • <head>
  •     <link rel="stylesheet" type="text/css" href="css/style.css">
  • </head>

Importing External Style Sheets

The @import rule is another way of loading an external style sheet. The @import statement instructs the browser to load an external style sheet and use its styles.

You can use it in two ways. The simplest is within the header of your document. Note that, other CSS rules may still be included in the <style> element.

  • <style type="text/css">
  •     @import url("css/style.css");
  •     p {
  •         color: blue;
  •         font-size: 16px;
  •     }
  • </style>

Similarly, you can use the @import rule to import a style sheet within another style sheet.

  • @import url("css/layout.css");
  • @import url("css/color.css");
  • body {
  •     color: blue;
  •     font-size: 14px;
  • }

Note: All @import rules must occur at the start of the style sheet. Any style rule defined in the style sheet itself override conflicting rule in the imported style sheets.


Your Feedback:

We would love to hear from you, please drop us a line.