Make CSS background animaton not fade but to snap between colours - css

I am currently playing / learning CSS animations and transitions and I want to make a simple animation where the background colour and text of a DIV alternate between two colors. This is pretty simple but my colours fade or blend from one to another and I would rather they snap or jump or toggle from one to another so the impact of the change is stronger. Here is my simple code
<div class="boss">
I am some text
</div>
Here is my CSS
.boss {
font-family: Helvetica, sans-serif;
position: absolute;
width: 100vw;
height: 100vh;
top: 0px;
left: 0;
background-color: #e91e63;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 2em;
text-transform: uppercase;
animation: bg 1s linear infinite;
}
#keyframes bg {
0% {
background-color: #e91e63;
color: white;
}
100% {
background-color: white;
color: #e91e63;
}
}
A jsbin of the code above is here: https://jsbin.com/macimep/edit?html,css,output
Can anyone tell me how I can make the colour change so it toggles rather than fades. I'm sure this isn't a hard question but everything I have tried so far hasn't worked. Also I want to do this with CSS, toggling with JavaScript is simple but I am trying to get to grips with CSS. Many thanks in advance

If you don't need production ready code and just play with existing solutions, you can use animation-timing-function: steps(2, end); (see https://developer.mozilla.org/en-US/docs/Web/CSS/animation-timing-function#Step_examples).
You also can do :
#keyframes bg {
0%,49% {
background-color: #e91e63;
color: white;
}
50%,100% {
background-color: white;
color: #e91e63;
}
}

Try this:
.boss {
font-family: Helvetica, sans-serif;
position: absolute;
width: 100vw;
height: 100vh;
top: 0px;
left: 0;
background-color: #e91e63;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 2em;
text-transform: uppercase;
animation: bg 1s linear infinite;
}
#keyframes bg {
0%,
49.99% {
background-color: #e91e63;
color: white;
}
50%,
100% {
background-color: white;
color: #e91e63;
}
}
<div class="boss">
I am some text
</div>

Related

CSS/SCSS fill text on hover, from bottom to top, with a gradient

I found this post to be really helpful in trying to make an icon fill up on hover. I'm using fontawesome, so it's text that I'm filling. However, the solution offered by Mohammad it doesn't seem to be working with a gradient of color. Is there a work around? I am not allowed to use any js or framework.
Here is my attempt.
.text {
font-size: 30px;
line-height: 40px;
display: inline-block;
vertical-align: top;
position: relative;
cursor: pointer;
margin: 20px;
color: hotpink;
// color: linear-gradient(0deg, rgba(255,18,222,1) 0%, rgba(156,45,253,1) 100%); not working
}
.text:before {
transition: height 0.5s ease-in;
content: attr(data-text);
position: absolute;
overflow: hidden;
height: 40px;
color: black;
left: 0;
top: 0;
}
.text:hover:before {
height: 0;
}
<div class="text" data-text="ICON">ICON</div>
Its good.
You may check https://animate.style/ this website for more animations with code.

Tricky CSS Positioning Animation

I'm learning about CSS animations and am trying to animate this stamp which uses relative and absolute positioning. I want the animation to:
Start at it's original size.
Grow 2-3xs that size and be at the center of the screen.
Pause for 2-3 seconds seconds.
Shrink back down to it's original size and spot.
The problem I'm running into is keeping the elements positioned on top of the stamp in the same place as the transitions occur. Looking to keep it pure CSS if possible (*side note I haven't added any web-kit/browser support yet because I'm just trying to make it work first).
Here is the code:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
height: 90vh;
justify-content: flex-end;
align-items: flex-start;
background: grey;
}
.park-img {
width: 10rem;
height: 11.2rem;
display: inline-block;
margin-left: 2px;
padding: 10px;
background: white;
position: relative;
top: 2rem;
left: -2rem;
/*-webkit-filter: drop-shadow(0px 0px 10px rgba(0,0,0,0.5));
/*The stamp cutout will be created using crisp radial gradients*/
background: radial-gradient( transparent 0px, transparent 4px, white 4px, white);
/*reducing the gradient size*/
background-size: 20px 20px;
/*Offset to move the holes to the edge*/
background-position: -10px -10px;
/*Animation*/
animation: stampAnimation 9s ease-in-out;
}
#keyframes stampAnimation {
0% {}
50% {
transform: scale(3, 3) translate(-35%, 35%);
}
75% {
transform: scale(3, 3) translate(-35%, 35%);
}
}
.stampText {
position: relative;
top: -1rem;
left: -1.8rem;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: #fafafa;
text-transform: uppercase;
animation: stampAnimation 9s ease-in-out;
}
.park-name {
font-weight: bold;
text-align: center;
font-size: 1rem;
}
.park-title {
width: 90%;
overflow: hidden;
text-align: center;
font-size: .5rem;
}
.park-title:before,
.park-title:after {
background-color: #fafafa;
content: "";
display: inline-block;
height: 1px;
position: relative;
vertical-align: middle;
width: 10%;
}
.park-title:before {
right: 0.5rem;
margin-left: -50%;
}
.park-title:after {
left: 0.5rem;
margin-right: -50%;
}
<div class="park-stamp">
<img src="https://res.cloudinary.com/saveallthethings/image/upload/v1614633821/daniel-burka-X_IwSPO2GH0-unsplash_zvoyst.jpg" class="park-img" />
<div class="stampText">
<div class="park-name">Zion</div>
<div class="park-title">National Park</div>
</div>
</div>
As well as the codepen:
https://codepen.io/aspirationalhobbit/pen/GRNPqxw?editors=0100
I have edited your code to make changes that are different from my initial answer.
Check https://codepen.io/udabasili/pen/VwmqmQK?editors=1100.
Basically, I created a css for the container of your element and moved the animation there. I also removed the translate from your animation. Check the css in the codepen to see the changes
.park-stamp{
animation: stampAnimation 9s ease-in-out infinite;
}

icon not showing on hover using sass

I am using scss and trying to hide an icon and when I hover the button I want the icon to display. I also want to animate it so I can't use display:none as I believe you can't animate that. So I have tried to use opacity and visibility.
I have 2 problems. Firstly, The text in the button is not centered because the icon is there in an invisible state yet when you go to dev tools you can still see it there. Secondly, the icon does not appear on hover.
.btn {
text-transform: uppercase;
text-decoration: none;
padding: 1.5rem 4rem;
display: inline-block;
font-size: 1.6rem;
box-shadow: 0 1rem 2rem rgba($color-black, .2);
&--primary {
background-color: $color-primary;
color: $color-white;
transition: all .2s;
&__icon {
visibility: hidden;
opacity: 0;
}
&:hover {
transform: translateY(-2px);
&__icon {
visibility: visible;
opacity: 1;
}
}
}
}
Discover More <i class="btn--primary__icon icon-basic-clockwise"></i>
I think the problem is that your icon is displayed inline with zero content. I think you might need to change the icon to display: inline-block; and give it some dimensions. You'll want to set the dimensions to 0px when the icon should not be visible.
I converted your code from SCSS to CSS and replaced the $variables with actual CSS colors to make a working snippet...
.btn {
text-transform: uppercase;
text-decoration: none;
padding: 1.5rem 4rem;
display: inline-block;
font-size: 1.6rem;
box-shadow: 0 1rem 2rem rgba(0,0,0, .2);
width: 240px;
}
.btn--primary {
background-color: red;
color: white;
transition: all .2s;
}
.btn--primary__icon {
display: inline-block;
width: 0px;
height: 0px;
visibility: hidden;
opacity: 0;
}
.btn--primary:hover {
transform: translateY(-2px);
}
.btn--primary:hover .btn--primary__icon {
width: 20px;
height: 20px;
background-color: blue;
visibility: visible;
opacity: 1;
}
<a href="#" class="btn btn--primary">
Discover More
<i class="btn--primary__icon icon-basic-clockwise"></i>
</a>

Lock a div in place after you have selected it

So I have the hover state figured out. I'd like to keep the card at it's translated stated once I click on it though. I've tried using pseudo class :focus and :active but it didn't work. Or I'm just not putting the right values in.
$('.wrapper').on('click', function() {
$(this).addClass('active');
});
.wrapper:hover {
transform: translateY(5px);
}
.wrapper:active {
transform: translateY(5px);
position: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="paper">
<img src="https://www.jbhifi.com.au/FileLibrary/ProductResources/Images/216422-M-LO.jpg">
<div class="poster">
<h2>Featured</h2>
<h1>Big Baby Swing</h1>
<p>Does he look like hes stupid. Did he drive well? No. Then is he stupid? He's a good kid that's like the devil behind a wheel. That's all you need to know about him..</p>
More
</div>
</div>
<div class="space"></div>
</div>
codepen to my question
You assign a active class on click so .wrapper.active instead of .wrapper:active should work
You missed jquery cdn and add some css following if you need to get back use toggleClass.
.wrapper.active{
transform: translateY(5px);
}
.wrapper.active div a.button{
width:auto;
font-size: 0.8rem;
background-color: #29426d;
box-shadow: none;
}
$('.wrapper').on('click', function() {
$(this).addClass('active');
// You can use here toggleClass also
});
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: #2c3e50;
font-family: 'Montserrat', sans-serif;
font-size: 14px;
}
/* this centers the card in the middle of any browser */
body {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
background-color: #D7D7DD;
}
/* You can rename this .wrapperCard or what ever you may have used. The important thing to notice is the z-index relative to the .papaer div in order for this to work. In the HTML document .space which contains the gradient at the bottom is out side of .paper that holds the information. */
.wrapper {
margin: 0;
padding: 0;
height: auto;
width: auto;
transition: 1s ease;
cursor: pointer;
z-index: 98;
}
.wrapper:hover{
transform: translateY(5px);
}
.wrapper.active{
transform: translateY(5px);
}
.wrapper.active div a.button{
width:auto;
font-size: 0.8rem;
background-color: #29426d;
box-shadow: none;
}
.wrapper:hover div a.button{
width:auto;
font-size: 0.8rem;
background-color: #29426d;
box-shadow: none;
}
/* This calls onto the div.paper on hover and looks for div.space, if you take a way div.space your gradient will not longer dissapear on hover. */
.wrapper:hover div.space{
transform: translateY(-7px);
z-index:-1;
}
/* This is the div that contains all of the informoation, to be noted. The div is set to a specific height of 41, it basically reaches to the bottom of the MORE button. The reason being is so I can hide the gradient which is the div space, placed all the way at the bottom with a z-index of -1. If you take out the height in div.paper you will see why I made the card a particular size, so keep that in mind when you are transfering the gradient code onto the cards for NASCA. */
.paper{
font-family: 'Titilium Web', sans-serif;
display: flex;
flex-wrap: wrap;
align-items: center;
align-content: center;
margin: 0 auto;
padding: 0;
max-width: 300px;
min-width: 300px;
background: white;
text-align: justify;
z-index: 99;
}
a.image{
flex: 1;
text-align: none;
font-size: none;
text-decoration: none;
color:none;
transition: none;
padding:0;
margin: 0;
height: auto;
}
a.image:hover{
color:none;
border-bottom:none;
text-align: none;
box-shadow: none;
transform: none;
}
.poster{
flex: 1;
height: auto;
}
h2{
flex: 1;
font-size: .55rem;
color: #A0A6AB;
letter-spacing: 2px;
text-transform: uppercase;
text-align: center;
font-family: $sans-serif;
font-weight: 500;
margin-bottom: 10px;
}
h1{
flex: 1;
display: flex;
justify-content: center;
margin-top:1rem;
font-weight:bolder;
letter-spacing: normal;
font-kerning: 1rem;
transition: 0.5s ease;
color: #29426d;
}
h1:hover{
transform: translateX(2px);
}
p{
flex: 1;
padding: 1rem 1rem;
color:#333333;
}
a.button{
position: relative;
display: flex;
justify-content: center;
cursor: pointer;
text-transform: uppercase;
text-decoration: none;
font-size: 0.8rem;
line-height: 20px;
letter-spacing: .06rem;
font-weight: bold;
color: white;
background: #e44d6e;
margin: 0 auto;
padding: 0.5rem 1rem;
width: auto;
transition: 1s ease;
}
/* To be noted, the top selector is set to -4px because the gradient is too strong initially, pulling it up some makes it less harsh. */
.space{
flex: 1;
position: relative;
padding: 0;
margin: 0;
left: 0;
bottom: 0;
width:100%;
height: 1rem;
background: grey;
background: -webkit-linear-gradient(#333333 -290%, transparent 100%);
background: -o-linear-gradient(#333333 -290%, transparent 100%);
background: -mox-linear-gradient(#333333 -290%, transparent 100%);
background: linear-gradient(#333333 -290%, transparent 100%);
transition: 1s ease;
z-index: -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="paper">
<img src="https://www.jbhifi.com.au/FileLibrary/ProductResources/Images/216422-M-LO.jpg">
<div class="poster">
<h2>Featured</h2>
<h1>Big Baby Swing</h1>
<p>Does he look like hes stupid. Did he drive well? No. Then is he stupid? He's a good kid that's like the devil behind a wheel. That's all you need to know about him..</p>
More
</div>
</div>
<div class="space"></div>
</div>
</body>

CSS button hover makes text blinking

I have button transformation on hover in my custom stylesheet. It allows me to create button from <a href> or <div>. During this transformation font is blinking in strange way. Could you kindly tell me why and how to solve it?
Here is the scss code:
https://gist.github.com/rafpiek/7e1b2df5690baa3b02e88eee10adfb98
and the video:
https://www.youtube.com/watch?v=4c4D96FukuQ&feature=youtu.be
.button {
text-decoration: none;
color: #ECECEC;
font-size: 20px;
height: 40px;
width: 100px;
background: #B3595F;
display: inline-block;
transition: all 0.30s linear;
display: flex;
justify-content: center;
align-items: center;
margin: 5px 3px;
cursor: pointer;
border-radius: 6px;
user-select: none;
font-weight: bold;
}
.button:-webkit-any-link {
color: #ECECEC;
}
.button:hover {
transform: scale(1.07);
color: #ECECEC;
}
.button:active, .button:focus, .button:visited {
text-decoration: none;
color: #ECECEC;
}
.button.blue {
background-color: #2C92FF;
}
.button.red {
background-color: #FF2113;
}
.button.green {
background-color: #1EBE32;
}
<button class="button">button</button>
Try this in .button:
-webkit-font-smoothing: antialiased;
This is caused AFAIK from the browser changing the text antialias type during the animation (new GPU implicit layer).
More info on causes and fixes here -> How to prevent Webkit text rendering change during CSS transition
Can be reduced using perspective and 3d scale:
transform: perspective(1px) scale3d(1.07,1.07,1);

Resources