Last Updated : 07 Jun, 2025
The HTML style attribute allows CSS to be applied directly within HTML tags. This enables styling of an element without the need for an external CSS file or a <style> block. It provides a quick, inline way to apply styles to individual elements.
Syntax
<tagname style="property: value;"> Content goes here </tagname>
When we apply styles using the style attribute, they only affect that specific HTML element. Unlike external or internal CSS, which can affect multiple elements at once, inline styles are individual and specific.
Now let's understand this with the help of example
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Style Attribute Example</title>
</head>
<body
style="display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; margin: 0;">
<h1 style="color: green; text-align: center;">Welcome to My Website</h1>
<p style="font-size: 18px; color: purple; text-align: center;">This is a simple paragraph with purple text and a
font size of 18px.</p>
<div
style="width: 300px; height: 150px; background-color: lightblue; padding: 20px; border: 2px solid blue; display: flex; justify-content: center; align-items: center;">
This box has a light blue background, padding, and a blue border.
</div>
</body>
</html>
Commonly Used CSS Properties in the style Attribute:
<p style="color: blue;">This is a blue paragraph.</p>
<p style="font-size: 20px;">This is a larger paragraph.</p>
<div style="background-color: yellow;">This is a yellow box.</div>
<h1 style="text-align: center;">This is a centered heading.</h1>
<div style="padding: 10px;">This box has padding around it.</div>
<div style="margin: 20px;">This box has margin around it.</div>
<div style="width: 200px; height: 100px; background-color: grey;">This box is 200px wide and 100px tall.</div>
<p style="border: 1px solid black;">This paragraph has a border.</p>Other Types of Styling in HTMLBelow are the different types of styling in HTML: 1. Inline Style
Inline styling applies CSS rules directly inside an HTML tag using the style attribute. Multiple styling properties are written within the attribute, and each property is separated by a semicolon.
HTML
<html>
<head>
<title>Inline Styling</title>
</head>
<body>
<h1 style="color:Blue;font-size:25px;">
Example of Inline Style
</h1>
<p style="color:red;">First paragraph</p>
<p style="color:green;font-size:40px;">
Second paragraph
</p>
<hr style="border-color:orange;">
</body>
</html>
Output:
style attribute 2. Embedded StyleEmbedded or internal styles are written within the <head> section of the HTML document, inside a <style> tag. These styles only apply to the specific document where they are defined.
HTML
<html>
<head>
<style type="text/css">
body {
background-color: powderblue;
}
h1 {
color: black;
font-family: arial;
}
p {
color: yellow;
font-family: verdana;
}
</style>
<title>Embedded Styling</title>
</head>
<body>
<h1>Example of Embedded Style</h1>
<p>First paragraph.</p>
</body>
</html>
Output:
Embedded Style 3. External Style Sheet:External style sheets are useful when CSS needs to be applied to multiple web pages. They store all the style rules in a separate file that can be linked to from an HTML document. There are two ways to link external style sheets:
<html>
<head>
<link rel="stylesheet" type="text/css"
href="/html/css/externalstyle.css">
<title>External Styling</title>
</head>
<body>
<h3>Example of Linking External Style Sheet</h3>
<p>First paragraph.</p>
</body>
</html>
Output:
External Style
<html>
<head>
<style type="text/css">
@import url("/html/css/importstyle.css");
p {
color: powderblue;
font-size: 30px;
}
</style>
<title>Importing external Styling</title>
</head>
<body>
<h3>Example of external style sheet using import</h3>
<p>First paragraph</p>
</body>
</html>
Output:
Importing External Style Conclusionthe HTML style attribute allows quick, inline styling of individual elements without the need for external CSS. While ideal for small, specific changes, for larger projects, external or internal CSS is more efficient and maintainable.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4