JSFiddle - React, Tailwind, and code Playground
HTML
On Github <a href="http://github.com/arbelh/HalfStyle">http://github.com/arbelh/HalfStyle</a>
<hr/>
<h4>class: <code>hs-base</code></h4>
<p>Single Characters: (Pure CSS, no Javascript)</p>
<span class="halfStyle hs-base" data-content="X">X</span>
<span class="halfStyle hs-base" data-content="Y">Y</span>
<span class="halfStyle hs-base" data-content="Z">Z</span>
<span class="halfStyle hs-base" data-content="A">A</span>
<hr/>
<p>Automated on any text: (Requires jQuery to automate the task)</p>
<span class="textToHalfStyle" data-halfstyle="hs-base">Half-style, please.</span>
<hr/>
On Github <a href="http://github.com/arbelh/HalfStyle">http://github.com/arbelh/HalfStyle</a>
CSS
/*!
* HalfStyle
* Copyright 2014 Arbel Hakopian
* Licensed under MIT (https://github.com/arbelh/HalfStyle/blob/master/license.md)
*/
/* start half-style hs-base */
.halfStyle.hs-base {
position:relative;
display:inline-block;
font-size:80px; /* or any font size will work */
overflow:hidden;
white-space: pre; /* to preserve the spaces from collapsing */
color: #000; /* for demo purposes */
}
.halfStyle.hs-base:before {
display:block;
z-index:1;
position:absolute;
top:0;
width: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
pointer-events: none; /* so the base char is selectable by mouse */
overflow:hidden;
color: #f00; /* for demo purposes */
}
/* end half-style hs-base */
JavaScript
/*!
* HalfStyle
* Copyright 2014 Arbel Hakopian
* Licensed under MIT (https://github.com/arbelh/HalfStyle/blob/master/license.md)
*/
jQuery(function($) {
var halfstyle_text, halfstyle_chars, $halfstyle_el, halfstyle_i, halfstyle_output, halfstyle_style;
// Iterate over all class occurrences
$('.textToHalfStyle').each(function(idx, halfstyle_el) {
$halfstyle_el = $(halfstyle_el);
halfstyle_style = $halfstyle_el.data('halfstyle');
halfstyle_text = $halfstyle_el.text();
halfstyle_chars = halfstyle_text.split('');
// Set the screen-reader text
$halfstyle_el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>');
// Reset output for appending
halfstyle_output = '';
// Iterate over all chars in the text
for (halfstyle_i = 0; halfstyle_i < halfstyle_chars.length; halfstyle_i++) {
// Create a styled element for each character and append to container
halfstyle_output += '<span aria-hidden="true" class="halfStyle ' + halfstyle_style + '" data-content="' + halfstyle_chars[halfstyle_i] + '">' + halfstyle_chars[halfstyle_i] + '</span>';
}
// Write to DOM only once
$halfstyle_el.append(halfstyle_output);
});
});