How can I create a round arrow with only HTML and CSS?

后端 未结 6 1139
挽巷
挽巷 2020-12-05 04:32

I\'m trying to create a round directional arrow with CSS and HTML. Below are my attempts.

Attempt 1

In this I have rotated the

and
6条回答
  •  孤城傲影
    2020-12-05 04:33

    You could use a pseudo element to generate the triangle (using the famous border hack).

    After that, you would be able to use a thick border on the actual element (with a border-radius of 50% to make it a circle). This allows you to rotate the arrow to your liking.

    div {
      border: 20px solid transparent;
      border-top-color: black;
      border-left-color: black;
      height: 100px;
      width: 100px;
      border-radius: 50%;
      position: relative;
      -webkit-transform: rotate(-45deg);
      -ms-transform: rotate(-45deg);
      transform: rotate(-45deg);
      margin:30px auto;
    }
    div:before {
      content: "";
      position: absolute;
      top: -20px;
      left: 80%;
      height: 0;
      width: 0;
      border-left: 30px solid black;
      border-top: 30px solid transparent;
      border-bottom: 30px solid transparent;
      -webkit-transform: rotate(45deg);
      -ms-transform: rotate(45deg);
      transform: rotate(45deg);
    }
    
    
    /*BELOW IS FOR DEMO ONLY*/
    
    div:hover {
      -webkit-transform: rotate(315deg);
      -ms-transform: rotate(315deg);
      transform: rotate(315deg);
      transition: all 0.8s;
    }
    html {
      text-align:center;
      color:white;
      font-size:30px;
      height: 100%;
      background: rgb(79, 79, 79);
      /* Old browsers */
      background: -moz-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* FF3.6+ */
      background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, rgba(79, 79, 79, 1)), color-stop(100%, rgba(34, 34, 34, 1)));
      /* Chrome,Safari4+ */
      background: -webkit-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* Opera 12+ */
      background: -ms-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* IE10+ */
      background: radial-gradient(ellipse at center, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* W3C */
      filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#4f4f4f', endColorstr='#222222', GradientType=1);
      /* IE6-9 fallback on horizontal gradient */
    }
    HOVER ME
    


    If you then wanted to lengthen the arrow, you could make the bottom border visible. For example;

    div {
      border: 20px solid transparent;
      border-top-color: black;
      border-left-color: black;
      border-bottom-color: black;
      height: 100px;
      width: 100px;
      border-radius: 50%;
      position: relative;
      transform: rotate(-45deg);
      margin:30px auto;
    }
    div:before {
      content: "";
      position: absolute;
      top: -20px;
      left: 80%;
      height: 0;
      width: 0;
      border-left: 30px solid black;
      border-top: 30px solid transparent;
      border-bottom: 30px solid transparent;
      transform: rotate(45deg);
    }
    
    
    /*BELOW IS FOR DEMO ONLY*/
    
    div:hover {
      transform: rotate(315deg);
      transition: all 0.8s;
    }
    html {
      text-align:center;
      color:white;
      font-size:30px;
      height: 100%;
      background: rgb(79, 79, 79);
      /* Old browsers */
      background: -moz-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* FF3.6+ */
      background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, rgba(79, 79, 79, 1)), color-stop(100%, rgba(34, 34, 34, 1)));
      /* Chrome,Safari4+ */
      background: -webkit-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* Opera 12+ */
      background: -ms-radial-gradient(center, ellipse cover, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* IE10+ */
      background: radial-gradient(ellipse at center, rgba(79, 79, 79, 1) 0%, rgba(34, 34, 34, 1) 100%);
      /* W3C */
      filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#4f4f4f', endColorstr='#222222', GradientType=1);
      /* IE6-9 fallback on horizontal gradient */
    }
    HOVER ME
    

提交回复
热议问题