【问题标题】:Diagonal line with fixed angle with curves in css具有固定角度的对角线与css中的曲线
【发布时间】:2016-11-17 22:51:28
【问题描述】:

你会如何画一条对角线,它总是有 11° 的角度,两端都有固定的圆角。它将用作图像的叠加层,例如以下示例:

灰色区域应在顶部,蓝色区域为图像。

理想情况下,可以通过纯 CSS 或 data:image/svg+xml 作为背景图像来设置样式。

可能类似于在图像顶部使用::after 选择器。或者甚至可能是内容 div 上的 ::before 选择器将被添加到其中。例如类似于:

<div class="card">
  <div class="header">
    <img src="#" />
  </div>
  <div class="content">
   <h1>Title</h1>
   <p>Body copy</p>
  </div>
</div>

<style>
  .content:before {
    content: '';
    display: block;
    width: 100%;
  }
</style>

【问题讨论】:

  • 那么您建议的解决方案有什么问题?
  • 如果你可以使用 SVG 就好了。但是使用 CSS + HTML 这将很难实现,因为您需要倾斜变换,并且您会遇到文本等问题。您是否尝试在图层上放置图像?

标签: html css


【解决方案1】:

您可以部署::before::after 伪元素。

每个伪元素使用以下组合:

  • absolute positioning
  • border
  • border-radius
  • box-shadow
  • transform: skewY:

div {
display:inline-block;
position: relative;
width: 256px;
height: 140px;
margin-right:32px;
background: url('/my-image.jpg') no-repeat rgb(15,150,196);
overflow: hidden;
}

div::before, div::after {
content: '';
position: absolute;
left: 0;
display: block;
width: calc(100% - 12px);
height: 100%;
border-width: 3px 6px;
border-style: solid;
border-color: rgb(178,178,178);
border-radius: 9px;
box-shadow: 36px -36px 0 36px rgb(178,178,178), -2px 2px 0 2px rgb(178,178,178);
transform: skewY(11deg);
}

div::before {
bottom: 50%;
}


div::after {
top: 50%;
box-shadow: -36px 36px 0 36px rgb(178,178,178), 2px -2px 0 2px rgb(178,178,178);
}

.with-image {
background: url('https://images.pexels.com/photos/115045/pexels-photo-115045.jpeg?w=940&h=650&auto=compress&cs=tinysrgb') no-repeat 0 0 / 256px 140px rgb(15,150,196);
}

.with-image::before, .with-image::after {
border-color: rgb(0,127,0);
box-shadow: 36px -36px 0 36px rgb(0,127,0), -2px 2px 0 2px rgb(0,127,0);
}

.with-image::after {
box-shadow: -36px 36px 0 36px rgb(0,127,0), 2px -2px 0 2px rgb(0,127,0);
}
<div></div>
<div class="with-image"></div>

【讨论】:

  • 看起来不错。干得好
【解决方案2】:

我认为这段代码将帮助您实现您想要做的事情。

.container{
  width:300px; 
  margin-top:30px;
}
.imagecontainer{
  width:100%; 
  height:200px;
  background:blue;
  transform: skew(0deg, 11deg);
  border-radius:10px;
  margin-bottom:4px;
}
<div class="container">
  <div class="imagecontainer"></div>
  <div class="imagecontainer"></div>
  <div class="imagecontainer"></div>
</div>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多