Html 带有倾斜边界的相邻div?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10568334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:23:23  来源:igfitidea点击:

Adjacent divs with angled borders?

csshtmlcss-shapes

提问by Mark

I want to create two divs which are floated left to each other, however with a slanted angled border separating them. I've attached a picture to demonstrate what I mean.

我想创建两个彼此向左浮动的 div,但是用倾斜的有角度的边框将它们分开。我附上了一张图片来说明我的意思。

Does anyone know if something like this is possible with CSS (cutting off the content with an overflow:hidden I guess)

有谁知道 CSS 是否可以实现这样的事情(用溢出切断内容:我猜是隐藏的)

adjacent div with slanted side

带有倾斜边的相邻 div

These divs need to contain images that get cut off by the border, here is an example :

这些 div 需要包含被边框截断的图像,这是一个示例:

divs with images and slanted adjacent sides

带有图像和倾斜相邻边的 div

回答by Zoltan Toth

Try this

尝试这个

.left, .right {
  position: relative;
  height: 100px;
  width: 200px;
  background: #000;
  float: left;
}

.left:after {
  content: '';
  line-height: 0;
  font-size: 0;
  width: 0;
  height: 0;
  border-top: 100px solid #000;
  border-bottom: 50px solid transparent;
  border-left: 0px solid transparent;
  border-right: 50px solid transparent;
  position: absolute;
  top: 0;
  right: -50px;
}

.right {
  margin-left: 60px;
  width: 100px;
}

.right:before {
  content: '';
  line-height: 0;
  font-size: 0;
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 100px solid #000;
  border-left: 50px solid transparent;
  border-right: 0px solid #000;
  position: absolute;
  top: -50px;
  left: -50px;
}
<div class="left"> </div>
<div class="right"> </div>



UPDATEwith images

更新图像

.left, .right {
    background: #000 url('http://lorempixel.com/300/100');
    position: relative;
    height: 100px;
    width: 250px;
    float: left;
}

.left:after {
    content: '';
    line-height: 0;
    font-size: 0;
    width: 0;
    height: 0;
    border-top: 50px solid transparent;
    border-bottom: 100px solid #fff;
    border-left: 30px solid transparent;
    border-right: 0 solid #fff;
    position: absolute;
    top: -50px;
    right: 0;
}

.right {
    background: #000 url('http://lorempixel.com/200/100');
    width: 150px;
}

.right:before {
    content: '';
    line-height: 0;
    font-size: 0;
    width: 0;
    height: 0;
    border-top: 100px solid #fff;
    border-bottom: 50px solid transparent;
    border-left: 0px solid transparent;
    border-right: 30px solid transparent;
    position: absolute;
    top: 0;
    left: 0;
}
<div class="left"> </div>
<div class="right"> </div>

回答by Jeff Lupinski

All of the solutions so far depend on having a really thick angled border to divide the photos.

到目前为止,所有的解决方案都依赖于有一个非常厚的有角度的边界来分割照片。

To avoid this you'd make a container and skew it. Then counter skew the image in the opposite direction.

为了避免这种情况,您可以制作一个容器并将其倾斜。然后反向倾斜图像。

Here's a CodePen http://cdpn.io/azvsA, but the gist of it is as follows:

这是一个 CodePen http://cdpn.io/azvsA,但它的要点如下:

.container {
  border-right: 10px solid white;
  overflow: hidden;
  transform (skewX(-20deg));
}

.image {
  transform (skewX(20deg));
}

回答by sandeep

You can write like this:

你可以这样写:

.left, .right {
    background: #000 url('http://lorempixel.com/300/100');
    position: relative;
    height: 100px;
    width: 250px;
    float: left;
}
.left{
    z-index:1;
}
.parent{
    overflow:hidden;
}

.right {
    background: #000 url('http://lorempixel.com/200/100');
    width: 150px;
}
.left:after{
    content:'';
    position:absolute;
    border-right:20px solid #fff;
    top:-25px;
    bottom:-10px;
    left:0;
    right:-10px;
    -moz-transform:rotate(10deg);
    -webkit-transform:rotate(10deg);
}

Check this http://jsfiddle.net/EJxFg/4/

检查这个http://jsfiddle.net/EJxFg/4/