用底部三角形制作div

cod*_*dek 5 html css geometry css-shapes

我一直试图用div 做白色的形状:

http://sircat.net/joomla/sircat/mies/2.png

如何获得div底部的对角线形状?

我有这个div:宽度:620px; 身高:440px; 背景颜色:白色;

谢谢

编辑:忘记div后面的bg,我想用对角线边框制作div,而不是借助于bg,因为它位于顶层

Ric*_*uza 13

您还可以使用边框和:after伪选择器:http://jsfiddle.net/qQySU/

#pointed {
    position: relative;
    margin: 0 auto;
    width: 200px;
    height: 200px;
    background-color: white;
}

#pointed:after,
#pointed::after {
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -50%;
    content: '';
    width: 0;
    height: 0;
    border-top: solid 150px red;
    border-left: solid 100px transparent;
    border-right: solid 100px transparent;
}
Run Code Online (Sandbox Code Playgroud)

我给小费着色,便于识别边框.播放最后3行的边框宽度,以获得所需的提示.

编辑.:

兼容性参考:http://caniuse.com/css-gencontent

编辑2:

作为语义的交换,你可以得到更多的crossbrowser,你可以将stle放在内部元素而不是:after伪选择器上.


Ana*_*Ana 5

最简单(最少量的代码)方法:只需使用CSS http://dabblet.com/gist/3610406linear-gradient

HTML:

<div class='box'>Text goes here...</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.box {
    width: 26em;
    min-height: 31em;
    padding: 1em;
    outline: solid 1px lightblue;
    margin: 0 auto;
    background: linear-gradient(45deg, dimgrey 47%, black 50%, transparent 50%) 
                    no-repeat 0 100%, 
                linear-gradient(-45deg, dimgrey 47%, black 50%, transparent 50%) 
                    no-repeat 100% 100%;;
    background-size: 50% 14em;
}
Run Code Online (Sandbox Code Playgroud)

更好的兼容性和更好看的:你可以使用一个伪元素box-shadow:http://dabblet.com/gist/3610548

HTML:

<div class='box'>text goes here... hover me ;)</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

html { background: darkgrey; }
.box {
    box-sizing: border-box;
    position: relative;
    width: 20em;
    height: 20em;
    padding: 1em;
    margin: 3em auto 0;
    background: white;
}
.box:before {
    position: absolute;
    right: 14.65%; /* 50% - 35.35% */ bottom: -35.35%; /* half of 70.71% */
    width: 70.71%; /* 100%*sqrt(2)/2 */
    height: 70.71%;
    box-shadow: 2px 2px 1px dimgrey;
    transform: rotate(45deg);
    background: white;
    content: '';
}
.box:hover, .box:hover:before {
    background: plum;
}
Run Code Online (Sandbox Code Playgroud)

  • **感谢**将我介绍给[dabblet.com](http://dabblet.com).我不知道为什么有人会使用[jsFiddle](http://jsfiddle.net)进行仅限CSS的演示,当有一个更整洁的选择:) (2认同)