How to use class In HTML

We can use class on that particular div and apply CSS either inside a <style> tag or linking an external CSS file.

  • In case of internal CSS: We need to define Class in the <head> section of HTML within <style> element.
  • In case of External CSS: We need to create a separate .css file and include it in HTML code inside <head> section using <link> element.

Example : This is the example with respect to above approach by using class.

html
<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="color.css">
    <title>
        HTML Div Tag
    </title>
</head>

<body>
    <center>
        <div class="color">
            <!--open tag of Div!-->
            <caption>
                <h1>w3wiki</h1>
            </caption>
            <h1>Inline CSS is not USED in THIS method.
            </h1>
        </div>
        <!--closing tag of Div!-->
    </center>
</body>

</html>
CSS
.color {
    height: 400px;
    width: 600px;
    border: 1px solid;
    background-color: 009900;
}

Output:          

HTML Div Tag Example Output

HTML Div Tag

The HTML <div> tag defines sections in HTML documents, serving as containers styled with CSS or controlled with JavaScript. It’s easily customized using class or id attributes and can contain various content.

Note: Browsers add line breaks before and after <div> elements by default.

Similar Reads

Div tag Usage:

The div tag is the block-level tag.It is used for applying styles and layout structure

1. Using class:

We can use class on that particular div and apply CSS either inside a