Я пытаюсь добиться такого эффекта:
А по мере того, как экран уменьшается в размерах, и все больше букв моего Н1 начинают перекрывать изображение, хотелось бы, чтобы они сменили цвет на белый. В конце концов, когда экран достаточно мал, текст может просто находиться внутри контейнера с фоновым изображением.
Вот код, который у меня есть до сих пор:
.container {
max-width:1350px;
margin:0 auto;
background-image: url(https://houniqueconstruction.com/wp-content/uploads/2023/02/kam-idris-_HqHX3LBN18-unsplash-scaled.jpg);
background-position: bottom left;
background-repeat: no-repeat;
background-size: cover;
padding-top:15em;
padding-bottom:15em;
position:relative;
}
.overlay {
background-color: transparent;
background-image: linear-gradient(90deg, #FFFFFF 30%, #F2295B00 0%);
opacity: 1;
transition: background 0.3s, border-radius 0.3s, opacity 0.3s;
height: 100%;
width: 100%;
top: 0;
left: 0;
position: absolute;
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
h1 {
font-size:60px;
letter-spacing:9px;
text-transform:uppercase;
}
.custom-cta {
display:block;
max-width:100px;
margin-top:10px;
text-align:center;
background:gold;
padding:20px 40px;
}
<div class = "container">
<div class = "overlay">
<div class = "text-box">
<h1>Complete </br>Remodeli<span style = "color:white;">ng</span></h1>
<p style = "max-width:300px;">With 30 years of experience and a track record of successful projects, we have the skills and expertise to remodel your house with precision, efficiency, and minimal stress for you.</p>
<a class = "custom-cta">Contact Us</a>
</div>
</div>
</div>
Используйте «mix-blend-mode:difference», как в статье css-tricks.com/methods-contrasting-text-backgrounds?
Я бы решил это, создав слои. Подумайте о том, чтобы иметь 2 слоя:
Теперь хитрость заключается в том, чтобы тексты обоих текстов идеально перекрывались. Используйте CSS Grid для создания макета и размещения текста и изображения там, где они вам нужны. С некоторой творческой обрезкой (overflow: hidden) и порядком слоев (z-index) вы можете контролировать, где заканчивается черный текст, а где продолжается белый.
Это создаст иллюзию изменения цвета в зависимости от размера экрана.
*, *::before, *::after {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
}
.container {
display: grid;
max-width: 1350px;
width: 100vw;
height: 100vh;
}
.layer {
grid-area: 1 / 1;
display: grid;
grid-template-columns: 30vw 70vw;
}
:is(.layer--front, .layer--back) .layer__title {
display: block;
font-weight: 700;
font-size: 60px;
letter-spacing: 9px;
text-transform: uppercase;
margin: 0 0 1rem;
}
.layer--front {
z-index: 2;
}
.layer--front .layer__title {
color: black;
}
.layer--back .layer__title {
color: white;
}
.layer__content {
grid-area: 1 / 1;
padding: 6rem 2rem;
z-index: 1;
}
.layer--front .layer__content {
overflow: hidden;
}
.layer__image {
grid-area: 1 / 2;
position: relative;
}
.layer__image img {
position: absolute;
inset: 0;
height: 100%;
width: 100%;
padding: 1rem 1rem 1rem 0;
object-fit: cover;
object-position: left;
}
.custom-cta {
display: block;
margin-top: 10px;
text-align: center;
background: gold;
padding: 10px 20px;
}
<div class = "container">
<div class = "layer layer--front">
<div class = "layer__content">
<h1 class = "layer__title">Complete <br>Remodeling</h1>
<p style = "max-width: 300px;">With 30 years of experience and a track record of successful projects, we have the skills and expertise to remodel your house with precision, efficiency, and minimal stress for you.</p>
<a class = "custom-cta">Contact Us</a>
</div>
</div>
<div class = "layer layer--back">
<div class = "layer__content">
<span class = "layer__title" aria-hidden = "true">Complete <br>Remodeling</span>
</div>
<div class = "layer__image">
<img src = "https://houniqueconstruction.com/wp-content/uploads/2023/02/kam-idris-_HqHX3LBN18-unsplash-scaled.jpg" alt = "" />
</div>
</div>
</div>
Обязательно посмотрите пример в полностраничном режиме.
Вау, это фантастическое решение! Спасибо большое, очень красиво и креативно. Я предполагал, что мне нужно будет использовать JS
Вот подход с использованием flexbox, трех перекрывающихся контейнеров и backdrop-filter: invert(100%).
По сути, решение состоит в том, чтобы создать три перекрывающихся контейнера (используя z-index), чтобы поместить один поверх другого.
Изображение идет как фоновое изображение в нижнем контейнере. Затем изображение инвертируется с помощью backdrop-filter: invert(100%) дважды, чтобы избежать негатива. Однако, когда текст скользит поверх изображения, он инвертируется только один раз, создавая требуемый негативный эффект скольжения.
Эффект лучше всего виден в скрипке решения ниже, так как вертикальную полосу можно перетаскивать влево или вправо, чтобы увидеть эффект скольжения.
Желтая кнопка меняется на синюю при скольжении по изображению, но я уверен, что это не критично.
:root {
--image-height: 500px;
--image-width: 600px;
--top-offset: 350px;
--sidebar-width: 100px;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
}
h1 {
margin-top: 50px;
font-size: 60px;
letter-spacing: 9px;
text-transform: uppercase;
}
p {
max-width: 300px;
}
.text-container {
margin-left: 20px;
}
.container {
display: flex;
align-items: stretch;
height: var(--image-height);
position: relative;
}
.sidebar {
flex: 1 1 var(--sidebar-width);
height: var(--image-height);
}
.image {
flex: 0 0 var(--image-width);
height: var(--image-height);
}
.container-under {
top: calc(0px - var(--top-offset));
z-index: -2;
}
.image-under {
background-image: url("https://houniqueconstruction.com/wp-content/uploads/2023/02/kam-idris-_HqHX3LBN18-unsplash-scaled.jpg");
background-size: cover;
}
.container-middle {
top: calc(0px - calc(var(--top-offset) + var(--image-height)));
z-index: -1;
}
.image-middle {
background-color: transparent;
backdrop-filter: invert(100%);
}
.container-over {
top: calc(0px - calc(var(--top-offset) + var(--image-height) * 2));
}
.image-over {
background-color: transparent;
backdrop-filter: invert(100%);
}
.custom-cta {
display: block;
margin-top: 10px;
background: gold;
padding: 10px 20px;
width: 150px;
}
<div class = "text-container">
<h1>Complete<br/>Remodeling</h1>
<p>With 30 years of experience and a track record of successful projects, we have the skills and expertise to remodel your house with precision, efficiency, and minimal stress for you.</p>
<a class = "custom-cta">Contact Us</a>
</div>
<div class = "container container-under">
<div class = "sidebar"></div>
<div class = "image image-under"></div>
</div>
<div class = "container container-middle">
<div class = "sidebar"></div>
<div class = "image image-middle"></div>
</div>
<div class = "container container-over">
<div class = "sidebar"></div>
<div class = "image image-over"></div>
</div>
Вы действительно хотите белый текст на почти белом фоне (стена на вашем изображении)?