How do I create a teardrop in HTML?

后端 未结 12 543
生来不讨喜
生来不讨喜 2020-12-02 03:59

How do I create a shape like this to display on a webpage?

I don\'t want to use images since they would get blurry on scaling

12条回答
  •  心在旅途
    2020-12-02 04:28

    CSS Version

    As there are a fair few answers here I thought why not add to it with another method. This is using both HTML and CSS to create the teardrop.

    This will allow you to change the colour of the border and background of the teardrop and also re-size the top part of it.

    Using a single div we can create a circle with border and border-radius. Then using pseudo elements (:before & :after) we create a CSS triangle more here, this will act as the tip of the teardrop. Using the :before as the border we place :after on top with a smaller size and the desired background colour.

    div {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      border: 4px solid;
      margin: 80px auto;
      position: relative;
    }
    div:before,
    div:after {
      content: "";
      display: block;
      position: absolute;
      width: 0;
      height: 0;
    }
    div:before {
      border-left: 50px solid transparent;
      border-right: 50px solid transparent;
      border-bottom: 104px solid black;
      top: -75px;
    }
    div:after {
      border-left: 46px solid transparent;
      border-right: 46px solid transparent;
      border-bottom: 96px solid #fff;
      top: -66px;
      left: 0;
      right: 0;
      margin: auto;
      z-index: 1;
    }


    Here is a demo of the teardrop with a background colour

    div {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      border: 4px solid;
      background: red;
      margin: 80px;
      position: relative;
    }
    div:before,
    div:after {
      content: "";
      display: block;
      position: absolute;
      width: 0;
      height: 0;
    }
    div:before {
      border-left: 50px solid transparent;
      border-right: 50px solid transparent;
      border-bottom: 100px solid black;
      top: -70px;
    }
    div:after {
      border-left: 46px solid transparent;
      border-right: 46px solid transparent;
      border-bottom: 96px solid red;
      top: -66px;
      left: 0;
      right: 0;
      margin: auto;
      z-index: 1;
    }

    It is as simple as putting a background colour onto the div and changing :after bottom-border colour to the same. To change the border you will need to change div border colour and :before background colour too.

提交回复
热议问题