Demo 4 (As you commented, you need to use top: 0; for :before and :after pseudo as well, because when you add text, it will shift both the triangles from the top. So inorder to prevent that, use top: 0;)
Here, am using a simple div element and placing 2 CSS triangles which are positioned absolute to the container. This is more compatible than above, if you are going for a NON CSS3 solution, you can choose this. Make sure you use display: block; for :before as well as :after. And ofcourse you can merge the common styles but I've kept both separate, so that you can get easability to customize them separately.
One major gripe I have with using triangular borders is that there is no easy way to have multiple triangles with different colours, even using javascript [because JS can't access the pseudo-elements :before and :after], the alternative being that I use 3 divs, align them properly, and give all of them the same colour, etc... Too much hassle.
The best way would be using transform: skew() for newer browsers.
But you need to keep in mind that this will transform every element inside that div as well. So the text inside your menu-bar would also come up skewed. To counter that, add a reverse-skew on the inner div like this:
How about using CSS3
transform skew
?Demo
Nothing much to explain here, it's a simple
div
element, which I've skewed by30deg
which will result in the shape you expected.Note: It's a CSS3 property, so older browsers, as well as IE will spoil your things, make sure you use CSS3 Pie.
Other way to achieve this is by using
:after
and:before
pseudo and CSS Triangles along withcontent
property.Demo 2 (Kept red triangles for demo purpose)
Demo 3 (Color Changed)
Demo 4 (As you commented, you need to use
top: 0;
for:before
and:after
pseudo as well, because when you add text, it will shift both the triangles from the top. So inorder to prevent that, usetop: 0;
)Here, am using a simple
div
element and placing 2 CSS triangles which arepositioned absolute
to the container. This is more compatible than above, if you are going for a NON CSS3 solution, you can choose this. Make sure you usedisplay: block;
for:before
as well as:after
. And ofcourse you can merge the common styles but I've kept both separate, so that you can get easability to customize them separately.HTML
CSS
One major gripe I have with using triangular borders is that there is no easy way to have multiple triangles with different colours, even using javascript [because JS can't access the pseudo-elements :before and :after], the alternative being that I use 3 divs, align them properly, and give all of them the same colour, etc... Too much hassle.
The best way would be using
transform: skew()
for newer browsers.But you need to keep in mind that this will transform every element inside that div as well. So the text inside your menu-bar would also come up skewed. To counter that, add a reverse-skew on the inner div like this:
Have fun experimenting... :)