使用CSS的倾斜边框

时间:2015-11-13 10:05:40

标签: css css3 css-shapes

我如何设置div以使其像下图一样?

enter image description here

3 个答案:

答案 0 :(得分:5)

试试这个:

<强> HTML

<div class="main"></div>
<div class="cornered"></div>

<强> CSS

.main {
    width: 300px;
    height: 200px;
    background-color: blue;
}
.cornered {
    width: 260px;
    height: 0px;
    border-top: 40px solid blue;
    border-right: 40px solid white;
}

您将有2 div个像:

Div main

enter image description here

Div cornered

enter image description here

<强>

<强> Check it out.

答案 1 :(得分:4)

您可以将半角三角形与伪元素一起使用。

&#13;
&#13;
.container {
  padding: 20px;
  background-color: #F8F8F8;
  max-width: 200px;
}
.rectangle {
  width: 200px;
  height: 100px;
  background-color: #003781;
  position: relative;
}
.rectangle:after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 25px 25px;
  border-color: transparent transparent #F8F8F8 transparent;
}
&#13;
<div class="container">
  <div class="rectangle"></div>
</div>
&#13;
&#13;
&#13;

Jsfiddle

答案 2 :(得分:3)

您可以将伪元素与矩形一起使用:

CSS:

<style media="screen" type="text/css">
    #rectangle{
        width: 200px;
        height: 100px;
        background: #ac60ec;
        position: relative;
    }
    #rectangle:after {
        content: "";
        width: 171px;
        height: 0;
        position: absolute;
        bottom: 0;
        left: 0;
        border-top: 29px solid #ac60ec;
        border-right: 29px solid #ffffff;
    }  
</style>

HTML:

<div id="rectangle"></div>

代码段:

#rectangle {
   width: 200px;
   height: 100px;
   background: #ac60ec;
   position: relative;
 }
 #rectangle:after {
   content: "";
   width: 171px;
   height: 0;
   position: absolute;
   bottom: 0;
   left: 0;
   border-top: 29px solid #ac60ec;
   border-right: 29px solid #ffffff;
 }
<div id="rectangle"></div>