Skip to content

Instantly share code, notes, and snippets.

@Wolfr
Created May 2, 2012 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wolfr/2574283 to your computer and use it in GitHub Desktop.
Save Wolfr/2574283 to your computer and use it in GitHub Desktop.
Context sensitive media queries
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Respond test</title>
<link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>
<body>
<div id="debugger">
current body width: <span></span>
</div>
<div id="container">
<div id="header">
<h1>Website name</h1>
<ul>
<li class="dd"><a href="#">Menu</a></li>
<ul class="clearfix">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</ul>
</div>
<div id="content">
<p>Lorem ipsum dolor ssit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('.dd').click(function(event) {
$(this).next().show();
});
$(window).resize(function(event) {
var bodyWidth = $('body').width();
$('#debugger span').html(bodyWidth);
});
});
</script>
</body>
</html>
/*
* Context sensitive media queries
*/
@import "compass";
@mixin respond-to($context) {
// Context: header is 500px wide, container padding is 20px, so 540px wide
// The header does not fit
@if $context == headerDoesntFit {
@media only screen and (max-width: 539px) { @content; }
}
// Context: header does fit
@else if $context == headerFits {
@media only screen and (min-width: 540px) { @content; }
}
}
html, body {
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
line-height: 1.5;
color: #333;
}
h1 {
font-size: 24px;
margin: 0;
}
#debugger {
font-size: 10px;
position: fixed;
bottom: 10px;
right: 10px;
font-family: monospace;
}
.clearfix {
@include pie-clearfix;
}
#container {
padding: 20px;
width: 500px;
margin: 0 auto;
}
.dd {
background: blue;
}
@include respond-to(headerDoesntFit) {
#container {
padding: 0;
}
#content {
padding: 0 12px;
}
}
#header {
ul {
@include reset-box-model;
@include reset-list-style;
li {
@include reset-box-model;
}
}
margin: 0 auto;
a {
display: block;
padding: 12px;
background: maroon;
color: #FFF;
text-decoration: none;
border-bottom: 1px solid #000;
font-size: 14px;
}
@include respond-to(headerDoesntFit) {
ul ul {
display: none;
}
}
@include respond-to(headerFits) {
li.dd {
display: none;
}
li {
float: left;
list-style: none;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment