【问题标题】:Skew a Partial Div but not its content.倾斜部分 Div 但不倾斜其内容。
【发布时间】:2023-03-19 02:44:01
【问题描述】:

我试图倾斜一个 div 但不是它的内容,无论它是应该垂直的图像还是文本。

我已经解决了许多与堆栈错误相关的问题,例如
Create a slanted edge to a div,
How to skew element but keep text normal (unskewed)

在我看来,似乎没有任何帮助。

我已将图像放在左侧,将文本放在右侧,它们都应该是倾斜的。但是当我像将图像放在右侧(反之亦然)一样翻转它们时,它应该证明自己是合理的。

这是一张鼓舞人心的图片。

这是我为它工作过的Fiddle

<div class="container">
  <div class="partial-section">
    <div class="section-inner">
      <div id="parallelogram">
        <div class="image"></div>
      </div>
    </div>
  </div>
  <div class="partial-section">
    <h1> This is some heading on the right partial section.</h1>
    <p>Some random text that should be appeared on the right side of the partial section. This should be below the heading the partial section has and should fill the empty space that it has.</p>
  </div>
</div>
body {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

* {
  box-sizing: border-box;
}

.container {
  max-width: 1160px;
  margin: 0 auto;
  background-color: green;
  height: 450px;
}

.partial-section {
  width: 50%;
  background-color: red;
  height: inherit;
  display: inline;
  float: left;
  border: 2px solid lightgrey;
  padding: 20px;
}

.partial-section h1 {
  font-size: 40px;
}

.partial-section p {
  font-size: 20px;
}

.section-inner {
  height: inherit;
}

#parallelogram {
  width: 100%;
  height: inherit;
  transform: skew(-40deg);
  position: relative;
  top: 0;
  overflow: hidden;
}

.image {
  background: url(http://placekitten.com/601/301);
  background-size: cover;
  position: absolute;
  top: 0;
  background-repeat: no-repeat;
  -webkit-transform: skew(-20deg);
  -moz-transform: skew(-20deg);
  -o-transform: skew(-20deg);
  height: inherit;
  width: 100%;
  transform: skew(40deg);
}

如果有人能帮我解决这个问题,我会很高兴。

提前致谢。

【问题讨论】:

  • 也检查一下post
  • 你能抽出一些时间来帮助我吗?
  • 好的。您应该与overflow hidden 有共同的父母。在左列将有没有任何转换的图像,在右列将有没有任何背景和转换的文本。在具有全高和背景的右列上使用伪元素,并将倾斜变换应用于此伪元素。你明白了吗?
  • 以防万一有人想要 jsFiddle,这里是链接:jsfiddle.net/83pdLum5

标签: html css


【解决方案1】:

我看到这个问题(关于倾斜的边缘)和思路(使用倾斜)经常出现。偏斜非常酷,并且对某些效果很有用,但 IMO 这不是那些时代之一。

我建议添加一个:after 伪元素并创建一个 CSS 三角形来创建您想要的效果。这将消除撤消内容倾斜的需要。

为了保持图像和内容元素的宽度相等,我使用了calc(50% +/- Xpx) 的宽度,但这只是一些挑剔的东西。如果您不介意一侧更大/更小,您可以将两者的宽度设置为固定的50%

body {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

* {
  box-sizing: border-box;
}

.container {
  max-width: 1160px;
  margin: 0 auto;
  background-color: green;
  height: 450px;
}

.partial-section {
  width: calc(50% - 25px);
  background-color: red;
  height: inherit;
  display: inline;
  float: left;
}

.partial-section h1 {
  font-size: 30px;
}

.partial-section p {
  font-size: 20px;
}

.partial--padded {
  padding: 20px;
}

.section-inner {
  height: inherit;
}

.image {
  background: url(http://placekitten.com/601/301);
  background-size: cover;
  top: 0;
  background-repeat: no-repeat;
  height: inherit;
  width: 100%;
}

.slanted {
  position: relative;
  width: calc(50% + 25px);
  background-color: red;
  height: inherit;
  display: inline;
  float: left;
}

.slanted:after {
  content: "";
  display: block;
  position: absolute;
  top: 0px;
  right: 0;
  width: 0;
	border-bottom: 450px solid red;
	border-left: 50px solid transparent;
}
<div class="container">
  <div class="slanted">
      <div class="image"></div>
  </div>
  <div class="partial-section partial--padded">
    <h1> This is some heading on the right partial section.</h1>
    <p>Some random text that should be appeared on the right side of the partial section. This should be below the heading the partial section has and should fill the empty space that it has.</p>
  </div>
</div>

如果你真的想使用skew来获得一些经验,我们仍然可以走:after伪元素路线,创建一个红色矩形,然后将其倾斜成一个斜边。这再次消除了撤消对内部内容的倾斜的需要。

body {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

* {
  box-sizing: border-box;
}

.container {
  max-width: 1160px;
  margin: 0 auto;
  background-color: green;
  height: 450px;
}

.partial-section {
  width: 50%;
  background-color: red;
  height: inherit;
  display: inline;
  float: left;
}

.partial-section h1 {
  font-size: 30px;
}

.partial-section p {
  font-size: 20px;
}

.partial--padded {
  padding: 20px;
}

.section-inner {
  height: inherit;
}

.image {
  background: url(http://placekitten.com/601/301);
  background-size: cover;
  top: 0;
  background-repeat: no-repeat;
  height: inherit;
  width: 100%;
}

.slanted {
  position: relative;
  width: 50%;
  background-color: red;
  height: inherit;
  display: inline;
  float: left;
  overflow: hidden;
}

.slanted:after {
  content: "";
  display: block;
  position: absolute;
  top: 0px;
  right: -25px;
  width: 50px;
  height: 100%;
  background-color: red;
  -webkit-transform: skew(-5deg);
  -moz-transform: skew(-5deg);
  -o-transform: skew(-5deg);
  transform: skew(-5deg);
}
<div class="container">
  <div class="slanted">
      <div class="image"></div>
  </div>
  <div class="partial-section partial--padded">
    <h1> This is some heading on the right partial section.</h1>
    <p>Some random text that should be appeared on the right side of the partial section. This should be below the heading the partial section has and should fill the empty space that it has.</p>
  </div>
</div>

【讨论】:

  • 从问题上来说,如果我把右边的图片和左边的文字这样的内容翻转呢?
  • 简单,只需将:after 伪元素重新定位到右侧,然后将三角形翻转/倾斜另一个方向。
猜你喜欢
  • 2016-12-25
  • 2016-12-23
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多