Page Widget How to - Create speech bubble in four directions








Question

We would like to know how to create speech bubble in four directions.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.speech-bubble {<!--from  w  w  w  . ja v a2 s .co  m-->
  position: relative;
  background-color: #EEE;
  width: 200px;
  height: 150px;
  line-height: 150px;
  color: white;
  text-align: center;
  border-radius: 10px;
  font-family: sans-serif;
  margin: 2em auto;
  box-shadow: 5px 5px 2px #DDD;
}

.speech-bubble:after {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  border: 15px solid;
}

.speech-bubble-top:after {
  border-bottom-color: #666;
  left: 50%;
  bottom: 100%;
  margin-left: -15px;
}

.speech-bubble-right:after {
  border-left-color: #666;
  left: 100%;
  top: 50%;
  margin-top: -15px;
}

.speech-bubble-bottom:after {
  border-top-color: #666;
  top: 100%;
  left: 50%;
  margin-left: -15px;
}

.speech-bubble-left:after {
  border-right-color: #666;
  top: 50%;
  right: 100%;
  margin-top: -15px;
}
</style>
</head>
<body>
  <div class="speech-bubble speech-bubble-top">java2s.com</div>
  <div class="speech-bubble speech-bubble-bottom">java2s.com</div>
  <div class="speech-bubble speech-bubble-left">java2s.com</div>
  <div class="speech-bubble speech-bubble-right">java2s.com</div>
</body>
</html>

The code above is rendered as follows: