Css width relative to div

CSS: width property

This CSS tutorial explains how to use the CSS property called width with syntax and examples.

Description

The CSS width property defines the width of the content area of an element.

Syntax

The syntax for the width CSS property is:

Parameters or Arguments

The width of the content area of an element. It can be one of the following:

Value Description
fixed Fixed value expressed in pixels, em’s, etc.
div < width: 150px; >
div
percentage Percentage value
div
auto Browser will calculate the width for the element
div
inherit Element will inherit the width from its parent element
div

Note

  • The content area of an element is inside the padding, border, and margin of the element.
  • The CSS width property applies to block level and replaced elements.
  • When the value is provided as a percentage, it is relative to the width of the containing block.
  • See also height, max-height, max-width, min-height, min-width.

Browser Compatibility

The CSS width property has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • IE Phone
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the width property below, exploring examples of how to use this property in CSS.

Using Fixed Value

Let’s look at a CSS width example where we have provided the width as a fixed value expressed in pixels.

In this CSS width example, we have set the tag to a width of 200px.

We can also express the width as a fixed value in em’s.

In this CSS width example, we have set the tag to a width of 175em.

Using Percentage

Let’s look at a CSS width example where we have provided the width as a percentage.

In this CSS width example, we have set the width of the to 90%.

Using auto

Let’s look at a CSS width example where we have used auto as the width value.

In this CSS width example, the browser will calculate the width of the .

Источник

width

The width CSS property sets an element’s width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.

Try it

The specified value of width applies to the content area so long as its value remains within the values defined by min-width and max-width .

  • If the value for width is less than the value for min-width , then min-width overrides width .
  • If the value for width is greater than the value for max-width , then max-width overrides width .

Syntax

/* values */ width: 300px; width: 25em; /* value */ width: 75%; /* Keyword values */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* Global values */ width: inherit; width: initial; width: revert; width: revert-layer; width: unset; 

Values

Defines the width as a distance value.

Defines the width as a percentage of the containing block’s width.

The browser will calculate and select a width for the specified element.

The intrinsic preferred width.

The intrinsic minimum width.

Uses the fit-content formula with the available space replaced by the specified argument, i.e. min(max-content, max(min-content, )) .

Accessibility concerns

Ensure that elements set with a width aren’t truncated and/or don’t obscure other content when the page is zoomed to increase text size.

Formal definition

Initial value auto
Applies to all elements but non-replaced inline elements, table rows, and row groups
Inherited no
Percentages refer to the width of the containing block
Computed value a percentage or auto or the absolute length
Animation type a length, percentage or calc();

Formal syntax

width =
auto |
|
min-content |
max-content |
fit-content( )

=
|

Examples

Default width

p class="goldie">The Mozilla community produces a lot of great software.p> 

Example using pixels and ems

.px_length  width: 200px; background-color: red; color: white; border: 1px solid black; > .em_length  width: 20em; background-color: white; color: red; border: 1px solid black; > 
div class="px_length">Width measured in pxdiv> div class="em_length">Width measured in emdiv> 

Example with percentage

.percent  width: 20%; background-color: silver; border: 1px solid red; > 
div class="percent">Width in percentagediv> 

Example using «max-content»

p.maxgreen  background: lightgreen; width: intrinsic; /* Safari/WebKit uses a non-standard name */ width: -moz-max-content; /* Firefox/Gecko */ width: -webkit-max-content; /* Chrome */ width: max-content; > 
p class="maxgreen">The Mozilla community produces a lot of great software.p> 

Example using «min-content»

p.minblue  background: lightblue; width: -moz-min-content; /* Firefox */ width: -webkit-min-content; /* Chrome */ width: min-content; > 
p class="minblue">The Mozilla community produces a lot of great software.p> 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jul 18, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Читайте также:  Parse domain name php
Оцените статью