Last update: 21.04.2016

The dimensions of the elements are set using the properties width (width) and height (height).

The default value for these properties is auto , which means that the browser determines the width and height of the element. You can also explicitly set dimensions using units (pixels, ems) or percentages:

Width: 150px width: 75% height: 15em;

Pixels define the exact width and height. The em unit depends on the height of the font in the element. If an element's font size is, for example, 16 pixels, then 1 em for that element would be 16 pixels. That is, if the width of the element is set to 15em, then in fact it will be 15 * 16 = 230 pixels. If the element does not have a font size defined, then it will be taken from the inherited parameters or default values.

The percentage values ​​for the width property are calculated based on the width of the container element. If, for example, the width of the body element on a web page is 1000 pixels, and the element nested within it

has a width of 75%, then the actual width of this block
is 1000 * 0.75 = 750 pixels. If the user resizes the browser window, then the width of the body element and, accordingly, the width of the nested div block will also change.

Percentage values ​​for the height property work similarly to the width property, only now the height is calculated from the height of the container element.

For example:

Dimensions in CSS3

At the same time, the actual dimensions of the element may end up being different from those set in the width and height properties. For example:

Dimensions in CSS3

As you can see in the screenshot, in reality, the value of the width property - 200px - determines only the width of the inner content of the element, and under the block of the element itself, space will be allocated, the width of which is equal to the width of the inner content (width property) + padding (padding property) + border width ( border-width property) + margins (margin property). That is, the element will have a width of 230 pixels, and the width of the element block, taking into account the margins, will be 250 pixels.

Such calculations should be taken into account when determining the dimensions of the elements.

By using additional set properties, you can set the minimum and maximum sizes:

    min-width : minimum width

    max-width : maximum width

    min-height : minimum height

    max-height : maximum height

min-width: 200px width:50%; max-width: 300px

In this case, the width of the element is equal to 50% of the width of the container element, however, it cannot be less than 200 pixels and more than 300 pixels.

Redefining block width

The box-sizing property allows you to override the set sizes of elements. It can take one of the following values:

    content-box : The default value of the property, in which the browser adds the border width and padding to the values ​​of the width and height properties to determine the actual width and height of elements

    padding-box: tells the web browser that the element's width and height should include padding as part of its value. For example, let's say we have the following style:

    Width: 200px height: 100px; margin: 10px padding: 10px border: 5px solid #ccc; background-color: #eee; box-sizing: padding-box;

    Here the actual width of the inner content of the block will be 200px (width) - 10px (padding-left) - 10px (padding-right) = 180px.

    It is worth noting that most modern browsers do not support this property.

    border-box: tells the web browser that the element's width and height should include padding and borders as part of its value. For example, let's say we have the following style:

    Width: 200px height: 100px; margin: 10px padding: 10px border: 5px solid #ccc; background-color: #eee; box-sizing: border-box;

    Here the actual width of the inner content of the block will be 200px (width) - 10px (padding-left) - 10px (padding-right) - 5px (border-left-width) - 5px (border-right-width) = 170px.

For example, let's define two boxes that differ only in the value of the box-sizing property:

Dimensions in CSS3

Determining the actual size in CSS 3
Determining the actual size in CSS 3

In the first case, when determining the size of the block, the width and height properties will be added to the border thickness, as well as padding and padding, so the first block will be large.

Description

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

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

Syntax

height: value | interest | auto | inherit

Values

Values ​​accept any CSS length units, such as pixels (px), inches (in), points (pt), etc. When using percentage notation, the height of an element is calculated based on the height of the parent element. If the parent is not explicitly specified, then the browser window acts as the 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.

Result this example shown in fig. one.

Rice. 1. Applying the height property

Object Model

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

Browsers

Browser Internet Explorer 6 incorrectly defines height as min-height .

In quirk mode, Internet Explorer up to and including version 8.0 incorrectly calculates the height of an element without adding padding, margins, and borders to it.

Internet Explorer up to version 7.0 does not support the inherit value.

By default, block elements use autowidth. This means that the element will be stretched horizontally exactly as much as it is. free space. The height of block elements is automatically set by default, i.e. the browser stretches the content area in the vertical direction so that all the content is displayed. To set custom sizes for an element's content area, you can use the width and height properties.

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

Note that the width and height properties only define the size of the content area, to calculate the total width of a block-level element, you need to add the width of the content area, the left and right padding, and the width of the left and right border. The same goes for the overall height of the element, only all values ​​are calculated vertically:

Document's name

For this paragraph, set only the width and height.

This paragraph contains, in addition to the width and height, an indent, a border, and an outdent.

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:


Total Height:5px+ 10px+ 100px+ 10px+ 5px= 130px
upper
frame
upper
indent
height lower
indent
lower
frame

i.e. 180x130 pixels.

element overflow

After you have defined the width and height for the element, you should pay attention to one important point - the content located inside the element may exceed the specified block size. In this case, part of the content will go beyond the boundaries of the element, to avoid this unpleasant moment, you can use CSS property overflow. 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. Specifying this value will have the same effect as not setting the overflow property.
  • scroll - Adds vertical and horizontal scrollbars to an element.
  • auto - Adds scrollbars if needed.
  • hidden - hides some of the content that goes beyond the bounds of the block element.
Document's name

In this article, we will look at how in CSS you can set the height of a block as a percentage of its width. We will consider the application of this technology using the example of creating a Bootstrap carousel (slider) from images of different sizes.

Creating a block with a height that has a certain percentage of its width

  1. Create an HTML structure from 2 blocks:
    The first block has 2 classes. Using the item-responsive class, let's set the block to relative positioning. This must be done in order for block 2 (which will have absolute positioning) is located relative to it. In addition, this class is also used to prepend the pseudo-element:before before the content of the corresponding elements (item-responsive). This element will set the desired block height relative to its width using the padding-top CSS property. Trick this method is that if the padding property is set to the value not in pixels, but as a percentage, then it will be calculated by the browser relative to its width. Thus, you can get a block with the required height. The next step is to give block 2 an absolute position and align it to the first block.
  2. Add the following CSS code to the page: .item-responsive ( position: relative; /* relative positioning */ ) .item-responsive:before ( display: block; /* display the element as a block */ content: ""; /* content pseudo-element */ width: 100%; /* element width */ ) .item-16by9 ( padding-top: 56.25%; /* (9:16)*100% */ ) .item-responsive>.content ( position: absolute; /* absolute position of the element */ /* position of the element */ top: 0; left: 0; right: 0; bottom: 0; ) /* Optionally (for blocks that have these classes) */ .item -4by3 ( padding-top: 75%; /* (3:4)*100% */ ) .item-2by1 ( padding-top: 50%; /* (1:2)*100% */ ) .item -1by1 ( padding-top: 100%; /* (1:1)*100% */ )

Applying the above technology when creating a Bootstrap carousel

If you are not familiar with Bootstrap and want to know what it is, you can use the Introduction to Bootstrap article.

Let's consider an example in which we will use the above HTML structure and CSS code to display Bootstrap carousel slides.

We will use the following files as images:

  • carousel_1.jpg (width = 736px, height = 552px, aspect ratio (height to width) = 1.33);
  • carousel_2.jpg (width = 1155px, height = 1280px, aspect ratio (height to width) = 0.9);
  • carousel_3.jpg (width = 1846px, height = 1028px, aspect ratio (height to width) = 1.8);
  • carousel_4.jpg (width = 1140px, height = 550px, aspect ratio (height to width) = 2.07);
  • carousel_5.jpg (width = 800px, height = 600px, aspect ratio (height to width) = 1.33);

Images will be set as a background. This will allow the Bootstrap 3 carousel to use images with different aspect ratios.


Carousel HTML markup:

Carousel CSS code:

Item-responsive ( position: relative; /* relative positioning */ ) .item-responsive:before ( display: block; /* display element as block */ content: ""; /* pseudo-element content */ width: 100%; /* element width */ ) .item-16by9 ( padding-top: 56.25%; /* (9:16)*100% */ ) .item-responsive>.content ( position: absolute; /* absolute element position * / /* element position */ top: 0; left: 0; right: 0; bottom: 0; background-size: cover !important; )



Most elements in HTML are typically given a specific height and width. Setting these parameters in CSS is quite easy, and as a result, practical. The height and width properties already known to you are used for this. However, in this lesson we will talk about non-fixed width and height or rubber, that is, depending on the width of the window itself. Let's get started)

width

This property sets a specific HTML width element. The value can be specified in pixels, percentages (other values ​​are used less often).

My Block (
width: 400px /* width 400px */

}
Or as a percentage:

MyBlockPercent50 (
width: 50% /* width 50% of the box or window width (if not inside a fixed-width box) */
color: #f1f1f1; /* light gray block */
}
Accordingly, we get a block that will always be half the width of the parent.

Now about the most interesting. width has parameters that specify the maximum or minimum width. These are the min-width and max-width properties, respectively. You can also specify values ​​for them in pixels, percentages, and other values. These properties are the basis for creating rubber and adaptive designs ().

Example adaptive design . Look, resizing the browser window:

I am, of course, jumping ahead of myself. But you must understand what and why you need. Yes, and touch a pleasant result)

As you already understood, with the help of these properties, you can more flexibly handle elements on the page without distorting the content. How it works? Let's consider the code.

Block (
max-width: 800px
background color: #f1f1f1; /* light gray block */
padding: 20px
}
A block with these properties with a parent width of 200 pixels will take on the corresponding value, but if the parent block is larger, for example, 1000 pixels, then it will already take on its maximum width, that is, 800 pixels. That is, it will increase until the width of the parent block is 801 pixels or more. Further, the block block will always have its maximum allowed width of 800px.

height

This property is responsible for the height of the element. As a value, all the same suitable for CSS are used. Most often the same percentages and pixels.

info(
height: 200px; /* block height will be 200px */
padding: 10px /* repeat about padding inside the block =) */
}
Which is logical, for the height, you can specify the minimum and maximum height values ​​​​for the element with the min-height and max-height properties, respectively.

info(
max-height: 360px /* maximum block height */
min-height: 120px; /* minimum block height */
}
As you can see, properties can, and often should, be used in pairs.
Or combine values:

Content(
height: 100%; /* height will always be 100% */
width: 760px /* but the width is fixed at 760px */
}
If you have any questions, write in the comments!

Thank you for your attention! Good luck in layout!)