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

后端 未结 19 1592
轻奢々
轻奢々 2020-11-22 16:46

What I am looking for:

A way to style one HALF of a character. (In this case, half the letter being transparent)

Wh

相关标签:
19条回答
  • 2020-11-22 17:08

    Just for the record in history!

    I've come up with a solution for my own work from 5-6 years ago, which is Gradext ( pure javascript and pure css, no dependency ) .

    The technical explanation is you can create an element like this:

    <span>A</span>
    

    now if you want to make a gradient on text, you need to create some multiple layers, each individually specifically colored and the spectrum created will illustrate the gradient effect.

    for example look at this is the word lorem inside of a <span> and will cause a horizontal gradient effect ( check the examples ):

     <span data-i="0" style="color: rgb(153, 51, 34);">L</span>
     <span data-i="1" style="color: rgb(154, 52, 35);">o</span>
     <span data-i="2" style="color: rgb(155, 53, 36);">r</span>
     <span data-i="3" style="color: rgb(156, 55, 38);">e</span>
     <span data-i="4" style="color: rgb(157, 56, 39);">m</span>
    

    and you can continue doing this pattern for a long time and long paragraph as well.

    But!

    What if you want to create a vertical gradient effect on texts?

    Then there's another solution which could be helpful. I will describe in details.

    Assuming our first <span> again. but the content shouldn't be the letters individually; the content should be the whole text, and now we're going to copy the same ‍‍<span> again and again ( count of spans will define the quality of your gradient, more span, better result, but poor performance ). have a look at this:

    <span data-i="6" style="color: rgb(81, 165, 39); overflow: hidden; height: 11.2px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    <span data-i="7" style="color: rgb(89, 174, 48); overflow: hidden; height: 12.8px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    <span data-i="8" style="color: rgb(97, 183, 58); overflow: hidden; height: 14.4px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    <span data-i="9" style="color: rgb(105, 192, 68); overflow: hidden; height: 16px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    <span data-i="10" style="color: rgb(113, 201, 78); overflow: hidden; height: 17.6px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    <span data-i="11" style="color: rgb(121, 210, 88); overflow: hidden; height: 19.2px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
    

    Again, But!

    what if you want to make these gradient effects to move and create an animation out of it?

    well, there's another solution for it too. You should definitely check animation: true or even .hoverable() method which will lead to a gradient to start based on cursor position! ( sounds cool xD )

    this is simply how we're creating gradients ( linear or radial ) on texts. If you liked the idea or want to know more about it, you should check the links provided.


    Maybe this is not the best option, maybe not the best performant way to do this, but it will open up some space to create exciting and delightful animations to inspire some other people for a better solution.

    It will allow you to use gradient style on texts, which is supported by even IE8!

    Here you can find a working live demo and the original repository is here on GitHub as well, open source and ready to get some updates ( :D )

    This is my first time ( yeah, after 5 years, you've heard it right ) to mention this repository anywhere on the Internet, and I'm excited about that!


    [Update - 2019 August:] Github removed github-pages demo of that repository because I'm from Iran! Only the source code is available here tho...

    0 讨论(0)
  • 2020-11-22 17:10

    Example


    JSFiddle DEMO

    We'll do it using just CSS pseudo selectors!

    This technique will work with dynamically generated content and different font sizes and widths.

    HTML:

    <div class='split-color'>Two is better than one.</div>
    

    CSS:

    .split-color > span {
        white-space: pre-line;
        position: relative;
        color: #409FBF;
    }
    
    .split-color > span:before {
        content: attr(data-content);
        pointer-events: none;  /* Prevents events from targeting pseudo-element */
        position: absolute;
        overflow: hidden;
        color: #264A73;
        width: 50%;
        z-index: 1;
    }
    

    To wrap the dynamically generated string, you could use a function like this:

    // Wrap each letter in a span tag and return an HTML string
    // that can be used to replace the original text
    function wrapString(str) {
      var output = [];
      str.split('').forEach(function(letter) {
        var wrapper = document.createElement('span');
        wrapper.dataset.content = wrapper.innerHTML = letter;
    
        output.push(wrapper.outerHTML);
      });
    
      return output.join('');
    }
    
    // Replace the original text with the split-color text
    window.onload = function() {
        var el  = document.querySelector('.split-color'),
            txt = el.innerHTML;
        
        el.innerHTML = wrapString(txt);
    }
    
    0 讨论(0)
  • 2020-11-22 17:11

    Limited CSS and jQuery Solution

    I am not sure how elegant this solution is, but it cuts everything exactly in half: http://jsfiddle.net/9wxfY/11/

    Otherwise, I have created a nice solution for you... All you need to do is have this for your HTML:

    Take a look at this most recent, and accurate, edit as of 6/13/2016 : http://jsfiddle.net/9wxfY/43/

    As for the CSS, it is very limited... You only need to apply it to :nth-child(even)

    $(function(){
      var $hc = $('.half-color');
      var str = $hc.text();
      $hc.html("");
    
      var i = 0;
      var chars;
      var dupText;
    
      while(i < str.length){
        chars = str[i];
        if(chars == " ") chars = "&nbsp;";
        dupText = "<span>" + chars + "</span>";
    
        var firstHalf = $(dupText);
        var secondHalf = $(dupText);
    
        $hc.append(firstHalf)
        $hc.append(secondHalf)
    
        var width = firstHalf.width()/2;
    
        firstHalf.width(width);
        secondHalf.css('text-indent', -width);
    
        i++;
      }
    });
    .half-color span{
      font-size: 2em;
      display: inline-block;
      overflow: hidden;
    }
    .half-color span:nth-child(even){
      color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="half-color">This is a sentence</div>

    0 讨论(0)
  • 2020-11-22 17:12

    A nice WebKit-only solution that takes advantage of the background-clip: text support: http://jsfiddle.net/sandro_paganotti/wLkVt/

    span{
       font-size: 100px;
       background: linear-gradient(to right, black, black 50%, grey 50%, grey);
       -webkit-background-clip: text;
       -webkit-text-fill-color: transparent;
    }
    
    0 讨论(0)
  • 2020-11-22 17:15

    Closest I can get:

    $(function(){
      $('span').width($('span').width()/2);
      $('span:nth-child(2)').css('text-indent', -$('span').width());
    });
    body{
      font-family: arial;
    }
    span{
      display: inline-block;
      overflow: hidden;
    }
    span:nth-child(2){
      color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <span>X</span><span>X</span>

    Demo: http://jsfiddle.net/9wxfY/2/

    Heres a version that just uses one span: http://jsfiddle.net/9wxfY/4/

    0 讨论(0)
  • 2020-11-22 17:16

    Here an ugly implementation in canvas. I tried this solution, but the results are worse than I expected, so here it is anyway.

    Canvas example

    $("div").each(function() {
      var CHARS = $(this).text().split('');
      $(this).html("");
      $.each(CHARS, function(index, char) {
        var canvas = $("<canvas />")
          .css("width", "40px")
          .css("height", "40px")
          .get(0);
        $("div").append(canvas);
        var ctx = canvas.getContext("2d");
        var gradient = ctx.createLinearGradient(0, 0, 130, 0);
        gradient.addColorStop("0", "blue");
        gradient.addColorStop("0.5", "blue");
        gradient.addColorStop("0.51", "red");
        gradient.addColorStop("1.0", "red");
        ctx.font = '130pt Calibri';
        ctx.fillStyle = gradient;
        ctx.fillText(char, 10, 130);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>Example Text</div>

    0 讨论(0)
提交回复
热议问题