Speech bubble with arrow

前端 未结 3 859
名媛妹妹
名媛妹妹 2020-11-22 02:08

I have a project where I need to insert speech bubbles / message boxes. The general shape I am trying to achieve is this one :

3条回答
  •  旧巷少年郎
    2020-11-22 02:33

    In order to achieve this, you should consider altering your markup in order to make your html more efficient. This can be achieved using a pseudo element. I'll address each point individually, and put it all together at the end of my answer.

    First of all,

    Use pseudo elements to avoid extra elements

    You could use a pseudo element to remove the extra .triangle div. This not only reduces your div numbers, but also helps with positioning as you can use the top: left: right: and bottom: css properties in order to position according to your main element. This can be seen below:

    .oneAndOnlyDiv {
      height: 100px;
      width: 200px;
      border: 3px solid gray;
      background: lightgray;
      position: relative;
    }
    .oneAndOnlyDiv:before {
      content: "";
      position: absolute;
      top: 100%;
      left: 20px;
      width: 0;
      border-top: 20px solid black;
      border-left: 20px solid transparent;
      border-right: 20px solid transparent;
    }
    Main div


    Hit testing

    In order to create your "hit test", you may wish to use a rotated element instead of a border hack.

    Something like:

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
    }
    div:before {
      content: "";
      position: absolute;
      top: 100%;
      left: 20px;
      height: 20px;
      width: 20px;
      background: black;
      transform: rotate(45deg);
      transform-origin:top right;
    }
    Only element

    or use a skewed pseudo element:

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
    }
    div:before {
      content: "";
      position: absolute;
      top: 90%;
      left: 20px;
      height: 30%;
      width: 20px;
      background: black;
      transform: skewY(-45deg);
      transform-origin:bottom left;
      z-index:-1;
    }
    Only element

    which will show the pointer only when the square or main element is hovered. But hang on, that messes up the positioning? how can you deal with that?

    There are a few solutions to that. One of which is to use the calc CSS property.

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
    }
    div:before {
      content: "";
      position: absolute;
      top: -webkit-calc(100% - 10px); /*may require prefix for old browser support*/
      top: calc(100% - 10px); /*i.e. half the height*/
      left: 20px;
      height: 20px;
      width: 20px;
      background: gray;
      transform: rotate(45deg);
    }
    Only element

    Adding a border

    You can add a border quite easily now, simply by adding a border declaration to the main element, and setting the border-bottom and border-right of the pseudo element to inherit

    Border

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
      border:3px double black;
    }
    div:before {
      content: "";
      position: absolute;
      top: -webkit-calc(100% - 10px); /*may require prefix for old browser support*/
      top: calc(100% - 10px); /*i.e. half the height*/
      left: 20px;
      height: 20px;
      width: 20px;
      background: gray;
      transform: rotate(45deg);
      border-bottom:inherit;
      border-right:inherit;
      box-shadow:inherit;
    }
    Only element

    Box Shadow:

    In order to have a box shadow, I've used the :after pseudo element in order to hide the box shadow over the other pseudo, making the element seem as one single element.

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
      box-shadow: 5px 5px 10px 2px black;
    }
    div:before,div:after {
      content: "";
      position: absolute;
      top: -webkit-calc(100% - 10px); /*may require prefix for old browser support*/
      top: calc(100% - 10px); /*i.e. half the height*/
      left: 20px;
      height: 20px;
      width: 20px;
      background: gray;
      transform: rotate(45deg);
    z-index:-1;
      box-shadow:inherit;
    }
    div:after{
      box-shadow:none;
      z-index:8;
      }
    Only element

    Putting it all together

    You can also add a border radius to your message box or speech bubble by again, using the border-radius property:

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
      border:3px double black;
      border-radius:10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: -webkit-calc(100% - 10px); /*may require prefix for old browser support*/
      top: calc(100% - 10px); /*i.e. half the height*/
      left: 20px;
      height: 20px;
      width: 20px;
      background: gray;
      transform: rotate(45deg);
      border-bottom:inherit;
      border-right:inherit;
      box-shadow:inherit;
    }
    Only element

    This even allows you to create not only a triangle, but how about a circle instead?

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor:pointer;
      border:3px double black;
      border-radius:10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: -webkit-calc(100% - 13px); /*may require prefix for old browser support*/
      top: calc(100% - 13px); /*i.e. half the height + border*/
      left: 20px;
      height: 20px;
      width: 20px;
      background: gray;
      transform: rotate(45deg);
      border:3px double transparent;
      border-bottom:inherit;
      border-right:inherit;
      box-shadow:inherit;
      border-radius:50%;
    }
    Only element

    If you are having issues with content overflowing and being 'hidden' behind this pseudo element, and you aren't fussed about having a border, you could use a negative z-index which will solve this issue.

    Don't like using 'magic numbers'?

    If you don't like the idea of using a calc value, in which the positioning in my answer is currently using (whilst working), you may wish to use transform:translate(50%)

    This would be a much better approach, since:

    • You do not need to know the size of the border, nor half the width
    • You will be making your message box/ bubble a lot more dynamic in its positioning, and would support further sizings.

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor: pointer;
      border: 3px double black;
      border-radius: 10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: 100%;
      left: 30px;
      height: 20px;
      width: 20px;
      background: gray;
      box-sizing:border-box;
      transform: rotate(45deg) translate(-50%);
      border-bottom: inherit;
      border-right: inherit;
      box-shadow: inherit;
    }
    Only element

    Want to move it? You can!

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor: pointer;
      border: 3px double black;
      border-radius: 10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: 100%;
      left: 10%;
      height: 20px;
      width: 20px;
      background: gray;
      box-sizing: border-box;
      transform: rotate(45deg) translate(-50%);
      border-bottom: inherit;
      border-right: inherit;
      box-shadow: inherit;
      transition: all 0.8s;
    }
    div:hover:before {
      left: 90%;
    }
    Only element

    Want it one the right?

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor: pointer;
      border: 3px double black;
      border-radius: 10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: 15%;
      left: 100%;
      height: 20px;
      width: 20px;
      background: gray;
      box-sizing:border-box;
      transform: rotate(45deg) translate(-50%);
      border-top: inherit;
      border-right: inherit;
      box-shadow: inherit;
      transition:all 0.8s;
    }
    div:hover:before{
      top:80%;
      }
    Only Element

    Want it to be a different shape of triangle?

    div {
      height: 100px;
      width: 200px;
      background: gray;
      position: relative;
      cursor: pointer;
      border-radius: 10px;
    }
    div:before {
      content: "";
      position: absolute;
      top: 70%;
      left: 100%;
      height: 20px;
      width: 20px;
      background: gray;
      box-sizing:border-box;
      transform:  translate(-50%) skewX(45deg);
      box-shadow: inherit;
      transition:all 0.8s;
      z-index:-1;
    }
    div:hover:before{
      transform:  translate(-50%);
      border-radius:50%;
      top:20%;
      }
    Only Element

提交回复
热议问题