Site icon All About HTML

An In-Depth Guide to Applying CSS in HTML: Techniques, Pros, and Cons.

An In-Depth Guide to Applying CSS in HTML

Mastering CSS: An In-Depth Guide to Applying CSS in HTML: Techniques, Pros, and Cons

Introduction:
CSS (Cascading Style Sheets) is an integral part of web development, and it plays a crucial role in determining the look and feel of a website. In this blog post, you will learn about the various ways to apply CSS in HTML, the pros and cons of each method, and which method is recommended for use.

An In-Depth Guide to Applying CSS in HTML5

There are three ways to implement CSS in HTML:

You might also want to learn difference between inline and block level elements

Applying CSS using Inline Styles:

Inline styles are added to an HTML element using the “style” attribute. The syntax for adding an inline style is as follows:

<p style="color: red; font-size: 16px;">
    This is a sample text with inline styles.
</p>

Pros:

Cons:

Applying CSS using Internal Stylesheets:

Internal stylesheets are added to an HTML document using the <style> tag, which is placed within the <head> section of the HTML document.

The HTML <style> tag is used to define styles for the content within a web page. The styles defined inside the <style> tag are known as internal styles and apply only to the current document. The <style> tag is placed within the <head> section of an HTML document. The styles defined within the <style> tag are only applied to the current document. This method is best suited for small projects or for adding styles to a single page.

The syntax for adding an internal stylesheet using <style> tag is as follows:

<head>
  <style>
    p {
      font-size: 20px;
      color: blue;
    }
  </style>
</head>

<body>
  <p>This is a blue, 20-pixel-high paragraph.</p>
</body>

In this example, the <style> tag is used to define the styles for all <p> elements within the document. The styles inside the <style> tag specify that the text within a <p> element should be 20 pixels high and blue. The <p> element in the <body> section of the document then follows these styles.

Internal styles are a convenient way to add styles to a web page, but they can become cumbersome as the size of the document grows. For larger projects, it’s often recommended to use external style sheets, which can be linked to a page using the <link> tag.

Pros:

Cons:

09 - Applying Styles/CSS - HTML5 Essential Training URDU / HINDI
Mastering CSS: An In-Depth Guide to Applying CSS in HTML: Techniques, Pros, and Cons

Applying CSS using External Stylesheets:

External stylesheets are added to an HTML document using the <link> tag, which is placed within the <head> tag of the HTML document.

The HTML <link> tag is used to specify the relationship between the current HTML document and an external resource, such as a style sheet. The <link> tag is placed within the <head> section of an HTML document and does not have a closing tag.

External style sheets are separate files that contain CSS code and are linked to an HTML document using the <link> tag. The styles defined within an external style sheet can be applied to multiple HTML documents. This method is best suited for large projects with many pages that require a consistent style.

Here’s an example of how to use the <link> tag to link to a CSS file:

The syntax for adding an external stylesheet is as follows:

/*style.css file*/
p {
      font-size: 20px;
      color: blue;
}
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
  <p>This is a blue, 20-pixel-high paragraph defined in an external stylesheet.</p>
</body>

In this example, the rel attribute specifies the relationship between the HTML document and the linked resource, which is a stylesheet. The type attribute indicates the type of resource being linked to, which is CSS. The href attribute specifies the location of the resource, which is a file named style.css.

Pros:

Cons:

It’s recommended to use external style sheets for larger projects and internal style sheets or inline styles for smaller projects or for adding styles to specific elements. The use of inline styles should be minimized as it can make it difficult to maintain a consistent style across a website.

Exit mobile version