Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min/max width/height with multiple values

Tags:

css

Edit in 2020 (min, max, clamp functions)

To anyone who get here by search engines:

CSS min / max / clamp functions had got some supports in recent (2020) browsers. You may try max-width: min(200px, 50%);. You can have a look at these CSS functions while be careful about compatibility issues.


Original post:

I want limit the width of an <div> no more than 50%, and no more than 200px.

Which means, when 50% is less than 200px, it should have the same behavior as max-width: 50%, otherwise its behavior becomes max-width: 200px.

How can I define this with CSS?
(without min function, which have not been supported yet)

<div class="some_element">Text here</div>
<style>
.some_element {
    background: red;
    float: left;
    width: auto;
    max-width: 200px;
    max-width: 50%; /* this will override previous one, but i want both of them work */
}
</style>
like image 412
tsh Avatar asked Jun 01 '15 07:06

tsh


People also ask

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 .

How do you use max-height and minimum height?

The min-height property in CSS is used to set the minimum height of a specified element. The min-height property always overrides both height and max-height . Authors may use any of the length values as long as they are a positive value.

In what way does the min width max width min height and max-height help in web development?

Keeping track of how things appear across multiple screen sizes may be challenging, but the max-width, min-width and max-height, and min-height properties help tackle these challenges. These properties let you adjust the size of elements without needing to use media queries.


4 Answers

You cannot set max-width to both 200px and 50%. You could put the element in another element with max-width of 400px:

<div id="parent">
    <div id="child">Text here</div>
</div>


#parent {
    background-color: red;
    max-width: 400px;
}
#child {
    background-color: green;
    max-width: 50%;
}
like image 30
Andy G Avatar answered Oct 17 '22 23:10

Andy G


You can state this desired rule as 50% max width when the page width is less than or equal to 400px, and 200px when the page width is greater than or equal to 400px. That sounds a lot like a media query! I handle smaller screen widths first and then override for larger widths, which is this CSS:

.some_element {
  background: red;
  float: left;
  width: auto;
  max-width: 50%;
}
@media (min-width: 400px) {
  .some_element {
    max-width: 200px;
  }
}

If you prefer, you can swap the 50% and the 200px and also change the min-width to max-width.

like image 122
misterhaan Avatar answered Oct 17 '22 22:10

misterhaan


<div id="parent">
    <div id="sub">test content</div>
</div>

// css

#parent {
    max-width: 200px;
}
#sub {
    width: 50%;
}
like image 2
Ravi Chauhan Avatar answered Oct 18 '22 00:10

Ravi Chauhan


create separate classes and add them dynamically based on outerWidth()

.some_element {
        background: red;
        float: left;
        width: auto;
    }

    .widthClass1{
     max-width:50%;
    }

    .widthClass2{
     max-width:200px;
    }


    $(document).ready(function(){
       if($("textDiv").outerWidth()<400){//instead of dividing by 2 I am comparing with 400
         $("textDiv").addClass('widthClass1')
       }else{
         $("textDiv").addClass('widthClass2')
       }
    });
like image 1
Sunil B N Avatar answered Oct 18 '22 00:10

Sunil B N