I have website that with several divs. One of those divs need to be a specific with but this can variabate between 2 width. Is it possible some how to get it to only allow 2 width?
Examaple:
With resolution of page >= 700px div width = 500px
when page resolution < 700px div width = 200px
Use css media query like #media only screen and (max-width: 300px)
when you add
#media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
When you add like this when width is 300px it will automatically change to rule you added
A simple example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgreen;
}
#media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
}
</style>
</head>
<body>
<p>Resize the browserwindow. When the width of this document is less than 300 pixels, the background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>
Answer
#media screen and (max-width: 700px ) {
div{
width: 500px;
}
#media screen and (min-width: 700px ) {
div{
width: 200px;
}
Use media queries:
#media only screen and (min-width: 700px) {
.my-div {
width: 500px;
}
}
Related
How could one go about creating a div, that would have a default size of XX%, however, if the screen gets too small it would switch into 100% size? I have tried using min-width property but the problem with this is that it will not be responsive after the min-width is reached and will overflow if the screen is even smaller.
You have to use #media queries. So let's say you have a <div> that should take up only 50% of the web page and then you need to show it full width once it enters mobile phone, say 640px width:
div {
width: 50%;
}
#media screen and (max-width: 640px) {
div {
width: 100%;
}
}
you must use #media for that like this :
#media screen and (min-width: 769px) {
/* STYLES HERE */
}
#media screen and (min-device-width: 481px) and (max-device-width: 768px) {
/* STYLES HERE */
}
#media only screen and (max-device-width: 480px) {
/* STYLES HERE */
}
You can do it with #media queries, e.g.:
div {
width: 50%;
height: 100px;
border: 1px solid;
}
#media (max-width: 568px) { /* adjust to your needs */
div {width: 100%}
}
<div></div>
I have the following code inside a media query (from Bootstrap):
#media (min-width: 768px) {
.col-sm-9 {
float: left;
width: 75%;
}
}
When resizing the window between 768px and 992px, the width of my column is always 562.5px. The width of wrapping div with the row class is always 750px. Why is that? I always thought that specifying width in percent meant that the width of our element will always be 75%. For example, when window is 992px wide it would be 744px wide and when window is 768px wide it would be 576px wide.
Am I missing something?
That is because bootstrap's own rule set the container to a fixed width, which also is not the same as its min-width rule value.
/* bootstrap rules */
#media (min-width: 768px) {
.container {
width: 750px; /* 75% of 750px = 562.5px */
}
}
#media (min-width: 992px) {
.container {
width: 970px; /* 75% of 970px = 727.5px */
}
}
If you want it to change while resizing the viewport, you could like this
Fiddle demo
#media (min-width: 768px) {
.col-sm-9 {
width: 75%;
}
.container {
width: 100%;
}
}
#media (min-width: 992px) {
.container {
width: 100%;
}
}
Or, if you want it to be fluid all the time, simply change the container class to container-fluid
Fiddle demo
I have my media queries at the bottom of my styles, I'm trying to make my grid be 50% of the screens width, however the 50% media query never seems to fire on a phone (iPhone 6s) however will on a browser re-size and I'm not sure why.
/* Media Queries*/
/* Max Width 1250px */
#media only screen and (min-width: 761px) and (max-width: 1000px) {
.boxes {
width: 33.3%;
}
}
/* Max Width 750px */
#media only screen and (min-width: 501px) and (max-width: 760px) {
.boxes {
width: 50%;
}
}
/* Max Width 500px */
#media only screen and (max-width: 500px) {
.boxes {
width: 100%;
}
}
Try adding this meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
Here's the MDN article on viewport meta tags
I am trying to make container fixed size 750px for all sized windows.
Here is HTML:
<div class="container">
<div class="thumbnail" >
..........<br/>
..........
</div>
</div>
and custom CSS:
#media (min-width: 1200px) {
.container {
width: 750px;
}
#media (min-width: 992px) {
.container {
width: 750px;
}
#media (min-width: 768px) {
.container {
width: 750px;
}
But the problem is when I am resizing window from big to small at some point size of thumbnail is getting a little larger and then reverses to its initial size.
Here is a fiddle http://jsfiddle.net/Wy22s/718/ . You can just resize browser window or slide inner window in fiddle itself to left and then to right to reproduce this behavior.
I have tried to add another div with row class. Tried combinations with col-sm, col-md etc, but I can not manage to achieve desired behavior. How can I fix this so the container/thumbnail size stays the same?
you forgot to close the #media brackets.
#media (min-width: 1200px) {
.container {
width: 750px !important;
}
}
#media (min-width: 992px) {
.container {
width: 750px !important;
}
}
#media (min-width: 768px) {
.container {
width: 750px !important;
}
}
.container{ width: 750px !important;}
.thumbnail{ width: 750px !important;}
I am using media queries as below
#media (min-width:100px) and (max-width:639px)
{
}
#media (min-width:640px) and (max-width:960px)
{
.box {background-color:red;}
}
#media (width:768px)
{
.box {background-color:green; }
}
#media (min-width:961px)
{
}
I want to specifically target some div element for screen 768 pixel so that it appears exactly as i want for example in general i want to overwrite css defined in #media (min-width:640px) and (max-width:960px) by css which is targeted for screen 768 #media (min-width:768px)
At present it is still showing me box as red while it should be red, I am not sure how css is complied i defined it after the second media query so that it will over right it.
How can i target certain element using media queries for specific devices
example :http://jsfiddle.net/X43Et/
Update:
I am not sure what exactly was wrong with it put i copy pasted #media (width:768px) { part from fiddle & it works in my actual page.
May be some invisible typo mistake..
This is just an example of media queries You would want to have your normal css before the media queries
#gallery-1 img {
width:375px;
}
#media screen and (min-width: 1366px) {
#gallery-1 img {width:375px;}
}
#media screen and (min-width: 1440px) {
#gallery-1 img {width:428px;}
}
#media screen and (min-width: 1600px) {
#gallery-1 img {width:434px;}
}
#media screen and (min-width: 1920px) {
#gallery-1 img {width:540px;}
}
And when you're using media queries, you want to specify that you want the screen size so you use screen after #media. I hope this is what you were looking for and will help you!
Here is a small example script I made
<style>
#box {
width: 500px;
height: 200px;
background: yellow;
border: 1px solid #000;
}
#media screen and (max-width:1000px) {
#box { background: red; }
}
#media screen and (min-width:1000px) and (max-width:1200px) {
#box { background: green; }
}
#media screen and (min-width:1200px) and (max-width:1400px) {
#box { background: blue; }
}
</style>
<div id="box">
</div>
On JSFiddle the screen size isn't the whole screen, it's the small box the preview is in so you would need to make the sizes smaller to see the effect, here is a DEMO resize your screen browser to see the preview.