简体   繁体   English

根据屏幕宽度调整 div 高度

[英]Resize div height based on screen width

I have a div of fixed height 445px, as following:我有一个固定高度 445px 的 div,如下所示:

header {
    text-align: center;
    height: 445px;
    overflow: hidden;
  margin-top: 0;
  box-shadow: 0 1px 1px rgba(57, 63, 72, 0.3);
}

I want this to stay the same height as the screen width shrinks.我希望它在屏幕宽度缩小时保持相同的高度。

The problem is, when the width shrinks under 500px, the title of the header becomes two lines, and the search input bar disappears as a consequence.问题是,当宽度缩小到 500px 以下时,header 的标题变成了两行,导致搜索输入栏消失。

So, I want to say something like:所以,我想说的是:

if (screen size >= 500) height = 600px

Is it possible?可能吗? I don't want to use vh , or vw , since it shouldn't be gradual, it's only for screens with under 500px.我不想使用vhvw ,因为它不应该是渐进的,它只适用于 500px 以下的屏幕。

I'm using react我正在使用反应

you can use media queries.您可以使用媒体查询。 Which you can define styles for different screen sizes.您可以为不同的屏幕尺寸定义 styles。

@media only screen and (max-width: 500px) {
    header {
        height:600px;
       }
 }

if I understood you correctly, you've to do something like that:如果我对你的理解正确,你必须做这样的事情:

@media only screen and (min-width: 500px) {
    header {
        height: 600px;
    }
}

This means that if the screen is more than 500px , the header's height will be 600px这意味着如果屏幕大于500px ,标题的高度将为600px

whats your html looking like?你的 html 是什么样的? This sounds like its a problem of the class of the div your trying to affect.这听起来像是您试图影响的 div 的 class 的问题。

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

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