DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: Css3

Apply CSS to half of a character

Share

Is there a way to apply CSS to half of a character in a text? Yes, it is possible. Half the letter being transparent and half is visible prominent.

All you need to do is to add a class on the text you want to apply CSS and the rest is taken care of. The accessibility of the main text is preserved with screen readers for the blind or visually impaired.

You need to do the following things for applying CSS to half of a character:

  • Methods for styling half of a character or letter
  • Methods for styling part of a character with CSS/JavaScript
  • Methods for applying CSS to 50% of a character

Just apply the class .halfStyle to each element which contains the character you want to be half-styled.

For example, see the below code.

HTML:

<span class="halfStyle" data-content="A">A</span>
<span class="halfStyle" data-content="B">B</span>
<span class="halfStyle" data-content="C">C</span>
<span class="halfStyle" data-content="D">D</span>

Read Also: HTML5 placeholder color with CSS

CSS:

.halfStyle {
position:relative;
font-size:80px;
display:inline-block;
color: black;
white-space: pre;
overflow:hidden;

}
.halfStyle:before {
display:block;
position:absolute;
z-index:1;
top:0;
left:0;
content: attr(data-content);
width: 50%;
overflow:hidden;
color: blue;
}

For each span element containing the each character, create a data attribute i.e. data-content=”A” etc. and on the pseudo element use content: attr(data-content);

So, the .halfStyle:before class will be more dynamic and you no need to hard code it for each instance.

Note: Every browser calculates the .5em value differently.

CSS Designer

Recent Posts

Writing Tools That Help Content Marketers Produce Cleaner Work

Content marketing has a hidden tax. It's not the writing itself, it's everything that happens…

2 weeks ago

How Data Analytics Is Actually Driving Smarter Marketing Decisions

Most marketing teams aren't failing because they lack data. They're failing because they can't act…

4 weeks ago

The Ultimate SPF Tester Guide: Boost Email Deliverability In Minutes

Email marketing continues to be one of the most effective ways for businesses to communicate,…

4 weeks ago

Sales Ops: A role that helps sales teams work as efficiently as possible

Xerox first introduced it around the mid-1970s. The need came up because the management activities…

1 month ago

Forex Investment Tips for Beginners

Investing in the forex market may look to be a dangerous game. With some worthwhile…

1 month ago

How Did The Restaurant Industry change In Post Pandemic Era?

The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…

2 months ago