CSS: Add CSS to HTML
3 ways to add CSS
3 ways to add CSS to HTML files.
- For single HTML element.
- For whole HTML page.
- For entire website.
CSS for Single HTML Tag
If you want to specify a style only for one particular HTML tag in a HTML file, you can embed a attribute style=val
inside the tag, like this:
<p>… this is <span style="color:red">RED</span></p>
CSS for Single HTML File
If you want to use CSS only for a single file, you can embed it in HTML file's header using the
<style>
tag,
like this:
<head> <style> p {color:red} </style> <title>My Title</title> </head>
This will make all “p” (paragraph) elements red.
CSS for Entire Site
To add CSS in your HTML file, do like this:
… <head> <link rel="stylesheet" href="mystyle.css" /> <title>My Title</title> </head> …
In the above example, whenever the HTML file is loaded, it will load the file mystyle.css
. The <link …>
line must be inside the “head” tag.
If you use the above for all your files, then you can control your whole site's appearance by modifying the single CSS file.