Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a triangular point to a div that changes with the content height with CSS?

I have a div and I need to give it an angular point. The height will vary based on its content so using pseudo content and borders like in the article below wont work.

http://css-tricks.com/snippets/css/css-triangle/

enter image description here

As this is a progressive enhancement I only need to support modern browsers.

like image 894
Evanss Avatar asked Jan 08 '15 14:01

Evanss


People also ask

How do Triangles work in CSS?

The idea is a box with zero width and height. The actual width and height of the arrow is determined by the width of the border. In an up arrow, for example, the bottom border is colored while the left and right are transparent, which forms the triangle.

How auto adjust the Div height according to content?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.


1 Answers

An alternate answer, using gradients and pseudo elements

#one {height: 100px;}
#two {height: 200px;}
.corner {
    width: 100px;
    background-color: green;
    margin: 10px;
    position: relative;
}
.corner:after, .corner:before {
    content: "";
    position: absolute;
    left: 100%;
    width: 40px;
    height: 50%;
}
.corner:before {
    top: 0px;
    background: linear-gradient(to top right, green 50%, transparent 51%);
}
.corner:after {
    bottom: 0px;
    background: linear-gradient(to bottom right, green 50%, transparent 51%);
}
<div id="one" class="corner"></div>
<div id="two" class="corner"></div>
like image 191
vals Avatar answered Oct 04 '22 09:10

vals