I am about to create my first 'mobile-first' website and am unsure on the best way to conditionally load content as the viewport increases.
For example, lets say I wish to load a twitter feed only for desktop browsers, not mobile, how would I do this?
Option 1) Display: none - This is bad as content still loads for mobile
Option 2) Have content in markup, but remove.element with javascript - I believe this content still loads first, then is removed after? If so, not good.
Option 3) Using javascript, if viewport is wide enough, load content - This seems to be the recommended approach, from what i've read, but Is it a good idea to have markup in javascript? I am thinking about accessibility, semantics and seo.
Are there any other better solutions?
Any advice would be greatly appreciated.
You might want to check out Modernizr . It's easy to install and you can use it check for HTML5 support in a visitors browser, as well as the window width, for example:
if (Modernizr.mq('(min-width: 400px)')) {
/* do this for tablets and desktops */
}else{
/* do this for handhelds */
}
Good luck!
UPDATE
thanks, but then in that case would it not be better to do that straight with javascript - if (document.documentElement.clientWidth > 640)
You are right, for that specific instance of deciding whether or not to load a twitter feed, there probably isn't a big advantage of using my suggestion over your option 3. Thinking ahead though, with repsonsive design, mobile sites and now HTML5, the next questions you are going to come up against are how to customise the CSS for different viewpoints, or how to test if a visitor's browser supports a certain HTML5 feature.
You could certainly take a roll-your-own approach and write custom javascript for each case, or you could use Modernizr to test if the visitors browswer supports media queries, and if it doesn't load respond.js, or use Modernizr to test if the vistor's browser supports geolocation or html5 forms or certain video formats ..., and if it doesn't conditionally load a cross browser polyfill.
There are usually multiple ways of achieving the same goal, I'm strong on not reinventing the wheel ;)
Who says? Display:none; is one the best practices in showing and hiding elements for any specific viewport.
And one thing Adam, mark-up is the least downloadable stuff for each device, they have to process some other things if you hide element by Jquery or Javascript. Because sometimes small devices either don't support loading them or take extra time that's why they are visible.
But it has the pitfall for SEO purposes.
For SEO optimization along with hiding elements you can do one of these things with your CSS.
First,
#media screen and (max-width:480px) {
div {
position: absolute;
left: -9999em;
width: 0;
height: 0;
overflow: hidden;
}
}
But it was the less efective because Google Webmaster takes care all such hacks. And You may be blacklisted from being Google indexed.So You should use one of these modern methods to hide elements.
Second,
div {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
padding: 0 !important;
border: 0 !important;
height: 1px !important;
width: 1px !important;
overflow: hidden;
}
or, third,
div {
color:transparent;
text-indent:100%;
overflow:hidden;
white-space:no-wrap;
font:0/0 a;
text-shadow:none;
}
Related
I was trying different solutions to use #media in css (or to be more precise, sass) and was thinking about the way we commonly use #media. The most common way, at least that I know and I see on blog posts, tutorials, etc.. Is to define some standard values and then we use them. For example:
$small: 420px;
$medium: 768px;
$large: 992px;
$extra-large: 1200px;
#media screen and (min-width: $medium) { ... }
#media screen and (min-width: $large) { ... }
The problem I'm facing with this approach is that my menu doesn't fit well any of the predefined values. So when it break the media size, it gets a huge blank space, even thought there is still space to fit the menu. In this case I could break it at 780px but the values are 768 or 992.
So my question is.. Is it okay to approach media queries this way and break the media based on content by defining the values manually?
The menu would be 780px, let's say that a grid that I display the company members would break at 820px and 540px. Each of them I would need to analyse it visually on the browser, measure and set the value manually.
I would still use (if there's not a better way) the pre defined values for common elements on the page (if needed). There is this article that I like a lot (a little out of date but a good concept), it covers the screen size usage and how to group them. But it doesn't approach the issue I'm questioning here.
Also, other big companies, like Airbnb, Spotify, Github they use predefined values to break the #media content. All based on predefined values, not the content. Spotify is one example. The menu breaks too soon, even with enough space to keep the items before moving to aside off screen.
--
The only reason I can think of to keep using this method is because we, as end users, are so used to have a menu toggle on the top header that it doesn't matter if there is enough space to keep the inline text. Since we are used to the button to open the menu, we use this technique, hence the use of pre defined media values.
I have a Drupal site where the site on mobile isn't shrinking. It was working fine a few days ago and when I logged in today to do more work, it isn't anymore. I can't seem to figure out why.
I've tried several options for the viewport and none have made a difference. It may be that I've just looked at this code for too long and I'm just missing something.
It's just the front page: http://inv3nt.tgci.com/home
The inner pages are still under construction as far as the theme and such goes, so they're all messed up right now.
Eventually the site will get its own mobile theme, but for now I need this one to scale appropriately on mobile devices. Wish I could figure out what changed in the last few days to make it not work when it was previously.
Thanks for any help you can give. :)
It looks like you have no responsive theming, you have also set a min-width on the page wrapper meaning the page can not go smaller than 1200px
#wrapper {
min-width: 1200px;
}
#main-content {
width: 1200px;
}
You will need to add some media queries http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
eg.
#media screen and (max-width: 1200px) {
#wrapper {
min-width: 100%;
}
#main-content {
width: 100%;
}
}
You will also need to fix other issues such as the header and where you have 2 or four column elements.
You should look at creating a mobile first theme for your site to avoid having to create multiple themes.
You should also look at implementing a mobile menu, I recommend https://www.drupal.org/project/responsive_menu although there is a small cost for commercial use of one of the scripts
I have an Orbit slider in a Foundation 3 site, and I need to be able to show slide numbers, as in "slide 3 of 8".
I see no options for that in the docs, but looking at the _settings.scss file I see these items:
$orbitSlideNumBgColor: rgba(0,0,0,0.7);
$orbitSlideNumFontColor: #fff;
$orbitSlideNumPadding: 5px;
But there's no indication how to use them. Uncommenting them doesn't do anything. How to integrate with a given slider, or do I need to do my oen coding to create numbers?
Take a look at the jquery.foundation.orbit.js file ... Here all options are listed. You want to:
$("#slider").orbit({ slideNumber: true });
Agreed that should have been documented :)
I got galaxy nexus with resolution is 720x1280. But when I call $(window).width() it just return 360px. Are there anyway to calculate value of $(window).width() on other devices? For an example, galaxy s 480x800? I need those values for my media queries:
#media only screen and (min-width: 360px){
body{
font-size: 62.5%;
}
}
I don't know if you're going to like my answer.. but here goes.
Forget about width (fixed widths at least), and make your website device-agnostic. Don't base your breakpoints on popular device resolutions, instead base them on your content. Keep stretching that browser window until you get that gut feeling that says "that doesn't look right" - that's your next breakpoint.
Keep it as fluid as you can and rely on relative units - even on your media query declaration.
I suggest giving this article by Chris Armstrong a read: http://www.alistapart.com/articles/the-infinite-grid/ (although ALA appears to be temporarily offline)
what would it take to allow media queries through HTMLPurifier + CSStidy?
In other words, I am using these libraries:
require_once 'inc_php_classes/htmlpurifier-4_4_0/library/HTMLPurifier.auto.php';
require_once 'inc_php_classes/csstidy-1_3/class.csstidy.php';
...to sanitize admin-user input before I save it to database.. for populating an external style sheet ... and so it happens that any input media queries are getting munged.. e.g. this:
#media only screen and (min-width: 600px) and (max-width: 939px) {
becomes
#media only screen and min-width 600px and max-width 939px {
..which breaks the media query, at least in Chrome 18/Mac.
The form/input is used for creating custom style sheets for trusted admin users.. and they need media queries.. but even though they are trusted, I do not want to just drop use of CSStidy.. because of the off chance an admin goes AWOL, not to mention I want to clean their newbie CSS errors.
Not sure, it's definitely not supported by vanilla HTML Purifier. It is reasonably plausible that CSSTidy does parse media selectors properly, so it's just a matter of teaching HTML Purifire not to strip them out.