CSS Animations - set duration on responsive animations - css

I'm trying to learn CSS animations and one thing I can't figure out and couldnt find on the web is how to set the proper duration.
I'm trying to have my website responsive therefore the font size would change depending on the size of the view. I have the following code so far:
#media screen {
.EntranceDiv{
top: 40%;
position: relative;
}
h1 {
font-size: 4rem;
margin: 0px;
}
.helloworld{
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid #D9FAFF; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .8em;
display: inline-block;
animation:
typing initial steps(30, end),
blink-caret .5s step-end infinite;
}
#keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: #D9FAFF }
}
#keyframes typing {
from { width: 0 }
to { width: 65% }
}
}
My problem is that with the changes in the font size, the animation either goes for too long or for too short and then the whole thing just pops on the screen. What is a good way of setting duration for responsive animations

Your main issue here is that the width you set is relative to parent container so it has no relation with the content of your inline-block element. You need to find a way to correctly set the width of the element.
Since you cannot make a transition to width:auto, here is an idea where I duplicate the content and I use a pseudo-element with absolute position. The first content will define the width and will be hidden and the second one will be visible and I can stretch to fit the defined width using left/right properties:
h1 {
font-size: 4rem;
margin: 0px;
}
.helloworld {
letter-spacing: .8em;
display: inline-block;
position:relative;
visibility:hidden;
white-space: nowrap;
}
.helloworld:after {
content:attr(data-content);
display:block;
visibility:visible;
overflow: hidden;
white-space: nowrap;
position:absolute;
top:0;
left:0;
bottom:0;
right:100%;
border-right: .15em solid #D9FAFF;
animation: typing 2s steps(30, end) forwards, blink-caret .5s step-end infinite;
}
#keyframes blink-caret {
from,
to {
border-color: transparent
}
50% {
border-color: #D9FAFF
}
}
#keyframes typing {
from {
right:100%
}
to {
right: 0%
}
}
}
<h1 data-content="lorem" class="helloworld">
Lorem
</h1>
<h1 data-content="lor" class="helloworld">
Lor
</h1>
<h1 data-content="lorem ipsume" class="helloworld">
Lorem ipsume
</h1>

Related

Make a smooth transition to items inside a container

I would like to bring "Hello world" inside a white container from left to right but I don't want the container to move.
Need to move just text and the text flashes from left to right like a quick slider movement pleasing to the eye.
How to achieve this using CSS animation?
body {
background: red
}
.container {
background: white;
color: black;
padding: 20px;
margin: 20px;
}
<div class="container">
<h1>Hello world</h1>
</div>
for making the animation use #keyframes
and for making the text not visible if outside use overflow
forwards for saving the last keyframes of animation.
body {
background: red
}
.container {
background: white;
color: black;
padding: 20px;
margin: 20px;
}
.container {
overflow: auto; /* use "hidden" instead if it shows a unnecessary scrollbar. */
}
h1 {
animation: toRight 0.2s ease-in forwards;
transform: translateX(-50%);
}
#keyframes toRight {
100% {
transform: translateX(0);
}
}
<div class="container">
<h1>Hello world</h1>
</div>

How to make an element disappear and stay gone after hovering over it?

So here is my code...
I understand how to make the text disappear by making it transparent but i want it to stay gone after hovering over it so it doesnt come back - how do I accomplish this?
.disappear {
margin-top: 60px;
text-align: center;
transition: all 5s ease .3s;
font-family: Bungee Spice;
}
.disappear:hover {
color: transparent;
}
you need to use onmouseover and remove() like this
function bye() {
const dis = document.getElementById("dis");
dis.remove();
}
* {
padding: 0;
margin: 0;
/* border: 1px solid red; */
overflow-x: hidden;
}
div {
height: 50vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
font-size: xx-large;
overflow: auto;
background: lightblue;
}
<div class="div">
<h2 onmouseover="bye()" id="dis">will go on hover</h2>
</div>
I don't think it's possible to make it run smoothly with pure CSS, so far, this is what I think is close to what you want to accomplish. So before hover, the animation to make it gone is already set, but the animation is not running yet, the animation will run only if the element is hovered. The problem here is that when it's hovered then it's unhovered before it's gone, the element will be half gone as the animation is paused.
.container {
width: 100%;
height: 800px;
background: #dddddd;
}
.disappear {
margin-top: 60px;
text-align: center;
font-family: Bungee Spice;
background: yellow;
animation: example 5s linear forwards;
animation-play-state: paused;
}
.disappear:hover {
animation-play-state: running;
}
#keyframes example {
from {opacity: 1}
to {opacity: 0}
}
<div class="container">
not disappear
<div class="disappear">
DISAPPEAR
</div>
</div>
The better way would be to use javascript and use onmouseover to add the animation instead of using :hover, the difference is that when you onmouseout, the function is still executed (the animation persists). This is with JS:
function fade(elm) {
elm.style.animation = "example 5s linear forwards";
}
.container {
width: 100%;
height: 800px;
background: #dddddd;
}
.disappear {
margin-top: 60px;
text-align: center;
font-family: Bungee Spice;
background: yellow;
}
#keyframes example {
from {
opacity: 1
}
to {
opacity: 0
}
}
<div class="container">
not disappear
<div class="disappear" onmouseover="fade(this)">
DISAPPEAR
</div>
</div>

100% high vertical marquee with CSS at fixed scroll speed

I need to replace a vertical scroller with a CSS equivalent, for compliance reasons. The replacement needs to be a 100% high marquee, scrolling vertically at a fixed speed.
This example does what I need it to with three exceptions: https://codepen.io/strongpom/pen/qmooZe
I've tried using the code linked above, but find a way to accommodate the following three requirements:
(1) I need it to be 100% high, and for the text to scroll from the top to the bottom.
(2) I can't specify the length of the slider div, because it will be determine by (variable) contents. This means I cant set absolute scroll positions, other than that the top of the div should start at the bottom of the viewport, and keep scrolling until the bottom of that div hits the top of the viewport.
and
(3) The scrollspeed cannot be fixed at e.g. 15 seconds, because if there's lots of content, it then scrolls too fast, whilst if there's little content, it scrolls too slowly. I need to be able to specify a fixed scrolling speed, independent of length.
.container {
width: 20em;
height: 10em;
margin: 2em auto;
overflow: hidden;
background: #ffffff;
position: relative;
}
.slider {
top: 1em;
position: relative;
box-sizing: border-box;
animation: slider 15s linear infinite;
list-style-type: none;
text-align: center;
}
.slider:hover {
animation-play-state: paused;
}
#keyframes slider {
0% { top: 10em }
100% { top: -14em }
}
.blur .slider {
margin: 0;
padding: 0 1em;
line-height: 1.5em;
}
.blur:before, .blur::before,
.blur:after, .blur::after {
left: 0;
z-index: 1;
content: '';
position: absolute;
width: 100%;
height: 2em;
background-image: linear-gradient(180deg, #FFF, rgba(255,255,255,0));
}
.blur:after, .blur::after {
bottom: 0;
transform: rotate(180deg);
}
.blur:before, .blur::before {
top: 0;
}
p {
font-family: helvetica, arial, sans serif;
}
<div class="container blur">
<ul class="slider">
<li><p> Hello, it's me</p></li>
<li><p> I was wondering if after all these years you'd like to meet</p></li>
<li><p>To go over everything</p></li>
<li><p> They say that time's supposed to heal ya</p></li>
<li><p> But I ain't done much healing</p></li>
</ul>
</div>
I could not find a pure CSS way to have a consistent speed, but with a little math and small javascript, you can achieve fixed speed with variable height items. The key factor below is the / 500 which is in pixels per second. The code below will scroll at approx 500 px per second, no matter the length of the items.
Height can accommodate your design.
This will loop smoothly according to your request.
The scroll speed is what you determine regardless of length.
The javascript/css is verbose for readability.
CodePen - If still available, otherwise code below.
HTML
<ul class=slider>
<li>Item</li>
...
</ul>
CSS
* { padding: 0; margin: 0; box-sizing: border-box; }
body { overflow: hidden; height: 100%; }
.slider {
position: absolute;
list-style-type: none;
text-align: center;
animation: slider linear infinite;
}
.slider li { line-height: 50px; width: 100vw; }
#keyframes slider {
0% { transform: translateY(100vh) }
100% { transform: translateY(-100%) }
}
JS
window.onload = function() {
var lineHeight = document.querySelector(".slider li").clientHeight;
var viewHeight = window.innerHeight;
var slider = document.querySelector(".slider");
var time = (slider.offsetHeight * 2.0 + viewHeight * 2) / 500.0;
slider.style.animationDuration = time + "s";
}

Stacking Order Changes with CSS3 Animation

I was following a guide for making a ribbon with CSS. However, I attempted to modify this by adding a CSS3 Animation to the position, as seen in this JSFiddle.
As you can see, with the animation, the main ribbon element falls behind the :before & :after pseudo-elements, instead of above them as it should (and does without the animation). I've tried explicitly setting the z-index on all elements but it doesn't seem to affect this. Does anyone know why this would be happening, or what I can do to fix it? For the record, I'm viewing through the latest Google Chrome and am not worried about cross-browser compatibility for the moment. Thank you!
Here's the ribbon code:
.ribbon {
font-size: 16px !important;
/* This ribbon is based on a 16px font side and a 24px vertical rhythm. I've used em's to position each element for scalability. If you want to use a different font size you may have to play with the position of the ribbon elements */
width: 50%;
position: relative;
background: #ba89b6;
color: #fff;
text-align: center;
padding: 1em 2em;
/* Adjust to suit */
margin: 2em auto 3em;
/* Based on 24px vertical rhythm. 48px bottom margin - normally 24 but the ribbon 'graphics' take up 24px themselves so we double it. */
/*CODE I ADDED*/
animation: flyRibbon 30s linear infinite;
-webkit-animation: flyRibbon 30s linear infinite;
}
.ribbon:before, .ribbon:after {
content:"";
position: absolute;
display: block;
bottom: -1em;
border: 1.5em solid #986794;
z-index: -1;
}
.ribbon:before {
left: -2em;
border-right-width: 1.5em;
border-left-color: transparent;
}
.ribbon:after {
right: -2em;
border-left-width: 1.5em;
border-right-color: transparent;
}
.ribbon .ribbon-content:before, .ribbon .ribbon-content:after {
content:"";
position: absolute;
display: block;
border-style: solid;
border-color: #804f7c transparent transparent transparent;
bottom: -1em;
}
.ribbon .ribbon-content:before {
left: 0;
border-width: 1em 0 0 1em;
}
.ribbon .ribbon-content:after {
right: 0;
border-width: 1em 1em 0 0;
}
Here's the animation code:
#keyframes flyRibbon {
100% {
transform: translateX(200vw);
-webkit-transform: translateX(200vw);
}
}
Adding a div around the ribbon and applying the animation css to it instead of to the h1 element should fix this.
HTML:
<div class="container">
<h1 class="ribbon">
<strong class="ribbon-content">Everybody loves ribbons</strong>
</h1>
</div>
CSS:
.container {
animation: flyRibbon 30s linear infinite;
-webkit-animation: flyRibbon 30s linear infinite;
}
JSFiddle
This is not a bug. This is a specified behavior of z-index for elements that create stacking contexts, and .ribbon creates a stacking context because it has transform (in the animation). With transform, the element becomes the root of the stacking context and no child box can be placed below it.
As a workaround without extra markup, you can give the inner contatiner block display and position it above the ribbon ends, like in this example.

Animated transition not starting at the right position

I'm working on a site with a knotted rope-style bar that expands to show more information on hover, and I'm having issues getting the animation to look right. (Here's a staging link: http://couchcreative.co/tcc/).
Currently, the bar for the first step will move down to the bottom of the box before it animates upwards to its new position, while I want it to just move up to its new position on hover without starting at the bottom of the hover box. Can anyone help explain why this is happening? The relevant CSS starts with the ".look" class.
Apologies if I'm not explaining this right, but hopefully when you visit the site you'll see what I mean about the animation looking a bit… off. Thanks for the help!
I would rework your HTML structure to make it more semantic and less repetitious.
Demo: http://jsfiddle.net/krmn4/5/
HTML:
<a href="/testicularcancer/" class="look">
<figure><img src="http://couchcreative.co/tcc/img/look.png" /></figure>
<div class="bar"></div>
<div class="off">
<h4>Look</h4>
</div>
<div class="on">
<h4>Relax your scrotum.</h4>
<p>Check your testicles just after you’ve had a bath or shower, when the muscles in the scrotum are relaxed, making it easier for you to feel any lumps, growths or tenderness. Stand in front of the mirror. Look for any swelling on the skin of your scrotum.</p>
<span>Learn More</span>
</div>
</a>
CSS:
.look {
position: absolute;
bottom: 0;
left: 0;
width: 235px;
overflow: hidden;
/* optional styling */
color: #000;
text-align: center;
text-decoration: none;
}
.look h4 {
/* optional styling */
line-height: 48px;
font-size: 20px;
font-weight: bold;
}
.look .bar {
height: 48px;
background: url(http://couchcreative.co/tcc/img/step_1.png) 0 0 repeat-x;
margin: -24px 0 0; /* half of height */
/* necessary so figure img doesn't overlap */
position: relative;
z-index: 1;
}
.look figure,
.look .off,
.look .on {
-webkit-transition: all 300ms linear;
-moz-transition: all 300ms linear;
transition: all 300ms linear;
height: 0;
opacity: 0;
overflow: hidden;
}
.look figure {
/* optional styling */
background-color: #b2d5e6;
padding: 12px;
margin: 0;
}
.look .off {
height: 48px;
opacity: 1;
}
/* hover state */
.look:hover .off {
height: 0;
opacity: 0;
}
.look:hover figure {
height: 120px; /* or however tall it needs to be */
opacity: 1;
}
.look:hover .on {
height: 220px; /* or however tall it needs to be */
opacity: 1;
}

Resources