Style Half of A Character Using jQuery and CSS - Splitchar.js

File Size: 5.09 KB
Views Total: 491
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Style Half of A Character Using jQuery and CSS - Splitchar.js

Splitchar.js is an ultra-lightweight (~ 1kb) jQuery plugin which make it possible to vertically or horizontally style half of a character using CSS before/after pseudo elements.

See Also:

Basic Usage:

1. Import jQuery library and the jQuery Splitchar.js script into your document.

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="js/splitchar.js"></script>

2. The markup. 

<!-- Horizontal - 2 Colors -->
<h1 class="demo horizontal">jQuery Splitchar.js Demos</h1>

<!-- Horizontal - 3 Colors -->
<h2 class="demo  horizontal-tri">Horizontal 3-color Demo</h2>

<!-- Vertical - 2 Colors -->
<h2 class="demo  vertical">Vertical Demo</h2>

<!-- Vertical - 3 Colors -->
<h2 class="demo  vertical-tri">Vertical 3-color Demo</h2>

3. Apply the following CSS styles to half of the character.

.horizontal {
  position: relative;
  display: inline-block;
  font-size: 64px;
  overflow: hidden;
  white-space: pre;
  color: transparent;
}

.horizontal:before {
  display: block;
  z-index: 1;
  position: absolute;
  top: 0;
  width: 50%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #ecf0f1;
}

.horizontal:after {
  display: block;
  direction: rtl;
  position: absolute;
  z-index: 2;
  top: 0;
  left: 50%;
  width: 50%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #bdc3c7;
}

.horizontal-tri {
  position: relative;
  display: inline-block;
  font-size: 64px;
  overflow: hidden;
  white-space: pre;
  color: #bdc3c7;
}

.horizontal-tri:before {
  display: block;
  z-index: 1;
  position: absolute;
  top: 0;
  width: 33%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #ecf0f1;
}

.horizontal-tri:after {
  display: block;
  direction: rtl;
  position: absolute;
  z-index: 2;
  top: 0;
  left: 68%;
  width: 33%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #aaafb2;
}

.vertical {
  position: relative;
  display: inline-block;
  font-size: 64px;
  overflow: hidden;
  white-space: pre;
  color: transparent;
}

.vertical:before {
  display: block;
  z-index: 2;
  position: absolute;
  top: 0;
  height: 50%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #ecf0f1;
}

.vertical:after {
  display: block;
  direction: rtl;
  position: absolute;
  z-index: 1;
  top: 0;
  height: 100%;
  left: 0;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #bdc3c7;
}

.vertical-tri {
  position: relative;
  display: inline-block;
  font-size: 64px;
  overflow: hidden;
  white-space: pre;
  color: #ecf0f1;
}

.vertical-tri:before {
  display: block;
  z-index: 2;
  position: absolute;
  top: 0;
  height: 33%;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #bdc3c7;
}

.vertical-tri:after {
  display: block;
  direction: rtl;
  position: absolute;
  z-index: 1;
  top: 0;
  height: 66%;
  left: 0;
  content: attr(data-content);
  overflow: hidden;
  pointer-events: none;
  color: #d2d8d9;
}

4. Call the function to enable the plugin.

$(".demo").splitchar();

This awesome jQuery plugin is developed by emisfera. For more Advanced Usages, please check the demo page or visit the official website.