#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
HTML Fundamentals

HTML Styles

HTML styles are the visual rules, written in CSS, that control how HTML elements look: colours, fonts, spacing, sizes and more. HTML gives you three places to put those rules, the style attribute on an element, a <style> element in the page, or a separate stylesheet file. The style attribute is the quickest to try, so it is a good place to start before comparing all three.

The style attribute

Any element can take a style attribute containing one or more CSS declarations, each written as property: value and separated by semicolons. These are called inline styles.

<h2 style="color: darkgreen; text-align: center;">Admissions Open</h2>

<p style="font-size: 18px; background-color: #fff8e1; padding: 12px;">
  Classes start next Monday.
</p>

<p>Fees are <span style="font-weight: bold; color: crimson;">non-refundable</span>.</p>

There is no selector and there are no curly braces; the style applies only to that one element. It is handy for quick experiments, but it does not scale well.

Properties you will use most often

PropertyChangesExample value
colorText colournavy, #333
background-colorBackground behind the elementlightyellow
font-familyTypefaceArial, sans-serif
font-sizeText size1.2rem, 18px
text-alignHorizontal alignmentleft, center

Older tutorials sometimes show tags like <font> and <center> or attributes like bgcolor. These are obsolete; use CSS properties instead.

Three ways to add CSS, compared

  1. Inline, with the style attribute: affects one element.
  2. Internal, with a <style> block in the <head>: affects one page and uses normal selectors.
  3. External, with <link rel="stylesheet" href="styles.css">: one file shared by every page of the site.

External stylesheets are the normal choice for real sites. The browser downloads the file once and caches it, every page looks consistent, and changing a brand colour means editing one line instead of hundreds of tags.

<head>
  <link rel="stylesheet" href="styles.css">
  <style>
    .notice { background-color: #fff8e1; padding: 12px; }
  </style>
</head>

Why inline styles cause trouble later

Inline styles have very high priority in the cascade. They beat rules from your stylesheet unless those rules use !important. This leads to a frustrating situation: you change the colour in styles.css, refresh, and nothing happens, because an inline style on the element is still winning.

They also mix content with presentation, so the same declarations get copied onto many elements, and they cannot express hover states, media queries for mobile screens, or pseudo-elements. When you notice the same inline style on two elements, move it into a class.

A small syntax slip to watch: writing style="color=red" with an equals sign instead of a colon. The browser ignores the invalid declaration silently, so double-check the colon and semicolon.

When inline styles are reasonable

Inline styles also make pages heavier. A declaration repeated on fifty elements is sent fifty times, while a class in a cached stylesheet is downloaded once.

They still have uses: values calculated by JavaScript, such as setting a progress bar's width, one-off tests while debugging, and HTML emails, where many mail apps ignore external stylesheets.

In practice: learn properties with the style attribute, but put real site styles in an external stylesheet and target elements with classes.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course