#media screen units are not pixels, so what are they? - css

This is what I want
If browser is bigger than 900px I want the boxes to be 700px.
Once the browser width is smaller than 900px I want the boxes to be 600px.
#media screen and (min-width : 750px) and (max-width : 900px) {
.multiSelect .checkboxLayer {
width: 600px;
}
}
#media screen and (min-width: 900px) {
.multiSelect .checkboxLayer {
width: 700px;
}
}
My question is, how come the browser width splits around 932px and not 900px?
What units are these if not pixels?
I just tried to measure the browser width with photoshop and it splits at 975 px exactly.
So CSS says 900 px, Chrome says 932 px and photoshop says 975 px
This is really confusing?!?!

The short answer:
You have not reset the box-sizing property for the parent container.
The long answer:
Consider this:
In CSS, by default, the width and height you assign to an element is applied only to the element's content box. If the element has any border or padding, this is then added to the width and height to arrive at the size of the box that's rendered on the screen.
There are two values in box-sizing that you should know about
content-box
[...] the default, and gives you the default CSS box-sizing behavior. If you
set an element's width to 100 pixels, then the element's content box
will be 100 pixels wide, and the width of any border or padding will
be added to the final rendered width.
border-box
[...] tells the browser to account for any border and padding in the
value you specify for width and height. If you set an element's width
to 100 pixels, that 100 pixels will include any border or padding you
added, and the content box will shrink to absorb that extra width.
This typically makes it much easier to size elements.
I usually start any CSS sheet with the following to avoid such issues
* {
box-sizing: border-box;
padding: 0;
margin: 0
}
There are many frameworks that reset more of the default properties such as normalize.css but I often don't have the need to use them.
I set up a demo below. There are two div elements. There is also a media query.
Both elements have the same fixed width in px. The only difference is the value for the box-sizing
The first is set to border-box and the second is set to content-box
div {
width: 400px;
height: 400px;
margin: 3em;
text-align: center;
background: darkred;
border: 20px solid #131418
}
#media (max-width: 800px) {
div {
background: darkgreen;
width: 300px;
}
}
.first {
box-sizing: border-box
}
.second {
box-sizing: content-box
}
<div class="first"></div>
<div class="second"></div>
Notice how they render with different sizes even when the width for both is the same?

Scroll bar width is 17px on most browsers, and does not contribute to the screen width. Try using Firefox's responsive design mode, in the dev tools.

Related

Using css to responsively resize the image and text inside a button tag

I have this:
.buttm{
background: url('http://redshadow67.com/deepest/img/butts/divm.png') no-repeat center black;
width: 100px; text-align: center; color: #f5c324; font-size: 18px; margin: 0 auto; border: 0px;
}
but I want to make the width between 60 and 100 depending on the viewscreen. When I use a percentage it doesn't resize the image, and also I need the text to be a little smaller also. Not sure what to do??? thanks
Have you tried media queries?
#media only screen and (min-width: 1024px) {
// Your custom styles go here.
}
To scale the background-image, use the background-size property. Try the values contain (full image shown) and cover (image scaled to fill entire element).
For width and font-size, use #media queries. You might also want to look into viewport relative units:
1vh: 1% of viewport height
1vw: 1% of viewport height
1vmin: 1% of viewport height or width, whichever is smaller
1vmax: 1% of viewport height or width, whichever is larger

Make modal-dialog fixed width but when screen re-sizes smaller then it, it must re-size responsive

I am using the default bootstrap3 css files.
I noticed that the default css width of the modal-dialog is 600px.
However I need the width to be 1000px for my screen.
If the screen is larger than 1000px the modal width still needs to be 1000px.
<style>
.modal-dialog { width: 1000px; }
</style>
This works correctly, however if I resize the window smaller than 1000px the dialog needs to resize responsively with the window width e.g. be the same as the window width.
When you resize the windows larger than 1000px it must stay the same width of 1000px.
I tried set
<style>
.modal-dialog { width: auto; }
</style>
but the behavior matches the default boostrap and the size is still the default 600px and if you resize that smaller than 600px it is adaptive. I basically want the same behaviour as default bootstrap css but for the size 1000px;
I tried combinations of the answers in the other stackoverflow question which deals with setting the modal dialog width but couldn't get a solution to work.
Just use #media rule
The #media rule is used to define different style rules for different media types/devices.
#media (min-width: 1000px)/* The minimum width of the display area, such as a browser window*/
{
.modal-dialog {
width: 1000px;
}
}
You should be able to do this by simply adding the following CSS properties to your .modal-dialog element.
width: 100%;
max-width:1000px;
This will make the modal be full width of the screen up until it reaches 1000px at which point it will stay at 1000px as the screen gets wider.
use this
<style>
.modal-dialog {
width: 1000px;
max-width: 100%;
}
</style>

Is it possible to set the minimum value of a margin set by a percentage using CSS?

What I want to accomplish is very similar to what the min-width attribute does for width values.
I want to be able to set a margin value by a percentage up to a point, and then switch over to a concrete predefined pixel value. So the margin would be margin-left: 20%; and then I would have a min-value: 100px.
Is this possible, and if not with CSS, what sorts of workarounds are there?
You can use media queries to achieve your goal.
If you have a div that takes up most of the screen, except for the 20% margin on the left, and you want that margin to be no less than 100px, then you can use a media query to set the margin to 100px when the frame width becomes less than 500px (20% of 500px is 100px).
If your HTML is this
<body>
<div class="left-margin">
My left margin is 20% unless that would become less than 100px, in which
case, my left margin would become 100px.
</div>
</body>
Then your CSS could look like this
body, html
{
margin: 0;
padding: 0;
}
.left-margin
{
margin-left: 20%;
}
#media screen and (max-width: 500px)
{
.left-margin
{
margin-left: 100px;
}
}
This basically means that everything inside the media query is only applied with frame widths of 500px or less. When the screen width becomes larger than 500px, the contents of the media query are ignored, and the margin becomes 20%.
http://jsfiddle.net/5g16xbtg/
Try stick an empty div with with width set by % and min-width by px. Just put in on before the element that you want to have a margin on. For example below.
<div style="position:relative; float:left; width:20%; min-width:100px">
<div style="position:relative; float:left; margin:0">

responsive design screens wider than 960px

I'm creating a responsive website based on this article
link
but I added this code for screens more than 980 px width
/* for more than 980px */
#media screen and (min-width: 981px)
{
#pagewrap {
width: 94%;
}
#content {
width: 20%;
}
#middle {
width: 50%;
}
#sidebar {
width: 20%;
}
}
you can see the result in codepen
link
when I set width of #middle 50% or 49%
and resize browser width to somthing near 990px
3rd Content Area goes at the bottom of two other area
but when set width to 48% its ok, I mean now 3 area are next to each other
but now another problem arises.
when browser size is something near 1395 px (my laptop)
theres a 53px Gap between 3rd Content Area and wrapper border like below (I dont want that gap)
any suggestion?
What you're running into are box-model issues. You see, you have widths set, but also margins and paddings and borders that are all messing with the actual, calculated width of your boxes. Worse, even, those margins/paddings/borders are defined in pixels while your widths you've done so in percentages.
To achieve what you want, you should use a border-box box model.
#content, #middle, #sidebar {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Now, when you say width:50%; that means "make my boxes' width 50%, including any padding and borders" (note, not margins).
Using border-box to layout your columns should help you immensely, but it will require you to change the calculations.

How to? Min-width, width and max-width (css)

I had my main div set with
max-width:600px;
However, when the content is not long enough that div will resize to the length of the content. Which makes sense.
So Instead I declared:
width:600px;
max-width:600px;
But now the div won't resize down when you resizing the window. I tried setting the min-width:
min-width:200px;
width:600px;
max-width:600px;
But it still won't resize down passed 600px (on window resize);
So what am I missing? Can I use all 3 width settings together? (min-width,width,max-width)?
If so, how? I am trying to understand the logic.
I want the content to stretch up to 600px, but also resize down on windows resize.
I think you want
width: 600px;
max-width: 100%;
This way, the element will attempt to have a width of 600px. But if the parent isn't wide enough, it will be less.
Demo
Just take out the "width: 600px" The div will be 600 px if the browser window is large enough, otherwise it will adjust down to your min width if the browser window is smaller than 600px.
You could use media queries to apply a width for certain screen sizes
.div {
width: 600px;
}
#media screen and (max-width 600px) {
.div {
width: 100%;
}
}

Resources