How to detect the browser width and move elements depending on size?
Similar to Mashable - http://mashable.com/2011/07/24/google-plus-jobs/
An old question, so answering on the off chance that someone else may stumble across it. I would recommend the usage of CSS media queries and looking into responsive web design where the design of the page is tailored to the device based on screen size.
More specifically, looking at the following CSS properties to load different CSS rules:
min-width:
max-width:
min-device-width:
max-device-width:
For example, you may target smart phones in the following manner:
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
Above example taken from http://www.databasepublish.com/blog/rendering-content-adaptively-responsive-web-design-and-css3-media-queries which has a few nice rules.
Related
I am using a wordpress theme in "responsive-design mode". Viewing website on iPad renders the way it should (including desktop, non-responsive), but viewing on iphone gets messy. Can someone help me figure out how to duplicate the iPad style to the iPhone and all non-desktop devices; and delete the smartphone styling completely. I am looking to duplicate the exact layout as the iPad for all mobile devices (just smaller versions). The author of the theme told me to look in the "responsive-all.css" file and fix things there, but I'm having trouble since I'm not well versed in css. Here is the link to the css. responsive-all.css
Any help is MUCH appreciated. Thanks
Michael
Step 1 : if you want to delete the mobile's view, just comment the end of the CSS file beginning with or delete and of course the following :
/*
* -------------------------------------------------------------------------------------------
* MOBILE BOTH VIEWS
* -------------------------------------------------------------------------------------------
*/ #media only screen and (max-width: 767px) {
...
Step 2 : Then change, the condition to get the same design for tablets/smartphones :
/*
* -------------------------------------------------------------------------------------------
* TABLET PORTRAIT
* -------------------------------------------------------------------------------------------
*/ #media only screen and (max-width: 979px) {
After, I don't know what you want on this mobile/tablet view exactly.
Replacing your current viewport meta tag with the following will probably make the iPhone (and most other mobile devices) look like the iPad:
<meta name="viewport" content="width=768">
I am building a responsive page at the moment but I have a problem.
For larger screen sizes I want to enlarge stuff like headings. This works, but testing the page on my iPhone in landscape mode, the headings are too big.
Why the iPhone in portrait triggers a media query for larger screens? Well, the CSS rule that enlarges the headings is enclosed in this media query:
#media only screen and (min-width: 30em) {
/* 480 pixel */
}
and the iPhone screen in landscape is 480px wide. So I tried to do it like this:
#media only screen and (min-width: 30em) not (orientation: landscape) {
/* 480 pixel* /
}
Now it works on my iPhone, but the headings aren't enlarged anymore on my MacBook. Probably it's just some kind of logical error, but where am I going wrong?
Try replacing not (orientation:landscape) with and (orientation:portrait). That failing, try changing the em values to px values. Some browsers don't play nice with em yet, so it's worth a shot.
EDIT: Why not just break it up into separate styles?
#media all and (min-width:480px) and (orientation:landscape) {
// styles for desktops, mouse-friendly interface
}
#media all and (min-width:480px) and (orientation:portrait) {
// styles for mobiles, touch-friendly interface
}
I've tried using several media queries I've found through Google to target the iPhone, but my site still isn't displaying like I want it to on the actual iPhone. It looks fine when I use iphone4simulator.com. Here's the media query I'm using now:
#media only screen and (device-width: 480px) and (orientation: portrait) and (resolution: 163dpi)
Here's the URL: cfbpreview
Thanks.
You have to add this for the iPhone 4 because of the retina display.
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
Here is a link to all media queries you might need for a responsive design.
IMPORTANT: This will apply to all devices with a pixel ratio of more then 1.5. If you want it to be more specific to the device you will have to add more conditions to your meida query.
I am working on a new website and have put a set width into my css file. I have a requirement to make the site adaptable and resizable to fit the smaller screens of phones and various tablet devices.
What in my css styles has to be set so that the site best adapts to the other screen sizes?
You will want to use media queries. This is the whole basis behind responsive design.
#media only screen and (max-device-width: 480px) {
/* write smartphone styles here */
}
#media only screen and (max-device-width: 768px) {
/* tablets in portrait mode */
}
#media only screen and (max-device-width: 1024px) {
/* tablets in landscape mode and smaller/older monitors */
}
Take a look here:
http://coding.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/
I haven't done any iPhone version yet, so I have this question,
In order to be able to browse the website properly -even if the user turns 90º his phone:
Shall i CSS set with's in px or in % ?
If you are programatically targeting the iOS devices such as the iPhone & iTouch then i would use pixels opposed to percentages, but if you are not targeting such devices and want a one stop mobile website for all (most smart-phones) then i would consider using percentages.
you can specificy min-width max-width and min-device-width and max-device-width in your media queries.
Here is a little more about media queries and the combinations that you can do;
/* Target iPhone Portrait */
#media screen and (max-width: 320px) and (orientation: portrait) { body{background:#F0F;} }
/* Target Android Portrait larger than 320px Width */
#media screen and (min-width: 321px) and (max-width: 480px) and (orientation: portrait) { body{background:#F00;} }
/* Target iPhone Landscape */
#media screen and (max-width: 480px) and (orientation: landscape) { body{background:#0F0;} }
/* Target Android Landscape */
#media screen and (min-width: 481px) and (max-width: 800px) and (orientation: landscape) { body{background:#FF0;} }
You can use % with one css class, and the size of components will be adjusted automatically.
You can also use px with two classes, one for portrait, one for landscape :
body[orient="portrait"] {
property: value;
}
body[orient="landscape"] {
property: value;
}
Personally I would use %, rather than px...
You will want the page to become 100% which ever way this is rotated, and then shift the content around accordingly.
The New York Times uses %, as you can see where the page is Landscape you are zoomed in further than Portrait.
If you are building a non specific website for iPhones (sub-domaine like iphone.mywebsite.com) I would suggest using CSS Media Queries as suggested by Xavier. It allows you to do much more than specify a min-width and max-width!
You can specify the type of device (but many devices aren't recognized like they should…) like: screen handheld print tv and many other
But most importantly you can also set if the browser window is in portait or landscape, it's resolution or aspect-ratio and so on…
As for % or px I'd definitely go for px, it's very difficult to have something working as you would wish using %. Because you don't necessarily want the same kind of information if your user comes with an iPhone or with an other device.
For exemple you could take out all the heavy images from your website for iPhone users because they'll probably be using a 3G connection and so making your site a lot faster to load!
A really nice example of what you can do with CSS Media Queries… unfortunately it0s not my work… :-(