HTML style attributes and CSS are essential tools for designing and enhancing the appearance of web pages. While HTML style attributes allow inline styling for specific elements, CSS provides a more robust and efficient way to apply consistent styles across multiple pages.
The style
attribute in HTML allows you to apply inline CSS directly to an element. This method is quick for small changes but not recommended for larger projects due to its lack of scalability.
<p style="color: red; font-size: 18px;">This is an inline styled paragraph.</p>
CSS (Cascading Style Sheets) is a stylesheet language used to control the look and feel of a web page. It separates content (HTML) from presentation (CSS), making websites easier to maintain and scale.
p { color: blue; font-family: Arial, sans-serif; }
<h1 style="color: green;">This is an inline-styled heading.</h1>
<style> body { background-color: #f0f0f0; } </style>
Link to an external CSS file for consistent styling across multiple pages.
<link rel="stylesheet" href="styles.css">
<p style="color: blue;">This text is blue using the <code>color</code> property.</p>
This text is blue using the color
property.
<p style="background-color: lightyellow; padding: 10px;">This paragraph has a light yellow background.</p>
This paragraph has a light yellow background.
<p style="margin: 20px; background-color: lightgray; padding: 10px;">This paragraph has a margin of 20px.</p>
This paragraph has a margin of 20px.
<p style="padding: 20px; background-color: lightblue;">This paragraph has 20px of padding inside it.</p>
This paragraph has 20px of padding inside it.
<div style="height: 100px; width: 100px; background-color: coral;"></div>
The box above is 100px by 100px.
<p style="background-color: lightgreen; padding: 10px;">Resize your browser and adjust this inline example for smaller screens in your projects.</p>
Resize your browser and adjust this inline example for smaller screens in your projects.
.hover-example {
background-color: pink;
transition: background-color 0.3s ease;
}
.hover-example:hover {
background-color: purple;
color: white;
}
Hover over this paragraph to change its background and text color.
<p style="border: 2px dashed green; padding: 10px;">This paragraph has a dashed green border.</p>
This paragraph has a dashed green border.
<p style="font-family: Arial, sans-serif; font-size: 20px; font-weight: bold; text-align: center; text-decoration: underline;">
This text is styled with various font and alignment properties.
</p>
This text is styled with various font and alignment properties.
<p style="color: white; background-color: navy; padding: 15px; margin: 10px; border: 2px solid white; text-align: center;">
This is a combined example using multiple inline CSS properties together.
</p>
This is a combined example using multiple inline CSS properties together.
<div>
, <p>
, and <h1>
.HTML style attributes apply inline styling, while CSS separates styles from HTML, offering greater flexibility and maintainability.
Yes, you can use a combination of inline, internal, and external CSS in a single project.
CSS rules are applied in a cascading order: inline styles have the highest priority, followed by internal, then external CSS.
Some common CSS properties include color
, background-color
, margin
, padding
, and font-size
.
External CSS ensures consistency, reduces redundancy, and simplifies maintenance for larger projects.