HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

HTML Style Attribute

Using the inline technique, the style property of HTML allows CSS to be applied to an HTML element.

Using the property and value pair enclosed in double quotes within the style attribute of the HTML tag, we can apply CSS to the style attribute. Multiple property-value pairs are denoted with a semicolon (;) if there are any.

In addition to seeing a few examples below, we will learn about the syntax in great depth.

The styles given in the HTML style element or an external style sheet are examples of styles that the style attribute will override unless they are set using the ! Important keyword.

Since the style property is a component of global HTML attributes, any HTML tag can utilize it.

This article will explain the HTML style property and provide examples to help you learn how to use it. In HTML, styles are guidelines that specify how a page should appear in a browser. Style sheets can be included in the HTML content or added separately. In HTML, style may be applied in three different ways:

  1. Inline Style: Using the style property within the HTML start element is how this technique works.

2. Embedded Style: This technique uses the style element inside the document's <head> element.

3. External Style: Using the <link> element, an external CSS file is referred to in this technique.

Syntax

<ElementName style="StyleRule; StyleRule2;......">content</ElementName>

4. Inline Style: The CSS rules for inline styling are written inside the beginning tag using the style attribute. The style attribute includes several CSS properties and value pairs. A semicolon (;) divides each pair of "property: value." For any applicable style set, this attribute will take precedence over the style properties worldwide.

Example: To add the different styling characteristics, supply the style property, which defines the internal style in this example.

<html>

<head>

<title>Example for Inline Styling</title>

</head>

<body>

<h2 style="color:red;font-size:39px;">

Example of Inline Style

</h2>

<h3 style="color:blue;">This is First line</h3>

<p style="color:maroon;font-size:30px;"> 

This is the Second line

</p>

<hr style="border-color:black;">

</body>

</html>

Output

HTML Style Attribute

5. Embedded StyleInternal or embedded style sheets only have an impact on the document in which they are embedded. The <style> element is used in the <head> section of an HTML page to declare embedded style sheets.

Example: The embedded style attribute is explained in this example.

<html lang="en">

<head>

<style type="text/css">

body {

background-color: lightpink;

}

h1 {

color: black;

font-family: arial;

}

p {

color: red;

font-family: Times New Roman;

}

</style>

<title>Example for embedded style</title>

</head>

<body>

<h1>Example for Embedded Style</h1>

<p>This is first line</p>

</body>

</html>

Output

HTML Style Attribute

6. External Style Sheet: When applying CSS to several web pages, the External Style Sheets approach might be helpful. You may connect to an external style sheet from an HTML file on your website, which contains all of the style guidelines in one document. External style sheets can be attached in two different ways:

  • Linking External Style Sheets: This technique uses the <link> tag to link an external style sheet to an HTML document.

Example: The external style sheet that adds the different stylistic features is described in this example.

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet"

type="text/css"

href="/html/css/externalstyle.css">

<title>Example for external styling</title>

</head>

<body>

<h3>Example for linking external style sheet</h3>

<p>This is first line</p>

</body>

</html>

Output

HTML Style Attribute
  • Importing External Style Sheets: "@import" may be used to load external style sheets into an HTML document. The browser is instructed to load the CSS file using the "@import" declaration. The <style> element can also be used to incorporate other CSS rules.

Example: To add the styling properties from the external style, follow the instructions in this example to import the external style sheet.

<html>

<head>

<style type="text/css">

@import url("/html/css/importstyle.css");

p {

color: blue;

font-size: 20px;

}

</style>

<title>Example for importing external styling</title>

</head>

<body>

<p>Example for external style sheet using import statement</p>

<h3>This is first line</h3>

</body>

</html>

Output

HTML Style Attribute

List of HTML style attribute values 

An HTML element's style attribute provides several values that allow a developer to modify its font color, font size, background color, background image, margin, padding, and other features.

1. Color

This attribute allows us to set the text's font color using the color name, HEX code, or RGB code of the color.

<html>

   <body>

      <h1 style="color:red;">This is an example for color attribute</h1>

   </body>

</html>

Output

HTML Style Attribute

2. background-color

This property sets the background color of various HTML elements, such as div, span, and body. We can use the HEX code, RGB code, or the color name of the color we want.

<html>

    <body>

     <h2 style="background-color:pink; color:black">This is an example for background-color</h2>

    </body>

</html>

Output

HTML Style Attribute

3. font-size

This attribute is used to adjust the text's font size. For this, we may utilize other units, such as px, em, or rem.

<html>

    <body>

        <h2 style="font-size:30px">This is an example for font-size</h2>

    </body>

</html>

Output

HTML Style Attribute

4. margin

This attribute allows an HTML element, such as a p, div, span, etc., to have a margin applied to all four of its sides.

  • Margin-top: To add a margin just to the HTML element's top.
  • Margin-right: To add margin to an HTML element to its immediate right.
  • Margin-bottom: To add a margin just to the HTML element's bottom.
  • Margin-left: This style applies a margin to the HTML element just to its left.
<html>

    <body style="background-color:pink">

<div style="margin:100px; background-color:black; width:50px; height:50px"></div>

    </body>

</html>

Output

HTML Style Attribute

5. Padding

This attribute specifies the distance between an HTML element's boundaries and content. All four sides can have padding applied to them using the "padding" property, or we can utilize specific padding values like

  • Padding-top: To add padding to the element's top.
  • Padding-bottom: To add padding to the element's top.
  • Padding-left: This style adds padding to the element's top.
  • Padding-right: To add padding to the element's top.
<html>

    <body>

        <div style="padding:20px; background-color:pink; width:100px; height:100px">

<div style="background-color:red; width:50px; height:50px"></div>

        </div>

    </body>

</html>

Output

HTML Style Attribute

6. Width and height

The width and height properties of a shape or an HTML element, such as a div, may be specified. In earlier div instances, width and height were used.

<!DOCTYPE html>

<html>

    <body style="background-color:pink">

<div style="background-color:black; width:50px; height:50px"></div>

    </body>

</html>

Output

HTML Style Attribute

7. letter spacing

This characteristic specifies the character spacing within a word. It can be given a positive or negative pixel value to increase or reduce the distance between characters.

<html>

    <body>

<p style="letter-spacing:2px; text-align:left; padding:5px 0; border: 1px solid blue; font-size:20px">This is an example for letter spacing</p>

    </body>

</html>

Output

HTML Style Attribute