×

How to include CSS

How to include CSS

CSS is included in HTML pages for formatting the documents based on the information inside a style sheet.

We can add CSS in HTML documents in the following three ways:

  1. Inline CSS
  2. Internal CSS
  3. External CSS

1. Inline CSS

The Inline style of CSS is used to insert a special style on an individual element or line. The style attribute can be used to style inline on the associated tag or element. It alleviates the benefits of the style sheets. Thus, it is suggested to use the inline method sparingly.

Example:

<!DOCTYPE html> 
<html> 
<body>  
<h1   style="text-align:center;color:green;">Inline Style   Sheet</h1> 
<p   style="color:blue;font-size:20px;">This is blue</p>
</body> 
</html>    
How to include CSS

Advantages of Inline CSS:

  • The inline style sheets play a crucial role in the interpretation of minimum number of styles.
  • These methods have the capability to override some other techniques of style specification at a local level.
  • It has the highest specificity or precedence within a document.
  • These styles are quick and easy to insert, and we don’t have to be worried about specifying a proper selector of CSS. Since we are entering various styles on the element, we can wish to change.
  • We can insert the attribute of the style, i.e., valid on every element of HTML.

Disadvantages of Inline CSS:     

  • An Inline CSS method does not separate any information of the style from content.
  • Control classes may not be designed to handle multiple types of elements inside the document.
  • These methods do not have the capability to be reused.
  • These are complicated to edit because these styles can’t be stored at just a place.
  • It does not allow to style the pseudo-classes and pseudo-codes in inline CSS.
  • Also, it does not allow for browser cache support.

2. Internal CSS

This style sheet can be used when one individual page of HTML has a specific style. All the other elements can be affected by this style of the existing page.

 It is specified in the style element or tag in the head part of HTML.

Example of Internal CSS:

 <!DOCTYPE html>  
 <html> 
  <head>  
 <style>   body {   background-color: pink;   }   h1 {   color: blue;   margin-left: 80px;   }   </style>  
 </head> 
  <body>  
 <h1>Internal Style Sheet</h1> 
  <p>This is blue</p>  
 </body>  
 </html>     
How to include CSS

Advantages of Internal CSS:

  • Internal style sheets of CSS affect only the page on which they are present.
  • These style sheets may use the IDs and the classes.
  • Internal CSS does not require to upload files in a collective form.
  • These types of style sheets may contain higher precedence as compared with the external CSS.
  • Internal CSS can be used to design specific layout of the page such as CMS template or email form, where the cascading style sheet is included once.

Disadvantages of Internal CSS:

  • These style sheets can affect only the page that it belong to.
  • The time for page loading may be increased with the use of internal CSS.

3. External CSS

These type style sheets are used to add style on all pages or multiple pages. It is optimal for this situation because it provides us to alter the entire look of the web site documents by altering only one file. Here, we will specify the code of CSS in the CSS file. The extension should be .css like style.css.

External CSS applies <link> element over all the pages. The <link> element must be inserted within the head part of HTML.

Example of External CSS:

 <!DOCTYPE html> 
  <html> 
  <head> 
  <link rel="stylesheet"   type="text/css" href="mystyle.css"> 
  </head>  
 <body> 
  <h1>This is a heading</h1>  
 <p>This is a paragraph.</p> 
  </body> 
  </html>    

These style sheets can be addressed inside each text editor. However, it should be saved with the .css extension. These files must not include the elements of HTML.  

.css file (“mystyle.css”)

    body {       background-color: pink;   }   h1 {     color: blue;     margin-left:   80px;   }    

Advantages of External CSS:

  • One of the main advantages of this CSS style sheet is that it is used on multiple records while being handled from only one style sheet. It improves convenience and efficiency, and also contains code DRY.
  • The grouping strategies and selectors are utilized to assemble the styles in distinct circumstances.
  • The classes may be written, which can be used in multiple elements types of HTML inside various documents.

Disadvantages of External CSS:

  • External CSS may reduce the loading time in a few situations.
  • It may not be rational when there is not sufficient styling condition for justifying the external sheets.
  • The exterior template should be stacked.
  • An additional download is required to import the style data for every document.
  • It is not feasible to deliver the document until external sheets are loaded.

Numerous Style sheets

If any property has been described for a similar selector in distinct style sheets, then the value of the end read will be applied.

Suppose that an external CSS has the style for any <h1> element as follows:

    body {   background-color: lightblue;   }   h1 {   color: orange;   }    

After that, suppose that an internal CSS has the style for any <h1> element. It is as follows:

    h1 {   color: navy;   }    

Example:

If above internal CSS is described after the external CSS link, then this <h1> tag will be “navy”:

<!DOCTYPE html> 
  <html> 
  <head>  
 <link rel="stylesheet"   type="text/css" href="mystyle1.css"> 
  <style>   h1 {   color: navy;   }   </style> 
  </head> 
  <body>  
 <h1>This is a heading</h1>  
 <p>This is a paragraph.</p> 
  </body>  
 </html>   
How to include CSS

Example:

But, if above internal CSS is described before the external CSS link, then this <h1> tag will be “orange”:

<!DOCTYPE html>   
<html>   
<head>  
 <style>   h1 {   color: navy;   }   </style> 
  <link rel="stylesheet"   type="text/css" href="mystyle1.css"> 
  </head>   
<body>  
 <h1>This is a heading</h1>  
 <p>This is a paragraph.</p>  
 </body>  
 </html>    
How to include CSS

Related Topics

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

Untitled Reusable Block

  ...

1 minute read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

4 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.