<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>CSS-Tricks</title>
	
	<link>http://css-tricks.com</link>
	<description>Tips, Tricks, and Techniques on using Cascading Style Sheets.</description>
	<lastBuildDate>Fri, 17 May 2013 18:16:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CssTricks" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="csstricks" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">CssTricks</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Slider with Sliding Backgrounds</title>
		<link>http://css-tricks.com/slider-with-sliding-backgrounds/</link>
		<comments>http://css-tricks.com/slider-with-sliding-backgrounds/#comments</comments>
		<pubDate>Fri, 17 May 2013 15:14:52 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21656</guid>
		<description><![CDATA[<p><p>Among the many super nice design features of the <a href="https://itunes.apple.com/us/app/yahoo!-weather/id628677149?mt=8">Yahoo! Weather app for iOS</a> is the transition between city screens. The background image doesn't just move away as the screen moves from one screen to the next, the background image itself slides. It appears to be hiding some of the "old" screen and revealing more of the "new" screen those closer you have it to being in full view. </p>
<p>Let's try and pull it off on the web.</p>
<p>&#8230;</p>
The HTML</p><p><p><small><a href="http://css-tricks.com/slider-with-sliding-backgrounds/">Slider with Sliding Backgrounds</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>Among the many super nice design features of the <a href="https://itunes.apple.com/us/app/yahoo!-weather/id628677149?mt=8">Yahoo! Weather app for iOS</a> is the transition between city screens. The background image doesn't just move away as the screen moves from one screen to the next, the background image itself slides. It appears to be hiding some of the "old" screen and revealing more of the "new" screen those closer you have it to being in full view. </p>
<p>Let's try and pull it off on the web.</p>
<p><span id="more-21656"></span></p>
<h3>The HTML</h3>
<p>Like any slider, there are three main components:</p>
<ul>
<li>The container that holds everything into shape</li>
<li>A sliding container that is as wide as all the slides in a row</li>
<li>Each individual side container</li>
</ul>
<p>We won't bother too much with content inside the slide. I'll just add the temperature to show each slide can indeed hold content on top.</p>
<pre rel="HTML"><code class="language-markup">&lt;div class="slider" id="slider"&gt;
  &lt;div class="holder"&gt;
    &lt;div class="slide" id="slide-0"&gt;&lt;span class="temp"&gt;74°&lt;/span&gt;&lt;/div&gt;
    &lt;div class="slide" id="slide-1"&gt;&lt;span class="temp"&gt;64°&lt;/span&gt;&lt;/div&gt;
    &lt;div class="slide" id="slide-2"&gt;&lt;span class="temp"&gt;82°&lt;/span&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>The container might be a <code>&lt;section&gt;</code>, slides might be <code>&lt;article&gt;</code>. It really depends. I'll let you make the semantic choices for your own needs.</p>
<p>The layout plan is like this:</p>
<figure id='post-21657 media-21657' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/layout.png' alt='' /></figure>
<h3>The CSS</h3>
<p>The "slider" (visual container) and the slides need to have explicity the same size. We'll use pixels here but you could make it work with anything.</p>
<pre rel="CSS"><code class="language-css">.slider {
  width: 300px;
  height: 500px;
  overflow-x: scroll;
}
.slide {
  float: left;
  width: 300px;
  height: 500px;
}</code></pre>
<p>Floating those slides to the left isn't going to make them line up in a row, because the parent element of the slides isn't wide enough to let them do that. That's one of the reasons we need the holder element. It will be 300% wide (Num. of slides × 100%) which will fit three slides exactly.</p>
<pre rel="CSS"><code class="language-css">.holder {
  width: 300%;
}</code></pre>
<p>Each one of our slides has a unique ID. This is useful because, if we choose, we can create anchor links that link to those ID's and the slider will "jump" to those slides. We'll add JavaScript to do some actual "sliding", but our slider will work even without that. ID's make that possible, so let's use them here to drop in some lovely background images.</p>
<pre rel="CSS"><code class="language-css">#slide-0 {
  background-image: url(http://farm8.staticflickr.com/7347/8731666710_34d07e709e_z.jpg);
}
#slide-1 {
  background-image: url(http://farm8.staticflickr.com/7384/8730654121_05bca33388_z.jpg);
}
#slide-2 {
  background-image: url(http://farm8.staticflickr.com/7382/8732044638_9337082fc6_z.jpg);
}</code></pre>
<p>With all this in place, our layout comes into shape:</p>
<figure id='post-21658 media-21658' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/css-layout.jpg' alt='' /></figure>
<p>The CSS (black fading)</p>
<p>Just as a small detail, the temperature set in white may be in danger of not being visible depending on the photo behind it. To ensure that it is, we can make the photo subtly fade to black toward the bottom. A pseudo element will do nicely.</p>
<pre rel="CSS"><code class="language-css">.slide:before {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 40%;
  background: linear-gradient(transparent, black);
}</code></pre>
<p>A picture explanation is in order here:</p>
<figure id='post-21663 media-21663' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/white-text.jpg' alt='' /></figure>
<h3>The JavaScript (background sliding)</h3>
<p>We're going to use jQuery here because we love life. Our goal is the adjust the background-position of the slides as we scroll. We can set background-position in percentages in CSS, but that alone doesn't do the cool hide/reveal more effect we're looking for. Based the amount scrolled (which we can measure in JavaScript), we'll adjust the background-position. Alone, that would look something like this:</p>
<pre rel="jQuery"><code class="language-javascript">$("#slider").on("scroll", function() {
  $(".slides").css({
    "background-position": $(this).scrollLeft()/6-100+ "px 0"
  });  
});</code></pre>
<p>The "6" and "-100" in there are magic numbers. Not <a href="http://css-tricks.com/magic-numbers-in-css/">CSS magic numbers</a> that are prone to frailty, but traditional magic numbers. Just some numbers that happen to make the effect work. A bummer, perhaps, but not that big of a deal. Design-y things are sometimes like that. Perhaps best to leave a comment in the code to that effect. These particular numbers are based on the images I used and their size and what looked good.</p>
<p>The effect here is the background shifting we're after:</p>
<figure id='post-21662 media-21662' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/slideeffect.jpg' alt='' /></p>
<figcaption>Notice the less of the yellow streetcar is visible when the slide is almost out of view.</figcaption>
</figure>
<h3>The JavaScript (imparting structure)</h3>
<p>That little snippet of JavaScript looks lonely up there without any structure behind it. A slider is a great excuse to look at a simple way to structure JavaScript.</p>
<p>We can make everything slider-related one object.</p>
<pre rel="JavaScript"><code class="language-javascript">var slider = {

};</code></pre>
<p>Then we'll group up the related elements into one area, bind our events together, and write little functions that do very specific things.</p>
<pre rel="jQuery"><code class="language-javascript">var slider = {
  
  el: {
    slider: $("#slider"),
    allSlides: $(".slide")
  },
 
  init: function() {
    // manual scrolling
    this.el.slider.on("scroll", function(event) {
      slider.moveSlidePosition(event);
    });
  },
  
  moveSlidePosition: function(event) {
    // Magic Numbers
    this.el.allSlides.css({
      "background-position": $(event.target).scrollLeft()/6-100+ "px 0"
    });  
  }
  
};

slider.init();</code></pre>
<h3>The HTML (adding navigation)</h3>
<p>Adding swipe stuff would be super (super) sweet (hint). But for now let's add little press-able links to change slides, rather than relying on the scrollbar. You might even remove the scrollbar in real life (straight up overflow: hidden; on the container). What we need is anchor links that link to the ID's of the individual slides.</p>
<pre rel="HTML"><code class="language-markup">&lt;nav class="slider-nav"&gt;
  &lt;a href="#slide-0" class="active"&gt;Slide 0&lt;/a&gt; 
  &lt;a href="#slide-1"&gt;Slide 1&lt;/a&gt; 
  &lt;a href="#slide-2"&gt;Slide 2&lt;/a&gt; 
&lt;/nav&gt;</code></pre>
<p>Style those as you will. For <a href="http://codepen.io/chriscoyier/pen/Fkczq">the demo</a>, I make them little tiny gray circles with the text hidden.</p>
<h3>The JavaScript (adding navigation)</h3>
<p>Our structure is more useful now. We simply add a few more elements we're dealing with, add a new event we're watching for (clicks on nav), and write a little function to deal with that event.</p>
<p>We know how far to animate the scroll position when a nav link is clicked from the ID on the link itself. The link might be href="#slide-1", which we can get "1" from easily. Then the position we need to scroll to is (1 × width of slide), so 300 in our case. We'll store that 300 value right in the JavaScript.</p>
<pre rel="jQuery"><code class="language-javascript">var slider = {
  
  el: {
    slider: $("#slider"),
    allSlides: $(".slide"),
    sliderNav: $(".slider-nav"),
    allNavButtons: $(".slider-nav &gt; a")
  },
  
  timing: 800,
  slideWidth: 300, // could measure this
  
  init: function() {
    // You can either manually scroll...
    this.el.slider.on("scroll", function(event) {
      slider.moveSlidePosition(event);
    });
    // ... or click a thing
    this.el.sliderNav.on("click", "a", function(event) {
      slider.handleNavClick(event, this);
    });
  },
  
  moveSlidePosition: function(event) {
    // Magic Numbers
    this.el.allSlides.css({
      "background-position": $(event.target).scrollLeft()/6-100+ "px 0"
    });  
  },
  
  handleNavClick: function(event, el) {
    // Don't change URL to a hash, remove if you want that
    event.preventDefault();

    // Get "1" from "#slide-1", for example
    var position = $(el).attr("href").split("-").pop();
    
    this.el.slider.animate({
      scrollLeft: position * this.slideWidth
    }, this.timing);
    
    this.changeActiveNav(el);
  },
  
  changeActiveNav: function(el) {
    // Remove active from all links
    this.el.allNavButtons.removeClass("active");
    // Add back to the one that was pressed
    $(el).addClass("active");
  }
  
};

slider.init();</code></pre>
<p>We have an "active" class on the nav links just to use in CSS to visually indicate which slide is active. We handle that by removing "active" from all links and then adding it back to the one that was clicked.</p>
<h3>Demo!</h3>
<pre class="codepen" data-height="630" data-type="result" data-href="Fkczq" data-user="chriscoyier" data-safe="true"><code></code><a href="http://codepen.io/chriscoyier/pen/Fkczq">Check out this Pen!</a></pre>
<p><script async src="http://codepen.io/assets/embed/ei.js"></script></p>
<p><p><small><a href="http://css-tricks.com/slider-with-sliding-backgrounds/">Slider with Sliding Backgrounds</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/slider-with-sliding-backgrounds/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Photo Swivel</title>
		<link>http://css-tricks.com/photo-swivel/</link>
		<comments>http://css-tricks.com/photo-swivel/#comments</comments>
		<pubDate>Wed, 15 May 2013 12:06:33 +0000</pubDate>
		<dc:creator>Guest Author</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21627</guid>
		<description><![CDATA[<p><p><em class="explanation">The following is a guest post by <a href="http://www.alexandery.net/">Alex Young</a> (<a href="http://twitter.com/the_alexyoung">@the_alexyoung</a>). Alex has created a simple technique to "rotate" the subject of a photo simply by hiding and showing multiple stacked photographs taken at different angles. Enjoy!</em></p>
<p></p>
<p>Web designers and developers have always fascinated us with fun and engaging ways we interact with their websites. I love discovering new techniques that these designers/developers have come up with. One technique in particular that sparked my interest is something that I &#8230;</p></p><p><p><small><a href="http://css-tricks.com/photo-swivel/">Photo Swivel</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p><em class="explanation">The following is a guest post by <a href="http://www.alexandery.net/">Alex Young</a> (<a href="http://twitter.com/the_alexyoung">@the_alexyoung</a>). Alex has created a simple technique to "rotate" the subject of a photo simply by hiding and showing multiple stacked photographs taken at different angles. Enjoy!</em></p>
<p><span id="more-21627"></span></p>
<p>Web designers and developers have always fascinated us with fun and engaging ways we interact with their websites. I love discovering new techniques that these designers/developers have come up with. One technique in particular that sparked my interest is something that I saw on <a href="http://www.warbyparker.com/">Warby Parker’s website</a>. They were able to create a cool effect where model follows the user’s mouse, showing off the different angles of the frames. </p>
<p>I’ve seen this technique mainly used for showing off sunglasses, but I’m interested to see what you guys can come up with for other real-world applications.</p>
<p>Before we begin, you will need a few things. Well, seven things to be exact. You are going to need seven photos of something taken at seven different angles. </p>
<h3>HTML Setup</h3>
<p>What we are setting up is a container called “faces”, then have created an area that we are going to use to display the photos – “face-area”.  We also have a <code>div</code> to hold each of the seven individual images. </p>
<pre rel="HTML"><code class="language-markup">&lt;div id="faces"&gt;

  &lt;div id="face-area"&gt;

    &lt;div id="image-1" style="display: none;"&gt;
      &lt;img src="/images/look-left-3.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-2" style="display: none;"&gt;
      &lt;img src="/images/look-left-2.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-3" style="display: none;"&gt;
      &lt;img src="/images/look-left-1.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-4" style="display: none;"&gt;
      &lt;img src="/images/look-center.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-5" style="display: none;"&gt;
      &lt;img src="/images/look-right-1.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-6" style="display: none;"&gt;
      &lt;img src="/images/look-right-2.jpg"&gt;
    &lt;/div&gt;

    &lt;div id="image-7" style="display: none;"&gt;
      &lt;img src="/images/look-right-3.jpg"&gt;
    &lt;/div&gt;</code></pre>
<p>Now we'll create a <code>div</code> that is going to do exactly what the name implies. It is going to lay on top of the other <code>div</code>. We can divide this <code>div</code> into 7 small columns and divide them equally across it. These 7 columns will be used to listen for when the mouse is hovering over that specific one. Adding “data-number” to each <code>div</code> will allow us to refer to each in jQuery later on.</p>
<pre rel="HTML"><code class="language-markup">    &lt;div id="the_faces_overlay"&gt;
      &lt;div class="the_faces" data-number="1"&gt;&lt;/div&gt;
      &lt;div class="the_faces" data-number="2"&gt; &lt;/div&gt;
      &lt;div class="the_faces" data-number="3"&gt;&lt;/div&gt;
      &lt;div class="the_faces" data-number="4"&gt;&lt;/div&gt;
      &lt;div class="the_faces" data-number="5"&gt;&lt;/div&gt;
      &lt;div class="the_faces" data-number="6"&gt;&lt;/div&gt;
      &lt;div class="the_faces" data-number="7"&gt;&lt;/div&gt;
    &lt;/div&gt; 

  &lt;/div&gt;&lt;!-- END #face-area --&gt;

&lt;/div&gt; &lt;!-- END #faces --&gt; </code></pre>
<h3>CSS Setup</h3>
<p>Most of the CSS is pretty self-explanatory. There is one thing I need to mention that is important, make sure that the 7 columns fit perfectly together across the <code>span</code> of the <code>div</code>.</p>
<pre rel="CSS"><code class="language-css">body {
  background: #333 
}

#faces {
  height: 333px;
  width: 500px;
  margin: 0 auto;
  border: 8px solid white;
}

#face-area {
  height: 500px;
  width: 333px;
  position: relative;
}

#the_faces_overlay {
  position: absolute;
  width: 500px;
  top: 0;
  left: 0;
}

#faces .the_faces {
  height: 333px;
  width: 14.2857143%;
  float: left;
  margin: 0;
  padding: 0;
}</code></pre>
<h3>jQuery Setup</h3>
<p>We need to listen for when each of the columns are hovered over. This is where those “data-numbers” come in handy. If we wanted to add more columns down the road we wouldn't have to add any more JavaScript.</p>
<pre rel="jQuery"><code class="language-javascript">// Reveal the "center" image
var centerImage = $("#image-4").show();

// Bind hovers to each column
$(".the_faces").each(function() {

  $(this).on("mouseover", function() {
  
    $("#image-" + $(this).attr("data-number")).show();
  
  }).on("mouseout",function() {
  
    $("#image-" + $(this).attr("data-number")).hide();

  });
  
});

// Reset center image
$("#face-area").on("mouseleave", function() {
  centerImage.show();
}).on("mouseenter", function() {
  centerImage.hide();
});</code></pre>
<h3>Tada!</h3>
<pre class="codepen" data-height="600" data-type="result" data-href="IJGxk" data-user="AlexanderY" data-safe="false"><code></code><a href="http://codepen.io/AlexanderY/pen/IJGxk">Check out this Pen!</a></pre>
<p>There you have it! Now you have your very own, head turning model. Use it to show off your very own products. There are tons of uses for this out there. Let me know what you come up with.</p>
<p><strong>Editor's note:</strong> this would be a fun demo to work on adding touch support, wouldn't it? Follows a swipe or something.</p>
<p><p><small><a href="http://css-tricks.com/photo-swivel/">Photo Swivel</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/photo-swivel/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Media Query Change Detection in JavaScript Through CSS Animations</title>
		<link>http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/</link>
		<comments>http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/#comments</comments>
		<pubDate>Tue, 14 May 2013 12:55:36 +0000</pubDate>
		<dc:creator>Guest Author</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21614</guid>
		<description><![CDATA[<p><p><em class="explanation">The following is a guest post by <a href="http://avedesign.me/">Alessandro Vendruscolo</a>. Media queries are relevant to both CSS and JS. The need and desire to manage those in one place is real. There have been some clever ways to do this, like Jeremy Keith's <a href="http://adactio.com/journal/5429/">Conditional CSS</a>. But in that case, the onus is on you to test after window state changes. You can get a true listener with MediaQueryList, but then you're maintaining the media queries in both places again. </em>&#8230;</p></p><p><p><small><a href="http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/">Media Query Change Detection in JavaScript Through CSS Animations</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p><em class="explanation">The following is a guest post by <a href="http://avedesign.me/">Alessandro Vendruscolo</a>. Media queries are relevant to both CSS and JS. The need and desire to manage those in one place is real. There have been some clever ways to do this, like Jeremy Keith's <a href="http://adactio.com/journal/5429/">Conditional CSS</a>. But in that case, the onus is on you to test after window state changes. You can get a true listener with MediaQueryList, but then you're maintaining the media queries in both places again. Ah well, I'll let Alessandro explain.</em></p>
<p><span id="more-21614"></span></p>
<p>Media queries were first introduced more than <a href="http://www.w3.org/TR/2001/WD-css3-mediaqueries-20010404/">twelve years ago</a> (yes, the first draft dates back to 4 April 2001!) and were introduced to <em>limit the scope of a style sheet</em>:</p>
<blockquote><p>A media query consists of a media type and one or more expressions to limit the scope of a certain style sheet. […] By using media queries, content presentations can be tailored to a range of devices without changing the content itself.</p></blockquote>
<p>With media queries in CSS we can selectively use styles depending on, for example, the width of browser screen:</p>
<pre rel="CSS"><code class="language-css">@media screen and (min-width: 960px) {
  body {
    padding: 50px;
  }
}</code></pre>
<p>In the example above, if your browser screen is wider than or equal to 960px, the body will have 50px of <a href="http://css-tricks.com/almanac/properties/p/padding/">padding</a>.</p>
<h3>DOM events triggered from CSS animations</h3>
<p>A simple click event:</p>
<pre rel="JavaScript"><code class="language-javascript">document.querySelector('a.button').addEventListener('click', function(event) {
  // do something
});</code></pre>
<p></code></p>
<p>Nothing fancy here, just standard DOM programming. The <code>click</code> event, among many other events, are generated when the user interacts with the page: clicks, moves the cursor, scrolls the page and so on.</p>
<p>All of these events are related to the DOM, and generates as a consequence of user&#39;s interactions. There are also some events which are specifically related to CSS <a href="http://css-tricks.com/almanac/properties/a/animation/">animations</a>: <code>animationStart</code>, <code>animationEnd</code> (and their <a href="http://css-tricks.com/almanac/properties/t/transition/">transition</a> cousins, <code>transitionStart</code>, <code>transitionEnd</code>).</p>
<p>If we insert a new element onto the page and it has an animation, we know that CSS animation will start as soon as it is inserted. So if we watch for the animationstart event of that animation, we can know when it was inserted.</p>
<pre rel="CSS"><code class="language-css">@keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}
.an-element {
  animation-duration: 0.001s;
  animation-name: nodeInserted;
}</code></pre>
<pre rel="JavaScript"><code class="language-javascript">document.addEventListener("animationstart", function (event) {
  if (event.animationName == "nodeInserted") {
    // an element with class an-element has been inserted in the DOM
  }
}, false);</code></pre>
<p>Thanks to <a href="http://davidwalsh.name/detect-node-insertion">David Walsh</a>, <a href="http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/">Daniel Buchner</a>, and <a href="http://blog.streak.com/2012/11/how-to-detect-dom-changes-in-css.html">Omar Ismail</a> for blogging about this around a year ago.</p>
<h3>Putting together media queries and animations</h3>
<p>Perhaps you can see where this is going. If we trigger the animation when a media query changes (rather than on node insertion), we can also detect when that media query change happened in our JavaScript.</p>
<pre rel="CSS"><code class="language-css">body {
  animation-duration: 0.001s;
}
@media screen and (min-width: 1000px) {
  body {
    animation-name: min-width-1000px;
  }
}
@media screen and (min-width: 700px) {
  body {
    animation-name: min-width-700px;
  }
}

@keyframes min-width-700px {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}

@keyframes min-width-1000px {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}</code></pre>
<pre rel="JavaScript"><code class="language-javascript">document.addEventListener(animationEnd, dispatchEvent, false);

// check the animation name and operate accordingly
function dispatchEvent(event) {
  if (event.animationName === 'min-width-700px') {
    document.body.innerHTML = 'Min width is 700px';
  } else if (event.animationName === 'min-width-1000px') {
    document.body.innerHTML = 'Min width is 1000px';
  }
}</code></pre>
<p>Editor's note: <a href="http://css-tricks.com/naming-media-queries/">choose your names wisely.</a></p>
<p><a href="http://codepen.io/vendruscolo/pen/Dpijq">Demo on CodePen:</a></p>
<pre class="codepen" data-height="150" data-type="result" data-href="Dpijq" data-user="vendruscolo" data-safe="false"></pre>
<p>We have the same simple animation that doesn&#39;t actually do anything, but this time, the animation is inside an <code>@media</code> block.</p>
<p>If the window becomes wider than 700px, the <code>min-width-700px</code> animation will trigger, and our <code>dispatchEvent</code> will get this information. Think of it like a callback. We can then adapt our page by showing new widgets, destroying others, or whatever else you need to do to adapt to the new screen size.</p>
<p>The cool thing is that we don&#39;t need to use the window resize event, which can be slow and fire too many times (throttling is normally needed). The <code>animationstart</code> event will trigger as soon as the Media query matches, even if we didn&#39;t resize the window. If we then resize the window, the event will fire just when the media query matches.</p>
<h3>The right way to do these things</h3>
<p>Now we've talked about how to get JavaScript callbacks of window width changes, without relying the <code>resize</code> event.</p>
<p>But I wouldn&#39;t suggest you to put this in production code. It works, but the right way to do this should be to use the <a href="https://developer.mozilla.org/en-US/docs/DOM/window.matchMedia"><code>matchMedia</code> method</a>, which returns a <code>MediaQueryList</code>. <code>MediaQueryList</code> objects can have listeners, and thus we can use the <code>addListener</code> method to get notified when a Media query matches or not.</p>
<p><a href="http://wicky.nillia.ms/enquire.js/">Enquire.js</a> has recently reached version 2, and wraps the <code>matchMedia</code> method, which gives us a clean and robust wrapper.</p>
<p><p><small><a href="http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/">Media Query Change Detection in JavaScript Through CSS Animations</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Naming Media Queries</title>
		<link>http://css-tricks.com/naming-media-queries/</link>
		<comments>http://css-tricks.com/naming-media-queries/#comments</comments>
		<pubDate>Fri, 10 May 2013 00:55:47 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21568</guid>
		<description><![CDATA[<p><p>It would be pretty cool if we could do this someday in CSS. The best idea for it that I've come across is to define it in a <code>&#60;meta&#62;</code> tag in the <code>&#60;head&#62;</code>. That way (perhaps) CSS could use it and JS could use it. Like <code>window.matchMedia(nameOfMyMediaQuery)</code> or <code>@media (nameOfMyMediaQuery) { }</code>. But since we can't, Sass has our back. Now the question is, how do we name them?</p>
<p></p>
<p>In Sass, the @mixin would look something like this:&#8230;</p></p><p><p><small><a href="http://css-tricks.com/naming-media-queries/">Naming Media Queries</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>It would be pretty cool if we could do this someday in CSS. The best idea for it that I've come across is to define it in a <code>&lt;meta&gt;</code> tag in the <code>&lt;head&gt;</code>. That way (perhaps) CSS could use it and JS could use it. Like <code>window.matchMedia(nameOfMyMediaQuery)</code> or <code>@media (nameOfMyMediaQuery) { }</code>. But since we can't, Sass has our back. Now the question is, how do we name them?</p>
<p><span id="more-21568"></span></p>
<p>In Sass, the @mixin would look something like this:</p>
<pre rel="SCSS"><code class="language-css">@mixin breakpoint($point) {
  @if $point == papa-bear {
    @media (max-width: 1600px) { @content; }
  }
  @else if $point == mama-bear {
    @media (max-width: 1250px) { @content; }
  }
  @else if $point == baby-bear {
    @media (max-width: 650px)  { @content; }
  }
}</code></pre>
<p>And you'd use it like:</p>
<pre rel="SCSS"><code class="language-css">.module {
  width: 25%;
  @include breakpoint(baby-bear) {
    width: 100%;
  }
}</code></pre>
<p>Although in reality I prefer something like "bp" just because it's shorter and you need to type it all the time. Which is why "+" would be so ideal instead of "@include". But I digress. Or you could go <a href="http://codepen.io/imathis/pen/BDzcI">Brandon Mathis style</a>:</p>
<pre rel="SCSS"><code class="language-css">$name1:  320px;
$name2:  500px;
$name3:  600px;
$name4:  992px;

@mixin at-least($device-width) {
  @media screen and (min-width: $device-width) {
    @content 
  }
}

@mixin until($device-width) {
  @media screen and (max-width: $device-width - 1) {
    @content 
  }
}</code></pre>
<p>The names should avoid actual numbers. The point here is to assign a name with abstracted meaning, so the numbers can change but the names stay the same. I'd avoid device names like "iPad" or whatever too, because that just sets up bad expectations and will date itself quickly.</p>
<p>Better are naming schemes that suggest relationships between the names themselves. Where one is obviously bigger or smaller than another. Of course this is assuming width queries, which are certainly the most common. But of course your media queries could be different or more complex, and the naming scheme should represent that.</p>
<p>Here's some ideas:</p>
<p>baby-bear<br />
mama-bear<br />
papa-bear</p>
<p>straw-house<br />
stick-house<br />
brick-house</p>
<p>private<br />
corporal<br />
sergeant<br />
captain<br />
major<br />
lieutenant<br />
colonel<br />
general</p>
<p>alpha<br />
beta<br />
gamma<br />
delta<br />
epsilon</p>
<p>very-small<br />
small<br />
medium<br />
large<br />
very-large</p>
<p>-<br />
--<br />
---<br />
----<br />
------</p>
<p>>-<<br />
[---]<br />
<-----></p>
<p>seed<br />
seedling<br />
sprout<br />
plant</p>
<p>pebble<br />
stone<br />
rock<br />
boulder</p>
<p>pawn<br />
knight<br />
queen<br />
king</p>
<p>bishop<br />
archbishop<br />
cardinal<br />
pope</p>
<p>eddard<br />
catelyn<br />
robb<br />
sansa<br />
arya<br />
bran<br />
rickon</p>
<p>knife<br />
dagger<br />
scimitar<br />
claidmore</p>
<p>A<br />
B<br />
C<br />
D<br />
E</p>
<p>From <a href="http://dribbble.com/shots/1062821-What-do-you-name-your-media-queries#comment-2526418">Vince Speelman</a></p>
<p>lil-bowow<br />
eminem<br />
jay-z<br />
krs-one<br />
rick-ross</p>
<p>From <a href="http://dribbble.com/shots/1062821-What-do-you-name-your-media-queries#comment-2526457">James Nowland</a></p>
<p>matryoshka-doll<br />
the-one-inside-matryoshka-doll<br />
the-one-inside-the-one-inside-matryoshka-doll<br />
the-one-inside-the-one-inside-the-one-inside-matryoshka-doll</p>
<p>From <a href="http://dribbble.com/shots/1062821-What-do-you-name-your-media-queries#comment-2527018">Philip Brown</a></p>
<p>michael<br />
jermaine<br />
jackie<br />
marlon<br />
tito</p>
<p>From <a href="http://dribbble.com/shots/1062821-What-do-you-name-your-media-queries#comment-2532074">Rob Stinogle</a></p>
<p>gandalf-the-gray<br />
gandalf-the-white</p>
<p>From <a href="https://twitter.com/darbybrown/status/332179179909046272">Darby Brown</a></p>
<p>stewie<br />
brian<br />
chris<br />
lois<br />
peter<br />
(I leave out Meg)</p>
<p>Got some other ideas?</p>
<p><p><small><a href="http://css-tricks.com/naming-media-queries/">Naming Media Queries</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/naming-media-queries/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Line Clampin’</title>
		<link>http://css-tricks.com/line-clampin/</link>
		<comments>http://css-tricks.com/line-clampin/#comments</comments>
		<pubDate>Wed, 08 May 2013 01:48:04 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21560</guid>
		<description><![CDATA[<p><p>You want X lines of text. Anything after that, gracefully cut off. That's "line clamping" and it is a perfectly legit desire. When you can count on text being a certain number of lines, you can create stronger and more reliable grids from the elements that contain that text, as well as achieve some symmetric aesthetic harmony. </p>
<p>There are a couple of ways to get it done, none of them spectacular.</p>
<p></p>
<p>In case that explanation wasn't clean, imagine you have &#8230;</p></p><p><p><small><a href="http://css-tricks.com/line-clampin/">Line Clampin&#8217;</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>You want X lines of text. Anything after that, gracefully cut off. That's "line clamping" and it is a perfectly legit desire. When you can count on text being a certain number of lines, you can create stronger and more reliable grids from the elements that contain that text, as well as achieve some symmetric aesthetic harmony. </p>
<p>There are a couple of ways to get it done, none of them spectacular.</p>
<p><span id="more-21560"></span></p>
<p>In case that explanation wasn't clean, imagine you have some HTML like this:</p>
<pre rel="HTML"><code class="language-markup">&lt;div class="module"&gt;
  &lt;p&gt;Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.&lt;/p&gt;
&lt;/div&gt;</code></pre>
<p>And you want to limit it to exactly three lines in a container. Like this:</p>
<figure id='post-21561 media-21561' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/clamped.png' alt='' /></figure>
<p>In all these examples, let's assume we have a "module".</p>
<pre rel="CSS"><code class="language-css">.module {
  width: 250px;
  overflow: hidden;
}</code></pre>
<h3>Weird WebKit Flexbox Way</h3>
<p>David DeSandro showed us <a href="http://dropshado.ws/post/1015351370/webkit-line-clamp">this little ditty</a> a few years ago. </p>
<pre rel="CSS"><code class="language-css">.line-clamp {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;  
}</code></pre>
<p>One one hand, this is awesome and exactly what we need. CSS the perfect place for this. It's a real need and it should be this simple. </p>
<p>On the other hand... it's weird. why does it need to be a flexbox thing (the old version at that)? It doesn't work without that. And it's extremely fragile. Let's say you want the module (or the paragraph) to have some padding. You can't, because the padding will expose extra lines. That's what we get with half-baked non-standardized properties.</p>
<h3>The Fade Out Way</h3>
<p>The root of this technique is just setting the height of the module in a predictable way. Let's say you set the <code>line-height</code> to <code>1.2em</code>. If we want to expose three lines of text, we can just make the height of the container <code>3.6em</code> (1.2em × 3). The hidden overflow will hide the rest. </p>
<p>But it can be a bit awkward to just the text off like that. Ideally we would add ellipsis, but we can't reliably position them. So instead we'll fade out the text achieving the same kind of communication ("there is more...").</p>
<p>To fade out the last line, we make a box (a pseudo element will work great) and overlay a transparent-to-background-color gradient over the top. Making it nearly as wide as the container is best in case the last line is short. Because we know the line-height, we can make the pseudo element box exactly one line tall.</p>
<pre rel="CSS"><code class="language-css">.fade {
  position: relative;
  height: 3.6em; /* exactly three lines */
}
.fade:after {
  content: "";
  text-align: right;
  position: absolute;
  bottom: 0;
  right: 0;
  width: 70%;
  height: 1.2em;
  background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
}</code></pre>
<h3>The Opera Overflow Way</h3>
<p>Like WebKit, Opera has their own way to handle this. They apply ellipsis on the line you wish to. Of course the clock is ticking for Presto (Opera's rendering engine pre-Blink) so this isn't particularly useful. Perhaps it can still inform a future implementation though.</p>
<pre rel="CSS"><code class="language-css">.last-line {
  height: 3.6em; /* exactly three lines */
  text-overflow: -o-ellipsis-lastline;
}</code></pre>
<h3>The Clamp.js Way</h3>
<p>Where there is a will there is a way (with JavaScript). I think that's a saying. Joseph J. Schmitt has an excellent library-free JavaScript thingy called <a href="http://github.com/josephschmitt/Clamp.js">Clamp.js</a> for making this happen in a cross-browser way.</p>
<pre rel="JavaScript"><code class="language-javascript">var module = document.getElementById("clamp-this-module");

$clamp(module, {clamp: 3});</code></pre>
<p>Make sure you target the element with the text directly inside of it. Originally I tried putting the ID on the module, which worked in Chrome/Safari, but failed in Firefox. Putting it on the <code>&lt;p&gt;</code> made it work in both (Thx <a href="http://css-tricks.com/line-clampin/#comment-436850">Merri</a>).</p>
<h3>The Demos</h3>
<pre class="codepen" data-height="660" data-type="result" data-href="iBtep" data-user="chriscoyier" data-safe="true"><code></code><a href="http://codepen.io/chriscoyier/pen/iBtep">Check out this Pen!</a></pre>
<h3>Updates</h3>
<p>Vesa Piittinen created <a href="http://codepen.io/Merri/pen/Dsuim">an alternative method to Clamp.js</a>.</p>
<blockquote><p>Unlike Clamp.js it retains all the text within the clamped element and uses text-overflow to do the magic.</p></blockquote>
<p>FT Labs also <a href="https://github.com/ftlabs/ftellipsis">has created a JavaScript plugin</a> to do the job. It's nice in that you don't have to specify the number of lines, it just happens when the text overflows the container, so the design decision stay in the CSS.</p>
<p>Both of these examples have been added to <a href="http://codepen.io/chriscoyier/pen/iBtep">the main Pen</a>.</p>
<p><p><small><a href="http://css-tricks.com/line-clampin/">Line Clampin&#8217;</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/line-clampin/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>CSS Zen Garden</title>
		<link>http://mezzoblue.com/archives/2013/05/07/10_years/</link>
		<comments>http://css-tricks.com/css-zen-garden/#comments</comments>
		<pubDate>Tue, 07 May 2013 20:32:45 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Link]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21558</guid>
		<description><![CDATA[<p><p>It's back.</p>
<p><a href="http://mezzoblue.com/archives/2013/05/07/10_years/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/css-zen-garden/">Permalink</a>&#8230;</p></p><p><p><small><a href="http://css-tricks.com/css-zen-garden/">CSS Zen Garden</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>It's back.</p>
<p><a href="http://mezzoblue.com/archives/2013/05/07/10_years/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/css-zen-garden/">Permalink</a></p><p><p><small><a href="http://css-tricks.com/css-zen-garden/">CSS Zen Garden</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/css-zen-garden/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to mark up subtitles</title>
		<link>http://html5doctor.com/howto-subheadings/</link>
		<comments>http://css-tricks.com/how-to-mark-up-subtitles/#comments</comments>
		<pubDate>Mon, 06 May 2013 15:14:49 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Link]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21542</guid>
		<description><![CDATA[<p><p>Now that <code>&#60;hgroup&#62;</code> is gone, use a <code>&#60;span&#62;</code> inside the main header, or group them with <code>&#60;header&#62;</code> and use a <code>&#60;p&#62;</code> for the subtitle.</p>
<p><a href="http://html5doctor.com/howto-subheadings/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/how-to-mark-up-subtitles/">Permalink</a>&#8230;</p></p><p><p><small><a href="http://css-tricks.com/how-to-mark-up-subtitles/">How to mark up subtitles</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>Now that <code>&lt;hgroup&gt;</code> is gone, use a <code>&lt;span&gt;</code> inside the main header, or group them with <code>&lt;header&gt;</code> and use a <code>&lt;p&gt;</code> for the subtitle.</p>
<p><a href="http://html5doctor.com/howto-subheadings/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/how-to-mark-up-subtitles/">Permalink</a></p><p><p><small><a href="http://css-tricks.com/how-to-mark-up-subtitles/">How to mark up subtitles</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/how-to-mark-up-subtitles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making accessible icon buttons</title>
		<link>http://www.nczonline.net/blog/2013/04/01/making-accessible-icon-buttons/</link>
		<comments>http://css-tricks.com/making-accessible-icon-buttons/#comments</comments>
		<pubDate>Mon, 06 May 2013 15:02:23 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Link]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21540</guid>
		<description><![CDATA[<p><p>Comprehensive research by Nicholas C. Zakas on the correct markup to make a graphic-only button accessible.</p>
<p><a href="http://www.nczonline.net/blog/2013/04/01/making-accessible-icon-buttons/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/making-accessible-icon-buttons/">Permalink</a>&#8230;</p></p><p><p><small><a href="http://css-tricks.com/making-accessible-icon-buttons/">Making accessible icon buttons</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>Comprehensive research by Nicholas C. Zakas on the correct markup to make a graphic-only button accessible.</p>
<p><a href="http://www.nczonline.net/blog/2013/04/01/making-accessible-icon-buttons/" title="Direct link to featured article">Direct Link to Article</a> &#8212; <a href="http://css-tricks.com/making-accessible-icon-buttons/">Permalink</a></p><p><p><small><a href="http://css-tricks.com/making-accessible-icon-buttons/">Making accessible icon buttons</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/making-accessible-icon-buttons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>3D Inset Parallax Effect</title>
		<link>http://css-tricks.com/3d-inset-parallax-effect/</link>
		<comments>http://css-tricks.com/3d-inset-parallax-effect/#comments</comments>
		<pubDate>Sat, 04 May 2013 13:21:34 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21471</guid>
		<description><![CDATA[<p><p><em class="explanation">The following is a guest post by <a href="http://joshuabader.com/">Joshua Bader</a>. Joshua noticed that certain 3D effects on the web could benefit from adjusting perspective as the web page is scrolled. I'll let him explain.</em></p>
<p>People love to make flat things appear as if they're three-dimensional. There are two ways to pull off this effect in a 2D environment, shape and movement.</p>
<p>By give a flat shape the appearance of edges or sides, we can make buttons or other objects appear &#8230;</p></p><p><p><small><a href="http://css-tricks.com/3d-inset-parallax-effect/">3D Inset Parallax Effect</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p><em class="explanation">The following is a guest post by <a href="http://joshuabader.com/">Joshua Bader</a>. Joshua noticed that certain 3D effects on the web could benefit from adjusting perspective as the web page is scrolled. I'll let him explain.</em></p>
<p>People love to make flat things appear as if they're three-dimensional. There are two ways to pull off this effect in a 2D environment, shape and movement.</p>
<p>By give a flat shape the appearance of edges or sides, we can make buttons or other objects appear as if they are raised or sunken into the screen. This is seen even here on CSS-Tricks:</p>
<figure id='post-21516 media-21516' class='align-none'><img src='http://cdn.css-tricks.com/wp-content/uploads/2013/05/raised-button.png' alt='' /></figure>
<p>The movement effect is a bit trickier. In a three-dimensional world, objects closer to you move fast, while the ones further away move slow. The further away an object is, the slower it moves. This is called the <a href="http://en.wikipedia.org/wiki/Parallax">parallax effect</a>. The parallax effect has been used in <a href="http://jalxob.com/cool-kitten/">all sorts</a> <a href="http://www.manufacturedessai.it/it">of places</a> for the past few years.</p>
<p>The first method, shape, works because it makes things appear to be popping out, or pushed away from you. The second method, movement, works because it makes things seem as if they are positioned in context with each other. But, what if we combined them. An object that looks sunken into the screen, like a shelf, and its edges shrink and grow, using the parallax effect, making the object appear as if it really is in 3D space.</p>
<p>First we need to add some markup on our page and give the items we want to affect the class of <code>inset</code>. Any block level elements will do. Here is a simple form with inputs that will have the effect applied.</p>
<pre rel="HTML"><code class="language-markup">&lt;form&gt;

  &lt;label&gt;
    First Name:
    &lt;input class="inset" type="text" placeholder="Enter First Name" /&gt;
  &lt;/label&gt;

  &lt;label&gt;
    Last Name:
    &lt;input class="inset" type="text" placeholder="Enter Last Name" /&gt;
  &lt;/label&gt;

  &lt;label&gt;
    Email Address:
    &lt;input class="inset" type="text" placeholder="Enter Email Address" /&gt;
  &lt;/label&gt;

  &lt;label&gt;
    Address:
    &lt;input class="inset" type="text" placeholder="Enter Address" /&gt;
  &lt;/label&gt;

  &lt;label&gt;
    Phone Number:
    &lt;input class="inset" type="text" placeholder="Enter Phone Number" /&gt;
  &lt;/label&gt;

&lt;/form&gt;</code></pre>
<p>Next we will need to style our <code>inset</code> elements. By giving the item slightly different color borders we can give the element the illusion of depth. This is because the joint between borders is an angle and with the different colors, it appears as if light is hitting different "sides" of a 3D area. This is the 'shape' method we mentioned earlier.</p>
<pre rel="CSS"><code class="language-css">.inset {
  width: 100%;
  box-sizing: border-box;
  background: #f9f9f9;
  border-style: solid;
  border-width: 30px;
  border-top-color: #e5e5e5;
  border-right-color: #eee;
  border-bottom-color: #e5e5e5;
  border-left-color: #eee;
}</code></pre>
<p>Now that the shape we want to manipulate is styled, we can use some simple jQuery to get the element's original border-width, window height, scroll position, target element's position on screen and convert that position to a percentage for the top edge and bottom edge.</p>
<pre rel="jQuery"><code class="language-javascript">var origBorderWidth = parseInt($('.inset').css('border-top-width')),
win = $(window),
windowHeight = win.height();

$('.inset').each(function() {
  var self = $(this),
    scrollHeight = win.scrollTop(),
    elementPosition = self.position(),
    positionPercentTop = Math.round((elementPosition.top - scrollHeight) / windowHeight * 100);
    positionPercentBottom = Math.round((elementPosition.top + self.outerHeight() - scrollHeight) / windowHeight * 100);
});</code></pre>
<p>Once we have all of these numbers as variables, we can resize the top and bottom border-widths of each element based on its position on the screen.</p>
<pre rel="jQuery"><code class="language-javascript">var origBorderWidth = parseInt($('.inset').css('border-top-width')),
win = $(window),
windowHeight = win.height();

$('.inset').each(function() {
  var self = $(this),
    scrollHeight = win.scrollTop(),
    elementPosition = self.position(),
    positionPercentTop = Math.round((elementPosition.top - scrollHeight) / windowHeight * 100);
    positionPercentBottom = Math.round((elementPosition.top + self.outerHeight() - scrollHeight) / windowHeight * 100);

  self.css({
    'border-top-width' : origBorderWidth - (origBorderWidth * (positionPercentTop / 100)) + 'px',
    'border-bottom-width' : origBorderWidth * (positionPercentBottom / 100) + 'px'
  });
});</code></pre>
<p>Now comes the fun part – recalculating each element's top and bottom border-widths while the page is scrolled. This will give us the desired parallax, 'movement' effect.</p>
<pre rel="jQuery"><code class="language-javascript">var origBorderWidth = parseInt($('.inset').css('border-top-width')),
  win = $(window);

function set3D() {
  var windowHeight = win.height();

  $('.inset').each(function() {
    var self = $(this),
      scrollHeight = win.scrollTop(),
      elementPosition = self.position(),
      positionPercentTop = Math.round((elementPosition.top - scrollHeight) / windowHeight * 100);
      positionPercentBottom = Math.round((elementPosition.top + self.outerHeight() - scrollHeight) / windowHeight * 100);

    self.css({
      'border-top-width' : origBorderWidth - (origBorderWidth * (positionPercentTop / 100)) + 'px',
      'border-bottom-width' : origBorderWidth * (positionPercentBottom / 100) + 'px'
    });
  });
};

win.on('load scroll resize', function() {
  set3D();
});</code></pre>
<p>There you have it. To see the effect, keep you eye on a single element as it moves up and down the page when scrolled. You will see its top and bottom border-width change depending on its position on the page. Its subtle, but effective.</p>
<pre class="codepen" data-height="500" data-type="result" data-href="tDaCn" data-user="joshbader" data-safe="true"><a href="http://codepen.io/joshbader/pen/tDaCn">Check out this Pen!</a></pre>
<div class="explanation"><strong>Editors note:</strong> pretty neat eh? Perhaps with even more tech we could create a demo that did eye-tracking and adjusted the perspectives depending on where your eyes are in relation to the screen. Here's another example that adjusts perspective <a href="http://codepen.io/FWeinb/pen/rngDa">based on the mouse.</a></div>
<p><p><small><a href="http://css-tricks.com/3d-inset-parallax-effect/">3D Inset Parallax Effect</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/3d-inset-parallax-effect/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Make Client Side Data Available Server Side</title>
		<link>http://css-tricks.com/make-client-side-data-available-server-side/</link>
		<comments>http://css-tricks.com/make-client-side-data-available-server-side/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 01:02:46 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://css-tricks.com/?p=21472</guid>
		<description><![CDATA[<p><p>That would be pretty useful, right? Right now it's very common to User Agent "sniff" when you want to make a server-side decision about what to give the client. But UA sniffing has always sucked and sucks more every day. What you really want to know is stuff like "how big is the screen I'm serving to?" or "does the device I'm serving to have touch events?" - that way you can serve resources that are appropriate to those questions. &#8230;</p></p><p><p><small><a href="http://css-tricks.com/make-client-side-data-available-server-side/">Make Client Side Data Available Server Side</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></description>
				<content:encoded><![CDATA[<p>That would be pretty useful, right? Right now it's very common to User Agent "sniff" when you want to make a server-side decision about what to give the client. But UA sniffing has always sucked and sucks more every day. What you really want to know is stuff like "how big is the screen I'm serving to?" or "does the device I'm serving to have touch events?" - that way you can serve resources that are appropriate to those questions. Is there a way to get accurate client side information on the server side? </p>
<p><span id="more-21472"></span></p>
<h3>Why Server Side?</h3>
<p>You might want to serve an entirely different layout or different part of a layout if you know (for sure) you are serving to a small mobile device. Or a TV. Or whatever. <a href="http://css-tricks.com/mixing-responsive-design-and-mobile-templates/">See this article.</a></p>
<h3>Put the Data in a Cookie</h3>
<p>I think the easiest way to make client side information available server side is by putting that data in a cookie. Cookies are sent in the headers of all requests to that domain, so the server has access to them. If that cookie has information in it about how big the browser window is, the server can use it. </p>
<h3>Cookie Logic</h3>
<p>In order to make the smart choices you want to make, you need this cookie to exist. If it does exist, cool. If it doesn't exist, do the bare minimum you need to do to client test, set the cookie, then refresh.</p>
<p>In PHP, just to illustrate:</p>
<pre rel="PHP"><code class="language-markup">&lt;?php if isset(($_COOKIE['_clientInfo'])) { ?&gt;

   &lt;p&gt;You have the cookie, so put in your logical stuff here and load the document as you will.&lt;/p&gt;

&lt;?php } else { ?&gt;

   &lt;p&gt;You don't have the cookie yet, so don't load anything but the resources you need to test the client and set the cookie. Then refresh.&lt;/p&gt;

&lt;?php } ?&gt;</code></pre>
<h3>Setting the Cookie</h3>
<p>What kind of information might you want? Screen size likely. Probably anything that <a href="http://modernizr.com/">Modernizr</a> would be able to tell you. </p>
<p>It's easy to set a cookie in JavaScript.</p>
<pre rel="JavaScript"><code class="language-javascript">document.cookie = "cookieName=" + value;</code></pre>
<p>Make the value easy for you to parse and do what you need with. Here let's make it a string of JSON. We'll make an object with all the data we want and use some jQuery and Modernizr to get the values we want to know.</p>
<pre rel="jQuery"><code class="language-javascript">var clientInfo = {
  browserWidth: $(window).width(),
  browserHeight: $(window).height(),
  flexboxSupport: Modernizr.flexbox,
  SVGSupport: Modernizr.svg
};

document.cookie = "_clientInfo=" + JSON.stringify(clientInfo);</code></pre>
<p>In testing, I had some issues with Opera so switched to the <a href="https://github.com/carhartl/jquery-cookie">jQuery cookie plugin</a> and it was fine. </p>
<pre rel="jQuery"><code class="language-javascript">var cookieVal = JSON.stringify(clientInfo);

$.cookie("_clientInfo", cookieVal, {
  expires: 7
});</code></pre>
<h3>Refresh After Setting the Cookie</h3>
<p>Now that the cookie is set, you can refresh the page so the document can actually load with all this juicy information available.</p>
<pre rel="JavaScript"><code class="language-javascript">window.location.reload();</code></pre>
<h3>Jank Alert?</h3>
<p>If you don't like the idea of refreshing, you could write it up so you load some kind of default document at the same time you are setting the cookie. No harm there.</p>
<h3>How Long Should the Cookie Last?</h3>
<p>I dunno. An hour maybe? Forever? There is some chance that you catch somebody when their desktop browser is in an unusual state. Like they have it super small, and they load your page, and they then get a small screen version forever. Or an update to that browser comes out with more advanced features but your old cookie is there. Your call.</p>
<p>Here's an example of one hour:</p>
<pre rel="JavaScript"><code class="language-javascript">var now = new Date();
var time = now.getTime();
time += 3600 * 1000; // one hour
now.setTime(time);

document.cookie = "_clientInfo=" + JSON.stringify(clientInfo) + ";expires=" + now.toGMTString();</code></pre>
<h3>Using the Info Server Side</h3>
<p>Using PHP just as an example, you can easily snag the cookie and access bits of the data we saved as JSON.</p>
<pre rel="PHP"><code class="language-markup">&lt;?php
  $json = $_COOKIE['_clientInfo'];
  $obj = json_decode(stripslashes($json));
  echo "&lt;p&gt;Browser Width: " . $obj-&gt;{'browserWidth'} . "&lt;/p&gt;";
?&gt;</code></pre>
<p>You wouldn't just echo it out like above, you'd use some logic on that data and do whatever fancy special serving of resources and content you are going to do.</p>
<h3>Using the Info Client Side</h3>
<p>The point of this is server-side access, but, you have access to that cookie on the client-side too. This means, theoretically, you wouldn't need to load Modernizr again because you already have that information. </p>
<p>Here's a simple example that gets the data and displays a part of it:</p>
<pre rel="jQuery"><code class="language-javascript">var clientInfo = JSON.parse("&lt;?php echo $_COOKIE['_clientInfo']; ?&gt;");
var output = "&lt;p&gt;Browser Width: " + clientInfo.browserWidth + "&lt;/p&gt;";
$("#clientInfo").append(output);</code></pre>
<h3>Demo</h3>
<p><a href="http://css-tricks.com/examples/ClientInfoInCookie/">World's simplest demo</a> just to show it's do-able.</p>
<h3>Downsides?</h3>
<p>I dunno. I suspect there are a bunch otherwise I would think this kind of thing would be done more often. Smart people: chime in in the comments and let me know the ups and downs.</p>
<p>I suspect it might complicate caching. Perhaps that much reading/sending of the cookie is an issue? What about people that don't allow cookies like this? Gotta make sure they don't get in any kind of infinite loop or have the site be unusable.</p>
<h3>Other Things</h3>
<ul>
<li>The first I heard of this kind of thing being used was in Matt Wilcox's <a href="https://github.com/MattWilcox/Adaptive-Images">Adaptive Images</a> where the screen resolution is saved as a cookie and used to make choices about what kind of images to use.</li>
<li><a href="https://github.com/igrigorik/http-client-hints">Client-Hints</a> is also a thing that aims to give us this information without all this fancy dancing.</li>
<li>James Pearce's <a href="https://github.com/jamesgpearce/modernizr-server">modernizr-server</a>:<br />
<blockquote><p>The modernizr-server library is a way to bring Modernizr browser data to your server scripting environment.</p></blockquote>
</li>
<li>Dave Molsen's <a href="https://github.com/dmolsen/Detector">Detector</a>:<br />
<blockquote><p>Detector is a simple, PHP- and JavaScript-based browser- and feature-detection library that can adapt to new devices &#038; browsers on its own without the need to pull from a central database of browser information.</p></blockquote>
</li>
<li>Shane Gliser talks about doing this but saving to HTML5 localstorage in his book <a href="http://www.packtpub.com/creating-mobile-apps-with-jquery/book">Creating Mobile Apps with jQuery Mobile</a></li>
</ul>
<p><p><small><a href="http://css-tricks.com/make-client-side-data-available-server-side/">Make Client Side Data Available Server Side</a> is a post from <a href="http://css-tricks.com">CSS-Tricks</a></small></p></p>]]></content:encoded>
			<wfw:commentRss>http://css-tricks.com/make-client-side-data-available-server-side/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Content Delivery Network via cdn.css-tricks.com

 Served from: css-tricks.com @ 2013-05-17 11:29:32 by W3 Total Cache -->
