Last updated: Apr 5, 2024
Reading time·4 min
style
tag to apply multiple properties to an elementTo apply multiple inline CSS styles to an HTML element:
style
attribute on the element.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div style=" margin-top: 100px; margin-left: 100px; background-color: lightgreen; padding: 10px 20px; " > bobbyhadz.com </div> </body> </html>
The code sample applies multiple styles to the div
element.
We set the
style
attribute on the div
element to a string.
The style
attribute contains CSS styling declarations to be applied to the
element.
The attribute enables you to apply inline styles to the specific element.
<div style=" margin-top: 100px; margin-left: 100px; background-color: lightgreen; padding: 10px 20px; " > bobbyhadz.com </div>
Each CSS property and value are separated by a colon :
.
The CSS property-value pairs are separated by a semicolon ;
.
You can use a multiline string to make your inline style more readable.
However, you could also set multiple styles on a single line.
<div style="margin-top: 100px; margin-left: 100px;" > bobbyhadz.com </div>
The syntax for applying multiple CSS styles to an element uses the following format.
<div style="propertyA: valueA; propertyB: valueB; propertyC: valueC;" > bobbyhadz.com </div>
However, it should be noted that as this section of the MDN docs states:
It is recommended for styles to be defined in separate file(s).
style
tag to apply multiple properties to an elementYou can also use the style
tag to apply multiple properties to an element.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .box { margin-top: 100px; margin-left: 100px; background-color: lightgreen; padding: 10px 20px; } </style> </head> <body> <div class="box">bobbyhadz.com</div> </body> </html>
The style tag
can be placed in the head
tag of your HTML page.
Notice that I set a box
class on the div
and used the class as a selector
when applying the styles.
This approach is a bit more readable than styling the element inline, especially if you have to apply more than a few styles.
You can also use an external .css
file to apply multiple styles to an element.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="box">bobbyhadz.com</div> </body> </html>
And here is the related style.css
file.
.box { margin-top: 100px; margin-left: 100px; background-color: lightgreen; padding: 10px 20px; }
The code sample produces the same output.
Notice that we used a link
tag in the head
of the HTML file to load the
style.css
file.
<link rel="stylesheet" href="style.css" />
Your style.css
file should contain selectors and blocks of CSS properties and
values that are used to style the corresponding element(s).
.box { margin-top: 100px; margin-left: 100px; background-color: lightgreen; padding: 10px 20px; }
This approach is recommended by MDN when it comes to applying multiple styles to an element because it splits your HTML markup and your styles into two files.
Using the inline style
attribute is just fine when you only have to apply a
few CSS properties or you have to overwrite a global CSS style.
However, if you need to apply more than a few styles, it is much better to use an external CSS file.
This enables you to reuse the styles of the box
class in multiple HTML files.
For example, you can load the style.css
file in index.html
and about.html
and apply the box
class to elements on both pages.
box
class, it would get applied to index.html
and about.html
as both HTML pages load your central style.css
file.Conversely, if you only use the inline style
attribute, you would have to
update all your .html
pages separately.
You can learn more about the related topics by checking out the following tutorials:
title
Attribute using CSS