Html height same as width

CSS Height, Width and Max-width

The CSS height and width properties are used to set the height and width of an element.

The CSS max-width property is used to set the maximum width of an element.

CSS Setting height and width

The height and width properties are used to set the height and width of an element.

The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.

CSS height and width Values

The height and width properties may have the following values:

  • auto — This is default. The browser calculates the height and width
  • length — Defines the height/width in px, cm, etc.
  • % — Defines the height/width in percent of the containing block
  • initial — Sets the height/width to its default value
  • inherit — The height/width will be inherited from its parent value
Читайте также:  Python strptime with timezone

CSS height and width Examples

Example

Set the height and width of a element:

Example

Set the height and width of another element:

Note: Remember that the height and width properties do not include padding, borders, or margins! They set the height/width of the area inside the padding, border, and margin of the element!

Setting max-width

The max-width property is used to set the maximum width of an element.

The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).

The problem with the above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.

Using max-width instead, in this situation, will improve the browser’s handling of small windows.

Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!

Note: If you for some reason use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property will be used (and the width property will be ignored).

Example

This element has a height of 100 pixels and a max-width of 500 pixels:

Try it Yourself — Examples

Set the height and width of elements
This example demonstrates how to set the height and width of different elements.

Set the height and width of an image using percent
This example demonstrates how to set the height and width of an image using a percent value.

Set min-width and max-width of an element
This example demonstrates how to set a minimum width and a maximum width of an element using a pixel value.

Set min-height and max-height of an element
This example demonstrates how to set a minimum height and a maximum height of an element using a pixel value.

All CSS Dimension Properties

Property Description
height Sets the height of an element
max-height Sets the maximum height of an element
max-width Sets the maximum width of an element
min-height Sets the minimum height of an element
min-width Sets the minimum width of an element
width Sets the width of an element

Источник

How TO — Aspect Ratio

Learn how to maintain the aspect ratio of an element with CSS.

Aspect Ratio

Create flexible elements that keep their aspect ratio (4:3, 16:9, etc.) when resized:

What is aspect ratio?

The aspect ratio of an element describes the proportional relationship between its width and its height. Two common video aspect ratios are 4:3 (the universal video format of the 20th century), and 16:9 (universal for HD television and European digital television, and default for YouTube videos).

How To — Height Equal to Width

Step 1) Add HTML:

Use a container element, like , and if you want text inside of it, add a child element:

Example

Step 2) Add CSS:

Add a percentage value for padding-top to maintain the aspect ratio of the DIV. The following example will create an aspect ratio of 1:1 (the height and width is always equal):

Example 1:1 Aspect Ratio

.container <
background-color: red;
width: 100%;
padding-top: 100%; /* 1:1 Aspect Ratio */
position: relative; /* If you want text inside of it */
>

/* If you want text inside of the container */
.text position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
>

Example 16:9 Aspect Ratio

Example 4:3 Aspect Ratio

Example 3:2 Aspect Ratio

Example 8:5 Aspect Ratio

The aspect-ratio CSS Property

In newer browsers, you can simply use the CSS aspect-ratio property:

Example 3:2 Aspect Ratio

The numbers in the table specify the first browser version that fully supports the property.

Источник

Height equals width with pure CSS

I was looking for a solution for flexible elements which remain the aspect ratio when resized. Until now I used some Javascript for resizing the elements.

But using the resize event and recalculate the height of an element is some kind of nasty and never felt right. Recently I found a solution in an article from A List Apart and a topic on StackOverflow, which works quite well. There was also potential for further reducing the code.

Examples (resize your browser to see the remaining ratios)

Description

If you have an image with a certain aspect ratio you can easily keep the proportion with the «auto» value. Like:

The problem is that you can’t use the «auto» value for the height property of a block element like a DIV or alike. It will always resize depending on the inner content/elements.

The trick

I found a solution which was already posted four years ago on A List Apart. Also a kind of follow-up on Stack Overflow. This is so brilliant that I want to share the beauty (NERD!) of that solution with you:

HTML

We need two block elements to achieve the desired behaviour. No images, no Javascript.

CSS

So, what’s this? We define a pseudo element for our box and give it a padding-top of 100%. Since this 100% value relates to the element’s width. you get it (height: 0; padding-bottom: 100%; would also work, but then you have to adjust the padding-bottom value every time you change the width).

So our box is already as high as wide. If you only want to display some colored tiles, you are already done. But since the user experience is way better if you also provide some content we add a content element to our box.

The content

And here is the trick: We just float the content element. This lets the content fill the outer box.

That’s it. Brilliant, isn’t it? Even a padding doesn’t break it, no need for box-sizing: border-box here.

Other aspect ratios

If you want to create other ratios just change the padding-top value of the pseudo element (see example):

/* Other ratios */ .ratio2_1:before < padding-top: 50%; >.ratio1_2:before < padding-top: 200%; >.ratio4_3:before < padding-top: 75%; >.ratio16_9:before

Replies

Zwei alte Artikel aus der deutschen Webentwickler-Szene lese ich immer wieder: — Webfonts *richtig* verwenden (6,5 Jahre alt ) von @maddesigns maddesigns.de/webfonts-richt… — Height equals width with pure CSS (7,5 Jahre alt) von @MadeMyDay mademyday.de/height-equals-… Beständig! Pascal «Pepo» Szewczyk – November 10, 2020 via twitter.com

mademyday.de/height-equals-… Matching heights and widths in CSS Useful bit of knowledge from @MadeMyDay 😀 ⚡ Beard.dev ⚡ – December 4, 2020 via twitter.com

Источник

Оцените статью