min-width property is used to set a minimum width of a specified element.
It is sometimes useful to constrain the width of an element in CSS to a certain range. Two properties are available to set a minimum and maximum width for an element: the
min-width property and the
min-width: <length> | <percentage> | inherit
- Initial: 0
- Applies To: all elements but non-replaced inline elements, table columns, and column groups
- Animatable: yes, as a length, percentage, or calc()
In CSS3, new values have been introduced to the
min-width property. The official syntax looks as follows:
min-width: [ [<a href="http://tympanus.net/codrops/css_reference/length"><length></a> | <a href="http://tympanus.net/codrops/css_reference/percentage"><percentage></a>] && [border-box | content-box]? ] | available | min-content | max-content | fit-content
- Specifies a fixed minimum computed width. See the <length> entry for a list of possible values.
The <percentage> is calculated with respect to the width of the containing block. If the containing block’s width is negative, the used value is zero. If the containing block’s width depends on this element’s width, then the resulting layout is undefined in CSS 2.1.
See the <percentage> entry for a list of possible values.
- The element inherits its minimum width value from its parent.
- Width is equal to the containing block width minus the current element’s margin, border, and padding.
The intrinsic preferred width. The
max-contentwidth is, roughly, the width the content would have if no “soft” line breaks were inserted, i.e., if each paragaph is one long line.
The intrinsic minimum width. The
min-contentwidth is, roughly, the narrowest the box can get by breaking all lines at all possible break points.
The following experimental keyword values have been introduced in CSS3.
The following image helps understand the
min-content value is the narrowest a box can get by breaking all lines at all possible break points. In the above image, we saw how a text-only content can affect the container’s width with
min-content. The same concept applies if you have an image inside an element. The element is an inline-element, and therefore a break point is also possible after it. So, if the element contained a piece of text, and an image, the image is more likely to be wider than any word inside the paragraph (unless you’re using some fancy and super long words, of course), so the image can be the content that specifies the minimum width of the element with
But, since the element can have a width greater than its minimum width, the value of
min-width in this case is the same as width
max-content, the content inside the element will not break at any soft break points, and the text will be rendered as one long line, and the element must stretch to fit the longest line inside it, and that will be the minimum width it can have.
Negative values are not allowed.
The keyword values (in contrast to length and percentage values) are not influenced by the box-sizing property, they always set the size of the content box.
fit-content are equivalent to 0 when set on the
min-width of vertical elements (when the writing mode is vertical, i.e. the writing-mode property has a value of
min-width: 250px; min-width: 50%; min-width: 100vh; min-width: inherit;
Live DemoView this demo on the Codrops Playground
min-width property works in all major browsers: Chrome, Firefox, Safari, Opera, Internet Explorer, and on Android and iOS.
The new experimental values added in CSS3 are not yet supported in all browsers, and some of them have different equivalents supported in some browsers. The browser support for the new values is shown in the following table:
Intrinsic & Extrinsic Sizing
Allows for the heights and widths to be specified in intrinsic values using the `max-content`, `min-content`, `fit-content` and `stretch` (formerly `fill`) properties.
W3C Working Draft
Supported from the following versions:
Mobile / Tablet
* denotes prefix required.
Stats from caniuse.com
CSS 2.1 explicitly leaves the behavior of
<table> undefined, so it is still not supported by all browsers.