尝试使用倾斜的div为背景图像提供两种不同的色调

时间:2017-07-03 06:53:47

标签: html css web frontend

我是学习HTML和CSS的新手,我正在尝试用倾斜的div给出背景图像两个倾斜的色调。我无法理解如何做到这一点,以便倾斜只在图像的中间而不在边缘。这是我到目前为止所尝试的内容。



.tinted-image {
  
  width: 100vw;
  height: 100vh;
  
  background: url(https://static.pexels.com/photos/175941/pexels-photo-175941.jpeg);
  background-size: cover;
  filter: grayscale(100%);
  display: flex;
  overflow: hidden;
}

#one{
  background: linear-gradient(rgba(0,0,0,0.85), rgba(0,0,0, 0.85));
  tranform-origin: bottom-left;
  transform: skewX(10deg);
/*   margin-left: 1em; */
}
#two{
  background: linear-gradient(rgba(255, 255, 255, .45), rgba(255, 255, 255, .45));
  transform: skewX(10deg);
}
#one, #two{
  height: 100vh;
  width: 50vw;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family:'Nova Mono', monospace;
  font-size: 5rem;
}

#one h1 {
  color: #eee;
  margin-left: auto;
  margin-right: 1rem;
}

#two h1 {
  color: #222;
  margin-right: auto;
  margin-left: 1rem;
}
 

<main class="tinted-image">
<link href="https://fonts.googleapis.com/css?family=Nova+Mono" rel="stylesheet">
<div id = "one"><h1>WEB</h1></div>
<div id = "two"><h1>DEV</h1></div>
</main>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我可以通过使用左侧margin-left: -200px;上的negative margin和带有相同金额padding-left: 200px;的正左侧填充来抵消它,从而获得所需的效果。

我将这两个添加到您的选择器#one旁边overflow: hidden;tidy things up

工作示例:

&#13;
&#13;
.tinted-image {
  width: 100vw;
  height: 100vh;
  background: url(https://static.pexels.com/photos/175941/pexels-photo-175941.jpeg);
  background-size: cover;
  filter: grayscale(100%);
  display: flex;
  overflow: hidden;
}

#one {
  background: linear-gradient(rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0.85));
  tranform-origin: bottom-left;
  margin-left: -200px;
  padding-left: 200px;
  overflow: hidden;
  transform: skewX(10deg);
  /*   margin-left: 1em; */
}

#two {
  background: linear-gradient(rgba(255, 255, 255, .45), rgba(255, 255, 255, .45));
  transform: skewX(10deg);
}

#one,
#two {
  height: 100vh;
  width: 50vw;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Nova Mono', monospace;
  font-size: 5rem;
}

#one h1 {
  color: #eee;
  margin-left: auto;
  margin-right: 1rem;
}

#two h1 {
  color: #222;
  margin-right: auto;
  margin-left: 1rem;
}
&#13;
<main class="tinted-image">
  <link href="https://fonts.googleapis.com/css?family=Nova+Mono" rel="stylesheet">
  <div id="one">
    <h1>WEB</h1>
  </div>
  <div id="two">
    <h1>DEV</h1>
  </div>
</main>
&#13;
&#13;
&#13;