0 votes
104 views
in HTML by (3.5k points)
edited

How to link CSS to HTML?

1 Answer

0 votes
by (3.5k points)
edited

Before start with how to link CSS with HTML, 

Let’s have a look at: What is CSS?

Full form of CSS stands for Cascading Style Sheets (CSS) which is used to format the layout of a webpage.

With the help of CSS, someone can control the color, font, the size of text, the spacing between elements and also how elements are positioned and laid out, what background images or background colors to be used, different displays for different devices and screen sizes, and so many more as well.

Types of CSS:

So there are three ways to add CSS to HTML documents :

  • Inline – by putting the style attribute inside HTML elements
  • Internal – by putting  a <style> element in the <head> section
  • External – by adding a <link> element to link to an external CSS file

The most common and used way to add CSS, is to have the styles in external CSS files. 

Inline CSS

An inline CSS can be used to apply a unique and also different style to a single HTML element.

An inline CSS has the style attribute of an HTML element.

Now put the text color of the <h1> element to red, and the text color of the <p> element to blue:

<h1 style="color:red;">A Blue Heading</h1>
 
<p style="color:blue;">A red paragraph.</p>

Internal CSS

An internal CSS can be used to define a style for a single HTML page.

An internal CSS is used to define in the <head> section of an HTML page and also  within a <style> element.

Now let’s have an example of the text color of ALL the <h1> elements (on that page) to blue, and the text color of ALL the <p> elements to red. 

The page will be displayed with “powderblue” background color:

<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1   {color: blue;}
p    {color: red;}
</style>
</head>
<body>

External CSS

An external style sheet concept is normally used to define the style for many HTML pages.

In order to start using an external style sheet, put a link to it in the <head> section of each HTML page:

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

Related questions

0 votes
1 answer 100 views
asked Jun 24, 2023 in HTML by Doubtly (98.9k points)
0 votes
1 answer 166 views
asked Aug 19, 2022 in HTML by codelikepro (3.5k points)
0 votes
1 answer 140 views
asked Jun 24, 2023 in HTML by Doubtly (98.9k points)
0 votes
1 answer 125 views
asked Aug 19, 2022 in HTML by codelikepro (3.5k points)
0 votes
1 answer 102 views
0 votes
1 answer 107 views
asked Jun 12, 2023 in HTML by radhe (2.2k points)

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

559 users

...