简体   繁体   English

如何防止最小宽度覆盖最大宽度?

[英]How to prevent min-width overriding max-width?

I've created a div that i want to be 50% (for arguments sake) but at least a certain width and at most 100% width, (so that it never extends the window)我创建了一个 div,我希望它是 50%(为了争论),但至少有一定的宽度,最多 100% 的宽度,(这样它就永远不会扩展窗口)

.about {
    position:absolute;
    right:0;
    width: 50%;
    min-width: 500px;
    max-width: 100%;
}

Basically, i want the min-width to work, but i want the max-width to be considered MORE important so that it is never wider than the window, i assumed that i could do this by the order, or at least by using !important, but this doesn't seem to be the case基本上,我希望 min-width 起作用,但我希望 max-width 被认为更重要,以便它永远不会比窗口宽,我认为我可以按顺序执行此操作,或者至少可以使用 !重要,但情况似乎并非如此

https://jsfiddle.net/ex716kam/2/ https://jsfiddle.net/ex716kam/2/

I've given it a few tries but I can't seem to make it work with CSS alone. 我已经尝试了几次,但似乎无法单独使用CSS。 I would recommend using simple javascript or media queries to make it work. 我建议使用简单的javascript或媒体查询来使其工作。

Working jsFiddle 工作jsFiddle

@media screen and (min-width:1000px){
    .about{
        width:50%;
    }
}

Note what @lmgonzalves wrote about min-width being the "strongest".. 请注意@lmgonzalves关于最小宽度是“最强”的内容。

Just switch the values of min-width and width: 只需切换min-width和width的值即可:

.about {
    position:absolute;
    right:0;
    width: 500px;
    min-width: 50%;
    max-width: 100%;
}

https://jsfiddle.net/8fkbp9d0/ https://jsfiddle.net/8fkbp9d0/

Instead of max-width: 100%; 而不是max-width: 100%; use max-width:100vw; 使用max-width:100vw; . It will not go out of your window. 它不会超出您的窗口。

It's difficult to know what behaviour you expect between 1000px down to 500px, but a media query is the simplest way to achieve what you're looking for: 很难知道您期望的行为在1000像素到500像素之间,但是媒体查询是实现所需内容的最简单方法:

 .about { position:absolute; right:0; width: 50%; min-width:500px; } @media screen and ( max-width: 500px ){ .about { width: 100%; min-width:0; } } 
 <div class="about"> <h1>About</h1> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> </div> 

try this code, this code will work when screen width will be between 500px to 100% screen resolution. 尝试使用此代码,当屏幕宽度在500px至100%屏幕分辨率之间时,此代码将起作用。

@media (min-width: 500px) and (max-width: 800px)
{
.about {
        position:absolute;
        right:0;
        width: 50%;
    }
}

min() function does the trick. min()函数可以解决问题。 Insert your max-width as an argument.插入您的max-width作为参数。 This way min-width can never be wider than your max-width这样min-width永远不会比你的max-width

min-width: min(500px, 100%);
max-width: 100%;

Currently supported in all major browsers except IE: caniuse.com目前支持除 IE 之外的所有主要浏览器: caniuse.com

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

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