Page Widget How to - Create CSS triangles with borders compatible across browsers








Question

We would like to know how to create CSS triangles with borders compatible across browsers.

Answer

revised from 
http://fiddle.jshell.net/gaby/FJEFP
http://stackoverflow.com/questions/10727244

<!-- ww  w .  j  a v  a  2s. c  o m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<style type='text/css'>
#flowBoxes {
  margin: auto;
  width: 730px;
  padding: 20px;
}

#flowBoxes div {
  display: inline-block;
  position: relative;
  height: 25px;
  line-height: 25px;
  padding: 0 20px;
  border: 1px solid #ccc;
  border-right: 0;
  margin-right: 2px;
}

#flowBoxes div.right:after {
  content: '';
  border-top: 1px solid #ccc;
  border-right: 1px solid #ccc;
  width: 18px;
  height: 18px;
  position: absolute;
  right: 0;
  top: -1px;
  background-color: white;
  z-index: 150;
  -moz-transform: translate(10px, 4px) rotate(45deg);
  -webkit-transform: translate(10px, 4px) rotate(45deg);
  -o-transform: translate(10px, 4px) rotate(20deg);
  -ms-transform: translate(10px, 4px) rotate(45deg);
  transform: translate(10px, 4px) rotate(45deg);
}

#flowBoxes div.left:before {
  content: '';
  border-top: 1px solid #ccc;
  border-right: 1px solid #ccc;
  width: 18px;
  height: 18px;
  position: absolute;
  left: 0;
  top: -1px;
  background-color: white;
  z-index: 50;
  -moz-transform: translate(-10px, 4px) rotate(45deg);
  -webkit-transform: translate(-10px, 4px) rotate(45deg);
  -o-transform: translate(-10px, 4px) rotate(20deg);
  -ms-transform: translate(-10px, 4px) rotate(45deg);
  transform: translate(-10px, 4px) rotate(45deg);
}

#flowBoxes .active {
  background-color: green;
  color: white;
}

#flowBoxes div.active:after {
  background-color: green;
}
</style>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$('#flowBoxes').on('click', 'div', function(){
    $(this).addClass('active').siblings().removeClass('active');
});
});//]]>  
</script>
</head>
<body>
  <div id="flowBoxes">
    <div class="right active">Create Account</div>
    <div class="left right">Favorites</div>
    <div class="left right">$5 Per Friend</div>
    <div class="left right">Ready to Shop!</div>
  </div>
  <p>you can click on the elements to change the "active" state</p>
</body>
</html>

The code above is rendered as follows: