CSS对角线 - 如何适应其父元素?

时间:2017-02-20 20:20:27

标签: html css css3 css-transforms diagonal

如何填充对角线并装入盒子(只是纯css - 不使用背景图像)?



div.diagonal-container {
    border: 1px solid #000;
    width:400px;
    height:400px;
    margin: 0 auto;
}

.to-right,
.to-left {
    border-top:1px solid #ff00ff;
    width:100%;

    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
}

.to-right {
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
}

<div class="diagonal-container" style="">
    <div class="to-right"></div>
</div>

<div class="diagonal-container" style="">
    <div class="to-right"></div>
</div>

<div class="diagonal-container" style="">
    <div class="to-left"></div>
</div>
&#13;
&#13;
&#13;

结果:

enter image description here

jsfiddle

此外,是否有可能只有一个元素没有包装它?例如:

<div class="to-right"></div>
<div class="to-right"></div>
<div class="to-left"></div>

有可能吗?

5 个答案:

答案 0 :(得分:3)

您可以添加linear-gradient background并抛弃内部元素,将该类添加到其他div

&#13;
&#13;
div.diagonal-container {
  border: 1px solid #000;
  width: 400px;
  height: 400px;
  margin: 0 auto;
}

.to-right {
  background: linear-gradient(135deg, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px) );
}

.to-left {
  background: linear-gradient(45deg, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px) );
}
&#13;
<div class="diagonal-container to-right"></div>

<div class="diagonal-container to-right"></div>

<div class="diagonal-container to-left"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用伪元素绘制线条。

.diagonal-container {
  border: 1px solid #000;
  width: 400px;
  height: 400px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

.diagonal-container:before {
  border-top: 1px solid #ff00ff;
  content: '';
  position: absolute;
  top: 0; left: 0; right: -50%;
  transform: rotate(45deg);
  transform-origin: 0 0;
}

.to-right:before {
  right: 0; left: -50%;
  transform: rotate(-45deg);
  transform-origin: 100% 0;
}
<div class="diagonal-container to-right">
</div>

<div class="diagonal-container to-left">
</div>

<div class="diagonal-container to-right">
</div>

答案 2 :(得分:1)

试试这个作为你的css:

div.diagonal-container {
  border: 1px solid #000;
  width: 400px;
  height: 400px;
  margin: 0 auto;
  overflow: hidden;
}

.to-right,
.to-left {
  border-top: 1px solid #ff00ff;
  width: 100%;
}

.to-left {
    transform: rotate(45deg) scale(1.5);
    transform-origin: top left;
}

.to-right {
  transform: rotate(-45deg) scale(1.5);
  transform-origin: bottom right;
}

答案 3 :(得分:1)

我的方法:

  1. 对容器使用position: relative,为行使用position: absolute
  2. 左侧使用transform-origin: leftleft: 0,右侧使用transform-origin: rightright: 0
  3. 如果容器的widthheight相等,那么该行的宽度将为〜141.5%
  4. .diagonal-container {
        border: 1px solid #000;
        width: 400px;
        height: 400px;
        margin: 0 auto;
        position: relative;
    }
    
    .to-right, .to-left {
        border-top: 1px solid #ff00ff;
        width: 141.5%;
        position: absolute;
        top: 0;
    }
    
    .to-left {
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        -webkit-transform-origin: left;
        -ms-transform-origin: left;
        transform-origin: left;
        left: 0;
    }
    
    .to-right {
        -webkit-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        transform: rotate(-45deg);
        -webkit-transform-origin: right;
        -ms-transform-origin: right;
        transform-origin: right;
        right: 0;
    }
    <div class="diagonal-container">
        <div class="to-right"></div>
    </div>
    
    <div class="diagonal-container">
        <div class="to-right"></div>
    </div>
    
    <div class="diagonal-container">
        <div class="to-left"></div>
    </div>

    如果您使用了CSS预处理器(例如LESS),那么您可以执行以下操作:

    @width: 400px;
    @height: 400px;
    
    .rotate(@angle, @origin) {
        -webkit-transform: rotate(@angle);
        -ms-transform: rotate(@angle);
        transform: rotate(@angle);
        -webkit-transform-origin: @origin;
        -ms-transform-origin: @origin;
        transform-origin: @origin;
    }
    
    .diagonal-container {
        border: 1px solid #000;
        width: @width;
        height: @width;
        margin: 0 auto;
        position: relative;
    }
    
    .to-right, .to-left {
        border-top: 1px solid #ff00ff;
        width: sqrt(@width*@width + @height*@height);
        position: absolute;
        top: 0;
    }
    
    .to-left {
        .rotate(45deg, left);
        left: 0;
    }
    
    .to-right {
        .rotate(-45deg, right);
        right: 0;
    }
    

    CodePen

答案 4 :(得分:0)

最好使用 SVG,它提供了很好的响应对角线,几乎适用于所有浏览器。

.box {
    width: 20rem;
    height: 10rem;
    background-color: hsla(0, 0%, 70%, 0.3);
    cursor: pointer;
    position: relative;
}

.svg-stroke {
    position: absolute;
    width: 100%;
    height: 100%;
}
<div class="box">
    <svg class='svg-stroke' viewBox='0 0 10 10' preserveAspectRatio='none'>
         <line x1='10' y1='0' x2='0' y2='10' stroke='red' stroke-width='.2' stroke-opacity='0.2' />
    </svg>
</div>