With CSS how can I smoothly change a div's width from 80% of container to 100% of container as container shrinks down from 1000px to 600px? - css

I'd like to have a div be 80% of its container's width, when that width is 1000px or greater.
I'd like it to be 100% of my its container's width, when that width is 600px or smaller.
In the middle I'd like it to be, well, in the middle.
http://jsfiddle.net/ayjtr1q4/9/
div {
background: #def;
height: 100px;
margin: 0 auto;
}
#media screen and (min-width: 1000px) {
div {
width: 80%
}
}
#media screen and (min-width: 601px) and (max-width: 999px) {
div {
/* what goes in here? */
}
}
#media screen and (max-width: 600px) {
div {
width: 100%
}
}
<div>
As you resize the screen this snaps from 80% to 100%. I'd like to achieve a smooth transition between the two instead, so that if you set it to 800px wide you'll end up with a div that's 90% of that, i.e. 720px.
</div>
ContainerSize -> divSizeAs% (divSizeInPixels)
400px -> 100% (400px)
500px -> 100% (500px)
600px -> 100% (600px)
700px -> 95% (665px)
800px -> 90% (720px)
900px -> 85% (765px)
1000px -> 80% (800px)
1100px -> 80% (880px)
1200px -> 80% (960px)
Etc.
I know I could set a dozen media queries with breakpoints, but I'd rather it was a smooth transition, if possible.
I feel I could do something clever with calc, but am having a bit of a blank and can't quite get my head around what! But then I guess you'd need some max/min functionality, which I don't believe CSS has?
My middle-ground I've come up with so far is 3 media queries. One for >= 1000px (width:800%), one for <= 600px (width:100%) and one for in the middle... But even then I can't work out what my rules for the middle-ground should be?

You can do that with Calc. You are only concerned with the amount of space over 600px (100% - 600px), and the increment is the total space to smooth (400px). I'm not really a mathematician, maybe someone else (smarter) can give you a better equation.
#media screen and (min-width:1000px) and (max-width:600px){
#container{
width: calc(100% - ((100% - 600px) * 0.466));
}
}
Full code snippet:
#container{
background: red;
width: 800px;
height: 20px;
}
#media screen and (max-width:600px){
#container{
width: 100%;
}
}
#media screen and (max-width: 1000px) and (min-width: 600px) {
#container{
background: green;
width: calc(100% - ((100% - 600px) * 0.466));
}
}
<div id="container">
</div>

Here's a somewhat more elegant way to achieve what you're looking for, using only one breakpoint. Also, I recommend using em instead of px for your breakpoints.
div {
background: #def;
width: 100%;
height: 6.25em;
margin: 0 auto;
}
/* 37.5em = 600px */
#media (min-width: 37.5em) {
div {
background: #fed;
/* Replace .5 with whatever value
looks best in your layout */
width: calc(37.5em + ((100% - 37.5em) * .5));
}
}
<div>
As you resize the screen this snaps from 80% to 100%. I'd like to achieve a smooth transition between the two instead, so that if you set it to 800px wide you'll end up with a div that's 90% of that, i.e. 720px.
</div>

Related

How to use #media to change width of iframe or div between specific screen sizes(min-width & max-width)

For my site on cruiseguru.co.th, the search box on the left overlaps with page content when screen size is between width 1200px and 765px so I was hoping to change width of the search box only on between two resolution with a css like the one below but of course it doesn't work so it would be great if anyone can advise me the right css.
#media screen (max-width:1200px) and (min-width:765px) {
#IFRAME_2 {
max-width: 360px;
};
You're missing and between screen and (max-width:1200px)
#IFRAME_2 {
width: 600px;
}
#media screen and (max-width:1200px) and (min-width:765px) {
#IFRAME_2 {
width: 360px;
}
}
<iframe id="IFRAME_2"></iframe>

css element height minus height of a element with changing height

aye folks!
is it possible to have dynamic height calculation with css3? i couldn't find anything.. but maybe i'm just using the wrong terms for my search.
the problem: i have a site where i want to include an iframe with 100% height minus the height of a nav-element. unfortunately the nav-element isn't always the same height (44px on one device 36px on another.. and so on)
is there a way to calculate that? or do i need to fix the height of the nav-element to get this to work properly?
You may use display: flex property on the wrapper of the two elements.
html, body, #wrapper {
height: 100%;
}
#wrapper {
display: flex;
flex-direction: column;
}
#frame-fill {
flex-grow: 1;
}
<div id="wrapper">
<div>HEADER DYNAMIC</div>
<iframe id="frame-fill" src=''></iframe>
</div>
Be sure that you look for browser support for flex. Its a newer concept.
Sure!
I'm assuming the nav-element height is dependant on screen size, for which you can give different heights with media queries. So you only need to determine the size or width at which the element height changes:
/*put your conditions here*/
#media (min-height: 500px), (min-width: 580px) {
iframe{
height: calc(100% - 44px);
}
}
/*put your conditions here*/
#media (max-height: 1000px), (min-width: 580px) {
iframe{
height: calc(100% - 36px);
}
}
More info: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

Bootstrap centered 100% width cover responsive

I have troubles using the container class of bootstrap because I need a responsive 100% width cover. If I use this class I get a small margin in the left and in the right. So I want to that margins goes. How can I do it and still getting responsive?
Here is the container class:
.container {
margin-left: auto;
margin-right: auto;
}
IF I use the col-12 class who is 100% width, the image goes to one side when I resize and I need a centered image when I resize.
Here is the cover using the container class, you notice the right and left margin
This is the look if i use the col-12 class:
And this is what I need but without the margins. (Of course only when I'm at 100%)
Thanks.
i fixed the 100% width changing this:
#media (min-width: 1200px) {
.container {
max-width: 1170px;
}
}
To this:
#media (min-width: 1200px) {
.container {
max-width: 1280px;
}
}
That's because 1280px is the resolution of my screen.

DIV with max-width and min-width to evenly spread

I am trying to create a layout where each DIV has a max-width of 300px.
If the screen is 600px then two 100% divs should be placed next to each other.
If the screen is 700px then three 233px (each DIV 100%) should be placed next to each other.
This means that the DIVs should always take up 100% of the screen width.
I would also like to have a min-width (such as 150px) so that each DIV cannot be smaller than a certain amount.
This means that on a small screen I might get two columns with DIVs and on a large scren I might get four or more columns with DIVs.
In this example you would have 4 columns when screen is larger than 900px, and it would go down to 3 columns when screen hits 900px, then 2 columns when the screen hits 600px, and one columns when it hits 300px. This will keep divs taking up 100% of screen and maximum width will always be 300px (except for screens larger than 1200px wide, where you will still get 4 columns)
div {
width: 25%; /* anything above 900px and there will be 4 columns */
min-width: 150px;
float: left;
height: 200px;
}
#media screen and (max-width: 900px) {
div { width: 33%; }
}
#media screen and (max-width: 600px) {
div { width: 50%; }
}
#media screen and (max-width: 300px) {
div { width: 100%; }
}
This can be modified to have any number of columns and any number of different threshold screen sizes where number of columns will change.
If you have borders or margins you will also have to make the width %s slightly smaller.
div
{
width:33.3333%;
min-width:150px;
max-width:300px;
display:inline-block;
background:#EFEFEF;
border:1px solid #CCC;
height:100px;
}
I haven't tested it (today) but I think this will work until the minimum size is reached.

Width doesn't respond according to the media query

I was experimenting with media queries to see the effects.
So I tried using min-width(480px) query to change the width of a div from 100% to 520px when the window was maximised but the width of the div stays 100%.
The code:
#box {
margin: auto;
background: white;
width: 100%;
}
// Media Queries
#media only screen and (min-width: 480px) {
#box {
width: 200px;
max-width: 200px;
background: black;
}
}
So my question is, why does the width of the #box stay as 100% when the window is maximised?
What am I doing wrong?
jsFiddled here is your code with min-width:480px. It applies when the size of available space is bigger than 480px (the black box)
try max-width. This context will apply when available screen space is less then 480 pixels. jsFiddled here, black box will be applied when available space width is lesser than 480px
#media only screen and (max-width: 480px) {
#box {
width: 200px;
max-width: 200px;
background: black;
}
}
So, your #box is by default 100% width except when the available space is greater than 480px. your code is working OK.
Maybe it's the comment : // Media Queries witch caused an error ?
I think you have the media query wrong. As you have it now, it's changes the content over 480px. Where I think you want it under 480px.
So it should be:
#media only screen and (max-width: 480px) {
//code
}
Hence, (max-width: xxx) not, (min-width: xxx).
Example fiddle
I had commented the code using // syntax by accident which isn't supported in CSS, hence the code below that line of comment not working. It now works after I changed it /**/ syntax.

Resources