维持使用CSS的DIV的纵横比
我想创建一个响应式 div
,该可以随着窗口的宽度而改变其宽度/高度。
是否有任何CSS规则可以使高度根据宽度,变化,同时保持其长宽比?
我知道我可以通过JavaScript执行此操作,但是我希望只使用CSS。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想创建一个响应式 div
,该可以随着窗口的宽度而改变其宽度/高度。
是否有任何CSS规则可以使高度根据宽度,变化,同时保持其长宽比?
我知道我可以通过JavaScript执行此操作,但是我希望只使用CSS。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
只需创建一个包装器
< div>
具有padding bottom
的百分比值,例如:它将导致高度等于其容器宽度的75%(4:3纵横比)的高度等于75%。
这取决于以下事实:
其他长宽比的填充值和100%宽度:
将内容放在DIV:中,
以保持DIV的长宽比并防止其内容伸展,您需要绝对添加一个将孩子放置在包装器的边缘上:
这是一个 demo 和另一个更深入 demo
Just create a wrapper
<div>
with a percentage value forpadding-bottom
, like this:It will result in a
<div>
with height equal to 75% of the width of its container (a 4:3 aspect ratio).This relies on the fact that for padding :
Padding-bottom values for other aspect ratios and 100% width :
Placing content in the div :
In order to keep the aspect ratio of the div and prevent its content from stretching it, you need to add an absolutely positioned child and stretch it to the edges of the wrapper with:
Here's a demo and another more in depth demo
有几种方法可以在元素上指定固定的长宽比,其中有2个:
1。
这是最简单,最灵活的解决方案。它直接指定了元素的固定宽度至高度(或高度至宽度)。这意味着您还可以根据元素高度。
指定纵横比。
它不依赖于父宽度(例如填充技术)或视口大小(例如以下
vw
单元技术),它依赖于元素自己的宽度或高度更多信息mdn 。与其他解决方法相比,这就是使其如此强大的原因。这是现代财产(2021)。所有现代浏览器都支持它,请参见 caniuse for精确浏览器支持。
以下是一些具有不同纵横比的示例:
2。使用
vw
单位:您可以将
vw
单位用于元素的宽度和高度。这允许保留元素的长宽比,基于视口宽度。另外]您也可以使用
vh
进行视口高,甚至vmin
/vmax
使用较小/更大的视口维度(讨论在这里)。示例:1:1纵横比
对于其他长宽比,您可以使用下表根据元素的宽度来计算高度的值:
示例:正方形divs的4x4网格
这是带有此演示的小提琴,这是使A
浏览器对VH/VW单元的支持是IE9+请参阅 caniuse有关更多信息
There are several ways to specify a fixed aspect ratio on an element like a div, here are 2 of them:
1. The
aspect-ratio
CSS propertyThis is the most simple and flexible solution. It directly specifies a fixed width to height (or height to width) aspect ratio for an element. This means you can also specify an aspect ratio according to the elements height.
It doesn't rely on the parent width (like the padding technique) or the viewport size (like the following
vw
unit technique) it relies on the element's own width or height More info on MDN. That is what make it so powerfull compared to other workarounds.This is a modern property (2021). All modern browsers support it, see caniuse for precise browser support.
Here are a few examples with different aspect ratios :
2. Using
vw
units:You can use
vw
units for both the width and height of the element. This allows the element's aspect ratio to be preserved, based on the viewport width.Alternatively, you can also use
vh
for viewport height, or evenvmin
/vmax
to use the lesser/greater of the viewport dimensions (discussion here).Example: 1:1 aspect ratio
For other aspect ratios, you can use the following table to calculate the value for height according to the width of the element :
Example: 4x4 grid of square divs
Here is a Fiddle with this demo and here is a solution to make a responsive grid of squares with verticaly and horizontaly centered content.
Browser support for vh/vw units is IE9+ see canIuse for more info
使用
faction-ratio
css属性Use the
aspect-ratio
CSS property该答案是在CSS中实现
方面比例
之前编写的。这就是您今天应该使用的 。原始答案
我偶然发现了我所考虑的使用
&lt; svg&gt;
和显示:网格
。显示:Grid
元素允许您使用相同的grid-area
。这意味着它们都是流量含量,重叠的,并且从开箱即用设定了比率 。
其中之一将是负责设置比率的
&lt; svg&gt;
。另一个实际内容。如果实际内容很短,并且永远不会填充整个比率(并且您只想以该比率为中心的空间中心),只需将其居中(请参见下面的第一个可运行的片段)。SET
&lt; svg&gt;
的比率与您想要的任何东西:&lt; svg viewbox =“ 0 0 4 3”&gt;&lt;&lt;/svg&gt;
&lt; svg; svg; svg;
如果您需要一个解决方案,其中内容元素具有更多的内容,则需要将其限制为具有所需比例的可滚动区域,请设置
位置:父母上的相对
,位置:absolute;身高:100%;在内容上溢出Y:auto;
,允许流量内容元素(&lt; svg&gt;
)设置大小,因此比率。正如@emjay在下面的评论中指出的那样,只要它正确编码,就可以将比率SVG放在父母的伪元素之一中:
当在伪元素中使用时,
&lt; svg&gt;
变成替换元素,默认情况下,它位于可变高度的基线(4px 在Chrome中,
3.5px
在Firefox中)。基线的高度根据line-height
而变化,这就是为什么我们需要在伪造上设置line-height:0
以获得准确的比率。更多详细信息在这里。我个人更喜欢
&lt; svg&gt;
放置在标记中的版本,因为我可以拥有一个与各种比率容器的单个类(.ratio
)(相反)要为每个单独比率提供课程)。This answer was written before
aspect-ratio
was implemented in CSS. That's what you should use today.Original answer
I stumbled upon what I consider a smart solution for this problem, using
<svg>
anddisplay:grid
.A
display:grid
element allows you to occupy the same space with two (or more) of its children, using the samegrid-area
.This means means they are all flow content, overlapped, and out of the box the taller one sets the ratio.
One of them will be an
<svg>
in charge of setting the ratio. The other, actual content. If actual content is short and never fills up the entire ratio (and you just want it centered in a space with this ratio), simply center it (see first runnable snippet below).Set
<svg>
s ratio to whatever you want:<svg viewBox="0 0 4 3"></svg>
<svg viewBox="0 0 16 9"></svg>
If you need a solution where the content element has more content you want confined into a scrollable area with desired ratio, set
position:relative
on the parent andposition:absolute; height:100%; overflow-y: auto;
on the content, allowing the flow content element (the<svg>
) to set the size, therefore the ratio.As @emjay noted in a comment below, the ratio svg can be placed in one of the parent's pseudo-elements, as long as it's properly encoded:
When used inside a pseudo-element, the
<svg>
becomes a replaced element which, by default, sits on a baseline of variable height (4px
in Chrome,3.5px
in Firefox). The height of the baseline varies according toline-height
, which is why we need to setline-height: 0
on the pseudo to get an accurate ratio. More details here.I personally prefer the version where the
<svg>
is placed in markup, as I can have a single class (.ratio
) dealing with containers of various ratios (as opposed to having a class for each individual ratio I might need).我找到了一种使用CSS来执行此操作的方法,但是您必须小心,因为它可能会根据您自己的网站的流程而改变。我这样做是为了将视频嵌入我网站的流体宽度部分中的视频。
假设您有一个类似的嵌入式视频:
然后,您可以将所有视频放在Div中,上面有“视频”课。该视频类可能是您网站中的流体元素,因此它本身没有直接的高度约束,但是当您调整浏览器大小时,它会根据网站的流量而变化。这将是您可能正在尝试获取嵌入式视频的元素,同时保持视频的一定纵横比。
为了做到这一点,我将图像放在“视频”类div中的嵌入式对象之前。
!!!!重要的部分是图像具有您希望维护的正确纵横比。另外,请确保图像的大小至少与您期望的视频(或所维护AR的任何内容)所获得的最小的大小相同。这将避免在图像衡量百分比时解决图像的任何潜在问题。例如,如果您想保持纵横比为3:2,则不要仅使用2PX图像。它可能在某些情况下起作用,但我没有测试过,这可能是个好主意。
您可能应该已经有一个最小宽度,例如为网站的流体元素所定义的。如果没有,那么这样做是一个好主意,以免在浏览器窗口太小时切碎元素或重叠。最好在某个时候拥有滚动条。网页应获得的宽度最小的位置约为600px(包括任何固定宽度列),因为除非您要处理电话友好的网站,否则屏幕分辨率不会较小。 !!!!
我使用一个完全透明的PNG,但我真的不认为如果您做对了,最终并不重要。像这样:
现在您应该能够添加与以下几个类似的CSS:
确保您还删除对象中的任何明确高度或宽度声明,以及通常带有复制/粘贴嵌入代码的标签。
它的工作方式取决于视频类元素的位置属性以及您想要的项目维持一定的纵横比。它利用图像在元素中调整大小时将保持适当的纵横比的方式。它告诉视频类元素中的其他内容,通过将其宽度/高度强加于图像调整的视频类元素的100%,以全面利用动态图像提供的房地产。
很酷,嗯?
您可能需要进行一些操作才能使其与自己的设计一起使用,但这实际上对我来说效果很好。一般概念在那里。
I've found a way to do this using CSS, but you have to be careful as it may change depending on the flow of your own web site. I've done it in order to embed video with a constant aspect ratio within a fluid width portion of my web site.
Say you have an embedded video like this:
You could then place this all inside a div with a "video" class. This video class will probably be the fluid element in your website such that, by itself, it has no direct height constraints, but when you resize the browser it will change in width according to the flow of the web site. This would be the element you are probably trying to get your embedded video in while maintaining a certain aspect ratio of the video.
In order to do this, I put an image before the embedded object within the "video" class div.
!!! The important part is that the image has the correct aspect ratio you wish to maintain. Also, make sure the size of the image is AT LEAST as big as the smallest you expect the video (or whatever you are maintaining the A.R. of) to get based on your layout. This will avoid any potential issues in the resolution of the image when it is percentage-resized. For example, if you wanted to maintain an aspect ratio of 3:2, don't just use a 3px by 2px image. It may work under some circumstances, but I haven't tested it, and it would probably be a good idea to avoid.
You should probably already have a minimum width like this defined for fluid elements of your web site. If not, it is a good idea to do so in order to avoid chopping elements off or having overlap when the browser window gets too small. It is better to have a scroll bar at some point. The smallest in width a web page should get is somewhere around ~600px (including any fixed width columns) because screen resolutions don't come smaller unless you are dealing with phone-friendly sites. !!!
I use a completely transparent png but I don't really think it ends up mattering if you do it right. Like this:
Now you should be able to add CSS similar to the following:
Make sure you also remove any explicit height or width declaration within the object and embed tags that usually come with copy/pasted embed code.
The way it works depends on the position properties of the video class element and the item you want have maintain a certain aspect ratio. It takes advantage of the way an image will maintain its proper aspect ratio when resized in an element. It tells whatever else is in video class element to take full-advantage of the real estate provided by the dynamic image by forcing its width/height to 100% of the video class element being adjusted by the image.
Pretty cool, eh?
You might have to play around with it a bit to get it to work with your own design, but this actually works surprisingly well for me. The general concept is there.
Elliot启发了我进入此解决方案 - 谢谢:
axpectRatio.png
是一个完全透明的png文件,在我的情况下,您首选的方面比例的大小,在我的情况下是30x10像素。HTML
CSS3
请注意:
背景大小
是CSS3-Feature,可能无法与您的目标螺旋体配合使用。您可以检查互操作性(Fe on caniuse.com )。Elliot inspired me to this solution - thanks:
aspectratio.png
is a completely transparent PNG-file with the size of your preferred aspect-ratio, in my case 30x10 pixels.HTML
CSS3
Please note:
background-size
is a css3-feature which might not work with your target-browsers. You may check interoperability (f.e. on caniuse.com).由于 @web-tiki已经显示了一种使用
vh
/vw
的方法,我还需要一种在屏幕中集中的方法,这是 9的摘要代码:16 肖像。Translatey
将使该中心放在屏幕中。calc(100vw * 16/9)
预计9/16的高度。(100VW * 16/9-100VH)
是溢流高度溢流高度/2 将其将其保持在屏幕上。对于景观,并保持 16:9 ,您显示的使用
比率为9/16很容易更改,无需预先定义
100:56.25
或100:75
。如果您想先确保高度,则应切换宽度和高度,例如高度:100VH;宽度:calc(100VH * 9/16)
9:16 肖像。如果您想适应不同的屏幕尺寸,也可能感兴趣
@Media(eTrientation:Portrait)
/@media(entrientation:landscape)
肖像
/景观
更改比率的媒体查询。As @web-tiki already show a way to use
vh
/vw
, I also need a way to center in the screen, here is a snippet code for 9:16 portrait.translateY
will keep this center in the screen.calc(100vw * 16 / 9)
is expected height for 9/16.(100vw * 16 / 9 - 100vh)
is overflow height, so, pull upoverflow height/2
will keep it center on screen.For landscape, and keep 16:9, you show use
The ratio 9/16 is ease to change, no need to predefined
100:56.25
or100:75
.If you want to ensure height first, you should switch width and height, e.g.height:100vh;width: calc(100vh * 9 / 16)
for 9:16 portrait.If you want to adapted for different screen size, you may also interest
@media (orientation: portrait)
/@media (orientation: landscape)
portrait
/landscape
to change the ratio.如接受答案,填充值是百分比(强调我的):
,一个响应式DIV的正确示例,该响应型Div保持16:9的宽高比如下:
CSS
HTML
jsfiddle上的演示
As stated in here on w3schools.com and somewhat reiterated in this accepted answer, padding values as percentages (emphasis mine):
Ergo, a correct example of a responsive DIV that keeps a 16:9 aspect ratio is as follows:
CSS
HTML
Demo on JSFiddle
使用CSS
faction-ratio
属性https://caniuse.com/# search = faction-ratio
https://web.dev/aspect-ratio/
如果您有兴趣如何使用它 - 请查看以下超级简单示例
Use the CSS
aspect-ratio
propertyhttps://caniuse.com/#search=aspect-ratio
https://web.dev/aspect-ratio/
If you are interested how to use it - take a look at the below super easy example
要添加到Web_designer的答案中,
&lt; div&gt;
将具有包含元素的其宽度的75%的高度(完全由底部填充)。这是一个很好的摘要: http://mattsnider.com/css-using-percent-percent-percent-percent-percent-for-保证金和衬托/。我不确定为什么会这样,但这就是这样。如果您希望您的Div是100%以外的宽度,则需要另一个包装div来设置宽度:
我最近使用了与Elliot类似的图像技巧相似的东西,以便我使用CSS媒体查询来根据不同的徽标文件提供其他徽标文件在设备分辨率上,但仍以成比例地缩放为
&lt; img&gt;
自然会执行(我将徽标作为背景图像设置为具有正确方面的透明.png 比率)。但是Web_designer的解决方案可以为我节省HTTP请求。To add to Web_Designer's answer, the
<div>
will have a height (entirely made up of bottom padding) of 75% of the width of it's containing element. Here's a good summary: http://mattsnider.com/css-using-percent-for-margin-and-padding/. I'm not sure why this should be so, but that's how it is.If you want your div to be a width other than 100%, you need another wrapping div on which to set the width:
I used something similar to Elliot's image trick recently to allow me to use CSS media queries to serve a different logo file depending on device resolution, but still scale proportionally as an
<img>
would naturally do (I set the logo as background image to a transparent .png with the correct aspect ratio). But Web_Designer's solution would save me an http request.这是对:
This is an improvement on web_designer's answer:
您可以使用SVG。使容器/包装器位置亲戚,将SVG首先放置为静态定位,然后放置绝对定位的内容(TOP:0;左:0;右:0; botton; bottom; bottom;)
示例具有16:9比例:
image.svg:image.svg: (可以在src中列入)
CSS:
html:
请注意,内联SVG似乎不起作用,但是您可以urlencode svg并将其嵌入IMG SRC中这样的属性:
You can use an svg. Make the container/wrapper position relative, put the svg first as staticly positioned and then put absolutely positioned content (top: 0; left:0; right:0; bottom:0;)
Example with 16:9 proportions:
image.svg: (can be inlined in src)
CSS:
HTML:
Note that inline svg doesn't seem to work, but you can urlencode the svg and embed it in img src attribute like this:
我的做法是:
例如:
source
Here's how I do it :
For example :
source
基于您的解决方案,我已经做了一些技巧:
当您使用它时,您的HTML只会以
这种方式使用它:
CSS:
和JS(jQuery)
,并拥有此功能,您将其设置为attr
data-keep-Ratio
将其设置为高度/宽度。Basing on your solutions I've made some trick:
When you use it, your HTML will be only
To use it this way make:
CSS:
and js (jQuery)
And having this you just set attr
data-keep-ratio
to height/width and that's it.使用伪元素的网格和填充物,
我找到了一些更新鲜的方法来解决这种情况。该解决方案是任何
padding bottom
方法的后代,但是没有任何位置:绝对
儿童,只需使用display> display:grid;
and pseudo元素。在这里,我们有
.ratio :: < / code>带有良好的旧
padding-bottom:xx%< / code>和
grid-area:1/1/1/1; < / code >,这迫使伪元素将位置保持在网格中。尽管在这里我们有
width:0;
以防止此元素溢出的主要元素(我们冷使用z-index
在这里,但这较短)。和我们的主要元素
.ratio&gt; *:First-Child < / code>具有与
.ratio :: < / code>之前的位置相同的位置他们俩都共享相同的网格单元。现在,我们可以将任何内容放在DIV中,伪元素是确定宽度/高度比率的内容。有关 grid-area 的更多信息。
尽管您可以使用
style
属性使用CSS VAL并将比例放在HTML中。还可以使用显示:Inline-Grid
。Using Grid and padding of pseudo element
I have found some more fresh way to solve this case. This solution is a descendant of a any
padding-bottom
method, but without anyposition: absolute
children, just usingdisplay: grid;
and pseudo element.Here we have
.ratio::before
with good oldpadding-bottom: XX%
andgrid-area: 1 / 1 / 1 / 1;
, which forces the pseudo element to keep the position in grid. Although here we havewidth: 0;
to prevent overflowing main element by this one (we cold usez-index
here, but this one is shorter).And our main element
.ratio > *:first-child
has the same position as.ratio::before
, which isgrid-area: 1 / 1 / 1 / 1;
, so they both shares the same grid cell place. Now we can put any content in our div, and the pseudo element is the one who determines the width/height ratio. More about grid-area.Although you can use CSS val and place you ratio in HTML using
style
attribute. Works withdisplay: inline-grid
as well.虽然大多数答案都很酷,但大多数答案都需要正确尺寸的图像尺寸...其他解决方案仅适用于宽度,并且不在乎可用的高度,但是有时您也希望将内容适合一定高度。
我尝试将它们搭接在一起以携带一个完全便携式和可重新设定的解决方案...诀窍是用于自动缩放图像,而是使用Inline SVG元素,而不是使用预渲染的图像或任何形式的第二个HTTP请求...
请注意,我使用了SVG的宽度和高度属性中的大值,因为它需要大于最大预期大小,因为它只能缩小。该示例使DIV的比率10:5
While most answers are very cool, most of them require to have an image already sized correctly... Other solutions only work for a width and do not care of the height available, but sometimes you want to fit the content in a certain height too.
I've tried to couple them together to bring a fully portable and re-sizable solution... The trick is to use to auto scaling of an image but use an inline svg element instead of using a pre-rendered image or any form of second HTTP request...
Notice that I have used big values in the width and height attributes of the SVG, as it needs to be bigger than the maximum expected size as it can only shrink. The example makes the div's ratio 10:5
如果要在肖像或景观视图上安装一个正方形(尽可能大,但没有粘在外面),请在使用
vw
/vh
之间切换在方向上肖像
/<代码>景观:If you want to fit a square inside the viewport on either portrait or landscape view (as big as possible, but nothing sticking outside), switch between using
vw
/vh
on orientationportrait
/landscape
:您可以使用SVG实现这一目标。
这取决于案例,但在某些情况下确实很有用。例如 - 您可以在不设置固定高度的情况下设置
background -image
,或使用比率16:9
嵌入YouTube&lt; iframe&gt;
和位置:绝对
等。对于
3:2
比率集viewbox =“ 0 0 3 2”
等等。例子:
You can achieve that by using SVG.
It depends on a case, but in some it is really usefull. As an example - you can set
background-image
without setting fixed height or use it to embed youtube<iframe>
with ratio16:9
andposition:absolute
etc.For
3:2
ratio setviewBox="0 0 3 2"
and so on.Example:
我想分享我的解决方案,其中有一个
img
-TAG填充特定纵横比。由于缺乏CMS的支持,我无法使用background
,我不想使用这样的样式标签: 。)“ /&gt; < /code>。另外,宽度为100%,因此不需要像某些解决方案一样以固定尺寸设置。它将响应地扩展!I'd like to share my solution, where I have an
img
-tag filling a certain aspect ratio. I couldn't usebackground
because of lack of support of the CMS and I'd not prefer to use a style tag like so:<img style="background:url(...)" />
. Also, the width is 100%, so it doesn't need to be set at a fixed size as in some of the solutions. It will scale responsively!可以说,您有2个Divs Outer Div是一个容器,内部可能是您需要保持其比率(IMG或YouTube Iframe或其他任何元素)
HTML看起来像这样:
您需要保持比率保持比率“元素”
比率=&gt; 4至1或2至1 ...
CSS看起来像该
填充时,当指定%时,它是根据宽度而不是高度计算的。
..
因此,基本上,您的宽度IT高度始终是根据该宽度的。这将保持比率。
lets say you have 2 divs the outher div is a container and the inner could be any element that you need to keep its ratio (img or an youtube iframe or whatever )
html looks like this :
lets say you need to keep the ratio of the "element"
ratio => 4 to 1 or 2 to 1 ...
css looks like this
padding when specified in % it is calculated based on width not height.
..
so basically you it doesn't matter what your width it height will always be calculated based of that . which will keep the ratio .
使用帆布元素维持长宽比的一种简单方法。
尝试调整下面的DIV大小以查看它的作用。
对我来说,这种方法效果最好,因此我与其他人分享,以便他们也可以从中受益。
还可以使用动态高度!
A simple way of maintaining the aspect ratio, using the canvas element.
Try resizing the div below to see it in action.
For me, this approach worked best, so I am sharing it with others so they can benefit from it as well.
Also works with dynamic height!
只是一个想法或骇客。
Just an idea or a hack.
新的Chrome 88中的新事物,即将在其他浏览器中遵循新的CSS
faction-ratio
属性。css技巧
a href =“ https://css-tricks.com/almanac/properties/a/aspect-ratio/” rel =“ nofollow noreferrer“> 更多信息
New in Chrome 88 and soon to follow in other browsers is the new CSS
aspect-ratio
property.CSS Tricks Article
More Information
CSS为此拥有一个新属性:
expact-Ratio
。自V88以来,Chrome and Edge一直在完全支持这一点,Firefox自V81(set
layout.css.spect-ratio.enabled.enabled
to to以来一直支持后面的 > true
in aid:config )。有关兼容性信息,请检查 https://caniuse.com/mdn-css_properties_aspect-ratio
CSS has a new property for this:
aspect-ratio
.Chrome and Edge have been supporting this fully since V88, and Firefox has been supporting it behind a flag since V81 (set
layout.css.aspect-ratio.enabled
totrue
in about:config).For compatibility information, check https://caniuse.com/mdn-css_properties_aspect-ratio
新的Faction-Ratio标签将很棒,但它弄乱了我的Divs的位置。
填充包装师Div的传统解决方案可以根据父或视口的宽度来调整大小,当高度是限制因子时,这会使它有问题。
我发现
min()
函数非常有用,并调整了这样的传统方法:The new aspect-ratio tag would be awesome, but it messed up the positioning of my divs.
The traditional solution of padding a wrapper div, works, but only adjusts the size according to the width of the parent or the viewport, which makes it problematic when the height is the limiting factor.
I found the
min()
function to be very useful, and adjusted the traditional method like so:说您想保持宽度:100px和高度:50px(即2:1)
只是做这个数学:
Say that you like to maintain Width: 100px and the Height: 50px (i.e., 2:1)
Just do this math:
我刚刚创建了一个2:1的div,该div大小以占据全宽度,但是如果它会导致顶部或底部超过,则缩小宽度。但是请注意,这仅适用于窗口的大小,而不仅仅是父母的大小。
我敢肯定,您可以解决
4:3
而不是2:1
的正确%。I just created a 2:1 div that resized to occupy the full width, but then shrinks the width if it would cause the top or bottom to exceed. But note, this will only work with the size of the window, not the size of the parent.
I'm sure you can work out the correct %'s to use for
4:3
instead of2:1
.https://developer.mozilla.orgla.org/en-en-us/ DOCS/WEB/CSS/FECTACT-RATIO
https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
我使用了一个新解决方案。
但是,至关重要的是
,这是相对于整个视口的。因此,如果您需要一个视口宽度的30%的DIV,则可以使用30VW,并且由于知道宽度,因此您使用Calc和VW单元将其重复使用。
I used a new solution.
To main aspect ratio
However, this is relative to the entire viewport. So, if you need a div that is 30% of the viewport width, you can use 30vw instead, and since you know the width, you reuse them in height using calc and vw unit.
这是我的解决方案,可在具有可选固定边缘的DIV上维持肖像或景观的16:9纵横比。
它是宽度/高度和最大宽度/最大高度属性与大众单元的组合。
在此样本中,悬停在该样本中添加了50px的顶部和底部边缘。
Here is my solution for maintaining an 16:9 aspect ratio in portrait or landscape on a div with optional fixed margins.
It's a combination of width/height and max-width/max-height properties with vw units.
In this sample, 50px top and bottom margins are added on hover.
Demo on JSFiddle