Make div with bottom triangle

2019-03-30 02:05发布

问题:

I have been trying to do the white shape with a div:

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

how do I get the diagonal shapes of the bottom of the div?

I have this for the div: width: 620px; height: 440px; background-color: white;

thank you

Edit: just forget the bg behind the div, I want to make the div with the diagonal borders, not with the help of the bg because it is in the top layer

回答1:

You can also use borders and the :after pseudo selector: 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;
}

I've colored the tip for easy identification of the borders. Play around the border widths on the last 3 lines to get the tip you want.

Edit.:

Reference for compability: http://caniuse.com/css-gencontent

Edit 2:

In exchange for semantics, you can get it more crossbrowser you can place the stle on a inner element instead of on the :after pseudo selector.



回答2:

Simplest (least amount of code) method: just use a 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;
}

Better compatibility & better looking: you could use a pseudo-element with a 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;
}