Slanted border using CSS [duplicate]

末鹿安然 提交于 2021-01-02 08:00:18

问题


How could I style a div to like the below image?


回答1:


You can use half triangle with the pseudo element.

.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;
}
<div class="container">
  <div class="rectangle"></div>
</div>

Jsfiddle




回答2:


Try this:

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;
}

You will have 2 divs like:

Div main

Div cornered

Check it out.




回答3:


You can use the pseudo element with a rectangle:

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>

Code snippet:

#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>


来源:https://stackoverflow.com/questions/33690168/slanted-border-using-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!