Finnnn
Finnnn

Reputation: 3580

attempting to recreate a loading gif in css

I'm trying to recreate the following gif in pure css -

loading image

Css here - http://codepen.io/anon/pen/FmCaL - (webkit/chrome only at the mo)

I'm attempting to recreate the missing chunk of the circle by using the before & after psuedo selectors, but I can't get the angles right.

Is it even possible to do? Is there a better approach?


Thanks for any help so far. I should have specified that I need the arrow to be transparent. I can't use a solid color for the missing part of the circle.

Upvotes: 5

Views: 295

Answers (2)

Mr. Alien
Mr. Alien

Reputation: 157324

How about simply doing it like this?

Demo

div {
    border: 10px solid #000;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    position: relative;    
}

div:after {
    height: 0;
    width: 0;
    display: block;
    position: absolute;
    right: -12px; 
    content: " ";
    border-bottom: 12px solid #fff;
    border-right: 12px solid transparent;
    transform: rotate(10deg);
}

Explanation: What we are doing here is using :after pseudo to position the element absolute to the circle and using transform property to rotate the triangle.


Demo 2 (With animation)

div {
    border: 10px solid #000;
    height: 50px;
    width: 50px;
    border-radius: 50%;
    position: relative;    
    animation: spin 1s infinite linear;
}

div:after {
    height: 0;
    width: 0;
    display: block;
    position: absolute;
    right: -12px; 
    content: " ";
    border-bottom: 12px solid #fff;
    border-right: 12px solid transparent;
    transform: rotate(10deg);
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

My Suggestion: As you updated your qustion, you said you wanted a transparent gutter, I would suggest you to use a .png image and rotate it, rather than writing 100 lines of CSS and worrying about cross browser compatibility. Alternatively as I've shared a link in the comments, you can use CSS masking.

Upvotes: 7

Dziad Borowy
Dziad Borowy

Reputation: 12579

If you do not care whether the triangle is transparent, can I suggest something like this:

DEMO (works in chrome and firefox)

.loading {
    display: inline-block;
    margin: 5em;
    border: 10px solid grey;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    -webkit-animation: spin 1s linear infinite;
    -moz-animation: spin 1s linear infinite;
    animation: spin 1s linear infinite;     
}
.loading:before {
    content: '';
    display: block;
    border: 13px solid transparent;
    border-right-color: white;
    width: 20px;
    height: 0;
    border-radius: 50%;
    margin: -3px 0 0 -13px;
    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    transform: rotate(-90deg);
}
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(359deg); }}
@-moz-keyframes spin { 100% { -moz-transform: rotate(359deg); }}
@keyframes spin { 100% { transform: rotate(359deg); }}

Upvotes: 0

Related Questions