HTML Styles

The HTML style attribute enables you to add custom styles directly to individual elements, giving you control over properties like color, font, size, and more. It allows you to tailor the appearance of specific elements, enhancing the overall visual design of your web pages.


HTML Styles vs. CSS

HTML styles are written in CSS, which stands for Cascading Style Sheets. CSS is a language that is used to control the presentation of web pages. CSS is separate from HTML, but it is often used in conjunction with HTML to create more visually appealing web pages.


Inline Styles

Inline styles are the simplest way to add styles to your web pages. They are written directly inside the HTML element that you want to style. For example, the following code will change the font size of the h1 tag to 24px:

Example
Try yourself
        
            <h1 style="font-size: 24px;">This is a heading</h1>
        
    

Border Styles

Apply different border styles to an element:

Try yourself
        
            <div style="border: 1px solid black;">This is a bordered element</div>
        
    
Background Image

Set a background image for an element:

Try yourself
        
            <div style="background-image: url('image.jpg');">This has a background image</div>
        
    
Text Decoration

Add text decoration, such as underline or strikethrough:

Try yourself
        
            <p style="text-decoration: underline;">This text is underlined</p>
<p style="text-decoration: line-through;">This text has a strikethrough</p>
        
    
Margin and Padding

Adjust the margin and padding of an element:

Try yourself
        
            <div style="margin: 10px; padding: 20px;">This has margin and padding</div>
        
    
Width and Height

Set the width and height of an element:

Try yourself
        
            <div style="width: 200px; height: 150px;">This has a specific width and height</div>
        
    
Text Shadow

Add a text shadow effect to an element:

Try yourself
        
            <h1 style="text-shadow: 2px 2px 4px #000000;">This has a text shadow effect</h1>
        
    

Internal Styles

Internal styles are stored in a style element within the head section of your HTML document. For example, the following code will create a style element and add a style rule to change the font size of all paragraphs to 18px:

Try yourself
        
            <style>
  p {
    font-size: 18px;
  }
</style>
        
    

External Styles

External styles are stored in a separate CSS file. External styles are often used when you want to share your styles between multiple web pages. To use an external style, you need to link to the CSS file from your HTML document. For example, the following code will link to a CSS file called style.css:

Try yourself
        
            <link rel="stylesheet" href="style.css">
        
    

The cascade

When you use multiple styles to control the same element, the cascade determines which style will be applied. The cascade is a set of rules that determines which style will be applied when multiple styles are applied to the same element. The cascade is based on the following rules:

1. Specificity

Specificity is a way of determining which style will be applied when multiple styles are applied to the same element. The more specific a style is, the more likely it is to be applied. The specificity of a style is determined by the following factors:

Importance

Importance is a way of overriding the cascade. A style with importance set to high will be applied, even if it is less specific than another style. To set a style's importance to high, use the !important keyword

Inheritance

Inheritance is a way of applying styles to elements that are not explicitly styled. When an element is not explicitly styled, it inherits the styles of its parent element. For example, if the h1 tag is not explicitly styled, it will inherit the styles of the body element.


Conclusion

HTML styles are a powerful way to control the appearance of your web pages. By using HTML styles, you can create a consistent look and feel across your web pages, make your web pages more accessible to users with disabilities, and improve the overall appearance of your web pages.