Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution for you, using only one very tiny and simple image and one automatically generated span element:</p> <h3>CSS</h3> <pre><code>span.stars, span.stars span { display: block; background: url(stars.png) 0 -16px repeat-x; width: 80px; height: 16px; } span.stars span { background-position: 0 0; } </code></pre> <h3>Image</h3> <blockquote> <p><a href="http://www.ulmanen.fi/stuff/stars.png" rel="noreferrer">alt text http://www.ulmanen.fi/stuff/stars.png</a></p> </blockquote> <p><strong>Note:</strong> do <em>NOT</em> hotlink to the above image! Copy the file to your own server and use it from there.</p> <h3>jQuery</h3> <pre><code>$.fn.stars = function() { return $(this).each(function() { // Get the value var val = parseFloat($(this).html()); // Make sure that the value is in 0 - 5 range, multiply to get width var size = Math.max(0, (Math.min(5, val))) * 16; // Create stars holder var $span = $('&lt;span /&gt;').width(size); // Replace the numerical value with stars $(this).html($span); }); } </code></pre> <p>If you want to restrict the stars to only half or quarter star sizes, add one of these rows before the <code>var size</code> row:</p> <pre><code>val = Math.round(val * 4) / 4; /* To round to nearest quarter */ val = Math.round(val * 2) / 2; /* To round to nearest half */ </code></pre> <h3>HTML</h3> <pre><code>&lt;span class="stars"&gt;4.8618164&lt;/span&gt; &lt;span class="stars"&gt;2.6545344&lt;/span&gt; &lt;span class="stars"&gt;0.5355&lt;/span&gt; &lt;span class="stars"&gt;8&lt;/span&gt; </code></pre> <h3>Usage</h3> <pre><code>$(function() { $('span.stars').stars(); }); </code></pre> <h3>Output</h3> <blockquote> <p><a href="http://www.ulmanen.fi/stuff/stars_output.png" rel="noreferrer">Image from fugue icon set (www.pinvoke.com) http://www.ulmanen.fi/stuff/stars_output.png</a></p> </blockquote> <h3>Demo</h3> <blockquote> <p><a href="http://www.ulmanen.fi/stuff/stars.php" rel="noreferrer">http://www.ulmanen.fi/stuff/stars.php</a></p> </blockquote> <p>This will probably suit your needs. With this method you don't have to calculate any three quarter or whatnot star widths, just give it a float and it'll give you your stars.</p> <hr> <p>A small explanation on how the stars are presented might be in order.</p> <p>The script creates two block level span elements. Both of the spans initally get a size of 80px * 16px and a background image stars.png. The spans are nested, so that the structure of the spans looks like this:</p> <pre><code>&lt;span class="stars"&gt; &lt;span&gt;&lt;/span&gt; &lt;/span&gt; </code></pre> <p>The outer span gets a <code>background-position</code> of <code>0 -16px</code>. That makes the gray stars in the outer span visible. As the outer span has height of 16px and <code>repeat-x</code>, it will only show 5 gray stars.</p> <p>The inner span on the other hand has a <code>background-position</code> of <code>0 0</code> which makes only the yellow stars visible.</p> <p>This would of course work with two separate imagefiles, star_yellow.png and star_gray.png. But as the stars have a fixed height, we can easily combine them into one image. This utilizes the <a href="http://css-tricks.com/css-sprites/" rel="noreferrer">CSS sprite technique</a>.</p> <p>Now, as the spans are nested, they are automatically overlayed over each other. In the default case, when the width of both spans is 80px, the yellow stars completely obscure the grey stars.</p> <p>But when we adjust the width of the inner span, the width of the yellow stars decreases, revealing the gray stars.</p> <p>Accessibility-wise, it would have been wiser to leave the float number inside the inner span and hide it with <code>text-indent: -9999px</code>, so that people with CSS turned off would at least see the floating point number instead of the stars.</p> <p>Hopefully that made some sense.</p> <hr> <h3>Updated 2010/10/22</h3> <p>Now even more compact and harder to understand! Can also be squeezed down to a one liner:</p> <pre><code>$.fn.stars = function() { return $(this).each(function() { $(this).html($('&lt;span /&gt;').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16)); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload