What I am looking for:
A way to style one HALF of a character. (In this case, half the letter being transparent)
What I have currently searched for and tried (With no luck):
- Methods for styling half of a character/letter
- Styling part of a character with CSS or JavaScript
- Apply CSS to 50% of a character
Below is an example of what I am trying to obtain.
Does a CSS or JavaScript solution exist for this, or am I going to have to resort to images? I would prefer not to go the image route as this text will end up being generated dynamically.
UPDATE:
Since many have asked why I would ever want to style half of a character, this is why. My city had recently spent $250,000 to define a new "brand" for itself. This logo is what they came up with. Many people have complained about the simplicity and lack of creativity and continue to do so. My goal was to come up with this website as a joke. Type in 'Halifax' and you will see what I mean.
Best Answer
Now on GitHub as a Plugin!
Feel free to fork and improve.
Demo | Download Zip | Half-Style.com (Redirects to GitHub)
Part 1: Basic Solution
Demo: http://jsfiddle.net/arbel/pd9yB/1694/
This works on any dynamic text, or a single character, and is all automated. All you need to do is add a class on the target text and the rest is taken care of.
Also, the accessibility of the original text is preserved for screen readers for the blind or visually impaired.
Explanation for a single character:
Pure CSS. All you need to do is to apply
.halfStyle
class to each element that contains the character you want to be half-styled.For each span element containing the character, you can create a data attribute, for example here
data-content="X"
, and on the pseudo element usecontent: attr(data-content);
so the.halfStyle:before
class will be dynamic and you won't need to hard code it for every instance.Explanation for any text:
Simply add
textToHalfStyle
class to the element containing the text.(JSFiddle demo)
Part 2: Advanced solution - Independent left and right parts
With this solution you can style left and right parts, individually and independently.
Everything is the same, only more advanced CSS does the magic.
(JSFiddle demo)
Part 3: Mix-Match and Improve
Now that we know what is possible, let's create some variations.
-Horizontal Half Parts
Without Text Shadow:
Possibility of Text Shadow for each half part independently:
(JSFiddle demo)
-Vertical 1/3 Parts
Without Text Shadow:
Possibility of Text Shadow for each 1/3 part independently:
(JSFiddle demo)
-Horizontal 1/3 Parts
Without Text Shadow:
Possibility of Text Shadow for each 1/3 part independently:
(JSFiddle demo)
-HalfStyle Improvement By @KevinGranger
(JSFiddle demo)
-PeelingStyle improvement of HalfStyle by @SamTremaine
(JSFiddle demo and on samtremaine.co.uk)
Part 4: Ready for Production
Customized different Half-Style style-sets can be used on desired elements on the same page. You can define multiple style-sets and tell the plugin which one to use.
The plugin uses data attribute
data-halfstyle="[-CustomClassName-]"
on the target.textToHalfStyle
elements and makes all the necessary changes automatically.So, simply on the element containing the text add
textToHalfStyle
class and data attributedata-halfstyle="[-CustomClassName-]"
. The plugin will do the rest of the job.Also the CSS style-sets' class definitions match the
[-CustomClassName-]
part mentioned above and is chained to.halfStyle
, so we will have.halfStyle.[-CustomClassName-]
(JSFiddle demo)