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

后端 未结 19 1594
轻奢々
轻奢々 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:17

    How about something like this for shorter text?

    It could even work for longer text if you did something with a loop, repeating the characters with JavaScript. Anyway, the result is something like this:

    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>

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

    enter image description here


    I've just finished developing the plugin and it is available for everyone to use! Hope you will enjoy it.

    View Project on GitHub - View Project Website. (so you can see all the split styles)

    Usage

    First of all, make sure you have the jQuery library is included. The best way to get the latest jQuery version is to update your head tag with:

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

    After downloading the files, make sure you include them in your project:

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

    Markup

    All you have to do is to asign the class splitchar , followed by the desired style to the element wrapping your text. e.g

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

    After all this is done, just make sure you call the jQuery function in your document ready file like this:

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

    Customizing

    In order to make the text look exactly as you want it to, all you have to do is apply your design like this:

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


    That's it! Now you have the Splitchar plugin all set. More info about it at http://razvanbalosin.com/Splitchar.js/.

    0 讨论(0)
  • 2020-11-22 17:23
    .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/

    You can crowbar this code into doing all sorts of interesting things - this is just one implementation my associate and I came up with last night.

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

    This can be achieved with just CSS :before selector and 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

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

    FWIW, here's my take on this doing it only with CSS: http://codepen.io/ricardozea/pen/uFbts/

    Several notes:

    • The main reason I did this was to test myself and see if I was able to accomplish styling half of a character while actually providing a meaningful answer to the OP.

    • I am aware that this is not an ideal or the most scalable solution and the solutions proposed by the people here are far better for "real world" scenarios.

    • The CSS code I created is based on the first thoughts that came to my mind and my own personal approach to the problem.

    • My solution only works on symmetrical characters, like X, A, O, M. **It does not work on asymmetric characters like B, C, F, K or lower case letters.

    • ** HOWEVER, this approach creates very interesting 'shapes' with asymmetric characters. Try changing the X to a K or to a lower case letter like an h or a p in the CSS :)

    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);
      }
    }
    
    0 讨论(0)
  • 2020-11-22 17:25

    Edit (oct 2017): background-clip or rather background-image options are now supported by every major browser: CanIUse

    Yes, you can do this with only one character and only CSS.

    Webkit (and Chrome) only, though:

    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>

    Visually, all the examples that use two characters (be it via JS, CSS pseudo elements, or just HTML) look fine, but note that that all adds content to the DOM which may cause accessibility--as well as text selection/cut/paste issues.

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