简体   繁体   English

如何按比例调整div的高度,直到达到一定的窗口宽度?

[英]How do I proportionally resize div height until I get to a certain window width?

I am looking at using Flexslider on my website and have seen an implementation on another site that I think is incredibly interesting. 我正在网站上使用Flexslider,并且在另一个网站上看到了一个我认为非常有趣的实现。 It resizes the divs in proportion with the images as background images (set via inline CSS styles) and a caption and link to a relevant post inside the container div. 它根据背景图像(通过内联CSS样式设置)和标题的大小按比例调整div的大小,并链接到容器div内的相关文章。 Something like: 就像是:

<ul class="flexslider">
  <li class="slide" style="background-image: url(MY_IMAGE);">
    <h1>Post heading</h1>
    <p>Post extract</p>
    <a href="#">Link</a>
  </li>
  <li class="slide" style="background-image: url(MY_IMAGE);">
    <h1>Post heading</h1>
    <p>Post extract</p>
    <a href="#">Link</a>
  </li>
  <li class="slide" style="background-image: url(MY_IMAGE);">
    <h1>Post heading</h1>
    <p>Post extract</p>
    <a href="#">Link</a>
  </li>
  <li class="slide" style="background-image: url(MY_IMAGE);">
    <h1>Post heading</h1>
    <p>Post extract</p>
    <a href="#">Link</a>
  </li>
  <li class="slide" style="background-image: url(MY_IMAGE);">
    <h1>Post heading</h1>
    <p>Post extract</p>
    <a href="#">Link</a>
  </li>
</ul>

NOTE: Not the exact code. 注意:不是确切的代码。

Each <li> is a maximum of 600px high, set via a max-height CSS attribute. 每个<li>的最大高度上限为600px,可通过max-height CSS属性设置。 The width of each <li> is 100%, and when the window is resized the slide remains in proportion. 每个<li>的宽度为100%,并且在调整窗口大小时,幻灯片会按比例保留。 However, by the time the container reaches a width of something like 1100px, it reaches the maximum 600px height, and then as the width keeps expanding to fill the window, the container height remains at 600px. 但是,当容器达到大约1100px的宽度时,它达到了最大600px的高度,然后随着宽度的不断扩大以填充窗口,容器的高度仍保持在600px。 Once the width goes below 1100px, the height changes in proportion as before. 一旦宽度低于1100像素,高度就会像以前一样按比例更改。

I know how to get the slides to change in proportion (using padding-bottom, however), but I can't seem to find a way to put this cap on the height in place (obviously because I'm using padding and not dimensions). 我知道如何更改幻灯片的比例(但是使用padding-bottom),但是我似乎找不到找到将这个上限设置在适当高度的方法(显然是因为我使用的是padding而不是尺寸)。 Could anyone advise? 有人可以建议吗? Is this only possible in JavaScript or can this be done in pure CSS too? 这仅在JavaScript中可行吗,或者也可以在纯CSS中完成吗?

You could do this using CSS with multiple media queries. 您可以将CSS与多个媒体查询一起使用。 Access the screen width with something like this: 通过以下方式访问屏幕宽度:

@media only screen and (max-width:500px){
    //large amount of padding
}
@media only screen and (max-width:400px){
    //medium amount of padding
}
@media only screen and (max-width:300px){
    //small amount of padding
}

having lots of queries could slow down load time. 有很多查询可能会减慢加载时间。 You'd have to play around with the values and number of queries to get it how you want it. 您必须处理查询的值和数量才能获得所需的查询。

使用wv(视口宽度)作为您的高度: height: 60vw例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM