Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent min-width overriding max-width?

Tags:

html

css

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)

.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

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

like image 683
MintWelsh Avatar asked Jun 21 '15 15:06

MintWelsh


People also ask

Does Min-width override max-width?

max-width overrides width , but min-width overrides max-width .

Can we set min-width and max-width for an element at the same time?

And min-width specify lower bound for width. So the width of the element will vary from min-width to ... (it will depend on other style). So if you specify min-width and max-width , you will set up a lower and upper bound and if both are equal it will be the same as simply specifing a width .

What happens when the tags min-width value becomes greater than the width of the page?

The min-width property defines the minimum width of an element. If the content is smaller than the minimum width, the minimum width will be applied. If the content is larger than the minimum width, the min-width property has no effect.

Should I use max-width or min-width in media queries?

Generally if you are starting small screen first use min-width and then build on top with media queries targeting larger resolutions.


1 Answers

I've given it a few tries but I can't seem to make it work with CSS alone. I would recommend using simple javascript or media queries to make it work.

Working jsFiddle

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

Note what @lmgonzalves wrote about min-width being the "strongest"..

like image 138
Yotam Omer Avatar answered Oct 26 '22 18:10

Yotam Omer