Skip to content
Advertisement

Is it possible to apply CSS to half of a character?

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.

x

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.

Advertisement

Answer

Now on GitHub as a Plugin!

enter image description here Feel free to fork and improve.

Demo | Download Zip | Half-Style.com (Redirects to GitHub)


  • Pure CSS for a Single Character
  • JavaScript used for automation across text or multiple characters
  • Preserves Text Accessibility for screen readers for the blind or visually impaired

Part 1: Basic Solution

Half Style on text

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 use content: 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.


JavaScript
JavaScript
JavaScript

(JSFiddle demo)


Part 2: Advanced solution – Independent left and right parts

Half Style on text - advanced - With Text Shadow

With this solution you can style left and right parts, individually and independently.

Everything is the same, only more advanced CSS does the magic.

JavaScript
JavaScript
JavaScript

(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:

    Horizontal Half Parts - No Text Shadow

  • Possibility of Text Shadow for each half part independently:

    halfStyle - Horizontal Half Parts - With Text Shadow

JavaScript
JavaScript
JavaScript

(JSFiddle demo)



-Vertical 1/3 Parts

  • Without Text Shadow:

    halfStyle - Vertical 1/3 Parts - No Text Shadow

  • Possibility of Text Shadow for each 1/3 part independently:

    halfStyle - Vertical 1/3 Parts - With Text Shadow

JavaScript
JavaScript
JavaScript

(JSFiddle demo)



-Horizontal 1/3 Parts

  • Without Text Shadow:

    halfStyle - Horizontal 1/3 Parts - No Text Shadow

  • Possibility of Text Shadow for each 1/3 part independently:

    halfStyle - Horizontal 1/3 Parts - With Text Shadow

JavaScript
JavaScript
JavaScript

(JSFiddle demo)



-HalfStyle Improvement By @KevinGranger

halfStyle - KevinGranger

JavaScript
JavaScript
JavaScript

(JSFiddle demo)



-PeelingStyle improvement of HalfStyle by @SamTremaine

halfStyle - SamTremaine

JavaScript
JavaScript
JavaScript

(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 attribute data-halfstyle="[-CustomClassName-]". The plugin will do the rest of the job.

halfStyle - Multiple on Same Page

Also the CSS style-sets’ class definitions match the [-CustomClassName-] part mentioned above and is chained to .halfStyle, so we will have .halfStyle.[-CustomClassName-]

JavaScript
JavaScript
JavaScript

(JSFiddle demo)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement