<h1 id="thumbnail_grid_layout_instructions">Thumbnail Grid Layout Instructions</h1>

<h2 id="semantic_markup_step_1html">Semantic Markup [step-1.html]</h2>

<ol>
<li><p>Our thumbnails with captions are a good example of a <code>figure</code> with a <code>figcaption</code> element.</p>

<blockquote>
<pre><code>  &lt;figure&gt;
    &lt;img src="images/yes_thumbs_analog_alarm_clock-430x480.jpg" alt="alarm clock" /&gt;
    &lt;figcaption&gt;
        &lt;p class="description"&gt;Analog Travel Alarm Clock Braun
            &lt;span class="price"&gt;$40.00&lt;/span&gt;
        &lt;/p&gt;
    &lt;/figcaption&gt;
&lt;/figure&gt;
</code></pre>
</blockquote>

<ul>
<li>Just style the first thumbnail block.</li>
</ul></li>
<li><p>We also want these thumbnails to be a link. Let&#8217;s wrap the entire <code>figure</code> in a link. As of HTML5, it&#8217;s ok to wrap a block level elements with a link.</p>

<blockquote>
<pre><code>  &lt;a href="#" title="View product details"&gt;
    &lt;figure&gt;
        &lt;img src="images/clock_thumb.jpg" alt="alarm clock" /&gt;
        &lt;figcaption&gt;
            &lt;p class="description"&gt;Analog Travel Alarm Clock Braun
                &lt;span class="price"&gt;$40.00&lt;/span&gt;
            &lt;/p&gt;
        &lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/a&gt;
</code></pre>
</blockquote>

<ul>
<li>Just style the first thumbnail block.</li>
</ul></li>
<li><p>Let&#8217;s also center our page and set a <code>max-width</code> of <strong>1200px</strong>. We can use the <code>container</code> class on the outermost <code>div</code> for that.</p>

<ul>
<li>See notes from Week 6 for help on centering a layout.</li>
</ul></li>
<li><p>Set the <code>margin</code> on the <code>body</code> to be <strong>1em</strong> on the top and<em>* 5%</em>* on the left and right.</p></li>
</ol>

<h2 id="create_a_grid_step_2html">Create a grid [step-2.html]</h2>

<ol>
<li><p>We have to make the thumbnails line up in a grid. We have 8 thumbnails and we&#8217;ll align them in a 4x2 grid.</p>

<ul>
<li><p>To fit the thumbnails across the page in a 4x2 layout each thumbnail should have a width of 25% of the total page width (100%/4). Set the width of each thumbnail <code>li</code> to <strong>25%</strong>. </p></li>
<li><p>You&#8217;ll probably only notice a change in length of the lines of text.</p></li>
</ul></li>
<li><p>Make the thumbnails line up by floating each <code>li</code> to the left</p></li>
<li><p>Our images are breaking out of their containing boxes because they are too big. Images are one of the few content elements that will not reflow to fit inside their containers. </p>

<ul>
<li>Fix this by setting the <code>max-width</code> of the images to <strong>100%</strong>. This will cause the images to never exceed 100% of their containing box.</li>
</ul></li>
<li><p>Our thumbnail blocks are very big. Use the web inspector to look at one of the <code>figure</code> elements. Notice that it has a lot of <code>margin</code> applied to it. This is a browser default that is not getting reset by the <code>reset.css</code> (because <code>figure</code> is an html5 element). Let&#8217;s reset it ourselves by adding a <code>margin</code> and <code>padding</code> of <strong>0</strong>.</p>

<ul>
<li><p>Once we&#8217;ve done this, our images will be larger and no longer be separated with a gutter. </p></li>
<li><p>Add a gutter by giving a <strong>1.33%</strong> <code>margin</code> on the right of each of the <code>li</code> items. This will cause our rows to only have three items, bumping the fourth one down.</p></li>
</ul></li>
<li><p>Our elements are only lining up three across. This is because our <code>width</code> and <code>margin</code> together equals more than 100% of the container width, causing the last item to bump down a line.</p>

<blockquote>
<pre><code>  24% * 4 + 1.33% * 4 = 101.32%
</code></pre>
</blockquote>

<p>Because we added a margin to the right of each item, we actually added an unnecessary margin to the right of the last item in each row. Removing just this last margin will make our rows fit across the page.</p>

<p>We need to remove the margin on the last item of each row. We can do this using the <code>nth-child</code> pseudo-selector.</p>

<blockquote>
<pre><code>  .thumbnail-grid li:nth-child(4n) {
    margin-right: 0;
}
</code></pre>
</blockquote>

<ul>
<li><p>Every fourth item will have a right margin of 0.</p></li>
<li><p>Read more about the <code>nth-child</code> pseudo-selector on <a href="https://css-tricks.com/how-nth-child-works/">CSS Tricks</a>.</p></li>
</ul></li>
<li><p>Our footer text is sneaking into the extra space in our grid. We need to make sure it clears all of the floats in our thumbnail grid. Use the <code>clear</code> property for this.</p>

<ul>
<li>Add <code>clear: left</code> to the <code>footer</code>.</li>
</ul></li>
<li><p>Depending on page width, the Boskee cube image might be breaking the grid.</p>

<ul>
<li><p>This happens because we have a grid with variable heights. The caption length changes for each element. There are several possible ways to fix this:</p>

<ul>
<li>Get rid of captions (probably undesirable)</li>
<li>Set a fixed height for each <code>li</code> block (could cause issues elsewhere)</li>
<li>Make sure captions are always the same size by more rigidly controlling the content (not always possible)</li>
<li>Use advanced CSS selectors to make sure we clear the floats for each row</li>
</ul></li>
<li><p>The last option is the most versatile so we&#8217;ll use it.</p></li>
<li><p>To get the first item in each row, we find the last item number of each row (4n) and then add 1. (4n+1).</p>

<blockquote>
<pre><code>  .thumbnail-grid li:nth-child(4n+1) {
    clear: left;
}
</code></pre>
</blockquote></li>
<li><p>With this rule, the first item in each row will always clear all of the rows above it. The rest of the items will follow the first.</p></li>
</ul></li>
<li><p>There should be more space between the thumbnail grid and the footer. Add a bottom margin of <strong>3em</strong> to the &#8216;thumbnail-grid&#8217;.</p>

<ul>
<li>Notice how this didn&#8217;t actually do anything. Temporarily add padding and a background color to the class so we can see it:</li>
</ul>

<blockquote>
<pre><code>  .thumbnail-grid {
    margin-bottom: 3em;
    background: rgb(0, 0, 255);
    padding: 1em;
}
</code></pre>
</blockquote>

<ul>
<li><p>Use the web inspector to look at the <code>thumbnail-grid</code> class. Notice how the <code>thumbnail-grid</code> container does not contain the actual thumbnails.</p>

<p>This happens when all of the children of an element are floated. There are a few ways to fix this.</p>

<ul>
<li><code>overflow: auto;</code></li>
<li><code>overflow: hidden;</code></li>
<li>float the container</li>
</ul></li>
<li><p>We&#8217;ll float the container here. Float the <code>.thumbnail-grid</code> to the left. See how the container now expands around its content.</p></li>
<li><p>Remove the padding and background color.</p></li>
</ul></li>
</ol>

<h2 id="style_the_captions_step_3html">Style the captions [step-3.html]</h2>

<ol>
<li><p>We want to style the captions and price so that the price is on the right side of the thumbnail. We&#8217;ll achieve this by careful use of positioning.</p>

<ul>
<li>Start by styling the caption text to be <strong>.75em</strong>. Use the <code>description</code> class.</li>
</ul></li>
<li><p>Absolutely position the <code>price</code> class to the top right.</p>

<blockquote>
<pre><code>  .thumbnail-grid .price {
    position: absolute;
    right: 0;
    top: 0;
}
</code></pre>
</blockquote>

<p>That made all of the prices go to the top right of the browser window. This is because we positioned them absolutely, <strong>relative to the browser window</strong>. That is the default behavior with absolute positioning. Instead, we want to position them relative to the description. </p>

<ul>
<li><p>Add a background color to the description class to better illustration what&#8217;s happening. The price should be in the top right corder of that background color.</p></li>
<li><p>In order to absolutely position the price <strong>relative to its container</strong> we must first create a <em>positioning context</em>. To do this, you have to set the position property on the elements container. </p></li>
<li><p>In this case, the container is <code>.description</code>. We set its position to relative to set the content. This by itself does not have any visual effect on the page, but when combined with absolute positioning, it sets the context.</p></li>
</ul>

<blockquote>
<pre><code>  .description {
    position: relative;
}
</code></pre>
</blockquote></li>
<li><p>We want to make sure out text doesn&#8217;t overlap, so we&#8217;ll add a right padding of <strong>25%</strong> to the <code>description</code> class. Then set the width of the <code>price</code> to <strong>25%</strong>.</p></li>
<li><p>Right align the price text to make it look better.</p></li>
</ol>

<h2 id="create_an_overlay_on_hover_step_4html">Create an overlay on hover [step-4.html]</h2>

<ol>
<li><p>When we hover over a thumbnail, we want a colored overlay to show over the picture with some text. Look at <code>final.html</code> for an example.</p>

<ul>
<li>We need to add markup to do this. First we need an element to be the colored block. We&#8217;ll use a <code>span</code> with a class of <code>overlay</code>. The text inside the span should say &#8220;View&#8221;. Add this immediately before the first image.</li>
</ul>

<blockquote>
<pre><code>  &lt;a href="#" title="View product details"&gt;
    &lt;figure&gt;
        &lt;span class="overlay"&gt;&lt;/span&gt;
        &lt;img src="images/clock_thumb.jpg" alt="alarm clock" /&gt;
        &lt;figcaption&gt;
            &lt;p class="description"&gt;Analog Travel Alarm Clock Braun
                &lt;span class="price"&gt;$40.00&lt;/span&gt;
            &lt;/p&gt;
        &lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/a&gt;
</code></pre>
</blockquote>

<ul>
<li>Style the overlay class to have a partially transparent background color, using <code>rgba</code> colors.</li>
</ul></li>
<li><p>We want to position the overlay so that it is exactly the same size as the image. Set it to 100% width and height in the css.</p>

<ul>
<li><p>We need to absolutely postion the overlay over the image. Set the <code>position</code> of the overlay to <code>absolute</code>. What happens?</p></li>
<li><p>This again, is relative to the browser window by default. We need to position the container relatively to set the <strong>positioning content</strong> on the overlay.</p></li>
<li><p>Relatively position the containing element of the overlay (That&#8217;s the one that surrounds the overlay element). What happens?</p></li>
</ul></li>
<li><p>The overlay now covers the entire figure element, including the caption. We only want it to cover the element. To do this, we need to create a new container for the overlay which only contains the element. </p>

<ul>
<li>Create a new container in the markup for our overlay. We&#8217;ll use a <code>div</code> since there is no semantic context for the container. Add a class of <code>thumbnail</code> to the <code>div</code>.</li>
</ul>

<blockquote>
<pre><code>  &lt;a href="#" title="View product details"&gt;
    &lt;figure&gt;
        &lt;div class="thumbnail"&gt;
            &lt;span class="overlay"&gt;View&lt;/span&gt;
            &lt;img src="images/clock_thumb.jpg" alt="alarm clock" /&gt;
        &lt;/div&gt;

<pre><code>    &amp;lt;figcaption&amp;gt;
        &amp;lt;p class="description"&amp;gt;Analog Travel Alarm Clock Braun
            &amp;lt;span class="price"&amp;gt;$40.00&amp;lt;/span&amp;gt;
        &amp;lt;/p&amp;gt;
    &amp;lt;/figcaption&amp;gt;
&amp;lt;/figure&amp;gt;
</code></pre>

&lt;/a&gt;
</code></pre>
</blockquote>

<ul>
<li>Relatively position the new container. What happens?</li>
</ul></li>
<li><p>Style the text on the overlay.</p>

<blockquote>
<pre><code>  background-color: rgba( 255, 0, 0, .5);
color: white;
font-weight: bold;
font-size: 200%;
padding-top: 40%;
text-align: center;
</code></pre>
</blockquote></li>
<li><p>Get rid of the tiny gap below the images. This happens because they are inline by default. Set our thumbnails to display block. Add a little margin below each image too.</p>

<blockquote>
<pre><code>  .thumbnail-grid img {
    display: block; /* Get rid of tiny space below images */
    margin-bottom: .3em;
}
</code></pre>
</blockquote></li>
<li><p>We only want the overlay to show when we&#8217;re hovering over the thumbnail.</p>

<ul>
<li><p>Hide the overlay by setting the <code>opacity</code> to <strong>0</strong>.</p></li>
<li><p>Use the <code>hover</code> pseudo class to set the <code>opacity</code> of the overlay back to <strong>1</strong> when we hover over it.</p>

<blockquote>
<pre><code>   .overlay:hover {
    opacity: 1;
 }
</code></pre>
</blockquote></li>
<li><p>If we just change the opacity when we hover over the overlay, nothing will happen when we hover over the captions. View this in the browser to see.</p></li>
<li><p>Instead, we can create a selector that will change the opacity of the overlay when the li is hovered. This will include the caption.</p>

<blockquote>
<pre><code>  .thumbnail-grid li:hover .overlay {
    opacity: 1;
}
</code></pre>
</blockquote></li>
</ul></li>
<li><p>To create a more smooth effect, we can use CSS transitions to make the overlay gradually fade in.</p>

<blockquote>
<pre><code>  .thumbnail-grid a .overlay {
    transition: 1s all;
}
</code></pre>
</blockquote></li>
</ol>
