【问题标题】:Make div with bottom triangle用底部三角形制作 div
【发布时间】:2012-08-28 09:14:28
【问题描述】:

我一直在尝试用 div 做 白色形状:

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

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

我为 div 准备了这个: 宽度:620 像素; 高度:440px; 背景颜色:白色;

谢谢

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

【问题讨论】:

    标签: html css geometry css-shapes


    【解决方案1】:

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

    HTML:

    <div class='box'>Text goes here...</div>
    

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

    更好的兼容性和更好的外观:您可以使用pseudo-element 和box-shadow:http://dabblet.com/gist/3610548

    HTML:

    <div class='box'>text goes here... hover me ;)</div>
    

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

    【讨论】:

    • 谢谢把我介绍给dabblet.com。我不知道当有更简洁的替代方案时,为什么有人会使用 jsFiddle 进行纯 CSS 演示 :)
    【解决方案2】:

    您还可以使用边框和: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;
    }
    

    我已对笔尖进行了着色,以便于识别边框。围绕最后 3 行的边框宽度进行调整以获得所需的提示。

    编辑:

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

    编辑 2:

    为了换取语义,您可以获得更多跨浏览器,您可以将 stle 放在内部元素上,而不是放在 :after 伪选择器上。

    【讨论】:

    • 这会阻止您在三角形上继续文本。
    • 如果您将其放置在内容之外并在其上使用其他容器来保留文本,则不会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多