Is there an easy way to contrast text over an image using css?

夙愿已清 提交于 2021-02-17 02:28:10

问题


I am trying to create this effect through code:

Right now I am duplicating the text on top,'masking' it, and setting it to white, but It's becoming pretty complex and hard to control/position consistently. I'm wondering if there's a simple way to do this with a single text element and css (mix-blend-mode, filter, etc...)

I saw this on css-tricks, but it's not exactly what I want. I need the text over the image to be completely white, and the text outside to be a different color. https://css-tricks.com/methods-contrasting-text-backgrounds/

My codepen: https://codepen.io/aalokt89/pen/eamrJM

HTML:

<div class="container">
    <div class="image-block">
        <div class="intro-heading">
            <h1 class="intro-heading">Professional Makeup Artist & Hair Stylist</h1>
        </div>
    </div>
</div>

CSS:

.container {
  display: grid;
  grid-template-columns: repeat(4, 100px) 400px;
  grid-template-rows: repeat(4, 100px)
}

.image-block {
  grid-column: 1 / 5;
  grid-row: 1 / 5;
  background: url('https://images.unsplash.com/photo-1542103749-8ef59b94f47e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80')100%/ cover;
}

.intro-heading {
  background: inherit;
  height: inherit;
  width: 600px;
  -webkit-background-clip: text;
  background-clip: text;
  filter: invert(1) grayscale(1) contrast(9);
  color: transparent;

  /*   grid-column: 2 / 6;
  grid-row: 1 / 5; */
  display: flex;
  align-items: center;
}

回答1:


For this particular case and since the width of the image is fixed, you can consider a simple gradient coloration for the text:

.container {
  display: grid;
  grid-template-columns: repeat(4, 100px) 400px;
  grid-template-rows: repeat(4, 100px)
}

.image-block {
  grid-column: 1 / 5;
  grid-row: 1 / 5;
  background: url('https://images.unsplash.com/photo-1542103749-8ef59b94f47e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80')100%/ cover;
}

.intro-heading {
   background: linear-gradient(to right,#fff 400px,#000 0);
  width: 600px;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  display: flex;
  align-items: center;
}
<div class="container">
    <div class="image-block">
        <div class="intro-heading">
            <h1 class="intro-heading">Professional Makeup Artist & Hair Stylist</h1>
        </div>
    </div>
</div>

Similar questions:

Change text color to white on any non-white background

Text blended over background color

Change text color black to white on overlap of bg



来源:https://stackoverflow.com/questions/56013514/is-there-an-easy-way-to-contrast-text-over-an-image-using-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!