[CSS] Raise the Curtains


Raise the Curtains


転がる背景が暗から光に変わり、上の内容もsticky positionの場合、光から暗に変わる効果です.

HTMLとCSSのみで実現します.

HTML

<div class="curtain">
  <div class="invert">
    <h2>Curtain Effect</h2>
  </div>
</div>

Set up some CSS variables


css変数を作成し、必要に応じて簡単に変更できるように設定します.
:root {
  --minh: 98vh;
  --color1: wheat;
  --color2: midnightblue;
}

.Curtain Draw

  • A linear-gradient for the “split” background
  • min-height for the extra space at the bottom of the container
  • 仮想要素(pseudo-element)を使用して、下部に余分なスペースを作成します.
    .curtain {
      /** create the "split" background **/
      background-image: linear-gradient(to bottom, 
      					var(--color2) 50%, var(--color1) 50%);
    }
    
    /** add extra space to the bottom
    (need this for the "sticky" effect) **/
    .curtain::after {
      content: "";
      display: block;
      min-height: var(--minh);
    }

    Making sticky content

  • position: sticky and top define the stickiness and where it sticks.
  • mix-blend-mode: difference blends the color of the content inside the h2 element into the .curtain‘s background gradient.
  • display: flex centers the content for presentation.
  • min-height defines the height of the container and allows for the extra space at the bottom.
  • color sets the color of the h2 heading.
  • .invert {
      /** make the content sticky **/
      position: sticky;
      top: 20px;
    
      /** blend the content with the contrast effect **/
      mix-blend-mode: difference;
    
      /** center the content **/
      display: flex;
      align-items: center;
      justify-content: center;
      
      /** set the minimum height of the section **/
      min-height: var(--minh);
    }
    
    h2 {
      /** set the color of the text **/
      color: var(--color1);
    }

    mix-blend-mode: difference ?


    これはCSSを用いてテキスト色を簡単に反転させる方法である.
    値をdifference(difference)に設定すると、下敷き要素とは反対の色を表示できます.

    コメントサイト

  • https://studiomeal.com/archives/888(CSSテキスト反転効果)
  • https://studiomeal.com/archives/852(CSSを使用して白黒画像を作成)
  • “Raise the Curtains” Demo





    REFERENCE