Search code examples
cssasp.nethtml2canvas

css- swap order of 2 words


I could really use some css help. Is it possible to swap the order of 2 words using css, WITHOUT using :after? (ex "Hello World" should become "World Hello")

The specific reason I cannot use :after which does in fact work, is that the div snapshot utility html2canvas.js does not take :before or :after into account, hence the workaround.

any help is very much appreciated.

Niko


Solution

  • You can stack the sentences with CSS z-index and using keyframes to make an effect of swapping. (see code-snippet).

    After that you need to decide how/when to call for the change, through buttonclick or time. For calling you probably need some javascript.

    If your are looking for a solution that really replaces the text in the HTML div, then you need solve that through javascript.

    .div-1 {
      position: absolute;
      z-index: 1;
      background-color: white;
      width: 100px;
      height: 20px;
      margin: 0px 0px 0px 20px;
    }
    
    .div-2 {
      position: relative;
      z-index: 2;
      background-color: white;
      width: 100px;
      height: 20px;
      margin: 0px 0px 0px 20px;
      animation-name: swap;
      animation-duration: 5s;
      animation-delay: 0;
    }
    
    @keyframes swap {
      from {
        opacity: 1;
      }
      to {
        opacity: 0;
      }
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <link rel="stylesheet" href="index.css">
    </head>
    <body>
    
    <div class="wrapper">
    
    <div class="content">
        <div class="div-1">Hello World</div>
        <div class="div-2">World Hello</div>
    </div>
    
    </div>
    
    </body>
    </html>