html

How to Assign Multiple Styles on an HTML Element?

The websites without styles are just a structure and are less effective for viewers. To make work more prominent and professional, developers apply multiple CSS properties to the same HTML element. Using these properties, the overall look of the website is more attractive. This article demonstrates different methods to apply multiple styles on HTML elements.

How to Assign Multiple Styles on an HTML Element?

There are three main ways by which multiple CSS styles are implemented to HTML elements. These are described below:

Method 1: Inline CSS

In Inline CSS, the styling properties are placed inside the opening tag of the HTML element before closing tags. This method of CSS is mostly used where the line of CSS code is not more than two. It makes code very messy sometimes and becomes a headache while applying changes.

The syntax is described below:

"style="property1:value1;property2:value2" "

Here, the “property1” is the name of the CSS property that is being used, and “value1” is its value; it can be in the keywords, pixels, percentages, etc.

Example: Assign CSS Properties Using Inline Method

In the HTML file, add a div element inside that and create multiple heading tags:

<div>

<h3>Hello</h3>

<h3>Welcome to Linuxhint</h3>

</div>

After compiling the above code, the webpage displays like this:

The output shows that elements are placed on the webpage.

Add CSS Properties

To add inline CSS property, add the “style” keyword. For instance, inline CSS properties are used in below code snippet:

<div>

<h3 style="color:darkcyan; font-size:20px">Hello</h3>

<h2 style="background-color: dimgrey;">Welcome to Linuxhint</h2>

</div>

The color and font-size of “<h3>” is set to darkcyan and 20px, respectively. The background of “<h2>” is set to dimgray color.

After adding CSS properties, the HTML page looks like this:

The output confirms that CSS properties apply to HTML elements.

Method 2: Internal CSS

In the internal CSS, the CSS properties are written in the “<style>” tag in the same HTML file inside the “<head>” tag. Let practice the below example:

Example: Assign CSS Properties Using Internal Method

In the HTML file, add a div element, and inside it makes a “<h3>” element:

<div>

<h3 class="heading">Hello</h3>

<h3 class="heading">Welcome to Linuxhint</h3>

</div>

Style “heading” Class

Create a “<style>” tag in the “<head>” tag and make heading class block inside it. After that, type CSS properties are below:

.heading{

color:white;

font-size:20px;

background-color: darkcyan;

}

In the above code snippet, the color is set to white and the background color of darkcyan is set to an HTML element that contains “heading” class.

After compiling the above script:

The output shows that CSS properties are applied using the internal method.

Method 3: External CSS

The most widely used method of CSS is external CSS. The separate file is created with the extension of “css”. This file is linked to multiple HTML pages and all those pages can write their CSS on this file. The CSS file is created as:

The above snapshot shows that the CSS file is created with the name “style.css”.

Style “heading” Class

Paste the same class with properties in “style.css”. In this file, there is no need to add the “<style>” tag:

Linking CSS File with HTML File

To link the CSS file with HTML, the “<link>” tag is used. It contains two attributes “rel” and “href”. The “rel” defines what sort of file is being linked and “href” contains the path of the file that is being linked:

<link rel="stylesheet" href="style.css" />

After linking of a CSS file, the webpage looks like:

That is how multiple styles can be assigned to an HTML element.

Conclusion

To assign multiple styles to an HTML element, use inline CSS, internal CSS, and external CSS. Inline CSS is added with the element tag before the closing angle of the starting tag using the “style” keyword. Internal CSS is added inside the “<style>” tag in the “<head>” portion. For external, the separate file is created with the extension of “css” and by using the “<link>” tag that CSS file is included inside the HTML file.

About the author

Abdul Moeed

I'm a versatile technical author who thrives on adaptive problem-solving. I have a talent for breaking down complex concepts into understandable terms and enjoy sharing my knowledge and experience with readers of all levels. I'm always eager to help others expand their understanding of technology.