How to apply CSS to half of a character, for example to make half of the character transparent?
This problem has been tried to be viewed through the following methods, but all failed:
Here is an example of the target effect:
[Image]
Is there a CSS or JavaScript solution to achieve this effect or do I have to resort to images ? Since the text will eventually be generated dynamically, we want to avoid using images.
Demo | Download zip | HalfStyle.com (redirect to GitHub)
[Picture]
Demo: http://jsfiddle.net/arbel/pd9yB/1694/
[Image]
Demo: http://jsfiddle.net/arbel/pd9yB/1696/
Instructions:
Applies to any text:
JavaScript (automation mode):
// jQuery for automated mode jQuery(function($) { var text, chars, $el, i, output; // Iterate over all class occurences $('.textToHalfStyle').each(function(idx, el) { $el = $(el); text = $el.text(); chars = text.split(''); // Set the screen-reader text $el.html('<span>
CSS:
.halfStyle { position: relative; display: inline-block; font-size: 80px; /* or any font size will work */ color: black; /* or transparent, any color */ overflow: hidden; white-space: pre; /* to preserve the spaces from collapsing */ } .halfStyle:before { display: block; z-index: 1; position: absolute; top: 0; left: 0; width: 50%; content: attr(data-content); /* dynamic content for the pseudo element */ overflow: hidden; color: #f00; }
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>Single Characters:</p> <span class="halfStyle" data-content="X">X</span> <span class="halfStyle" data-content="Y">Y</span> <span class="halfStyle" data-content="Z">Z</span> <span class="halfStyle" data-content="A">A</span> <hr/> <p>Automated:</p> <span class="textToHalfStyle">Half-style, please.</span>
The above is the detailed content of How Can I Apply CSS to Half a Character, Making One Half Transparent?. For more information, please follow other related articles on the PHP Chinese website!