【问题标题】:CSS Design: Help placing diagonal columns to fill screen from top-rightCSS 设计:帮助从右上角放置对角列以填满屏幕
【发布时间】:2019-07-19 04:09:34
【问题描述】:

晚上好,

我目前正在尝试在我正在开发的新网站上设计对角柱效果,但在尝试正确放置背景时遇到了麻烦。

An image of what I am trying to achieve.

理想情况下,我想根据滚动事件和/或一些动画来更改颜色,这就是为什么我尝试使用 CSS 来实现这种效果,而不是使用模拟图像作为背景。该网站将需要做出响应,这消除了一些硬编码的可能性..

Here is a CodeSandBox 包含我为解决此问题所做的尝试。不幸的是,transform-origin 属性似乎没有按预期工作。

编辑:从 Sandbox 在此处添加代码以供将来参考。

EDIT2:Adding a second example image to show opposing gradients. 这在 Bryce Howitson 的回答中提到。

.container {
  width: 100vw;
  height: 100vh;
  overflow-y: hidden;
  border: 1px solid black;
}

.rotated {
  transform: rotate(45deg);
  transform-origin: center top;
  display: flex;
  flex-direction: row;
}

.column {
  width: 200px;
  height: 1200px;
  background: blue;
}
.column1 {
  width: 200px;
  height: 1200px;
  background: red;
}
 <div class="container">
      <div class="rotated">
        <div class="column"></div>
        <div class="column1"></div>
        <div class="column"></div>
        <div class="column1"></div>
        <div class="column"></div>
        <div class="column1"></div>
      </div>
    </div>

感谢阅读!

【问题讨论】:

  • 寻求代码帮助的问题必须包括在问题本身中重现它所需的最短代码,最好是在堆栈片段中。尽管您提供了一个链接,但如果它变得无效,那么您的问题对于未来遇到同样问题的其他 SO 用户将毫无价值。见Something in my website/example doesn't work can I just paste a link
  • 看看linear-gradient吧。
  • 感谢@Paulie_D 的评论,这是一个很好的观点。我将使用 Stack Snippet 编辑我的问题以包含代码。
  • @ChrisW。线性梯度实际上是最终目标。我打算为每个不同的列添加一个颜色渐变,渐变方向不同。

标签: css multiple-columns css-transforms diagonal css-gradients


【解决方案1】:

您可以用一个linear-background很多。这种方法的好处是您不必转换任何东西并且它是响应式的,因为它填充了容器。

body {
 background: #333;
}

.stripes {
  width: 80%;
  min-height: 300px;
  margin: 40px auto;
  border-radius: 10px;
  
background-image: 
linear-gradient(-45deg, rgba(2,0,36,1) 0%, rgba(2,0,36,1) 9%, rgba(224,14,14,1) 9%, rgba(224,14,14,1) 22%, rgba(0,212,255,1) 22%, rgba(0,212,255,1) 45%, rgba(255,89,0,1) 45%, rgba(255,89,0,1) 63%, rgba(2,0,36,1) 63%, rgba(2,0,36,1) 65%, rgba(0,212,255,1) 65%);
}
&lt;div class="stripes"&gt;&lt;/div&gt;

编辑#1:

您可以通过在同一元素上堆叠多个背景渐变来做更多事情。

body {
 background: #333;
}

.stripes {
  width: 80%;
  min-height: 300px;
  margin: 40px auto;
  border-radius: 10px;
  
background-image: 
linear-gradient(34deg, rgba(9,9,15,0.8) 0%, rgba(0,212,255,0) 70%),
linear-gradient(-45deg, rgba(2,0,36,1) 0%, rgba(2,0,36,1) 9%, rgba(224,14,14,1) 9%, rgba(224,14,14,1) 22%, rgba(0,212,255,1) 22%, rgba(0,212,255,1) 45%, rgba(255,89,0,1) 45%, rgba(255,89,0,1) 63%, rgba(2,0,36,1) 63%, rgba(2,0,36,1) 65%, rgba(0,212,255,1) 65%);
}
&lt;div class="stripes"&gt;&lt;/div&gt;

编辑#2: SVG 似乎是解决这个问题的好方法。我喜欢它的可扩展性而不会模糊。

body {
  background: #333;
}

.stripes {
  width: 80%;
  min-height: 300px;
  margin: 40px auto;
  border-radius: 10px;
  
  background-size: cover;
  background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/10307/stripes.svg);
  background-repeat: no-repeat;
}
&lt;div class="stripes"&gt;&lt;/div&gt;

不幸的是,这需要编辑 SVG 文件本身来添加旋转。最后一种选择是在容器内使用 SVG 内联。我们将使用大小(使其比容器大)和绝对定位来确保它在旋转时覆盖所有内容。

body {
  background: #333;
}

.stripes {
  width: 80%;
  min-height: 300px;
  margin: 40px auto;
  border-radius: 10px;
  
  overflow:hidden;
  position: relative;
}

.stripes svg {
  position: absolute;
  z-index: -1;
  top:-50%;
  left:-50%;
  width: 200%;
  height: 200%;
  transform: rotate(45deg);
}
<div class="stripes">
  <svg width="500px" height="500px" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <linearGradient x1="50%" y1="0%" x2="50%" y2="83.2079305%" id="linearGradient-1">
            <stop stop-color="#E8FF04" offset="0%"></stop>
            <stop stop-color="#CD1144" offset="100%"></stop>
        </linearGradient>
        <linearGradient x1="50%" y1="0%" x2="50%" y2="86.756968%" id="linearGradient-2">
            <stop stop-color="#3816E3" offset="0%"></stop>
            <stop stop-color="#00FF81" offset="100%"></stop>
        </linearGradient>
    </defs>
    <g id="stripes2" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <rect id="R1" fill="#00CEFF" transform="translate(81.836327, 250.000000) rotate(-360.000000) translate(-81.836327, -250.000000) " x="0" y="2.84217094e-14" width="163.672655" height="500"></rect>
        <rect id="R2" fill="#060120" transform="translate(168.982036, 250.000000) rotate(-360.000000) translate(-168.982036, -250.000000) " x="163.982036" y="0" width="10" height="500"></rect>
        <rect id="R2" fill="url(#linearGradient-1)" transform="translate(203.441118, 250.000000) rotate(-360.000000) translate(-203.441118, -250.000000) " x="174" y="0" width="58.8822355" height="500"></rect>
        <rect id="R4" fill="url(#linearGradient-2)" transform="translate(285.395210, 250.000000) rotate(-360.000000) translate(-285.395210, -250.000000) " x="233" y="0" width="104.790419" height="500"></rect>
        <rect id="R5" fill="#DF0E0D" transform="translate(373.425150, 250.000000) rotate(-360.000000) translate(-373.425150, -250.000000) " x="336" y="0" width="74.8502994" height="500"></rect>
        <rect id="R6" fill="#060120" transform="translate(455.500000, 250.000000) rotate(-360.000000) translate(-455.500000, -250.000000) " x="411" y="0" width="89" height="500"></rect>
    </g>
</svg>
</div>

【讨论】:

  • 嗨,布莱斯,感谢您的出色回应!你可以用线性背景做很多事情!但是,我希望为每个“条纹”添加相反的渐变,所以我不确定这是否适用于这种方法。我看到您可以堆叠多个背景渐变,添加不透明度以进行混合。但我不明白如何为每个不同的条纹添加单独的渐变。
  • 当您说“相反的渐变”时,您是指仅与可能垂直于渐变的特定条纹相互作用的渐变吗?还是更倾向于底部/左侧比顶部/右侧更暗?
  • @Onomatopoeia 我更新了我的答案,添加了另一个穿过第一组的渐变。我认为它不包含在一条条纹中,但与您的示例图像相匹配。
  • 嘿布莱斯,我很感激答案。你已经很准了!不幸的是,我没有在原始帖子中提出我的全部问题,这是我的错。我已经编辑了我的原始帖子,以包含第二张显示我所指的“相反渐变”的图像。如果您对如何实现它有任何想法,那将是非常棒的。但是感谢您提高了我的线性梯度知识!
  • @Onomatopoeia 让它变得简单一点也不好玩吧? :-) 我对如何解决它有一些想法,但今天没有时间。暂时不要放弃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 2013-05-20
  • 2011-11-09
  • 2013-02-17
  • 1970-01-01
  • 2017-02-01
相关资源
最近更新 更多