带有边框的一侧偏斜背景图像 [英] Skew Background Image on one side with a border

查看:54
本文介绍了带有边框的一侧偏斜背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试倾斜具有边框的一侧的背景图像。

I am trying to skew a background image on one side that has a border.

我基本上试图创建的布局如下:

The layout I am basically trying to create is as follows:

这是我制作的一个快速JSFiddle当前设置:

Here is a quick JSFiddle I made with the current setup:

https:// jsfiddle.net/silvawebdesigns/b2ae0k69/11/

#about-gallery {
  margin: 60px 0;
  display: -ms-flex;
  display: -webkit-flex;
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 500px;
  width: 100%;
}

.about-img-wrapper {
  position: relative;
  width: 100%;
  display: -ms-flex;
  display: -webkit-flex;
  display: flex;
  flex-wrap: wrap;
  margin: 5px;
}

#about-gallery .about-bg {
  border: solid 10px black;
  float: left;
  width: 100%;
  height: 300px;
  background-size: cover;
  background-position: center;
}

#about-gallery .image-1 {
  width: -moz-calc(55% - 10px);
  width: -webkit-calc(55% - 10px);
  width: calc(55% - 10px);
}

#about-gallery .image-2 {
  width: -moz-calc(45% - 10px);
  width: -webkit-calc(45% - 10px);
  width: calc(45% - 10px);
}

#about-gallery .image-4,
#about-gallery .image-5 {
  width: -moz-calc(50% - 10px);
  width: -webkit-calc(50% - 10px);
  width: calc(50% - 10px);
}

<div id="about-gallery">
  <div class="about-img-wrapper img-about image-1">
    <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
  </div>
  <div class="about-img-wrapper img-about image-2">
    <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
  </div>
  <div class="about-img-wrapper img-about image-3">
    <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
  </div>
  <div class="about-img-wrapper img-about image-4">
    <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
  </div>
  <div class="about-img-wrapper img-about image-5">
    <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
  </div>

</div>

我已经尝试结合使用正&负偏斜可实现此目的,

I've tried using a combination of positive & negative skews to achieve this,

-webkit-transform: skew(20deg);
   -moz-transform: skew(20deg);
     -o-transform: skew(20deg);

但似乎无法获得所需的布局,有可能吗?

but can't seem to get the desired layout, is it even possible?

推荐答案

诀窍是正确设置transform-orgin,border,然后容易应用两个偏斜变换。您可能还需要一些负余量来纠正某些空格。

The trick is to correctly set the transform-orgin, the border and then it should be easy to apply both skew transformation. You may also need some negative margin to rectify some spaces.

我使用CSS变量来简化代码,但这不是强制性的。

I used CSS variables to make the code easy but it's not mandatory.

#about-gallery {
  margin: 60px 0;
  display: flex;
  flex-wrap: wrap;
  --s:-8deg; /* The skew factor (the same for all)*/
}

.about-img-wrapper {
  margin: 5px;
  overflow: hidden; /* Don't forget this !!*/
  flex-grow:1;
}

.about-img-wrapper > * {
  height: 300px;
  background: center/cover no-repeat;
  overflow: hidden; /* Don't forget this !!*/
  transform:skewX(var(--s,0deg));
}
/* We need skewY for the 3 last element */
.about-img-wrapper:nth-child(n + 3) > *  {
  transform:skewY(var(--s,0deg));
}
/* The pseudo element will get the image and the inverse of skewing */
.about-img-wrapper > *::before {
  content:"";
  display:block;
  height:100%;
  background:inherit; /* inherit the image */
  transform-origin: inherit; /* inherit the same transform-origin */
  transform:skewX(calc(-1*var(--s,0deg)));
}
.about-img-wrapper:nth-child(n + 3) > *::before  {
  transform:skewY(calc(-1*var(--s,0deg)));
}

/**/
.image-1 {
  border-left: solid 10px black;
  flex-basis:50%;
  margin-right:-15px;
}

.image-1>* {
  transform-origin: top;
  border: solid 10px black;border-left: none;
}
/**/
.image-2 {
  border-right: solid 10px black;
  flex-basis:40%;
  margin-left:-15px;
}
.image-2>* {
  transform-origin: bottom;
  border: solid 10px black;border-right: none;
}
/**/
.image-3 {
  border-top: solid 10px black;
  flex-basis:100%;
}
.image-3>* {
  transform-origin: left;
  border: solid 10px black;border-top: none;
}
/**/
.image-4,
.image-5{
  border-bottom: solid 10px black;
  flex-basis:40%;
  margin-top:-5vw;
}
.image-4>*,
.image-5>* {
  transform-origin: right;
  border: solid 10px black;border-bottom: none;
}
.image-5 {
  margin-top:-12vw;
}
.image-5>* {
  height:calc(300px + 7vw); /* We need a bigger height here */
}
/**/
* {
  box-sizing: border-box;
}

<div id="about-gallery">
  <div class="about-img-wrapper  image-1">
    <div style="background-image: url(https://picsum.photos/id/10/800/800);"></div>
  </div>
  <div class="about-img-wrapper image-2">
    <div style="background-image: url(https://picsum.photos/id/102/800/800);"></div>
  </div>
  <div class="about-img-wrapper  image-3">
    <div style="background-image: url(https://picsum.photos/id/123/800/800);"></div>
  </div>
  <div class="about-img-wrapper image-4">
    <div style="background-image: url(https://picsum.photos/id/14/800/800);"></div>
  </div>
  <div class="about-img-wrapper  image-5">
    <div style="background-image: url(https://picsum.photos/id/20/800/800);"></div>
  </div>

</div>

这里是另一个使用更少的代码并依靠 mask 来创建偏斜效果的想法:

Here is another idea using less of code and relying on mask to create the skew effect:

.about-gallery {
  margin: 60px 0;
  display: flex;
  flex-wrap: wrap;
}

.about-gallery > * {
  margin: 5px;
  flex-grow:1;
  height: 300px;
  background: 
    var(--g),
    var(--img) center/cover no-repeat;
  border: solid 10px black;
  -webkit-mask:
    linear-gradient(#fff,#fff) no-repeat,
    linear-gradient(to var(--p,bottom right),#fff 49.5%,transparent 50%) no-repeat;
  mask:
    linear-gradient(#fff,#fff) no-repeat,
    linear-gradient(to var(--p,bottom right),#fff 49.5%,transparent 50%) no-repeat;
}

/**/
.image-1 {
  flex-basis:50%;
  margin-right:-15px;
  --g:linear-gradient(to bottom right,transparent 49.8%,black 50%) right/50px 100% no-repeat;
  -webkit-mask-position:left,right;
  mask-position:left,right;
  -webkit-mask-size:calc(100% - 50px) 100%, 50px 100%;
  mask-size:calc(100% - 50px) 100%, 50px 100%;
}

/**/
.image-2 {
  flex-basis:40%;
  margin-left:-15px;
  --g:linear-gradient(to top left,transparent 49.8%,black 50%) left/50px 100% no-repeat;
  -webkit-mask-position:right,left;
  mask-position:right,left;
  -webkit-mask-size:calc(100% - 50px) 100%, 50px 100%;
  mask-size:calc(100% - 50px) 100%, 50px 100%;
  --p:top left;
}

/**/
.image-3 {
  flex-basis:100%;
  --g:linear-gradient(to bottom right,transparent 49.8%,black 50%) bottom/100% 80px no-repeat;
  -webkit-mask-position:top,bottom;
  mask-position:top,bottom;
  -webkit-mask-size:100% calc(100% - 80px),100% 80px;
  mask-size:100% calc(100% - 80px),100% 80px;
}

/**/
.image-4,
.image-5{
  flex-basis:40%;
  margin-top:-30px;
  --g:linear-gradient(to top left,transparent 49.8%,black 50%) top/100% 40px no-repeat;
  -webkit-mask-position:bottom,top;
  mask-position:bottom,top;
  -webkit-mask-size:100% calc(100% - 40px),100% 40px;
  mask-size:100% calc(100% - 40px),100% 40px;
  --p:top left;
}
.image-5 {
  margin-top:-70px;
  height:340px;
}

/**/
* {
  box-sizing: border-box;
}

<div class="about-gallery">
  <div class="image-1" style="--img: url(https://picsum.photos/id/10/800/800);"></div>
  <div class="image-2" style="--img: url(https://picsum.photos/id/102/800/800);"></div>
  <div class="image-3" style="--img: url(https://picsum.photos/id/123/800/800);"></div>
  <div class="image-4" style="--img: url(https://picsum.photos/id/14/800/800);"></div>
  <div class="image-5" style="--img: url(https://picsum.photos/id/20/800/800);"></div>
</div>

这篇关于带有边框的一侧偏斜背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆