Just tried my hand at using media queries to make my site responsive.
I have a div that, on a normal size desktop browser is 50% of the total width of the viewport. When using mobile iOS, I want that to change to 100% of the width. That works currently with the following HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta name="viewport" content="initial-scale=1.0" />
<link href='style.css' rel='stylesheet' type='text/css'>
<link href="smalldevice.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="social">Content</div>
</body>
</html>
The regarding CSS for the normal page is:
#social {
position:relative;
margin-top:50px;
left:0;
right:0;
margin-left:auto;
margin-right:auto;
text-align:center;
width: 100%;
max-width:640px;
}
And the smalldevice.css is:
#media screen and (min-width: 640px) {
#social {
width:50%;
}
When resizing my browser window that works when I reach a viewport width of 640px, and it changes the width of #social to 100%. Testing on iOS mobile works too. But when changing to landscape mode (viewport width becomes 1136px, thus using the media query, it does not change the width of the #social element to 50%. On a desktop with a resizable window it does work. What's going wrong?
Edit: I set up a demo that seems to work when resizing the browser (I added a background-color so the div-width is visible) here: http://codepen.io/anon/pen/AtysL
#media screen and (min-width: 640px), (orientation: landscape)
Should work just fine.
Related
I have an image that is a link also at the top of my website, like a banner, and when I go on my mobile to look at it, it stays the same size. How do I make it smaller for mobile devices?
Assuming the rest of your website is responsive you can just add a width of 100% to the image.
So for example, if your image had a class of 'my-img'
.my-img {
width:100%;
max-width:600px;
height:auto;
}
Or if you wanted to do it inline
<img src="" class="my-img" style="width:100%;max-width:600px;height:auto" />
probably it allready is responsive, even if you didnĀ“t notice. you must have something like this in your code:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Add media queries on your code like:
#media screen and (max-width: 600px) {
#logo{
width: 100%;
height: auto;
//in case you want a max-width-> max-width: 200px;
}
}
1. Use Bootstrap js and Bootstrap CSS in that use media queries according to
screen
2. width do the coding or use class xs sm md lg.
# Mobile
only screen and (min-width: 480px)
# Tablet
only screen and (min-width: 768px)
# Desktop
only screen and (min-width: 992px)
# Huge
only screen and (min-width: 1280px)
Please make sure that you have the following tag in your web-page
<meta name="viewport" content="width=device-width, initial-scale=1.0">
and then add image like following
<img src="your image path" alt="" class="heroImage">
<style>
.heroImage{width: 100%;max-width: 100%;}
</style>
I use Meta tag inside head
<meta id="viewport" name="viewport" content="width=device-width"/>
Responsive css
In firefox even when the viewport width is 768px it is going to 767px media query can you please help me with it
#media only screen and (max-width: 767px)
{
header{position: fixed;top: 0px;width: 100%;background: #fff;z-index: 9999;}
}
Check this Example Fiddle
It works perfectly in chrome but not in firefox
I'm trying to accomodate a really really old website to mobile standards and for some reason it doesn't get a width of 320 or whatever pixel width when i activate the responsive view in chrome dev tools. The result of this makes the pixels small while still maintaining the original 900 px width (in the original pc version that's the fixed size according to which the site was built on)
This would be my media query CSS:
#media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
body * {
width: auto;
}
#container,
#header,
#nav,
#container #wrapper,
#content,
#sidebar,
#wrapper-bottom,
#footer,
#footer-inner,
#footer-content
{
width: auto;
height: auto;
float: none;
}
/* Header
-------------------------------------------------------------------*/
#header h1{
display: block;
text-align: center;
}
#header #search{
position: static;
display: block;
}
/* Navigation
-------------------------------------------------------------------*/
#nav ul{
position: static;
}
}
If you need more are viewing into the mobile device and media query is not working then please check the meta tag in head
<meta name="viewport" content="width=device-width, initial-scale=1">
You must use meta viewport along with your CSS. Something like this by instance :
<meta name="viewport" content="width=device-width, initial-scale=1">
If you're viewing on your PC remove the word device from min-device-width and max-device-width.
In addition, make sure to use the meta viewport tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
It's likely that some element on the page has a fixed width that doesn't allow for the page to resize as desired. You need to go through the elements and find it. I've ran across this issue on several sites.
Problem that bugs me at the moment. Have not found answer so far.
I've got a site with minimum width of 480px applied for devices with screen smaller than 640px;
<meta name="viewport" content=" initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no" />
<style>
body {margin:0; padding:0;}
#media screen and (max-width: 640px) {
.div {width:100%; min-width:480px; background:#ff0000; color:#ffffff;}
}
</style>
Thing is, when you open a file on mobile, it does not fit the screen in portrait mode. You need to double click to fit it.
Is there anything could be done so that opens fit to screen on portrait mode?
Thanks guys.
You have maximum scale and initial scale in your viewport. Change your viewport to : <meta name="viewport" content="width=device-width, initial-scale=1.0">
Ahhh got ya. Right, below are two links... the 2nd one works keeping the color red - I think that's what your after (code for 2nd link example below, you will need both #medias)! Works on my phone now anyway!
http://www.bootply.com/render/115758
http://www.bootply.com/render/115760
#media (min-width:480px) and (max-width: 640px){
.div {
width: 100%;
background: #ff0000;
color: #ffffff;
}
}
#media (max-width: 640px){
.div {
background: #ff0000;
color: #ffffff;
}
}
Hello guys i have made a website by the help of Dreamweaver in HTML, CSS and JavaScript now my screen is 15.4 inch whenever i m opening in small screens so it give me an scroll bar to see whole of my website in the width form.
i have used PX with divs that i created my whole website instead of percentage but i don't know why i m facing this problem.
.wrapper
{
width:1320px;
height:760px;
border:solid 1px #000;
margin:0 auto;
background:url(images/Background_Image.jpg) no-repeat;
}
.wrapper .logo
{
background:url(images/logo.png) no-repeat;
width:395px;
height:180px;
margin-left:323px;
margin-top:10px;
float:left;
// same method for other divs
}
Add this meta tag to your head
viewport - most important and most useful of all the meta tags. width controls the width of viewport of device, initial-scale loads the initial zoom level when page loads
<meta name="viewport" content="width=device-width, initial-scale=1">
Check Mozilla Docs - Viewport
make use of media queries
#media screen and (min-width: 700px) and (orientation: landscape) { ... }
Mozilla Docs - Media queries
Percentage/em is good if you want your site to be responsive