How To do I write CSS within HTML

The answer is – it depends! 1st, who cares if you apply notepad? Blaze, I acknowledge my web applied science* well enough that I could and accept answered entire projects out of notepad or about each by paper. Whatsoever is comfy for you to begin with. Just make sure you’re open to acquiring in the future. About programmers who are closed-minded end up not abiding ahead of the curve.

There are 3 main ways to put CSS in an HTML page.

In order from highest priority to lowest priority (inheritance), we have

  • inline CSS — these is rarely done via hypertext markup language. JavaScript tends to apply these formula when dynamically styling chemical element*. You should alone be using these during developing, but move to some kind of collection from rules (a stylesheet) in the future.
  • Home stylesheet inside the body/header — these are most often gone very belittled websites that do not require a lot of bells and whistlings. Administers of single page websites often employment these method just fine, but it’s not 100% compromising because you are placing CSS directly into an HTML file cabinet rather than keeping going a separate file to make it easier to switch out.
  • external stylesheet — most people will advocate this method. It’s the most flexile, in terms from just swapping come out styles, but it’s easier to automatically generate this CSS rules via LESS/SASS/SCSS. It’s as well has the added together benefit of allowing gzip compressing which web browser* could decompress for you, allowing for you to send even a lot data with less latency.

Inline CSS

  1. <nav class="main" style="background-color: red;">
  2. <div class="header" style="font-weight: bold; font-size: 24px;" />
  3. <div class="content" style="background-color: rgba(255, 255, 255, 0.5);" />
  4. </nav>

Internal Stylesheet

  1. <head>
  2. <style type="text/css">
  3. nav.main {
  4. background-color: green; /* will be overridden by body internal styles */
  5. }
  6. </style>
  7. </head>
  8. <body>
  9. <nav class="main">
  10. <div class="header" />
  11. <div class="content" />
  12. </nav>
  13. <style type="text/css">
  14. nav.main {
  15. background-color: red;
  16. }
  17. div.header {
  18. font-weight: bold;
  19. font-size: 24px;
  20. }
  21. div.content {
  22. background-color: rgba(255, 255, 255, 0.5);
  23. }
  24. </style>
  25. </body>

External CSS

  1. <!-- index.html -->
  2. <head>
  3. <!-- can place it here -->
  4. <link rel="stylesheet" type="text/css" href="main.css" />
  5. </head>
  6. <body>
  7. <nav class="main">
  8. <div class="header" />
  9. <div class="content" />
  10. </nav>
  11. <!-- or here -->
  12. <link rel="stylesheet" type="text/css" href="main.css" />
  13. </body>


  1. /* main.css */
  2. nav.main {
  3. background-color: red;
  4. }
  5. div.header {
  6. font-weight: bold;
  7. font-size: 24px;
  8. }
  9. div.content {
  10. background-color: rgba(255, 255, 255, 0.5);
  11. }

(You van write your CSS codes anywhere inside the <style> tags if used in the same page as the HTML code)

1)Adding CSS inside HTML is called inline CSS.
Its pretty simple

<p style=”color:blue;”>This will be displayed in blue</p>
<p style=”color:red;”>This will be displayed in red</p>

2)You can also add attributes to all occurances of an element:
Called internal CSS:
3)Having a separate CSS document and linking the same in HTML, called an external style sheet.
You can link it using:
<link rel=”stylesheet” type=”text/css” href=”mystyle.css”>
Assuming your CSS file is called mystyle.CSS



%d bloggers like this: