×

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 Text Orientation

Introduction: The text-orientation property of CSS is used to specify the orientation of the text characters in the line of content. This property applies only with the vertical writing-mode and not...

4 minutes read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

5 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 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.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 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.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.