Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min-width and max-width with the same value?

Tags:

css

width

In the past, I saw the next css and I was thinking if there is some actual difference between

min-width: 90px;
max-width: 90px;

and

width: 90px;
like image 827
fcortes Avatar asked Nov 06 '17 09:11

fcortes


People also ask

Can we use max width and min width at the same time?

Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide. This can be used to target specific devices with known widths.

How do you give both min and max width in media query?

If you want to include both min and max width for responsiveness in the browser, then you can use the following: @media (min-width: 768px) and (max-width: 992px){...} @media (min-width: 480px) and (max-width: 767px) {...}

Does width override min width?

The min-width property in CSS is used to set the minimum width of a specified element. The min-width property always overrides the width property whether followed before or after width in your declaration. Authors may use any of the length values as long as they are a positive value.

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.


1 Answers

using width will simply specify fixed width over the element without paying attention to its content (so you can have overflow) :

div {
  width: 80px;
  border:2px solid red;
}
<div>
  <img src="https://lorempixel.com/200/100/" />
</div>

Using max-width means that the element will have an upper bound for its width. So its width can be from 0 to max-width depending on its content.

div {
  max-width: 300px;
  border: 2px solid red;
}

.diff {
  display: inline-block;
}
<div>
  <!-- this i a block element so max-width prevent it from taking 100% width -->
  <img src="https://lorempixel.com/200/100/" />
</div>
<div class="diff">
  <!-- this i an inline-block element so max-width has no effect in this case cause the content is taking less than 300px  -->
  <img src="https://lorempixel.com/200/100/" />
</div>
<div>
  <!-- You have overflow because the element cannot have more than 300 of width -->
  <img src="https://lorempixel.com/400/100/" />
</div>

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).

div {
  min-width: 300px;
  border: 2px solid red;
}

.diff {
  display: inline-block;
  min-height:50px;
}
<div>
  <img src="https://lorempixel.com/200/100/" />
</div>
<div class="diff">
  
</div>
<div class="diff">
  <img src="https://lorempixel.com/200/100/" />
</div>
<div>
  <img src="https://lorempixel.com/400/100/" />
</div>

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.

div {
  min-width: 300px;
  max-width: 300px;
  border: 2px solid red;
}

.diff {
  display: inline-block;
  min-height:50px;
}
<div>
  <img src="https://lorempixel.com/200/100/" />
</div>
<div class="diff">
  
</div>
<div class="diff">
  <img src="https://lorempixel.com/200/100/" />
</div>
<div>
  <img src="https://lorempixel.com/400/100/" />
</div>

Special Cases

In some particular cases, width will not give the same result as min-width/max-width like with Flexbox where we have the shrink feature that allow an element to shrink to fit its container

.box {
  width:200px;
  border:1px solid red;
  display:flex;
  margin:5px;
}
.box > div {
  border:2px solid;
  height:50px;
}
<div class="box">
  <div style="width:300px"></div>
</div>

<div class="box">
  <div style="min-width:300px;max-width:300px;"></div>
</div>

As you can see in the second case the element will not shrink because, unlike width, min-width will prevent this.

Another case is the use of resize property:

div {
  border: 2px solid;
  height: 50px;
  overflow: auto;
  resize: both;
}
<div style="width:300px"></div>

<div style="min-width:300px;max-width:300px;"></div>

As you can see, we can resize the element defined by width and not the one defined by min-width/max-width


We should also note that min-width/max-width is more powerful than width. Setting the 3 properties to different values will make min-width the winner

.box {
  border: 2px solid;
  height: 50px;
  width:100px;
  min-width:200px;
  max-width:150px;
}
<div class="box"></div>

This means that we can override a value set by width using min-width/max-width but not the opposite

like image 177
Temani Afif Avatar answered Oct 19 '22 08:10

Temani Afif