HTML - style Attribute



The HTML style attribute contains a CSS styling declaration and is used to apply it to an element. This is a global attribute, and it is recommended to define styles in a separate files. The style attribute and the <style> element have the same purpose, to allow quick styling.

If the style attribute is used inside any element for styling, you can call it inline CSS.

Example

The following program shows the usage of the ‘style’ attribute.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <h1 style="color: red; font-style: italic; font-size: 30px;">This h1 heading.</h1>
   <p style="color: green; font-weight: bold; font-size: 25px;">This is the paragraph.</p>
</body>
</html>

On running the above code, it will generate an output consisting of the text applied with CSS displayed on the webpage.

Example

Consider the another scenario, where we are going to use the style attribute with the input field.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <form> userName: <input type="text" style="width: 300px; height: 40px; border: 2px solid green; border-radius: 10px;">
      <br>
      <br> Password: <input type="text" style="width: 250px; height: 35px; border: 1px solid blue; border-radius: 5px;">
   </form>
</body>
</html>

When we run the above code, the output window will pop up displaying the input field applied with CSS on the webpage.

Example

Let's look at the following example, where we are going to use the style attribute with the button element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <p>HTML button styled with inline CSS</p>
   <button style="width: 100px; padding: 10px; font-size: 16px; border-radius: 10px; border: none; background-color: green; color: white;">Submit</button>
</body>
</html>

On running the above code, the output window will pop up displaying the button applied with CSS on the webpage.

Example

Following is the example, where we are going to use the style attribute with the div element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <p>HTML div element styled with inline CSS</p>
   <div style="width: 210px; height: 120px; border: 1px solid red; text-align: center; border-radius: 5px; float: left; margin: 0px 10px;">
      <h1>Section 1</h1>
      <p>HTML: It is stand for Hyper Text Markup Language.</p>
   </div>
   <div style="width: 210px; height: 120px; background-color: aqua; border-radius: 10px; text-align: center; float: left;">
      <h1>Section 2</h1>
      <p>CSS: It is stand for Cascading Style Sheet.</p>
   </div>
</body>
</html>

When we run the above code, it will generate an output consisting of the dive element applied with CSS displayed on the webpage.

Example

In the following example, we are going to use the style attribute with the list tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <p>Frontend Technologies: </p>
   <ul style="list-style: none; width: 40%; height: 220px; background-color: aquamarine; margin: 0; padding: 0;">
      <li style="color: red; background-color: rgb(192, 192, 16); padding: 10px; width: 90%; margin: 5px auto; position: relative;">HTML</li>
      <li style="color: rgb(215, 215, 223); background-color: rgb(12, 95, 4); padding: 10px; width: 90%; margin: 5px auto;">CSS</li>
      <li style="color: rgb(235, 230, 231); background-color: rgb(49, 49, 3); padding: 10px; width: 90%; margin: 5px auto;">JavaScript</li>
      <li style="color: rgb(0, 183, 255); background-color: rgb(111, 19, 151); padding: 10px; width: 90%; margin: 5px auto;">Angular</li>
      <li style="color: rgb(18, 20, 19); background-color: beige; padding: 10px; width: 90%; margin: 5px auto;">React</li>
</body>
</html>

On running the above code, the output window will pop up , displaying the list items applied with CSS on the webpage.

Example

In this example, we are using the style attribute with the table tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML style attribute</title>
</head>
<body>
   <!--example of the style attribute-->
   <p>'Style' attribute with table</p>
   <table style='border: 1px solid black; border-collapse: collapse; width: 100%;' border='1'>
      <tr>
         <th style="background-color: blueviolet; color: white;">Name</th>
         <th style="background-color: blueviolet; color: white;">Email</th>
         <th style="background-color: blueviolet; color: white;">Gender</th>
      </tr>
      <tr>
         <td style="background-color: aqua;">Aman kumar</td>
         <td style="background-color: aqua;">aman12@gmail.com</td>
         <td style="background-color: aqua;">Male</td>
      </tr>
      <tr>
         <td style="background-color: aquamarine;">Revathi Satya</td>
         <td style="background-color: aquamarine;">revathi23@gmail.com</td>
         <td style="background-color: aquamarine;">Female</td>
      </tr>
      <tr>
         <td style="background-color: rgb(8, 197, 197);">Sarika Singh</td>
         <td style="background-color: rgb(8, 197, 197);">sarika34@gmail.com</td>
         <td style="background-color: rgb(8, 197, 197);">Female</td>
      </tr>
   </table>
</body>
</html>

When we run the above code, it will generate an output consisting of the table applied with CSS displayed on the webpage.

html_attributes_reference.htm
Advertisements