Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
CSS width Property
Next article icon

CSS width Property

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The width property in CSS is used to set the width of elements such as text and images. It can be specified in various units, including pixels (px), percentages (%), and centimeters (cm). This property defines the width of the content area, excluding padding, borders, and margins.

The width property can be overridden by the min-width and max-width properties. When the box-sizing property is set to border-box, the width includes padding and borders.

Try CSS Width Property

Syntax:

width: auto | value | initial | inherit;

Note: The width property for the element does not involve the padding, border & margin.

Default Value:

Its default value is auto. 

Property Values:

ValueDescription
autoDefault value; the browser calculates the width of the element automatically.
valueSets the width using units like pixels (px), percentages (%), or centimeters (cm). Cannot be negative.
initialSets the width property to its default value.
inheritInherits the width property from its parent element.

All the properties are described well with the example below.

Setting Width to auto

It is used to set the width property to its default value. If the width property is set to auto then the browser calculates the width of the element.

Syntax

width: auto;

Example: This example demonstrates the use of the width property whose value is set to auto.

HTML
<!DOCTYPE html> <html>  <head>     <title> CSS width Property </title>     <style>         .gfg {             width: auto;             color: white;             font-size: 20px;             background-color: rgb(0, 150, 0);         }          h2 {             font-size: 20px;             color: black;         }     </style> </head>  <body>     <h2>         CSS width Property     </h2>     <p class="gfg">         This is an example of auto width property     </p>   </body>  </html> 

Output:

Setting Width with Specific Units

It is used to set the width in the form of pixels(px), Percentage(%), centimetre(cm) etc. The width can not be negative.

Syntax:

width: value;

Example: This example demonstrates the use of the width property whose value can be defined in pixels(px), Percentage(%), centimetre(cm) etc.

HTML
<!DOCTYPE html> <html>  <head>     <title> CSS Width Property </title>     <style>         .gfg {             width: 150px;             color: white;             font-size: 20px;             background-color: RGB(0, 150, 0);         }          .gfg1 {             width: 50%;             color: white;             font-size: 20px;             background-color: RGB(0, 200, 0);         }          h2 {             color: black;         }     </style> </head>  <body>     <h2>         CSS width Property     </h2>     <p class="gfg">         An example of width property in form of pixels.     </p>       <p class="gfg1">         An example of width property in form of percentage.     </p>   </body>  </html> 

Output:

Setting Width to initial

It is used to set an element’s CSS property to its default value.

Syntax:

width: initial;

Example: This example demonstrates the use of the width property whose value is set to the initial.

HTML
<!DOCTYPE html> <html>  <head>     <title> CSS Width Property </title>     <style>         .gfg {             width: initial;             color: white;             font-size: 20px;             background-color: RGB(0, 150, 0);         }          h2 {             color: black;         }     </style> </head>  <body>     <h2>         CSS width Property     </h2>     <p class="gfg">         An example of width property for its initial width value.     </p>   </body>  </html> 

Output:

Inheriting Width from Parent

It is used to inherit a property to an element from its parent element property value.

Syntax

 width: inherit;

Example: This example demonstrates the width property set to inherit.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <title>Width: inherit Example</title>     <style>         .parent {             width: 300px;             border: 1px solid black;         }          .child {             width: inherit;             border: 1px solid red;         }     </style> </head>  <body>     <div class="parent">         Parent div         <div class="child">Child div (inherits width)</div>     </div> </body>  </html> 

Output:

inheritingParent

Supported Browsers:

  • Google Chrome
  • Edge 
  • Firefox
  • Opera
  • Safari

Next Article
CSS width Property

S

snigdha_yambadwar
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Properties

Similar Reads

    CSS min-width Property
    The min-width property in CSS is used to define the minimum width of an element. The value of the width cannot be less than the value of the min-width. If the content specified within the element is smaller, min-width maintains the specified minimum width. Syntax:min-width: length|initial|inherit;Pr
    3 min read
    CSS max-width Property
    The max-width property in CSS defines the maximum width an element can occupy, ensuring that it doesn't exceed a specified width. This property plays a crucial role in responsive design, allowing the content to adjust within a defined limit. It helps in creating flexible layouts that look great on v
    4 min read
    CSS column-width Property
    The columns-width property in CSS is used to define the width of the columns. The minimum number of columns are required to show the content across the element. It is a flexible property. If the browser cannot fit at least two-columns at a given column-width then the two columns will be put into a s
    4 min read
    CSS border-width Property
    The border-width property in CSS sets the width of an element’s border. You can specify a single width for all four sides or define different widths for each side (top, right, bottom, and left). This property is crucial for creating visually appealing borders and enhancing the layout of your web pag
    5 min read
    CSS outline-width Property
    The outline is the line that is created around the specified element outside the border of the element to make the particular distinct and easier to distinguish. The outline-width Property is used to specify the width of this outline for a specific element. The outline-style property must be declare
    4 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