带有倾斜和背景图像的框

box with skew and background image

我想放在方框内的图像与方框一样倾斜。我希望图像不会像盒子那样歪斜。问题是我已经将 skew(-20deg) 设置为框,当我在图像上设置 skew(20deg) 时,它不起作用。图像不会直立。有什么解决办法吗?

.accordion {
  width: 100%;
  height: 300px;
  overflow: hidden;
  max-width: 100%;
  margin: 50px auto;
  background-color: #FFAE00;
}

.accordion ul {
  width: 90%;
  display: table;
  table-layout: fixed;
  margin: 0;
  padding: 0;
  margin-left: 5%;
  height: 100%;
}

.accordion ul li {
  display: table-cell;
  vertical-align: bottom;
  position: relative;
  margin-left: 0;
  margin-right: 0;
  width: 25%;
  height: 250px;
  background-repeat: no-repeat;
  background-position: center center;
  transition: all 500ms ease;
  float: none;
  transform: skewX(-20deg);
}

.accordion ul li div {
  display: block;
  overflow: hidden;
  width: 100%;
}

.accordion ul li div a {
  display: block;
  height: 300px;
  width: 37vw;
  position: relative;
  z-index: 0;
  vertical-align: bottom;
  padding: 55px 45px;
  box-sizing: border-box;
  color: #fff;
  margin-left: -53px;
  text-decoration: none;
  transform: skewX(20deg);
  font-family: Open Sans, sans-serif;
  transition: all 200ms ease;
}

.accordion ul li div a * {
  opacity: 0;
  margin: 0;
  width: 100%;
  text-overflow: ellipsis;
  position: relative;
  z-index: 0;
  white-space: nowrap;
  overflow: hidden;
  -webkit-transform: translateX(-20px);
  transform: translateX(-20px);
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion ul li div a h2 {
  font-family: Montserrat, sans-serif;
  text-overflow: clip;
  font-size: 24px;
  text-transform: uppercase;
  text-align: left;
  border-bottom: unset;
  margin-bottom: 2px;
  top: 165px;
}

.accordion ul li div a p {
  top: 165px;
  font-size: 13.5px;
  font-weight: 100;
  margin-left: 0;
  height: auto;
}

.accordion ul li:nth-child(1) {
  background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
}

.accordion ul li:nth-child(2) {
  background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
}

.accordion ul li:nth-child(3) {
  background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
}

.accordion ul li:nth-child(4) {
  background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
}

.accordion ul:hover li:hover {
  width: 50%;
}

.accordion ul:hover li:hover a {
  background: rgba(0, 0, 0, 0.4);
}

.accordion ul:hover li:hover a * {
  opacity: 1;
  transform: none;
}
<div class="accordion">
  <ul>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
  </ul>
</div>

Fiddle: https://jsfiddle.net/c3wbvutw/

transform:SkewX 不像 addition/deduction 那样工作,它更像是从原始图片倾斜 X 到 Y 度。

为了撤消转换,您必须使用 `transform: unset;" 我在你的fiddle中添加了以下代码,它对我有用。(这也有-webkit-版本)参考:Remove applied CSS transformation

.accordion ul li {
transform: unset;
}

编辑: 如果您希望文本保持倾斜,请在文本上添加 class 并使用 :not() 选择器

我用我之前在评论中说的方法能够管理它。 基本上,我使用 ::before 伪元素来放置背景图像,然后将它们倾斜。

查看:

/* +++++++++++++++ NEW INFORMATION ++++++++++++++ */

.accordion ul li::before {
  content: "";
  transform: skewX(20deg);
  -ms-transform: skewX(20deg);
  -webkit-transform: skewX(20deg);
  background-image: url(http://michael-ferry.com/assets/accordion3.jpg);
  background-repeat: no-repeat;
  background-position: top left;
  position: absolute;
  transform: translateX(-50%) skewX(20deg);
  -webkit-transform-origin: top left;
  -ms-transform-origin: top left;
  transform-origin: top left;
  width: 200%;
  height: 100%;
}

/* +++++++++++++++++++++++++++++++++++++++++++++ */

.accordion {
  width: 100%;
  height: 300px;
  overflow: hidden;
  max-width: 100%;
  margin: 50px auto;
  background-color: #FFAE00;
}

.accordion ul {
  width: 90%;
  display: table;
  table-layout: fixed;
  margin: 0;
  padding: 0;
  margin-left: 5%;
  height: 100%;
}

.accordion ul li {
  display: table-cell;
  vertical-align: bottom;
  position: relative;
  margin-left: 0;
  margin-right: 0;
  width: 25%;
  height: 250px;
  background-repeat: no-repeat;
  background-position: center center;
  transition: all 500ms ease;
  float: none;
  transform: skewX(-20deg);
  overflow: hidden;
}

.accordion ul li div {
  display: block;
  overflow: hidden;
  width: 100%;
}

.accordion ul li div a {
  display: block;
  height: 300px;
  width: 37vw;
  position: relative;
  z-index: 0;
  vertical-align: bottom;
  padding: 55px 45px;
  box-sizing: border-box;
  color: #fff;
  margin-left: -53px;
  text-decoration: none;
  transform: skewX(20deg);
  font-family: Open Sans, sans-serif;
  transition: all 200ms ease;
}

.accordion ul li div a * {
  opacity: 0;
  margin: 0;
  width: 100%;
  text-overflow: ellipsis;
  position: relative;
  z-index: 0;
  white-space: nowrap;
  overflow: hidden;
  -webkit-transform: translateX(-20px);
  transform: translateX(-20px);
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion ul li div a h2 {
  font-family: Montserrat, sans-serif;
  text-overflow: clip;
  font-size: 24px;
  text-transform: uppercase;
  text-align: left;
  border-bottom: unset;
  margin-bottom: 2px;
  top: 165px;
}

.accordion ul li div a p {
  top: 165px;
  font-size: 13.5px;
  font-weight: 100;
  margin-left: 0;
  height: auto;
}

.accordion ul:hover li:hover {
  width: 50%;
}

.accordion ul:hover li:hover a {
  background: rgba(0, 0, 0, 0.4);
}

.accordion ul:hover li:hover a * {
  opacity: 1;
  transform: none;
}
<div class="accordion">
  <ul>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
  </ul>
</div>