I have just started to play around with Twitter Bootstrap API for a project I have coming up. The main nav contains 3 main elements:
Building on @Raphael_ and @user568109 's answers, in Bootstrap 3, Responsive is now built in.
To detect device type in Javascript, create an object that is only displayed on your required device using Bootstrap's Responsive classes. Then check its :hidden
property.
Example:
Create a <div>
panel with no content that would be shown on anything bigger that an eXtra Small device (thanks to @Mario Awad) :
<div id="desktopTest" class="hidden-xs"></div>
or, to exclude specific devices:
<div id="desktopTest" class="visible-sm visible-md visible-lg"></div>
Check value of #desktopTest
:
if ($('#desktopTest').is(':hidden')) {
// device is == eXtra Small
} else {
// device is >= SMaller
}
If you want to know what environment you're on, try using Bootstrap's own CSS classes. Create an element, add it to the page, apply its helper classes and check if it's hidden to determine if that's the current environment. The following function does just that:
function findBootstrapEnvironment() {
let envs = ['xs', 'sm', 'md', 'lg', 'xl'];
let el = document.createElement('div');
document.body.appendChild(el);
let curEnv = envs.shift();
for (let env of envs.reverse()) {
el.classList.add(`d-${env}-none`);
if (window.getComputedStyle(el).display === 'none') {
curEnv = env;
break;
}
}
document.body.removeChild(el);
return curEnv;
}
function findBootstrapEnvironment() {
var envs = ['xs', 'sm', 'md', 'lg'];
var $el = $('<div>');
$el.appendTo($('body'));
for (var i = envs.length - 1; i >= 0; i--) {
var env = envs[i];
$el.addClass('hidden-'+env);
if ($el.is(':hidden')) {
$el.remove();
return env;
}
}
}
function findBootstrapEnvironment() {
var envs = ['phone', 'tablet', 'desktop'];
var $el = $('<div>');
$el.appendTo($('body'));
for (var i = envs.length - 1; i >= 0; i--) {
var env = envs[i];
$el.addClass('hidden-'+env);
if ($el.is(':hidden')) {
$el.remove();
return env;
}
}
}
Combining the answers from above, this one works for me:
function findBootstrapDeviceSize() {
var dsize = ['lg', 'md', 'sm', 'xs'];
for (var i = dsize.length - 1; i >= 0; i--) {
// Need to add for Chrome. Works fine in Firefox/Safari/Opera without it.
// Chrome seem to have an issue with empty div's
$el = $('<div id="sizeTest" class="hidden-'+dsize[i]+'"> </div>');
$el.appendTo($('body'));
if ($el.is(':hidden')) {
$el.remove();
return dsize[i];
}
}
return 'unknown';
}
I originally posted answer here, the solution for Bootstrap v.4.x.
The Bootstrap v.4.0.0 (and the latest version Bootstrap 4.1.x) introduced the updated grid options, so the old concept on detection may not directly be applied (see the migration instructions):
sm
grid tier below 768px
for more granular control. We now have xs
, sm
, md
, lg
, and xl
;xs
grid classes have been modified to not require the infix.I written the small utility function that respects an updated grid class names and a new grid tier:
/**
* Detect the current active responsive breakpoint in Bootstrap
* @returns {string}
* @author farside {@link https://stackoverflow.com/users/4354249/farside}
*/
function getResponsiveBreakpoint() {
var envs = {xs:"d-none", sm:"d-sm-none", md:"d-md-none", lg:"d-lg-none", xl:"d-xl-none"};
var env = "";
var $el = $("<div>");
$el.appendTo($("body"));
for (var i = Object.keys(envs).length - 1; i >= 0; i--) {
env = Object.keys(envs)[i];
$el.addClass(envs[env]);
if ($el.is(":hidden")) {
break; // env detected
}
}
$el.remove();
return env;
};
The latest Bootstrap v4-alpha and Bootstrap v4-beta had different approach on grid breakpoints, so here's the legacy way of achieving the same:
/**
* Detect and return the current active responsive breakpoint in Bootstrap
* @returns {string}
* @author farside {@link https://stackoverflow.com/users/4354249/farside}
*/
function getResponsiveBreakpoint() {
var envs = ["xs", "sm", "md", "lg"];
var env = "";
var $el = $("<div>");
$el.appendTo($("body"));
for (var i = envs.length - 1; i >= 0; i--) {
env = envs[i];
$el.addClass("d-" + env + "-none");;
if ($el.is(":hidden")) {
break; // env detected
}
}
$el.remove();
return env;
}
I think it would be useful, as it's easy to integrate to any project. It uses native responsive display classes of the Bootstrap itself.
Original answer:
Based on @Alastair McCormack answer, I suggest you to use this code
<div class="visible-xs hidden-sm hidden-md hidden-lg">xs</div>
<div class="hidden-xs visible-sm hidden-md hidden-lg">sm</div>
<div class="hidden-xs hidden-sm visible-md hidden-lg">md</div>
<div class="hidden-xs hidden-sm hidden-md visible-lg">lg</div>
Just add it in the end of your container div, you will get a simple dynamic information about current view.
Update (2019-03-03):
Previous code is not compatible with Bootstrap 4, since all hidden-*
and visible-*
classes have been removed. Here you have the new code you can use, compatible with both Bootstrap 3 and Bootstrap 4 versions (some credits goes to this SO answer):
<div class="visible-xs hidden-sm hidden-md hidden-lg hidden-xl d-block d-sm-none">xs</div>
<div class="hidden-xs visible-sm hidden-md hidden-lg hidden-xl d-none d-sm-block d-md-none">sm</div>
<div class="hidden-xs hidden-sm visible-md hidden-lg hidden-xl d-none d-md-block d-lg-none">md</div>
<div class="hidden-xs hidden-sm hidden-md visible-lg hidden-xl d-none d-lg-block d-xl-none">lg</div>
<div class="hidden-xs hidden-sm hidden-md hidden-lg visible-xl d-none d-xl-block">xl</div>
You can test it with this fiddle.
Please note that I included hidden-xl
and visible-xl
too, but I believe they are not really used by any Bootstrap version.
Niche case, but you can apply @Kar.ma's to a Mediawiki with Chameleon (bootstrap skin) installed. Pass the "results" of the DIV as a template argument, then test against that within the template.