Chiều rộng - Thủ thuật CSS

Anonim

Các widthbất động sản trong CSS xác định chiều rộng của khu vực nội dung của phần tử. Vùng "nội dung" này là phần bên trong phần đệm, đường viền và lề của một phần tử (mô hình hộp).

.element ( width: 80%; )

Trong ví dụ trên, các phần tử có tên lớp .wrapsẽ rộng bằng 80% so với phần tử mẹ của chúng. Các giá trị được chấp nhận là bất kỳ giá trị độ dài nào, ngoài một số từ khóa mà chúng tôi sẽ đề cập sau.

Chiều rộng có thể được ghi đè bởi các thuộc tính có tương quan chặt chẽ với chiều rộng tối thiểu và chiều rộng tối đa.

.wrapper-1 ( width: 100%; max-width: 320px; /* Will be AT MOST 320px wide */ ) .wrapper-2 ( width: 100%; min-width: 20em; /* Will be AT LEAST 20em wide */ )

Đào sâu hơn

Khi sử dụng phần trăm (%) cho chiều rộng, tác giả phải lưu ý rằng phần trăm dựa trên phần tử gốc, hay nói cách khác, chiều rộng của khối chứa. Nếu cha mẹ của bạn được đặt ở 480px - như được minh họa trong bản trình diễn của chúng tôi - thì tỷ lệ phần trăm dựa trên giá trị đó. Vì vậy, trong trường hợp của chúng ta, 50% của 480px để lại cho chúng ta 240px dưới dạng giá trị pixel được tính toán.

Lưu ý rằng widtháp dụng cho tất cả các yếu tố ngoại trừ yếu tố phi thay thế hoặc nội tuyến, hàng bảng và các nhóm hàng (ví dụ thead, tfoottbody). Dường như có một chút không khớp về cách HTML xác định các phần tử không được thay thế và cách CSS định nghĩa nó, nhưng chúng tôi đang đề cập đến nó theo cách CSS thực hiện: các phần tử có nội dung không được xác định bởi chính thẻ, chẳng hạn nhưCodePen Embed Fallback

Keyword values

With some special keyword values, it is possible to define width (and/or height) according to the content of the element.

min-content

The min-content value is the smallest measure that would fit around its content if all soft wrap opportunities within the box were taken.

The best example for this kind of value is a properly written figure element:

What a lovely kitten we got there in this image which is encapsulated in a figure element. How dear, look how long this caption is!

Once we have applied some basic styles to this markup, we get:

If we wanted that figure element to essentially be the size of that image, so the text wraps at the edges of the image. We could float it left or right, because float will exhibit that same kind of shrink-to-fit behavior, but what if we wanted to center it? min-content allows us to center it:

Because we’ve assigned min-content to the figure element, it takes the minimum width it can have when taking all soft wrap opportunities (like spaces between words) so that the content still fits in the box.

max-content

The max-content property refers to the narrowest measure a box could take while fitting around its content - if no soft wrap opportunities within the element were taken.

Check out what happens if we apply this to our simple kitten/figure demo:

Because the caption is very longer than the image is wide (it doesn’t take any soft wrap opportunity, like the spaces between words), it means it has to display the caption on a single line, thus the figure is as wide as that line.

fill-available

???. One of life’s great mysteries.

fit-content

The fit-content value is roughly equivalent to margin-left: auto and margin-right: auto in behaviour, except it works for unknown widths.

For instance, let’s say we need to center an inline navigation across the page. Your best bet would be to apply text-align: center to the ul, and display: inline-block to the li. This would give you something like this:

However, the blue background (from the ul element) spreads across the entire document because the ul is a block-level element, which means its width is restricted only by its containing element. What if we want to have the blue background collapsing around the list items? fit-content to the rescue!

With fit-content and margin: 1em auto, this works like a charm and only the navigation has a colored background, not the whole document width.

If you’re into this sort of thing, you’ll be happy to know the formula to define the size of a fit-content length is:

fit-content = min(max-content, max(min-content, fill-available))

This is a pretty unused value, so if you come up with a great use-case, let us know!

Browser support

IE Edge Firefox Chrome Safari Opera
All All All All All All
Android Chrome Android Firefox Android Browser iOS Safari Opera Mobile
All All All All All
Source: caniuse

Related properties

Almanac on Jan 15, 2021

height

.element ( height: 500px; ) layout width Sara Cope