I’m trying to make this traffic light to turn on all the lights, but the CSS animation is not working. I have checked numerous times for any missing brackets as well as any other issues.
div {
background-color: rgb(0, 0, 0);
border: 15px solid rgba(128, 119, 119, 0.877);
padding: 100px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column-reverse;
width: 400px;
height: 500px;
}
@keyframes colorOne {
25% {color: black;}
50% {color: red;}
75% {color: black;}
100% {color: red;}
}
#c1 {
border: 99px solid white;
border-radius: 50%;
animation-name: colorOne;
animation-duration: 5s;
}
@keyframes colorTwo {
25% {color: black;}
50% {color: yellow;}
75% {color: black;}
100% {color: yellow;}
}
#c2 {
border: 99px solid white;
border-radius: 50%;
animation-name: colorTwo;
animation-duration: 5s;
}
@keyframes colorThree {
25% {color: black;}
50% {color: green;}
75% {color: black;}
100% {color: green;}
}
#c3 {
border: 99px solid white;
border-radius: 50%;
animation-name: colorThree;
animation-duration: 5s;
}
<div>
<p id="c1"></p>
<p id="c2" ></p>
<p id="c3"></p>
</div>
3
Answers
Your CSS for each
p
element uses a large border which means that the white colour you can see is the border.In your animation keyframes declaration
color
sets the color of the text.Therefore if you change this to
border-color
; it will then start to animate.Of course I do not know the full extent of the project but you will need to incorporate an animation delay to make it look like a real traffic light or consider another way to control the timings.
Repeat the below for each of the three animations:
There are a number of things to compare and contrast between the CSS in the working example below and the CSS in your question, above.
Did you mean for the lights to be upside down (
flex-direction: column-reverse
) or the right way up (flex-direction: column
) ?If you want to animate the background color of an element, then, conventionally, we give the element a
width
and aheight
and use thebackground-color
property.As above, normally, block level elements have a
width
and aheight
. But in a verticalflexbox
environment, an element will have awidth
and aflex-basis
. If you want aflex-basis
(equivalent to aheight
in this instance) to be198px
and you want this to neither grow nor shrink, you will declare:If you want the final frame of an
animation
to persist, then useanimation-fill-mode: forwards
Working Example:
I see you have a great answer but might I suggest a grid and let the orb content size determine the width/height given some padding on the container and between the orbs as a gap. Note I changed from a paragraph to a div to make that gap work best/better here.
Using a grid like I have for the BODY and orb container "super-centers" the element/grid contents easily.
Give the orbs a class to make it more DRY, then just put the "color" on each (I used a class for clarity rather than the IDs)
As an addition I put color variables in place for the background of each and added a border to show the use of that can be for the orb size and not all this huge bordering you had.
Try changing the orb size from
8em
and see how it adjusts.