CSS Introduction

CSS Tutorial What is CSS CSS Syntax CSS Selector How to include CSS CSS Comments

CSS Attributes

CSS Background CSS Border CSS Display CSS Float CSS Font CSS Color CSS Hover CSS Important CSS Line-height CSS Margin CSS Opacity CSS Filter CSS Images CSS Overflow CSS Padding CSS Position CSS Vertical align CSS White space CSS Width Word-wrap in CSS Box-shadow in CSS Text-transform in CSS CSS Outline CSS Visibility CSS Counters CSS Clear fix CSS Icons CSS Justify-content Text-decoration in CSS CSS Lists CSS nth selector CSS Sticky CSS Background-clip CSS Checkbox-style CSS Letter-spacing CSS Navigation bar CSS Overlay CSS Root CSS Specificity CSS Text-indent CSS Text-stroke CSS Zoom CSS Order CSS Descendent selector CSS Clip CSS calc() CSS Background-blend-mode CSS radio-button CSS Superscript and subscript CSS Text-effects CSS Text-align CSS Variables CSS Page-break-before CSS Page-break-inside CSS Page-break-after CSS Content property CSS Word-spacing CSS Animation CSS @keyframes rules CSS Pseudo-classes CSS Pseudo-elements CSS Radial-gradient CSS Translate CSS Gradients CSS z-index CSS Loaders CSS Units CSS Transition CSS Masking CSS Arrow CSS Pagination

Questions

What is Bootstrap CSS What is CSS used for How to center a table in CSS What is a CSS File How to center a button in CSS How to change background color in CSS How to change the font in CSS How to change font size in CSS How to resize an image in CSS How to get rid of bullet pioints in CSS Is CSS a programming language How to edit CSS in WordPress How to use google fonts in CSS How to create blinking text using CSS How to Set Space between the Flexbox Is CSS a Programming Language

Difference

Difference between HTML and CSS Grid Vs Flexbox in CSS Difference between CSS Grid and CSS Flexbox

Misc

Create a 3D text effect using HTML and CSS Hover condition for a:before and a:after in CSS Bem CSS Boder Types CSS Features of CSS Font Face CSS Image Overlay CSS B Tag CSS Carousel CSS CSS Arrow CSS Focus Flex Grow in CSS Bem CSS Features of CSS Font Face CSS Removing Underline from Link in CSS Width Property in CSS Radio Button in CSS

Width Property in CSS

The width attribute controls the width of a content area. The property width determines the width of the border region if the box-sizing is set to border-box.

The width property's value stays between the limits set by the min-width and max-width parameters.

The width of text and pictures may be set using the width property in CSS. Text and pictures can have their width specified in terms of pixels (px), percentages (%), centimetres (cm), etc. There are no padding, borders, or margins in the width property. The min-width and max-width properties take precedence over the width property. By default, the width property determines the width of the content area; however, if the box-sizing value is border-box, it also determines the width of the border area.

Syntax:

width: auto | value | initial | inherit;

The width of an element may be set in CSS in several different ways. Let's examine every single syntactic option for setting an element's width.

Length Value

With CSS, you may specify the width of an element in terms of pixels (px), centimetres (cm), inches (in), etc. Here is an illustration of adding width in length units to a div element:

  • width: 120px;
  • width: 10em;
  • width: 100vh;

Percentage Value

With CSS, you may specify a percentage of the contained element's width as the height of an element. Here is an illustration of how to add a width in percentage values to a div element:

  • width: 75%;
  • width: 50%;

Keyword Value

  • width: auto;
  • width: max-content;
  • width: min-content;
  • width: fit-content(20em);

Global Value

  • width: inherit;
  • width: initial;
  • width: revert;
  • width: revert-layer;
  • width: unset;

We do not allow negative values such as width: -200px.

Note: The element's width parameter does not include the padding, border, and margin.

  • Default Value: Auto is the default value for this field. 
  • Property Values: The sample below describes all the properties well.

Auto:

This setting makes the width property's default setting. The browser determines the element's width if the width attribute is auto-generated.

Here, the browser will determine the width automatically based on the content. It is an element's default width. Here is an illustration of how to add a width as auto to a div element:

Syntax:

width: auto;

Example: This example shows how to utilise the width property with the auto value.

<!DOCTYPE html>

<html>

<head>

<title>Example of CSS width Property </title>

<style>

.main {

width: auto;

color: pink;

font-size: 30px;

background-color: black;

}

h2 {

font-size: 30px;

color: red;

}

</style>

</head>

<body>

<h2>

An example of CSS width Property

</h2>

<p class="main">

This is an example of the auto-width property

</p>

</body>

</html>

Output

Width Property in CSS

Value: 

It is used to provide the width in terms of pixels (px), percentages (%), centimetres (cm), and other measurements. There can never be a negative breadth.

Syntax:

width: value;

Example: The width property, whose value may be specified in pixels (px), percentages (%), centimetres (cm), etc., is used in this example to show how it works.

<!DOCTYPE html>

<html>

<head>

<title> CSS Width Property </title>

<style>

.main {

width: 150px;

color: black;

font-size: 24px;

background-color: pink

}

.main1 {

width: 30%;

color: black;

font-size: 20px;

background-color: pink;

}

h2 {

color: green;

}

</style>

</head>

<body>

<h2>

Example of CSS width Property

</h2>

<p class="main">

An example of width property using pixels. 

</p>

<p class="main1"> 

An example of width property using percentage. 

</p>

</body>

</html>

Output

Width Property in CSS

Initial: 

It is used to set the default value of a CSS property on an element.

Syntax:

width: initial;

Example: This example shows how to use the width property, which has the initial value set.

<!DOCTYPE html>
<html>
<head>
<title>Example of CSS Width Property </title>
<style>
.main {
width: initial;
color: black;
font-size: 25px;
background-color: pink;
}
h2 {
color: blue;
}
</style>
</head>

<body>
<h2>
Example of CSS width Property
</h2>
<p class="main">
An illustration of a width property's initial width value.
</p>
</body>
</html>

Outpu

Width Property in CSS

Inherit:

The value inherit keyword is used to pass a property from a parent element's property value to an element. Any CSS attribute may be inherited, as well as any HTML element, using the inherit keyword.

Syntax:

width: inherit;

Example

<!DOCTYPE html>

<html>

<head>

<style>

span {

font-size: 20px;

}

.main {

font-size: inherit;

}

</style>

</head>

<body>

<h1 style="text-align: left;

color: red;">

Example of inherited property

</h1>

<div style="text-align: left; font-size: 25px;">

<span class="main">Inherited size</span>

</div>

</body>

</html>

Output

Width Property in CSS