Width and height: determine the size of the element. Css width and height parameters for sizing html page elements Overriding block width

Description

Sets the height of block or replaceable elements (for example, the tag ). Height does not include the thickness of the borders around the element, padding and margins.

If the content of the block exceeds the specified height, then the element's height will remain unchanged and the content will be displayed on top of it. This feature can cause the content of elements to overlap when the elements in the HTML code are sequential. To prevent this from happening, add overflow: auto to the element's style.

Syntax

height: value | interest | auto | inherit

The values

Any length units accepted in CSS are accepted as values \u200b\u200b- for example, pixels (px), inches (in), points (pt), etc. When using percentage notation, the height of the element is calculated depending on the height of the parent element. If the parent is not explicitly specified, then the browser window is used as its parent. auto sets the height based on the content of the element

HTML5 CSS2.1 IE Cr Op Sa Fx

height

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.

The result of this example is shown in Fig. 1.

Figure: 1. Applying the height property

Object Model

document.getElementById ("elementID") .style.height

Browsers

Internet Explorer 6 does not correctly define height as min-height.

In quirk mode, Internet Explorer up to and including 8.0 incorrectly calculates element height without adding padding, margin, and border values \u200b\u200bto it.

Internet Explorer 7.0 or later does not support the inherit value.

The height CSS property specifies the height of an element. By default, the property defines the height of the content area. If box-sizing is set to border-box, however, it instead determines the height of the border area.

The source for this interactive example is stored in a GitHub repository. If you "d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

The min-height and max-height properties override height.

Syntax

/ * Keyword value * / height: auto; / * values \u200b\u200b* / height: 120px; height: 10em; / * Value * / height: 75%; / * Global values \u200b\u200b* / height: inherit; height: initial; height: unset;

Values

CSS data type represents a distance value. Lengths can be used in numerous CSS properties, such as width, height, margin, padding, border-width, font-size, and text-shadow. "\u003e Defines the height as an absolute value. CSS data type represents a percentage value. It is often used to define a size as relative to an element "s parent object. Numerous properties can use percentages such as width height margin padding and font-size.\u003e Defines the height as a percentage of the containing block "s height. Auto The browser will calculate and select a height for the specified element. Max-content The intrinsic preferred height. Min-content The intrinsic minimum height. Fit-content (CSS data type represents a value that can be either a or a ."> ) Uses the fit-content formula with the available space replaced by the specified argument, i.e. min (max-content, max (min-content,)).

Formal syntax

CSS data type represents a percentage value. It is often used to define a size as relative to an element "s parent object. Numerous properties can use percentages such as width height margin padding and font-size.\u003e border-box content-box available min-content max-content fit-content auto

Example

Html

I "m 50 pixels tall.
I "m 25 pixels tall.
I "m half the height of my parent.

CSS

div (width: 250px; margin-bottom: 5px; border: 2px solid blue;) #taller (height: 50px;) #shorter (height: 25px;) #parent (height: 100px;) #child (height: 50% ; width: 75%;)

Result

Accessibility concerns

Ensure that elements set with a height are not truncated and / or do not obscure other content when the page is zoomed to increase text size.

Specifications

Specification Status Comment
CSS Intrinsic & Extrinsic Sizing Module Level 4
Editor "s Draft
CSS Intrinsic & Extrinsic Sizing Module Level 3
The definition of "height" in that specification.
Working Draft Added the max-content, min-content, fit-content keywords.
CSS Transitions
The definition of "height" in that specification.
Working Draft Lists height as animatable.
CSS Level 2 (Revision 1)
The definition of "height" in that specification.
Recommendation Adds support for the CSS data type represents a distance value. Lengths can be used in numerous CSS properties, such as width, height, margin, padding, border-width, font-size, and text-shadow. "\u003e values \u200b\u200band precises on which element it applies to.
CSS Level 1
The definition of "height" in that specification.
Recommendation Initial definition.
Initial value auto
Applies toall elements but non-replaced inline elements, table columns, and column groups
Inheritedno
PercentagesThe percentage is calculated with respect to the height of the generated box "s containing block. If the height of the containing block is not specified explicitly (ie, it depends on content height), and this element is not absolutely positioned, the value computes to auto. A percentage height on the root element is relative to the initial containing block.
Mediavisual
Computed valuea percentage or auto or the absolute length
Animation typea CSS data type are interpolated as real, floating-point numbers. "\u003e length, CSS data type are interpolated as real, floating-point numbers."\u003e percentage or calc ();
Canonical orderthe unique non-ambiguous order defined by the formal grammar

Browser compatibility

The compatibility table on this page is generated from structured data. If you "d like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
heightChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 7Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
fit-contentChrome Full support 46Edge No support NoFirefox No support NoIE No support NoOpera Full support 33Safari Full support 11 Full support 11 Full support 9

Prefixed

Prefixed
Opera Android?Safari iOS Full support 11 Full support 11 Full support 9

Prefixed

Prefixed Implemented with the vendor prefix: -webkit-
max-contentChrome Full support 46Edge No support No

Prefixed

Prefixed
IE No support NoOpera Full support 44Safari Full support 11WebView Android Full support 46Chrome Android Full support 46

Prefixed

Prefixed Implemented with the vendor prefix: -moz-
Samsung Internet Android Full support 5.0
min-contentChrome Full support 46Edge No support NoFirefox Full support 66 Full support 66 Full support 3

Prefixed

Prefixed Implemented with the vendor prefix: -moz-
IE No support NoOpera Full support 44Safari Full support 11WebView Android Full support 46Chrome Android Full support 46Firefox Android Full support 66 Full support 66 Full support 4

Prefixed

Prefixed Implemented with the vendor prefix: -moz-
Opera Android Full support 43Safari iOS Full support 11Samsung Internet Android Full support 5.0
stretchChrome Full support 28

Webkit-fill-available"\u003e Alternate Name

Full support 28

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name

Edge No support NoFirefox No support NoIE No support NoOpera Full support 15

Webkit-fill-available"\u003e Alternate Name

Full support 15

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

Safari Full support 9

Webkit-fill-available"\u003e Alternate Name

Full support 9

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

WebView Android Full support 4.4

Webkit-fill-available"\u003e Alternate Name

Full support 4.4

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

Chrome Android Full support 28

Webkit-fill-available"\u003e Alternate Name

Full support 28

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

Firefox Android No support NoOpera Android?Safari iOS Full support 9

Webkit-fill-available"\u003e Alternate Name

Full support 9

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

Samsung Internet Android Full support 5.0

Webkit-fill-available"\u003e Alternate Name

Full support 5.0

Webkit-fill-available"\u003e Alternate Name

Webkit-fill-available"\u003e Alternate Name Uses the non-standard name: -webkit-fill-available

Legend

Full support Full support No support No support Compatibility unknown Compatibility unknown Uses a non-standard name. Uses a non-standard name. Requires a vendor prefix or different name for use. Requires a vendor prefix or different name for use.

By default, autowidth is used for block elements. This means that the element will be stretched horizontally by exactly as much as there is free space. The default height of block elements is set automatically, i.e. the browser stretches the content area vertically to display all of the content. To set custom dimensions for the content area of \u200b\u200ban element, you can use the width and height properties.

The width CSS property allows you to set the width of the element's content area, and the height property the height of the content area:

Note that the width and height properties only determine the size of the content area; to calculate the total width of a block element, add the width of the content area, left and right padding, and the width of the left and right border. The same goes for the total height of the element, only all values \u200b\u200bare calculated vertically:

Document's name

For this paragraph, we will only set the width and height.

This paragraph contains, in addition to the width and height, padding, border and padding.

Try "

The example clearly shows that the second element takes up more space than the first. If we calculate according to our formula, then the dimensions of the first paragraph are 150x100 pixels, and the dimensions of the second paragraph are:


Overall height:5px+ 10px+ 100px+ 10px+ 5px \u003d 130px
upper
frame
upper
indent
height lower
indent
bottom
frame

that is, 180x130 pixels.

Overflow of elements

After you have defined the width and height for the element, it is worth paying attention to one important point - the content located inside the element can exceed the specified block size. In this case, some of the content will go beyond the borders of the element, to avoid this unpleasant moment, you can use the CSS overflow property. The overflow property tells the browser what to do if the content of the block exceeds its size. This property can take one of four values:

  • visible is the default value used by the browser. Setting this value will have the same effect as not setting the overflow property.
  • scroll - adds vertical and horizontal scrollbars to the element.
  • auto - adds scrollbars as needed.
  • hidden - hides some of the content that goes beyond the bounds of the block element.
Document's name

The css width properties are responsible for the value of the width of the content area of \u200b\u200bthe element.

Width: value | interest | auto | inherit

The css height property is responsible for the value of the height of the element's content area.

Height: value | interest | auto | inherit

Negative values \u200b\u200bfor the height and width are not allowed.

It would seem that everything is simple and clear with these CSS properties, but, in fact, everything turns out to be not quite that simple.

Setting an element on a web page to a width of 500px does not mean that the total width of that element will be 500 pixels or less.

The most important thing to understand is that the width and height properties set the width and height for the content area.

The width and height properties are just one of the components from which the total width and height of the element will be calculated. They indicate the width and height of the Content area, which can be seen in the following image. Also, properties such as padding, margin and border are involved in the formation of the overall width and height.

This can be easily seen now.

Let's, for example, take two blocks with the same width, but one of them will additionally set the padding property.

width: 100px
width: 100px + padding

By default, web pages have the behavior that the padding value for the corresponding side is added to the height and width values.

Those. the total width of the element will consist of the value of the element's width property, and the padding-left and padding-right values \u200b\u200bwill be added to it.

For different types of elements that can be found on the page, the total width and height of the elements is calculated using special formulas.

For different types of elements that may be found on web pages, the width and height values \u200b\u200bcan have different behavior and values.

A similar situation will be with the height property.

height: 100px
height: 100px + padding

CSS has a special algorithm that calculates the width and height values \u200b\u200bfor different elements on a web page. I will not dwell on this algorithm now, because it is somewhat complicated.

Now the main thing for us to understand is that the width and height properties allow us to work with the width and height of the area with the element's content, and not the total width and height of the element.