Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
HTML | <th> height Attribute
Next article icon

HTML width/height Attribute vs CSS width/height Property

Last Updated : 26 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don’t follow them.
The width and height attributes are affected in img, svg tags, those are weak kind of styling, it can be overridden by CSS. If you have assigned an img tag with the width=”500″ and height=”200″ and load a CSS width and height property for img tag like width: 400px; then that 500 will be dully 400 will effect the image. 
 

<!-- Normally  in HTML --> <img src=" " width="600" height="200">  /* In CSS for override*/ img {     width: 400px;     height: 400px; }

There are few more attributes that are not selected for a few elements those are very strongly affected on the output. To apply CSS properties on those element you have to know how to apply !important in CSS?. 
Differences between HTML width/height attribute and CSS width/height property: 

HTML width/height Attribute CSS width/height Property
It is used for presentational. It is used for actual effect on the web.
It affects on few HTML elements like img, svg, canvas etc. It affects on all the HTML elements (!important may required for override inline styles).
It reserves required space with fluid images for the smooth loading. It does not reserve required spaces so when the elements occur page will be shake in few times.

Below example uses height/width attributes and height/width property.
Example: 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Comparison between height/width
        attributes and height/width
        property
    </title>
     
    <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>
</head>
 
<body>
    <center>
        <h1>GeeksforGeeks</h1>
         
        <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>
</body>
 
</html>
 
 

Output: 



Next Article
HTML | <th> height Attribute

S

Sabya_Samadder
Improve
Article Tags :
  • CSS
  • HTML
  • Web Technologies
  • CSS-Misc
  • HTML-Misc

Similar Reads

  • HTML | <th> height Attribute
    The HTML <th> height Attribute is used to specify the height of the table header cell. If the <th> height attribute is not set then it takes default height according to content. It is not supported by HTML 5. Syntax: <th height="pixels | %"> Attribute Values: pixels: It sets the he
    1 min read
  • HTML | <video> height Attribute
    The HTML <video> height attribute is used to specify the height of the video player in terms of pixels. Syntax: <video height="pixels"> Attribute Values: It contains single value pixels which specifies the height of the video player in terms of pixels. Below example illustrates the use o
    1 min read
  • HTML <td> height Attribute
    The HTML <td> height Attribute is used to specify the height of the table cell. If the <td> height attribute is not set then it takes the default height according to content. Note: The <td> height Attribute is not supported by HTML. Syntax<td height="pixels | %">Attribute Val
    2 min read
  • HTML <video> height Attribute
    The height attribute in the HTML <video> element specifies the height of the video player in pixels, controlling the vertical size of the video display area within the webpage. HTML <video> height Attribute Syntax:<video height="pixels">HTML <video> height Attribute Values:It
    2 min read
  • HTML <input> height Attribute
    The HTML <input> element's height attribute sets the height of the input field in pixels. It only applies to image inputs (<input type="image">). For other input types, use CSS to control the height instead. Syntax<input height="pixels"> Attribute ValuesIt contains the value i.e pi
    2 min read
  • HTML | <img> height Attribute
    The <img> height attribute is used to specify the height of the image in pixels. Syntax: <img height="pixels"> Attribute Values: It contains single value pixels which specify the height of the image in pixel. Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;
    1 min read
  • HTML | <iframe> height Attribute
    The HTML <iframe> height attribute is used to specify the height of the <iframe> in terms of pixels. Syntax: <iframe height="pixels"> Attribute Values: It contains single value pixel which specifies the height of the iframes in terms of pixels. Below example illustrates the use of
    1 min read
  • HTML <td> width Attribute
    The HTML <td> width attribute was traditionally used to specify the width of a table cell. If this attribute is not set, the cell automatically adjusts its width based on the content it contains. This attribute provides flexibility as it accepts values in both pixels and percentages, allowing
    3 min read
  • HTML <canvas> height Attribute
    The HTML <canvas> height Attribute is used to specify the height of the <canvas> element in terms of pixels. Syntax: <canvas height="pixels"> Attribute Values: It contains single value pixels which specify the height of the canvas in terms of pixel. It has a Default value which is
    1 min read
  • HTML | <input> width Attribute
    The HTML <input> width Attribute is used to specify the width of the <input> element. This Attribute is only used for input type="image". Syntax: <input width="pixels"> Attribute Values: It contains the value i.e pixels which specify the width of the input Element. Example: C/C++ C
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences