0% completed
CSS adds style to HTML elements. When you load an HTML page, the browser reads both the HTML and the CSS code. The browser then applies the style rules from the CSS code to the HTML elements.
There are three common ways to add CSS:
style
attribute.<style>
block in the HTML document.<link>
tag.This example shows how to add CSS rules directly to HTML elements.
Explanation:
style
attribute is used on the <h1>
and <p>
tags.<h1>
text is blue.<p>
text has a font size of 18 pixels.This example shows how to add CSS rules within the HTML file.
Explanation:
<style>
block in the <head>
section contains the CSS rules.<h1>
elements get a red color.<p>
elements have a font size of 18 pixels.This example shows how to add CSS rules by linking to a separate CSS file.
HTML file (index.html):
CSS file (style.css):
Explanation:
<link>
tag connects to the CSS file named style.css
.<h1>
text to green and the <p>
text to have a font size of 18 pixels.Each method has its use cases. For small changes, inline CSS might be enough. Use internal CSS when you want all styles in one file. External CSS works best for larger projects where you need to reuse the same styles across multiple pages.
.....
.....
.....