首页 文章

是否可以将CSS应用于角色的一半?

提问于
浏览
2522

What I am looking for:

设置一个 HALF 字符的方法 . (在这种情况下,一半字母是透明的)

What I have currently searched for and tried (With no luck):

  • 为一半字符/字母设置样式的方法

  • 使用CSS或JavaScript设置角色的一部分

  • 将CSS应用于角色的50%

以下是我想要获得的一个例子 .

x

是否存在CSS或JavaScript解决方案,或者我将不得不诉诸图像?我宁愿不去图像路线,因为这个文本最终会动态生成 .


UPDATE:

因为很多人都问过为什么我想要为一半角色设计风格,这就是原因所在 . 我的城市最近花了25万美元为自己定义一个新的"brand" . 他们想出了这个 logo . 许多人抱怨简单性和缺乏创造力,并继续这样做 . 我的目标是把这个 website 作为一个笑话 . 输入'Halifax',你会明白我的意思 . :)

18 回答

  • 462

    如果你对此感兴趣,那么Lucas Bebber的Glitch是一个非常相似和超酷的效果:

    enter image description here

    使用简单的SASS Mixin创建

    .example-one {
      font-size: 100px;
      @include textGlitch("example-one", 17, white, black, red, blue, 450, 115);
    }
    

    Chris Coyer's CSS TricksLucas Bebber's Codepen page的更多细节

  • 35

    Enter image description here

    我刚刚玩@ Arbel的解决方案:

    var textToHalfStyle = $('.textToHalfStyle').text();
    var textToHalfStyleChars = textToHalfStyle.split('');
    $('.textToHalfStyle').html('');
    $.each(textToHalfStyleChars, function(i,v){
        $('.textToHalfStyle').append('<span class="halfStyle" data-content="' + v + '">' + v + '</span>');
    });
    
    body{
        background-color: black;
    }
    .textToHalfStyle{
        display:block;
        margin: 200px 0 0 0;
        text-align:center;
    }
    .halfStyle {
        font-family: 'Libre Baskerville', serif;
        position:relative;
        display:inline-block;
        width:1;
        font-size:70px;
        color: black;
        overflow:hidden;
        white-space: pre;
        text-shadow: 1px 2px 0 white;
    }
    .halfStyle:before {
        display:block;
        z-index:1;
        position:absolute;
        top:0;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow:hidden;
        color: white;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <span class="textToHalfStyle">Dr. Jekyll and M. Hide</span>
    
  • 67

    这可以通过CSS :before 选择器和 content property value 来实现 .

    .halfed, .halfed1 {
      float: left;
    }
    
    .halfed, .halfed1 {
      font-family: arial;
      font-size: 300px;
      font-weight: bolder;
      width: 200px;
      height: 300px;
      position: relative; /* To help hold the content value within */
      overflow: hidden;
      color: #000;
    }
    
    
    
    
    .halfed:before, .halfed1:before   {
      width: 50%; /* How much we'd like to show */
      overflow: hidden; /* Hide what goes beyond our dimension */  
      content: 'X'; /* Halfed character */
      height: 100%;
      position: absolute;
      color: #28507D;
    
    }
    
    
    
    /* For Horizontal cut off */ 
    
    .halfed1:before   {
      width: 100%;
      height: 55%;
      
    }
    
    <div class="halfed"> X </div>
    
    <div class="halfed1"> X </div>
    

    >> See on jsFiddle

  • 2722

    enter image description here

    我刚刚完成了插件的开发,每个人都可以使用它!希望你会喜欢它 .

    在GitHub上查看项目 - 查看项目网站 . (所以你可以看到所有的分割风格)

    用法

    首先,确保包含 jQuery 库 . 获取最新jQuery版本的最佳方法是更新头标记:

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    

    下载文件后,请确保将它们包含在项目中:

    <link rel="stylesheet" type="text/css" href="css/splitchar.css">
    <script type="text/javascript" src="js/splitchar.js"></script>
    

    标记

    您所要做的就是将类 splitchar ,然后是包含文本的元素所需的样式 . 例如

    <h1 class="splitchar horizontal">Splitchar</h1>
    

    完成所有这些后,只需确保在文档就绪文件中调用jQuery函数,如下所示:

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

    自定义

    为了使文本看起来完全符合您的要求,您所要做的就是应用您的设计:

    .horizontal { /* Base CSS - e.g font-size */ }
    .horizontal:before { /* CSS for the left half */ }
    .horizontal:after { /* CSS for the right half */ }
    

    而已!现在你已经设置了 Splitchar 插件 . 关于它的更多信息,请点击http://razvanbalosin.com/Splitchar.js/ .

  • 51

    FWIW,这是我对此的看法,仅使用CSS:http://codepen.io/ricardozea/pen/uFbts/

    几点说明:

    • 我这样做的主要原因是测试自己,看看我是否能够完成造型的一半角色,同时实际上为OP提供了有意义的答案 .

    • 我知道这不是一个理想的或最具可扩展性的解决方案,而且这里人们提出的解决方案对于“真实世界”场景来说要好得多 .

    • 我创建的CSS代码基于我想到的第一个想法和我个人对问题的处理方法 .

    • 我的解决方案仅适用于X,A,O,M等对称字符 . **它不适用于不对称字符,如B,C,F,K或小写字母 .

    • **然而,这种方法创造了非常有趣的'shapes',具有不对称字符 . 尝试将X更改为K或小写字母,如CSS中的h或p :)

    HTML

    <span class="half-letter"></span>
    

    SCSS

    .half-character { 
      display: inline-block;
      font: bold 350px/.8 Arial;
      position: relative;
    
      &:before, &:after {
        content: 'X'; //Change character here
        display: inline-block;
        width: 50%;
        overflow: hidden;
        color: #7db9e8;
      }
      &:after {
        position: absolute;
        top: 0;
        left: 50%;
        color: #1e5799;
        transform: rotateY(-180deg);
      }
    }
    
  • 21

    现在在GitHub上作为插件!

    enter image description here
    随意分叉和改进 .

    演示|下载Zip | Half-Style.com(重定向到GitHub)


    单个字符

    • Pure CSS

    • JavaScript用于跨文本或多个字符的自动化

    • 为盲人或视障人士的屏幕阅读器保留文本可访问性

    第1部分:基本解决方案

    Half Style on text

    Demo: http://jsfiddle.net/arbel/pd9yB/1694/


    这适用于任何动态文本或单个字符,并且都是自动化的 . 您需要做的就是在目标文本上添加一个类,其余的都要处理 .

    此外,对于盲人或视障人士的屏幕阅读器,保留了原始文本的可访问性 .

    Explanation for a single character:

    纯CSS . 您需要做的就是将 .halfStyle 类应用于包含您想要半个样式的字符的每个元素 .

    对于包含该字符的每个span元素,您可以创建一个数据属性,例如 data-content="X" ,并且在伪元素上使用 content: attr(data-content); ,因此 .halfStyle:before 类将是动态的,您不需要为每个实例对其进行硬编码 .

    Explanation for any text:

    只需将 textToHalfStyle 类添加到包含文本的元素即可 .


    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
        $el = $(el);
        text = $el.text();
        chars = text.split('');
    
        // Set the screen-reader text
        $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
        // Reset output for appending
        output = '';
    
        // Iterate over all chars in the text
        for (i = 0; i < chars.length; i++) {
            // Create a styled element for each character and append to container
            output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
        }
    
        // Write to DOM only once
        $el.append(output);
      });
    });
    
    .halfStyle {
        position: relative;
        display: inline-block;
        font-size: 80px; /* or any font size will work */
        color: black; /* or transparent, any color */
        overflow: hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
    }
    
    .halfStyle:before {
        display: block;
        z-index: 1;
        position: absolute;
        top: 0;
        left: 0;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        color: #f00;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo


    第2部分:高级解决方案 - 独立的左右部分

    Half Style on text - advanced - With Text Shadow

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

    一切都是一样的,只有更高级的CSS才能实现 .

    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
            $el = $(el);
            text = $el.text();
            chars = text.split('');
    
            // Set the screen-reader text
            $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
            // Reset output for appending
            output = '';
    
            // Iterate over all chars in the text
            for (i = 0; i < chars.length; i++) {
                // Create a styled element for each character and append to container
                output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
            }
    
            // Write to DOM only once
            $el.append(output);
        });
    });
    
    .halfStyle {
        position: relative;
        display: inline-block;
        font-size: 80px; /* or any font size will work */
        color: transparent; /* hide the base character */
        overflow: hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
    }
    
    .halfStyle:before { /* creates the left part */
        display: block;
        z-index: 1;
        position: absolute;
        top: 0;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #f00; /* for demo purposes */
        text-shadow: 2px -2px 0px #af0; /* for demo purposes */
    }
    
    .halfStyle:after { /* creates the right part */
        display: block;
        direction: rtl; /* very important, will make the width to start from right */
        position: absolute;
        z-index: 2;
        top: 0;
        left: 50%;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #000; /* for demo purposes */
        text-shadow: 2px 2px 0px #0af; /* for demo purposes */
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo



    第3部分:混合匹配和改进

    现在我们知道什么是可能的,让我们创建一些变化 .


    -水平半部件

    没有文字阴影:

    Horizontal Half Parts - No Text Shadow

    文本阴影的可能性每半个部分独立:

    halfStyle - Horizontal Half Parts - With Text Shadow

    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
            $el = $(el);
            text = $el.text();
            chars = text.split('');
    
            // Set the screen-reader text
            $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
            // Reset output for appending
            output = '';
    
            // Iterate over all chars in the text
            for (i = 0; i < chars.length; i++) {
                // Create a styled element for each character and append to container
                output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
            }
    
            // Write to DOM only once
            $el.append(output);
        });
    });
    
    .halfStyle {
      position: relative;
      display: inline-block;
      font-size: 80px; /* or any font size will work */
      color: transparent; /* hide the base character */
      overflow: hidden;
      white-space: pre; /* to preserve the spaces from collapsing */
    }
    
    .halfStyle:before { /* creates the top part */
      display: block;
      z-index: 2;
      position: absolute;
      top: 0;
      height: 50%;
      content: attr(data-content); /* dynamic content for the pseudo element */
      overflow: hidden;
      pointer-events: none; /* so the base char is selectable by mouse */
      color: #f00; /* for demo purposes */
      text-shadow: 2px -2px 0px #af0; /* for demo purposes */
    }
    
    .halfStyle:after { /* creates the bottom part */
      display: block;
      position: absolute;
      z-index: 1;
      top: 0;
      height: 100%;
      content: attr(data-content); /* dynamic content for the pseudo element */
      overflow: hidden;
      pointer-events: none; /* so the base char is selectable by mouse */
      color: #000; /* for demo purposes */
      text-shadow: 2px 2px 0px #0af; /* for demo purposes */
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo



    -垂直1/3零件

    没有文字阴影:

    halfStyle - Vertical 1/3 Parts - No Text Shadow

    文本阴影的可能性为每1/3部分独立:

    halfStyle - Vertical 1/3 Parts - With Text Shadow

    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
        $el = $(el);
        text = $el.text();
        chars = text.split('');
    
        // Set the screen-reader text
        $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
        // Reset output for appending
        output = '';
    
        // Iterate over all chars in the text
        for (i = 0; i < chars.length; i++) {
            // Create a styled element for each character and append to container
            output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
        }
    
        // Write to DOM only once
        $el.append(output);
      });
    });
    
    .halfStyle { /* base char and also the right 1/3 */
        position: relative;
        display: inline-block;
        font-size: 80px; /* or any font size will work */
        color: transparent; /* hide the base character */
        overflow: hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
        color: #f0f; /* for demo purposes */
        text-shadow: 2px 2px 0px #0af; /* for demo purposes */
    }
    
    .halfStyle:before { /* creates the left 1/3 */
        display: block;
        z-index: 2;
        position: absolute;
        top: 0;
        width: 33.33%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #f00; /* for demo purposes */
        text-shadow: 2px -2px 0px #af0; /* for demo purposes */
    }
    
    .halfStyle:after { /* creates the middle 1/3 */
        display: block;
        z-index: 1;
        position: absolute;
        top: 0;
        width: 66.66%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #000; /* for demo purposes */
        text-shadow: 2px 2px 0px #af0; /* for demo purposes */
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo



    -水平1/3零件

    没有文字阴影:

    halfStyle - Horizontal 1/3 Parts - No Text Shadow

    文本阴影的可能性为每1/3部分独立:

    halfStyle - Horizontal 1/3 Parts - With Text Shadow

    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
        $el = $(el);
        text = $el.text();
        chars = text.split('');
    
        // Set the screen-reader text
        $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
        // Reset output for appending
        output = '';
    
        // Iterate over all chars in the text
        for (i = 0; i < chars.length; i++) {
            // Create a styled element for each character and append to container
            output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
        }
    
        // Write to DOM only once
        $el.append(output);
      });
    });
    
    .halfStyle { /* base char and also the bottom 1/3 */
      position: relative;
      display: inline-block;
      font-size: 80px; /* or any font size will work */
      color: transparent;
      overflow: hidden;
      white-space: pre; /* to preserve the spaces from collapsing */
      color: #f0f;
      text-shadow: 2px 2px 0px #0af; /* for demo purposes */
    }
    
    .halfStyle:before { /* creates the top 1/3 */
      display: block;
      z-index: 2;
      position: absolute;
      top: 0;
      height: 33.33%;
      content: attr(data-content); /* dynamic content for the pseudo element */
      overflow: hidden;
      pointer-events: none; /* so the base char is selectable by mouse */
      color: #f00; /* for demo purposes */
      text-shadow: 2px -2px 0px #fa0; /* for demo purposes */
    }
    
    .halfStyle:after { /* creates the middle 1/3 */
      display: block;
      position: absolute;
      z-index: 1;
      top: 0;
      height: 66.66%;
      content: attr(data-content); /* dynamic content for the pseudo element */
      overflow: hidden;
      pointer-events: none; /* so the base char is selectable by mouse */
      color: #000; /* for demo purposes */
      text-shadow: 2px 2px 0px #af0; /* for demo purposes */
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo



    -HalfStyle由@KevinGranger改进

    halfStyle - KevinGranger

    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
        $el = $(el);
        text = $el.text();
        chars = text.split('');
    
        // Set the screen-reader text
        $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
        // Reset output for appending
        output = '';
    
        // Iterate over all chars in the text
        for (i = 0; i < chars.length; i++) {
            // Create a styled element for each character and append to container
            output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
        }
    
        // Write to DOM only once
        $el.append(output);
      });
    });
    
    body {
        background-color: black;
    }
    
    .textToHalfStyle {
        display: block;
        margin: 200px 0 0 0;
        text-align: center;
    }
    
    .halfStyle {
        font-family: 'Libre Baskerville', serif;
        position: relative;
        display: inline-block;
        width: 1;
        font-size: 70px;
        color: black;
        overflow: hidden;
        white-space: pre;
        text-shadow: 1px 2px 0 white;
    }
    
    .halfStyle:before {
        display: block;
        z-index: 1;
        position: absolute;
        top: 0;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        color: white;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demo



    @SamTremaine

    -PelelingStyle对HalfStyle的改进

    halfStyle - SamTremaine

    // jQuery for automated mode
    jQuery(function($) {
        var text, chars, $el, i, output;
    
        // Iterate over all class occurences
        $('.textToHalfStyle').each(function(idx, el) {
        $el = $(el);
        text = $el.text();
        chars = text.split('');
    
        // Set the screen-reader text
        $el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
    
        // Reset output for appending
        output = '';
    
        // Iterate over all chars in the text
        for (i = 0; i < chars.length; i++) {
            // Create a styled element for each character and append to container
            output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
        }
    
        // Write to DOM only once
        $el.append(output);
      });
    });
    
    .halfStyle {
        position: relative;
        display: inline-block;
        font-size: 68px;
        color: rgba(0, 0, 0, 0.8);
        overflow: hidden;
        white-space: pre;
        transform: rotate(4deg);
        text-shadow: 2px 1px 3px rgba(0, 0, 0, 0.3);
    }
    
    .halfStyle:before { /* creates the left part */
        display: block;
        z-index: 1;
        position: absolute;
        top: -0.5px;
        left: -3px;
        width: 100%;
        content: attr(data-content);
        overflow: hidden;
        pointer-events: none;
        color: #FFF;
        transform: rotate(-4deg);
        text-shadow: 0px 0px 1px #000;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>Single Characters:</p>
    <span class="halfStyle" data-content="X">X</span>
    <span class="halfStyle" data-content="Y">Y</span>
    <span class="halfStyle" data-content="Z">Z</span>
    <span class="halfStyle" data-content="A">A</span>
    
    <hr/>
    <p>Automated:</p>
    
    <span class="textToHalfStyle">Half-style, please.</span>
    

    JSFiddle demosamtremaine.co.uk



    第4部分:准备 生产环境

    可以在同一页面上的所需元素上使用自定义的不同半风格样式集 . 您可以定义多个样式集并告诉插件使用哪个样式集 .

    该插件在目标 .textToHalfStyle 元素上使用数据属性 data-halfstyle="[-CustomClassName-]" ,并自动进行所有必要的更改 .

    因此,只需在包含文本的元素上添加 textToHalfStyle 类和数据属性 data-halfstyle="[-CustomClassName-]" . 该插件将完成剩下的工作 .

    halfStyle - Multiple on Same Page

    CSS样式集的类定义也与上面提到的 [-CustomClassName-] 部分匹配,并且链接到 .halfStyle ,所以我们将 .halfStyle.[-CustomClassName-]

    jQuery(function($) {
        var halfstyle_text, halfstyle_chars, $halfstyle_el, halfstyle_i, halfstyle_output, halfstyle_style;
    
        // Iterate over all class occurrences
        $('.textToHalfStyle').each(function(idx, halfstyle_el) {
            $halfstyle_el = $(halfstyle_el);
            halfstyle_style = $halfstyle_el.data('halfstyle') || 'hs-base';
            halfstyle_text = $halfstyle_el.text();
            halfstyle_chars = halfstyle_text.split('');
    
            // Set the screen-reader text
            $halfstyle_el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>');
    
            // Reset output for appending
            halfstyle_output = '';
    
            // Iterate over all chars in the text
            for (halfstyle_i = 0; halfstyle_i < halfstyle_chars.length; halfstyle_i++) {
                // Create a styled element for each character and append to container
                halfstyle_output += '<span aria-hidden="true" class="halfStyle ' + halfstyle_style + '" data-content="' + halfstyle_chars[halfstyle_i] + '">' + halfstyle_chars[halfstyle_i] + '</span>';
            }
    
            // Write to DOM only once
            $halfstyle_el.append(halfstyle_output);
        });
    });
    
    /* start half-style hs-base */
    
    .halfStyle.hs-base {
        position: relative;
        display: inline-block;
        font-size: 80px; /* or any font size will work */
        overflow: hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
        color: #000; /* for demo purposes */
    }
    
    .halfStyle.hs-base:before {
        display: block;
        z-index: 1;
        position: absolute;
        top: 0;
        width: 50%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        pointer-events: none; /* so the base char is selectable by mouse */
        overflow: hidden;
        color: #f00; /* for demo purposes */
    }
    
    /* end half-style hs-base */
    
    
    /* start half-style hs-horizontal-third */
    
    .halfStyle.hs-horizontal-third { /* base char and also the bottom 1/3 */
        position: relative;
        display: inline-block;
        font-size: 80px; /* or any font size will work */
        color: transparent;
        overflow: hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
        color: #f0f;
        text-shadow: 2px 2px 0px #0af; /* for demo purposes */
    }
    
    .halfStyle.hs-horizontal-third:before { /* creates the top 1/3 */
        display: block;
        z-index: 2;
        position: absolute;
        top: 0;
        height: 33.33%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #f00; /* for demo purposes */
        text-shadow: 2px -2px 0px #fa0; /* for demo purposes */
    }
    
    .halfStyle.hs-horizontal-third:after { /* creates the middle 1/3 */
        display: block;
        position: absolute;
        z-index: 1;
        top: 0;
        height: 66.66%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow: hidden;
        pointer-events: none; /* so the base char is selectable by mouse */
        color: #000; /* for demo purposes */
        text-shadow: 2px 2px 0px #af0; /* for demo purposes */
    }
    
    /* end half-style hs-horizontal-third */
    
    
    /* start half-style hs-PeelingStyle, by user SamTremaine on Stackoverflow.com */
    
    .halfStyle.hs-PeelingStyle {
      position: relative;
      display: inline-block;
      font-size: 68px;
      color: rgba(0, 0, 0, 0.8);
      overflow: hidden;
      white-space: pre;
      transform: rotate(4deg);
      text-shadow: 2px 1px 3px rgba(0, 0, 0, 0.3);
    }
    
    .halfStyle.hs-PeelingStyle:before { /* creates the left part */
      display: block;
      z-index: 1;
      position: absolute;
      top: -0.5px;
      left: -3px;
      width: 100%;
      content: attr(data-content);
      overflow: hidden;
      pointer-events: none;
      color: #FFF;
      transform: rotate(-4deg);
      text-shadow: 0px 0px 1px #000;
    }
    
    /* end half-style hs-PeelingStyle */
    
    
    /* start half-style hs-KevinGranger, by user KevinGranger on StackOverflow.com*/
    
    .textToHalfStyle.hs-KevinGranger {
      display: block;
      margin: 200px 0 0 0;
      text-align: center;
    }
    
    .halfStyle.hs-KevinGranger {
      font-family: 'Libre Baskerville', serif;
      position: relative;
      display: inline-block;
      width: 1;
      font-size: 70px;
      color: black;
      overflow: hidden;
      white-space: pre;
      text-shadow: 1px 2px 0 white;
    }
    
    .halfStyle.hs-KevinGranger:before {
      display: block;
      z-index: 1;
      position: absolute;
      top: 0;
      width: 50%;
      content: attr(data-content); /* dynamic content for the pseudo element */
      overflow: hidden;
      color: white;
    }
    
    /* end half-style hs-KevinGranger
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p>
        <span class="textToHalfStyle" data-halfstyle="hs-base">Half-style, please.</span>
    </p>
    <p>
        <span class="textToHalfStyle" data-halfstyle="hs-horizontal-third">Half-style, please.</span>
    </p>
    <p>
        <span class="textToHalfStyle" data-halfstyle="hs-PeelingStyle">Half-style, please.</span>
    </p>
    <p style="background-color:#000;">
        <span class="textToHalfStyle" data-halfstyle="hs-KevinGranger">Half-style, please.</span>
    </p>
    

    JSFiddle demo

  • 56

    我最近可以得到:

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

    演示:http://jsfiddle.net/9wxfY/2/

    这是一个只使用一个 Span 的版本:http://jsfiddle.net/9wxfY/4/

  • 41

    编辑(2017年10月):现在每个主要浏览器都支持background-clip或者背景图像选项:CanIUse

    是的,你可以只用一个字符,只用CSS .

    但是,仅限Webkit(和Chrome):

    http://jsbin.com/rexoyice/1/

    h1 {
      display: inline-block;
      margin: 0; /* for demo snippet */
      line-height: 1em; /* for demo snippet */
      font-family: helvetica, arial, sans-serif;
      font-weight: bold;
      font-size: 300px;
      background: linear-gradient(to right, #7db9e8 50%,#1e5799 50%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    
    <h1>X</h1>
    

    在视觉上,所有使用两个字符的示例(通过JS,CSS伪元素或只是HTML)看起来很好,但请注意,所有这些都会向DOM添加可能导致可访问性的内容 - 以及文本选择/剪切/粘贴问题 .

  • 28

    Limited CSS and jQuery Solution

    我不确定这个解决方案有多优雅,但它将所有内容减少到一半:http://jsfiddle.net/9wxfY/11/

    否则,我已经为您创建了一个很好的解决方案......您需要做的就是为您的HTML提供:

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

    至于CSS,它非常有限......你只需要将它应用到 :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>
    
  • 7

    Example


    JSFiddle DEMO

    我们只使用CSS伪选择器来做到这一点!

    此技术适用于动态生成的内容和不同的字体大小和宽度 .

    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;
    }
    

    要包装动态生成的字符串,可以使用如下函数:

    // 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);
    }
    
  • 90

    您可以使用以下代码 . 在这个例子中,我使用了 h1 标签并添加了一个属性 data-title-text="Display Text" ,它将在 h1 标签文本元素上显示不同的颜色文本,这将生成半色调文本,如下例所示

    body {
      text-align: center;
      margin: 0;
    }
    
    h1 {
      color: #111;
      font-family: arial;
      position: relative;
      font-family: 'Oswald', sans-serif;
      display: inline-block;
      font-size: 2.5em;
    }
    
    h1::after {
      content: attr(data-title-text);
      color: #e5554e;
      position: absolute;
      left: 0;
      top: 0;
      clip: rect(0, 1000px, 30px, 0);
    }
    
    <h1 data-title-text="Display Text">Display Text</h1>
    
  • 65

    这是一个丑陋的画布实现 . 我尝试了这个解决方案,但结果比我预期的要糟糕,所以无论如何它都在这里 .

    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>
    
  • 143

    如果您愿意,也可以使用SVG执行此操作:

    var title = document.querySelector('h1'),
        text = title.innerHTML,
        svgTemplate = document.querySelector('svg'),
        charStyle = svgTemplate.querySelector('#text');
    
    svgTemplate.style.display = 'block';
    
    var space = 0;
    
    for (var i = 0; i < text.length; i++) {
      var x = charStyle.cloneNode();
      x.textContent = text[i];
      svgTemplate.appendChild(x);
      x.setAttribute('x', space);
      space += x.clientWidth || 15;
    }
    
    title.innerHTML = '';
    title.appendChild(svgTemplate);
    
    <svg style="display: none; height: 100px; width: 100%" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
        <defs id="FooDefs">
            <linearGradient id="MyGradient" x1="0%" y1="0%" x2="100%" y2="0%">
                <stop offset="50%" stop-color="blue" />
                <stop offset="50%" stop-color="red" />
            </linearGradient>
        </defs>
        <text y="50%" id="text" style="font-size: 72px; fill: url(#MyGradient)"></text>
    </svg>
    
    <h1>This is not a solution X</h1>
    

    http://codepen.io/nicbell/pen/jGcbq

  • 14
    .halfStyle {
        position:relative;
        display:inline-block;
        font-size:68px; /* or any font size will work */
        color: rgba(0,0,0,0.8); /* or transparent, any color */
        overflow:hidden;
        white-space: pre; /* to preserve the spaces from collapsing */
        transform:rotate(4deg);
        -webkit-transform:rotate(4deg);
        text-shadow:2px 1px 3px rgba(0,0,0,0.3);
    }
    .halfStyle:before {
        display:block;
        z-index:1;
        position:absolute;
        top:-0.5px;
        left:-3px;
        width: 100%;
        content: attr(data-content); /* dynamic content for the pseudo element */
        overflow:hidden;
        color: white;
        transform:rotate(-4deg);
        -webkit-transform:rotate(-4deg);
        text-shadow:0 0 1px black;
    
    }
    

    http://experimental.samtremaine.co.uk/half-style/

    你可以将这段代码撬开来做各种有趣的事情 - 这只是我的同事和我昨晚想出来的一个实现 .

  • 18

    如此短的文字怎么样?

    如果您使用循环执行某些操作,使用JavaScript重复这些字符,它甚至可以用于更长的文本 . 无论如何,结果是这样的:

    p.char {
      position: relative;
      display: inline-block;
      font-size: 60px;
      color: red;
    }
    
    p.char:before {
      position: absolute;
      content: attr(char);
      width: 50%;
      overflow: hidden;
      color: black;
    }
    
    <p class="char" char="S">S</p>
    <p class="char" char="t">t</p>
    <p class="char" char="a">a</p>
    <p class="char" char="c">c</p>
    <p class="char" char="k">k</p>
    <p class="char" char="o">o</p>
    <p class="char" char="v">v</p>
    <p class="char" char="e">e</p>
    <p class="char" char="r">r</p>
    <p class="char" char="f">f</p>
    <p class="char" char="l">l</p>
    <p class="char" char="o">o</p>
    <p class="char" char="w">w</p>
    
  • 206

    一个很好的WebKit解决方案,利用 background-clip: text 支持: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;
    }
    
  • 15

    另一个仅限CSS的解决方案(如果您不想编写特定于字母的CSS,则需要数据属性) . 这个更全面的工作(测试IE 9/10,Chrome最新和FF最新)

    span {
      position: relative;
      color: rgba(50,50,200,0.5);
    }
    
    span:before {
      content: attr(data-char);
      position: absolute;
      width: 50%;
      overflow: hidden;
      color: rgb(50,50,200);
    }
    
    <span data-char="X">X</span>
    
  • 23

    它可能是无关紧要的,也许不是,但不久前,我创建了一个jQuery函数,它可以执行相同的操作,但是是横向的 .

    我叫它"Strippex" For 'stripe' 'text',demo:http://cdpn.io/FcIBg

    我不是说这是任何问题的解决方案,但我已经尝试将css应用于一半的角色,但横向,所以这个想法是一样的,实现可能是可怕的,但它的确有效 .

    啊,最重要的是,我很开心创造它!

    enter image description here

相关问题