简体   繁体   English

使用导航栏在视口中使3列等高

[英]Make 3 columns equal height in viewport with nav bar

I have a nav bar that is 70px, under the nav bar I will have 3 columns stacked on top of each other. 我有一个70px的导航栏,在导航栏下,我会彼此堆叠3列。 Im trying to figure out a way to make the columns all of equal height. 我试图找出一种使列都相等的高度的方法。 Basically something like: 基本上像这样:

height: calc((100vh-70px)/3);

I don't think this works, any solutions similar to this? 我认为这行不通,有没有类似的解决方案?

 <nav>
   <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
   </ul>
 </nav>

 <div id="workTop">
     <div class="one_third column workSquare"></div>
      <div class="one_third column workSquare2"></div>
      <div class="one_third column workSquare"></div>
 </div>

If you want it to be responsive then you can use media queries to account for different screen sizes. 如果您希望它具有响应能力,则可以使用媒体查询来说明不同的屏幕尺寸。 Or, use Bootstrap or some other grid system. 或者,使用Bootstrap或其他网格系统。

If your goal is to change the size of the columns when the screen changes size then you can use something like: 如果您的目标是在屏幕更改大小时更改列的大小,则可以使用以下方法:

$(window).resize(function(){
    $('.column').css("height", $(window).height() / 3);
});

Obviously this doesn't account for the nav but you get the idea. 显然,这并不能解释资产净值,但您知道了。

Edit: 编辑:

@gyre pointed out we should focus on CSS so here's an expanded option. @gyre指出我们应该关注CSS,因此这是一个扩展的选项。

If you are wanting the columns to always stretch from the top to the bottom, then you can use percentages and mix them with media queries to approximate the desired balance across sizes. 如果希望各列始终从顶部延伸到底部,则可以使用百分比,并将它们与媒体查询混合,以近似估计各个大小之间的所需平衡。 For example: 例如:

@media screen and (max-width: 480px){
    .column{
        height: 85%;
    }
}
@media screen and (max-width: 780px){
    .column{
        height: 95%;
     }
}  

This will however, only make the column 85% of the parent element. 但是,这只会使该列占父元素的85%。 So you should account for that. 因此,您应该考虑到这一点。

calc() should work for what you are trying to do, note that by calc() docs + and - operators must be surrounded by whitespace. calc()应该可以满足您的需求,请注意calc()docs +和-运算符必须由空格包围。

Note: The + and - operators must always be surrounded by whitespace. 注意: +和-运算符必须始终被空格包围。 The operand of calc(50% -8px) for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is a percentage followed by a minus sign and a length. 例如,calc(50%-8px)的操作数将被解析为百分比,然后是负长度,一个无效的表达式,而calc(50%-8px)的操作数将是一个百分比,后跟一个负号和一个长度。 Even further, calc(8px + -50%) is treated as a length followed by a plus sign and a negative percentage. 更进一步,将calc(8px + -50%)视为长度,后跟加号和负百分比。 The * and / operators do not require whitespace, but adding it for consistency is allowed, and recommended. *和/运算符不需要空格,但是允许添加空格以保持一致性,因此建议使用。

so change your height to 所以把你的身高改为

height: calc((100vh - 70px)/3);

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

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