390

I'm using Bootstrap 3 to build a responsive layout where I want to adjust a few font sizes according to the screen size. How can I use media queries to make this kind of logic?

1

17 Answers 17

674

Bootstrap 3

Here are the selectors used in BS3, if you want to stay consistent:

@media(max-width:767px){}
@media(min-width:768px){}
@media(min-width:992px){}
@media(min-width:1200px){}

Note: FYI, this may be useful for debugging:

<span class="visible-xs">SIZE XS</span>
<span class="visible-sm">SIZE SM</span>
<span class="visible-md">SIZE MD</span>
<span class="visible-lg">SIZE LG</span>

Bootstrap 4

Here are the selectors used in BS4. There is no "lowest" setting in BS4 because "extra small" is the default. I.e. you would first code the XS size and then have these media overrides afterwards.

@media(min-width:576px){}
@media(min-width:768px){}
@media(min-width:992px){}
@media(min-width:1200px){}

Bootstrap 5

@media(min-width:576px){}
@media(min-width:768px){}
@media(min-width:992px){}
@media(min-width:1200px){}
@media(min-width:1400px){}

Update 2021-05-20: Info is still accurate as of versions 3.4.1, 4.6.0, 5.3.0.

8
  • 117
    Just FYI, this may be useful for debugging: <span class="visible-xs">SIZE XS</span><span class="visible-sm">SIZE SM</span><span class="visible-md">SIZE MD</span><span class="visible-lg">SIZE LG</span> Commented Sep 19, 2013 at 15:41
  • 15
    +1 for ordering queries by increasing screen sizes, staying consistent with BS3's mobile-first methodology. Commented Jan 21, 2014 at 4:10
  • 5
    What about 480px (@screen-xs)? Did that appear later? Got it from here.
    – brejoc
    Commented Apr 16, 2014 at 13:45
  • 1
    To be consistent with BS3 use the default screen-size variables per each viewport. See: stackoverflow.com/a/24921600/2817112
    – Oriol
    Commented Jul 23, 2014 at 21:49
  • 1
    @brejoc BS3+ is "mobile-first" - the "XS" (mobile) view is the default view in BS3+ ...
    – jave.web
    Commented Apr 18, 2016 at 20:01
264

Based on bisio's answer and the Bootstrap 3 code, I was able to come up with a more accurate answer for anyone just looking to copy and paste the complete media query set into their stylesheet:

/* Large desktops and laptops */
@media (min-width: 1200px) {

}

/* Landscape tablets and medium desktops */
@media (min-width: 992px) and (max-width: 1199px) {

}

/* Portrait tablets and small desktops */
@media (min-width: 768px) and (max-width: 991px) {

}

/* Landscape phones and portrait tablets */
@media (max-width: 767px) {

}

/* Portrait phones and smaller */
@media (max-width: 480px) {

}
4
  • Here high voted answer uses only min-width , but you have used max-width also , so what is the difference ?, is it necessary ?
    – Shaiju T
    Commented Oct 23, 2016 at 11:38
  • How does bootstrap know if the phone is in portrait mode? Commented Jan 9, 2017 at 19:15
  • @SuperUberDuper it doesn't. It just deals with elements according to the viewport's horizontal size rather than screen layout. Commented Jan 9, 2017 at 20:17
  • Shouldnt the order be from LOW-to-HIGH? Commented May 27, 2017 at 17:48
134

If you're using LESS or SCSS/SASS and you're using a LESS/SCSS version of Bootstrap, you can use variables as well, provided you have access to them. A LESS translation of @full-decent's answer would be as follows:

@media(max-width: @screen-xs-max){}
@media(min-width: @screen-sm-min){}  /* deprecated: @screen-tablet, or @screen-sm */
@media(min-width: @screen-md-min){}  /* deprecated: @screen-desktop, or @screen-md */
@media(min-width: @screen-lg-min){}  /* deprecated: @screen-lg-desktop, or @screen-lg */

There are also variables for @screen-sm-max and @screen-md-max, which are 1 pixel less than @screen-md-min and @screen-lg-min, respectively, typically for use with @media(max-width).

EDIT: If you're using SCSS/SASS, variables start with a $ instead of a @, so it'd be $screen-xs-max etc.

7
  • 1
    When i want to edit data on .grid.less always is overwrite from main style. Is there any solution or i just need all the style that i have added on the media queries to add !important ? Commented Feb 5, 2014 at 21:10
  • 2
    I'm not able to get this to work. The compiler complains if I use anything besides a hard-coded number.
    – laertiades
    Commented Feb 13, 2014 at 15:35
  • 7
    @JesseGoodfellow the example above uses LESS syntax; if you're using SASS/SCSS github.com/twbs/bootstrap-sass/blob/master/vendor/assets/…, you'd want $screen-xs-max etc. (And if you're using LESS/SCSS locally but importing the CSS version of Bootstrap, you're out of luck altogether.)
    – carpeliam
    Commented Feb 17, 2014 at 20:25
  • 2
    @screen-tablet, @screen-desktop, and @screen-lg-desktop have been deprecated. Might be time to update your already-awesome answer. ;-) Commented Nov 3, 2014 at 11:49
  • 2
    Assuming you building bootstrap; this should be marked as the answer Commented Feb 16, 2015 at 11:51
45

These are the values from Bootstrap3:

/* Extra Small */
@media(max-width:767px){}

/* Small */
@media(min-width:768px) and (max-width:991px){}

/* Medium */
@media(min-width:992px) and (max-width:1199px){}

/* Large */
@media(min-width:1200px){}
2
  • 4
    This is the correct answer. Must have "and" conditions Commented Dec 11, 2014 at 17:20
  • To allow Large screens to use the css from Medium screen css, remove the and conditions. @JasonMiller so it's not exactly a "must", it depends on specs and requirement of template.
    – Onimusha
    Commented Aug 27, 2015 at 23:58
29

Here are two examples.

Once the viewport becomes 700px wide or less make all h1 tags 20px.

@media screen and ( max-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}

Make all the h1's 20px until the viewport reaches 700px or larger.

@media screen and ( min-width: 700px ) {
  h1 {
     font-size: 20px;
  }
}

Hope this helps :0)

1
  • You use font-size: 20px for h1 tags in both cases. For better understanding you may have use different font-size as asked in question. BTW It's still okay.
    – fWd82
    Commented Dec 22, 2017 at 17:57
23

Bootstrap 3

For the final version release of Bootstrap 3 (v3.4.1) the following media queries were used which corresponds with the documentation that outlines the responsive classes that are available. https://getbootstrap.com/docs/3.4/css/#responsive-utilities

/* Extra Small Devices, .visible-xs-* */
@media (max-width: 767px) {}

/* Small Devices, .visible-sm-* */
@media (min-width: 768px) and (max-width: 991px) {}

/* Medium Devices, .visible-md-* */
@media (min-width: 992px) and (max-width: 1199px) {}

/* Large Devices, .visible-lg-* */
@media (min-width: 1200px) {}

Media queries extracted from the Bootstrap GitHub repository from the following less files:-

https://github.com/twbs/bootstrap/blob/v3.4.1/less/variables.less#L283 https://github.com/twbs/bootstrap/blob/v3.4.1/less/responsive-utilities.less

Bootstrap 5

From the documentation for version 5 you can see the media query breakpoints have been updated since version 3 to better fit modern device dimensions.

// X-Small devices (portrait phones, less than 576px)
// No media query for `xs` since this is the default in Bootstrap

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) { ... }

// X-Large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

// XX-Large devices (larger desktops, 1400px and up)
@media (min-width: 1400px) { ... }

Source: Bootstrap 5 Documentation


You can see the breakpoints for v5.1.3 in the Bootstrap GitHub repository:-

https://github.com/twbs/bootstrap/blob/v5.1.3/scss/_variables.scss#L461 https://github.com/twbs/bootstrap/blob/v5.1.3/scss/mixins/_breakpoints.scss

Updated on 2021-12-19

21

Here is a more modular example using LESS to mimic Bootstrap without importing the less files.

@screen-xs-max: 767px;
@screen-sm-min: 768px;
@screen-sm-max: 991px;
@screen-md-min: 992px;
@screen-md-max: 1199px;
@screen-lg-min: 1200px;

//xs only
@media(max-width: @screen-xs-max) {

}
//small and up
@media(min-width: @screen-sm-min) {

}
//sm only
@media(min-width: @screen-sm-min) and (max-width: @screen-sm-max) {

}
//md and up
@media(min-width: @screen-md-min) {

}
//md only
@media(min-width: @screen-md-min) and (max-width: @screen-md-max) {

}
//lg and up
@media(min-width: @screen-lg-min) {

}
1
  • Tried this. Works perfectly! Commented Mar 2, 2017 at 20:29
20

Based on the other users' answers, I wrote these custom mixins for easier usage:

Less input

.when-xs(@rules) { @media (max-width: @screen-xs-max) { @rules(); } }
.when-sm(@rules) { @media (min-width: @screen-sm-min) { @rules(); } }
.when-md(@rules) { @media (min-width: @screen-md-min) { @rules(); } }
.when-lg(@rules) { @media (min-width: @screen-lg-min) { @rules(); } }

Example usage

body {
  .when-lg({
    background-color: red;
  });
}

SCSS input

@mixin when-xs() { @media (max-width: $screen-xs-max) { @content; } }
@mixin when-sm() { @media (min-width: $screen-sm-min) { @content; } }
@mixin when-md() { @media (min-width: $screen-md-min) { @content; } }
@mixin when-lg() { @media (min-width: $screen-lg-min) { @content; } }

Example usage:

body {
  @include when-md {
    background-color: red;
  }
}

Output

@media (min-width:1200px) {
  body {
    background-color: red;
  }
}
12

Or simple Sass-Compass:

@mixin col-xs() {
    @media (max-width: 767px) {
        @content;
    }
}
@mixin col-sm() {
    @media (min-width: 768px) and (max-width: 991px) {
        @content;
    }
}
@mixin col-md() {
    @media (min-width: 992px) and (max-width: 1199px) {
        @content;
    }
}
@mixin col-lg() {
    @media (min-width: 1200px) {
        @content;
    }
}

Example:

#content-box {
    @include border-radius(18px);
    @include adjust-font-size-to(18pt);
    padding:20px;
    border:1px solid red;
    @include col-xs() {
        width: 200px;
        float: none;
    }
}
1
  • Thank you. I was looking for something that gives examples of the bootstrap sass mixin (bootstrap's own example is complicated with all the content). This explains a lot! :)
    – CleverNode
    Commented Oct 22, 2015 at 14:26
11

keep in mind that avoiding text scaling is the main reason responsive layouts exist. the entire logic behind responsive sites is to create functional layouts that effectively display your content so its easily readable and usable on multiple screen sizes.

Although It is necessary to scale text in some cases, be careful not to miniaturise your site and miss the point.

heres an example anyway.

@media(min-width:1200px){

    h1 {font-size:34px}

}
@media(min-width:992px){

    h1 {font-size:32px}

}
@media(min-width:768px){

    h1 {font-size:28px}

}
@media(max-width:767px){

    h1 {font-size:26px}

}

Also keep in mind the 480 viewport has been dropped in bootstrap 3.

6

We use the following media queries in our Less files to create the key breakpoints in our grid system.

/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) { ... }

/* Medium devices (desktops, 992px and up) */
@media (min-width: @screen-md-min) { ... }

/* Large devices (large desktops, 1200px and up) */
@media (min-width: @screen-lg-min) { ... }

see also on Bootstrap

5

you can see in my example font sizes and background colors are changing according to the screen size

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
    background-color: lightgreen;
}
/* Custom, iPhone Retina */ 
@media(max-width:320px){
    body {
        background-color: lime;
        font-size:14px;
     }
}
@media only screen and (min-width : 320px) {
     body {
        background-color: red;
        font-size:18px;
    }
}
/* Extra Small Devices, Phones */ 
@media only screen and (min-width : 480px) {
     body {
        background-color: aqua;
        font-size:24px;
    }
}
/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {
     body {
        background-color: green;
        font-size:30px;
    }
}
/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {
     body {
        background-color: grey;
        font-size:34px;
    }
}
/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {
     body {
        background-color: black;
        font-size:42px;
    }
}
</style>
</head>
<body>
<p>Resize the browser window. When the width of this document is larger than the height, the background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>

2

Here is an even easier one stop solution, including separate responsive files based on media queries.

This allows all the media query logic and include logic to only have to exist on one page, the loader. It also allows to not have the media queries clutter up the responsive stylesheets themselves.

//loader.less

// this twbs include adds all bs functionality, including added libraries such as elements.less, and our custom variables
@import '/app/Resources/less/bootstrap.less';

/*
* Our base custom twbs overrides
* (phones, xs, i.e. less than 768px, and up)
* no media query needed for this one since this is the default in Bootstrap
* All styles initially go here.  When you need a larger screen override, move those     
* overrides to one of the responsive files below
*/
@import 'base.less';

/*
* Our responsive overrides based on our breakpoint vars
*/
@import url("sm-min.less") (min-width: @screen-sm-min); //(tablets, 768px and up)
@import url("md-min.less") (min-width: @screen-md-min); //(desktops, 992px and up)
@import url("large-min.less") (min-width: @screen-lg-min); //(large desktops, 1200px and up)

base.less would look like this

/**
* base.less
* bootstrap overrides
* Extra small devices, phones, less than 768px, and up
* No media query since this is the default in Bootstrap
* all master site styles go here
* place any responsive overrides in the perspective responsive sheets themselves
*/
body{
  background-color: @fadedblue;
}

sm-min.less would look like this

/**
* sm-min.less
* min-width: @screen-sm-min
* Small devices (tablets, 768px and up)
*/
body{
  background-color: @fadedgreen;
}

your index would just have to load the loader.less

<link rel="stylesheet/less" type="text/css" href="loader.less" />

easy peasy..

2
  • Hi, i am trying to use less from 2days in bootstrap but still trying. will you please help me regarding less using on windows. i read lots of tuts and articles but not found proper solutions i think you can help me. Thanks in advance Commented May 13, 2015 at 7:25
  • you need to make sure you include the less javascript library, then your application will know what to do with those files. how you include less in your application is up to you. i use a library found at github, via assetic. you dont have to do that. you can just include the javascript file, and then load your less file via a standard css style meta tag. you dont worry about the compiling. that info is here. lesscss.org/#client-side-usage.
    – blamb
    Commented May 13, 2015 at 18:52
1

@media only screen and (max-width : 1200px) {}

@media only screen and (max-width : 979px) {}

@media only screen and (max-width : 767px) {}

@media only screen and (max-width : 480px) {}

@media only screen and (max-width : 320px) {}

@media (min-width: 768px) and (max-width: 991px) {}

@media (min-width: 992px) and (max-width: 1024px) {}

0

Use media queries for IE;

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) 
and (orientation : landscape) and (-ms-high-contrast: none), (-ms-high-contrast: active) {
}
@media only screen 
and (min-device-width : 360px) 
and (max-device-width : 640px) 
and (orientation : portrait) and (-ms-high-contrast: none), (-ms-high-contrast: active) {
}
0

:)

In latest bootstrap (4.3.1), using SCSS(SASS) you can use one of @mixin from /bootstrap/scss/mixins/_breakpoints.scss

Media of at least the minimum breakpoint width. No query for the smallest breakpoint. Makes the @content apply to the given breakpoint and wider.

@mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints)

Media of at most the maximum breakpoint width. No query for the largest breakpoint. Makes the @content apply to the given breakpoint and narrower.

@mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints)

Media that spans multiple breakpoint widths. Makes the @content apply between the min and max breakpoints

@mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints)

Media between the breakpoint's minimum and maximum widths. No minimum for the smallest breakpoint, and no maximum for the largest one. Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.

@mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints)

For example:

.content__extra {
  height: 100%;

  img {
    margin-right: 0.5rem;
  }

  @include media-breakpoint-down(xs) {
    margin-bottom: 1rem;
  }
}

Documentation:

Happy coding ;)

-1

To improve the main response:

You can use the media attribute of the <link> tag (it support media queries) in order to download just the code the user needs.

<link href="style.css" rel="stylesheet">
<link href="deviceSizeDepending.css" rel="stylesheet" media="(min-width: 40em)">

With this, the browser will download all CSS resources, regardless of the media attribute. The difference is that if the media-query of the media attribute is evaluated to false then that .css file and his content will not be render-blocking.

Therefore, it is recommended to use the media attribute in the <link> tag since it guarantees a better user experience.

Here you can read a Google article about this issue https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css

Some tools that will help you to automate the separation of your css code in different files according to your media-querys

Webpack https://www.npmjs.com/package/media-query-plugin https://www.npmjs.com/package/media-query-splitting-plugin

PostCSS https://www.npmjs.com/package/postcss-extract-media-query

Not the answer you're looking for? Browse other questions tagged or ask your own question.