4

I've made some small icon sprites for a userscript for another Stack site. It embeds some icon elements onto the page that look like the Magic: the Gathering mana symbols using a sprite sheet. The code snippet at the end is a sample of its output.

All of the icons contain a hidden text element (using Bootstrap's .sr-only) which is the plain text equivalent of the symbol. This means you can select the text around them, and copy and paste them to get useful plain text! For example if I run the code snippet below, highlight the following, and press CTRL+C:

a selection wrapping the mana symbols and some text around them

Then I've copied these: → {W} {U} {B} {R} {G} ← onto my clipboard and can paste that elsewhere. This is pretty useful, and exactly what I want to happen. However, the symbols don't look like they're getting selected with the text around them even though they are — all the text is obviously highlighted, the symbols themselves are not — and this is weird for a user and a confusing detriment to user experience I'd prefer to overcome. If it's getting selected like it could be copied and pasted, it should look like it.

(At least, it works this way in Chrome and Firefox on Windows.)

How can I ensure these elements look like they're being highlighted just like regular text?

It is OK if different HTML output is required, but I would like to keep the basic sprite sheet mechanism intact (load one image, not more than one). If the output's changed, it should still copy plaintext alternatives to the sprites to your clipboard.

body {
  font-family: sans-serif;
}

.mana-symbol {
  display: inline-block;
  width: 16px;
  height: 16px;
  background: url('//i.stack.imgur.com/kF1U5.png') no-repeat black;
  background-size: 169px;
  position: relative;
  top: 0.2em;
  box-shadow: 1px 1px 0px 0px #000;
  border-radius: 8px;
}

.mana-symbol .sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

.mana-symbol.W { background-position: -68px -34px }
.mana-symbol.U { background-position: -85px -34px }
.mana-symbol.B { background-position: -102px -34px }
.mana-symbol.R { background-position: -119px -34px }
.mana-symbol.G { background-position: -136px -34px }
try selecting, copying and pasting these: →
<span class="mana-symbol W"><span class="sr-only">{W}</span></span>
<span class="mana-symbol U"><span class="sr-only">{U}</span></span>
<span class="mana-symbol B"><span class="sr-only">{B}</span></span>
<span class="mana-symbol R"><span class="sr-only">{R}</span></span>
<span class="mana-symbol G"><span class="sr-only">{G}</span></span>
&larr;

The script above packs a sheet of 32px sprites into a 16px element (using background-size to forcibly resize it). This provides support to high-resolution screens, e.g. retina, and people using their browser zoom. Maintaining this support (via high-resolution sprites or SVG) is optional but preferable.

Resources available for you to use:


Image asset credits: Edited by me for the userscript. Most symbols created by micku on github, in turn based on the work of Goblin Hero and skibulk of Slightly Magic. Energy symbol by ahkren of Slightly Magic.

Community
  • 1
  • 1
doppelgreener
  • 4,809
  • 10
  • 46
  • 63
  • instead of `these: → {W} {U} {B} {R} {G} ←` what do u want to copy? – jafarbtech Dec 05 '16 at 20:56
  • @jafarbtech That is exactly what I want to copy. Copying that is fine and not the problem. What I want is for the symbols to *look* selected when they are within the text selection. – doppelgreener Dec 05 '16 at 21:02

2 Answers2

2

Turns out the <img> element has evolved for spriting over the past couple of years. Based on Robin Rendle's guide on CSS Tricks I've been able to recreate the desired effect using object-position.

With this one I can use either the 16px sprites or the SVG sprites. I'm not sure how I'd pack the 32px sprites in, but with the SVG sprites available I don't think I have to worry about that.

body {
  font-family: sans-serif;
}

.mana-symbol {
  object-fit: none;
  object-position: 0 0;
  width: 16px;
  height: 16px;
  box-shadow: 1px 1px 0px 0px #000;
  border-radius: 50%;
}

.mana-symbol.W { object-position: -68px -34px }
.mana-symbol.U { object-position: -85px -34px }
.mana-symbol.B { object-position: -102px -34px }
.mana-symbol.R { object-position: -119px -34px }
.mana-symbol.G { object-position: -136px -34px }
<p>try selecting, copying and pasting these: &rarr;
<img src="//i.stack.imgur.com/eyrtu.png" class="mana-symbol W" alt="{W}">
<img src="//i.stack.imgur.com/eyrtu.png" class="mana-symbol U" alt="{U}">
<img src="//i.stack.imgur.com/eyrtu.png" class="mana-symbol B" alt="{B}">
<img src="//i.stack.imgur.com/eyrtu.png" class="mana-symbol R" alt="{R}">
<img src="//i.stack.imgur.com/eyrtu.png" class="mana-symbol G" alt="{G}">
&larr;</p>

<p>SVG version &rarr;
<img src="http://svgur.com/i/Mg.svg" class="mana-symbol W" alt="{W}">
<img src="http://svgur.com/i/Mg.svg" class="mana-symbol U" alt="{U}">
<img src="http://svgur.com/i/Mg.svg" class="mana-symbol B" alt="{B}">
<img src="http://svgur.com/i/Mg.svg" class="mana-symbol R" alt="{R}">
<img src="http://svgur.com/i/Mg.svg" class="mana-symbol G" alt="{G}">
&larr;</p>
bishop
  • 37,830
  • 11
  • 104
  • 139
doppelgreener
  • 4,809
  • 10
  • 46
  • 63
1

One option is to use color: transparent; to hide the text instead of position: absolute; etc. You may still have to tweak the sizes, but it looks pretty good in my browser.

body {
  font-family: sans-serif;
}

.mana-symbol {
  display: inline-block;
  width: 16px;
  height: 16px;
  background: url('//i.stack.imgur.com/kF1U5.png') no-repeat black;
  background-size: 169px;
  top: 0.2em;
  box-shadow: 1px 1px 0px 0px #000;
  border-radius: 8px;
  color: transparent;
}

.mana-symbol.W { background-position: -68px -34px }
.mana-symbol.U { background-position: -85px -34px }
.mana-symbol.B { background-position: -102px -34px }
.mana-symbol.R { background-position: -119px -34px }
.mana-symbol.G { background-position: -136px -34px }
try selecting: &rarr;
<span class="mana-symbol W"><span class="sr-only">{W}</span></span>
<span class="mana-symbol U"><span class="sr-only">{U}</span></span>
<span class="mana-symbol B"><span class="sr-only">{B}</span></span>
<span class="mana-symbol R"><span class="sr-only">{R}</span></span>
<span class="mana-symbol G"><span class="sr-only">{G}</span></span>
&larr;
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Haha, this looks kind of weird in my browser but also shows the user what they're selecting. That's unexpected but kind of nice. – doppelgreener Dec 05 '16 at 21:29