HTML width/height Attribute vs CSS width/height Property (original) (raw)

Last Updated : 5 Jun, 2026

In HTML5, the width and height attributes are supported by certain elements such as images and graphics-related tags. These attributes define the element's dimensions but can be overridden by CSS styles.

Example: CSS Overrides HTML Width and Height Attributes

**HTML:

**CSS:

/* CSS */ img { width: 400px; height: 400px; }

Some attributes have a stronger effect on an element's appearance, and overriding them may require using the !important rule in CSS.

**Example: Uses height/width attributes and height/width property.

html `

Comparison between height/width attributes and height/width property
<style>
    h1 {
        color: green;
    }
    .container {
        width: 95%;
        border: 2px solid black;
        padding: 5px;
        height: 400px;
    }
    
    .first {
        text-align: center;
        width: 45%;
        float: left;
    }
    
    .second {
        text-align: center;
        width: 45%;
        float: right; 
    }
</style>

GeeksforGeeks

    <h2>
        HTML height/width Attribute Vs
        CSS height/width property
    </h2>
    
    <div class="container">
        <div class="first">
            <h3>HTML width/height Attributes</h3> 
            <img src="" width="150px" height="150px"
                    style="border:2px solid #000000;">
            
            <iframe src="" width="150px" height="150px"
                    style="border:2px solid #000000;">
            </iframe>
            
            <canvas src="" width="150px" height="150px"
                    style="border:2px solid #000000;">
            </canvas>
            
            <svg src="" width="150px" height="150px"
                    style="border:2px solid #000000;">
            </svg>
        </div>
        
        <div class="second">
            <h3>CSS width/height Property</h3>
            <div width="150px" height="150px"
                    style="border:2px solid #000000;">
                This is div tag
            </div>
            
            <br><br> 
            
            <span width="150px" height="150px"
                    style="border:2px solid #000000;">
                This is span tag
            </span>
            
            <br><br>
            
            <article width="150px" height="150px"
                    style="border:2px solid #000000;">
                This is article tag
            </article>
            
            <br><br>
            
            <section width="150px" height="150px"
                    style="border:2px solid #000000;"> 
                This is section tag
            </section>
        </div>
    </div>
</center>

`

Differences between HTML width/height attribute and CSS width/height property

Here are some differences:

HTML width/height Attributes CSS width/height Properties
Primarily used for presentation and defining default dimensions. Used to control the actual rendered size of elements on a web page.
Supported by specific elements such as , , , and . Can be applied to most HTML elements.
Helps reserve space before content loads, reducing layout shifts and improving page stability. Does not reserve space before content loads, which may cause layout shifts during loading.
Can be overridden by CSS styles. Takes precedence over HTML width and height attributes when both are specified.