Ensure a div fits p to one line - html

I have the following, which animates a message. The issue is with the width of #AdvertBox. If I don't set a width, it expands to 100% of the page. If I set an arbitrary width and it's too small, the second message takes up two lines. What should I set it to so it will always be as wide as the second message, maybe with a small amount of padding?
#AdvertBox {
height: 55px;
overflow: hidden;
position: relative;
background: black;
color: white;
border: 1.75px solid yellow;
font-size: 35px;
font-style: italic;
border-radius: 1px;
width: 45vw;
}
.scroll-left p {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
line-height: 55px;
text-align: center;
/* Starting position */
transform: translateX(100%);
/* Apply animation to this element */
animation: scroll-left 10s linear infinite;
}
#keyframes scroll-left {
0% {
transform: translateX(100%);
}
25% {
opacity: 1;
transform: translateX(0%);
}
39% {
opacity: 0;
transform: translateX(0%);
}
100% {
opacity: 0;
transform: translateX(0%);
}
}
.popIn p {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
line-height: 55px;
text-align: center;
/* Starting position */
transform: translateY(-100%);
/* Apply animation to this element */
animation: popIn 10s linear infinite;
}
#keyframes popIn {
/* Move it left */
0% {
transform: translateY(-100%);
}
/* Stop It */
30% {
transform: translateY(-100%);
}
/* fade out */
42% {
transform: translateX(0%);
}
/* fade out */
70% {
transform: translateX(0%);
}
100% {
transform: translateX(-100%);
}
}
<div id="AdvertBox">
<div class="scroll-left">
<p style="position: absolute; z-index: 1 ">
First Message
</p>
</div>
<div class="popIn">
<p style="position: absolute; z-index: 2 ">
Second, longer message to drop down
</p>
</div>
</div>

You can add white-space: nowrap; to your .popIn p style to prevent the text from wrapping.
I would also recommend removing the width: 100%; from the .popIn p so that your scrolling animation does end to early. With width: 100%; set on the <p> it will inherit the width of the parent, which will be shorter then the overflowing text of the <p>, making you animation end early.
You might also want to make these same style updates to your other animated <p>
See this fiddle for a demo

Related

I am trying to apply the below animation of vertical flip but it doesn't work

I have tried applying animation of rotating word vertically with the help of CSS and HTML but it does't work. Can anyone tell me that what is the problem with the code.
.line {
width: 100%;
height: 4rem;
overflow: hidden;
border: 1px solid black;
padding: 0;
margin-bottom: 16px;
}
/* flipping class and key frames*/
keyframes anim-flipX {
0% {
opacity: 0;
transform: rotateX(90def);
}
50% {
opacity: 1;
transform: rotateX(720deg);
}
100% {
/* animate nothing to pause animation at the end */
opacity: 1;
transform: rotateX(720deg);
}
}
<div class='line'>
<h2 class='flipX'>flip vertical</h2>
</div>
You have a couple of things wrong with your syntax. Here is the fixed up version that works. You were missing in # at beginning of keyframe, missing the animation duration and animation name in the class. There was also a type (def instead of deg).
.line {
width: 100%;
height: 4rem;
overflow: hidden;
border: 1px solid black;
padding: 0;
margin-bottom: 16px;
animation-duration: 3s;
animation-name: anim-flipX;
}
/* flipping class and key frames*/
#keyframes anim-flipX {
0% {
opacity: 0;
transform: rotateX(90deg);
}
50% {
opacity: 1;
transform: rotateX(720deg);
}
100% {
/* animate nothing to pause animation at the end */
opacity: 1;
transform: rotateX(720deg);
}
}
<div class='line'>
<h2 class='flipX'>flip vertical</h2>
</div>
You have to add animation-name and animation-duration properties:
.line {
width: 100%;
height: 4rem;
overflow: hidden;
border: 1px solid black;
padding: 0;
margin-bottom: 16px;
animation-name: anim-flipX; /* new */
animation-duration: 2s; /* new */
}
Also you need to add # before keyframes:
#keyframes anim-flipX {
0% {
opacity: 0;
transform: rotateX(90deg);
}
50% {
opacity: 1;
transform: rotateX(720deg);
}
100% {
opacity: 1;
transform: rotateX(720deg);
}
}
And your animation should work.

CSS animation not behaving as expected

Problem
I've made a simple css animation, but it's not behaving as I expect it.
The idea is for the animation to draw a straight line (from top downwards) , and the disappear (also from the top downwards).
The start of the line moves down a bit, as the animation starts, then up again to stay at set position (same goes for the bottom at the end of the animation).
Question
How do I get the start of the line to stay at one position instead of 'bouncing' down and up?
Expected behavior
Actual behavior
Code
.lineWrapper {
width: 1px;
height: 300px;
margin: auto;
position: relative;
}
.lineWrapper .line {
width: 100%;
height: 100%;
background: #000;
animation: scrollLine 5s linear infinite;
}
#keyframes scrollLine {
0% {
transform: scaleY(0);
}
10% {
transform: scaleY(0);
transform-origin: top;
}
30% {
transform: scaleY(1);
}
70% {
transform: scaleY(1);
}
90% {
transform: scaleY(0);
transform-origin: bottom;
}
100% {
transform: scaleY(0);
}
}
<div class="lineWrapper">
<div class="line"></div>
</div>
Codepen
https://codepen.io/strazan/pen/RwPYgjq
The default transform-origin is center so if you omit it in the initial and last state it will be set to center. You need to also have an instant change of the transform-origin in the middle:
.lineWrapper {
width: 1px;
height: 300px;
margin: auto;
position: relative;
}
.line {
height: 100%;
background: #000;
animation: scrollLine 5s infinite;
}
#keyframes scrollLine {
0%,10% {
transform: scaleY(0);
transform-origin: top;
}
49.9% {
transform: scaleY(1);
transform-origin: top;
}
50% {
transform: scaleY(1);
transform-origin: bottom;
}
90%,100% {
transform: scaleY(0);
transform-origin: bottom;
}
}
<div class="lineWrapper">
<div class="line"></div>
</div>
I have made similar CSS animation with some different code lines.
body {
margin: 0px;
display: flex;
justify-content: center;
background: black;
overflow: hidden;
}
.line-wrapper {
height: 800px;
width: 8px;
background: tranparent;
display: flex;
justify-content: center;
animation: down 2s linear infinite;
}
#keyframes down {
0% {
transform: translateY(0px);
}
15% {
transform: translateY(0px);
}
30% {
transform: translateY(0px);
}
60% {
transform: translateY(90px);
}
90% {
transform: translateY(115px);
}
100% {
transform: translateY(115px);
}
}
.line {
height: 8px;
width: 4px;
background: Gray;
animation: scrollLine 2s ease-in-out infinite;
}
#keyframes scrollLine {
100% {
height: 800px;
}
}
.eraser {
height: 0px;
width: 4px;
background: black;
animation: rmv 2s linear infinite;
}
#keyframes rmv {
55% {
height: 0px;
}
100% {
height: 800px;
}
}
<div class="line-wrapper">
<div class="line">
<div class="eraser"></div>
</div>
</div>

z-index issues with CSS animation

First time posting here, have long used the site as a resource for any problems I come across but for the first time I haven't been able to fix my code using previous answers.
There's probably a very simple answer for what I'm asking and I'm just missing something.
I am trying to create a snippet that I can use in a production site soon. I want to create an image "frame" that sits around an image. I also then want this frame to be animated and swing from side to side.
Before I insert the animation code, the code is as below, and works perfectly (the frame is above the image).
.frame {
margin-top: 5rem;
margin-left: 35%;
display: inline-flex;
background-image: url(http://www.studiopastimes.info/tester/images/frame.svg);
background-repeat: no-repeat;
z-index: 1000;
padding: 3rem;
box-sizing: border-box;
}
.internal-frame {
position: relative;
width: 400px;
z-index:-5;
}
.internal-frame img {
width: 100%;
}
However, when I add the animation code, this breaks the z-index.
#-webkit-keyframes swinging{
0%{-webkit-transform: rotate(10deg);}
33%{-webkit-transform: rotate(-5deg);}
66%{-webkit-transform: rotate(10deg);}
100%{-webkit-transform: rotate(-5deg);}
}
#keyframes swinging{
0%{-webkit-transform: rotate(10deg);}
33%{-webkit-transform: rotate(-5deg);}
66%{-webkit-transform: rotate(10deg);}
100%{-webkit-transform: rotate(-5deg);}
}
.swingimage{
-webkit-transform-origin: 50% 0;
transform-origin: 50% 0;
-webkit-animation: swinging 5s ease-in-out ;
animation: swinging 5s ease-in-out ;
animation-fill-mode: forwards;
backface-visibility: hidden;
}
This can all be seen in the Codepen here: https://codepen.io/anon/pen/RMrrWB
Any idea how I can get the CSS animation to work, whist still having the frame above the image? I understand from my research there issues with CSS animations and z-index, but I couldn't get any of the suggested fixes to work. Any help would be great and if there's any issues with how I have asked this question please let me know!
Thanks
I'd put the frame on a pseudo-element:
.frame {
margin-top: 5rem;
margin-left: 35%;
box-sizing: border-box;
width: 400px;
padding-top: 69.25%; /* this is the aspect ratio of the image: height / width */
position: relative;
}
.frame:after {
content:'';
display:block;
position: absolute;
z-index: 2;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-image: url(http://www.studiopastimes.info/tester/images/frame.svg);
background-repeat: no-repeat;
padding: 20px;
}
img {
position: absolute;
top: 20px; /* match the padding */
left: 0;
width: 100%;
z-index: 1;
}
#-webkit-keyframes swinging {
0% {
-webkit-transform: rotate(10deg);
}
33% {
-webkit-transform: rotate(-5deg);
}
66% {
-webkit-transform: rotate(10deg);
}
100% {
-webkit-transform: rotate(-5deg);
}
}
#keyframes swinging {
0% {
-webkit-transform: rotate(10deg);
}
33% {
-webkit-transform: rotate(-5deg);
}
66% {
-webkit-transform: rotate(10deg);
}
100% {
-webkit-transform: rotate(-5deg);
}
}
.swingimage {
-webkit-transform-origin: 50% 0;
transform-origin: 50% 0;
-webkit-animation: swinging 5s ease-in-out;
animation: swinging 5s ease-in-out;
animation-fill-mode: both;
backface-visibility: hidden;
}
<div class="frame swingimage">
<img src="http://www.studiopastimes.info/tester/images/cabaret.jpg" class="img">
</div>
The z-index property needs the element to have a set position. then if you just add position: relative; to your .frame it'll make the job.
full css :
.frame {
margin-top: 5rem;
margin-left: 35%;
display: inline-flex;
position: relative;
background-image: url(http://www.studiopastimes.info/tester/images/frame.svg);
background-repeat: no-repeat;
position: relative
z-index: 10;
padding: 3rem;
box-sizing: border-box;
/* height: 10rem;
width: 20rem;*/
}
.internal-frame {
position: relative;
width: 400px;
z-index:-5;
}
.internal-frame img {
width: 100%;
z-index:-5;
}
#-webkit-keyframes swinging{
0%{-webkit-transform: rotate(10deg);}
33%{-webkit-transform: rotate(-5deg);}
66%{-webkit-transform: rotate(10deg);}
100%{-webkit-transform: rotate(-5deg);}
}
#keyframes swinging{
0%{-webkit-transform: rotate(10deg);}
33%{-webkit-transform: rotate(-5deg);}
66%{-webkit-transform: rotate(10deg);}
100%{-webkit-transform: rotate(-5deg);}
}
.swingimage{
-webkit-transform-origin: 50% 0;
transform-origin: 50% 0;
-webkit-animation: swinging 5s ease-in-out ;
animation: swinging 5s ease-in-out ;
animation-fill-mode: both;
backface-visibility: hidden;
}
with your html :
<!DOCTYPE html>
<html>
<head>
<title>FRAME TESTER</title>
</head>
<body>
<div class="frame">
<div class="internal-frame">
<img src="http://www.studiopastimes.info/tester/images/cabaret.jpg">
</div>
</div>
</body>
</html>

Why doesn't my animation work in ie 11

I have the following animation (fiddle):
<style style="text/css">
#AdvertBox {
height: 55px;
overflow: hidden;
position: relative;
background:black;
color: white;
border: 1.75px solid yellow;
font-size: 35px;
font-style: italic;
border-radius: 1px;
width:45vw;
}
.scroll-left p
{
position: absolute;
width: 100%;
height: 100%;
margin: 0;
line-height: 55px;
text-align: center;
/* Starting position */
transform:translateX(100%);
/* Apply animation to this element */
animation: scroll-left 10s linear infinite;
}
#keyframes scroll-left {
0% {
transform: translateX(100%);
}
25% {
opacity: 1;
transform: translateX(0%);
}
39% {
opacity: 0;
transform: translateX(0%);
}
100% {
opacity: 0;
transform: translateX(0%);
}
}
.popIn p
{
position: absolute;
width: 100%;
height: 100%;
margin: 0;
line-height: 55px;
text-align: center;
/* Starting position */
transform:translateY(-100%);
/* Apply animation to this element */
animation: popIn 10s linear infinite;
}
#keyframes popIn {
/* Move it left */
0% {
transform: translateY(-100%);
}
/* Stop It */
30% {
transform: translateY(-100%);
}
/* fade out */
42% {
transform: translateX(0%);
}
/* fade out */
70% {
transform: translateX(0%);
}
100% {
transform: translateX(-100%);
}
}
</style>
<div id="AdvertBox" >
<div class="scroll-left">
<p style="position: absolute; z-index: 1 ">
First Message
</p>
</div>
<div class="popIn">
<p style="position: absolute; z-index: 2 ">
Second, longer message to drop down
</p>
</div>
</div>
It should have one item slide in from the right, fade out, then one item drop down on it. It looks fine in chrome and firefox, and the first item that slides left works in ie, but the second item, dropping down, doesn't do anything.
i have updated your fiddle please have a look again, it works on IE 11, but you may need to tweak the animation.
basically instead of translateX and translateY, i used translate(x,y);
it worked on IE 11
ok i forked that old one and i created my own fiddle. so your popIn animation has been changed to the following.
#keyframes popIn {
/* Move it left */
0% {
transform: translate(0%,-100%);
}
/* fade out */
50% {
transform: translate(0%,0%);
}
/* fade out */
75% {
transform: translate(0%,0%);
}
100% {
transform: translate(-100%,0%);
}
}
https://jsfiddle.net/qvx4b311/

Centered animation is consistently off-center

I've been struggling with this for the past few days, so help would be greatly appreciated. I have a Title with a line (hr element) right below it. I'm trying to have a div centered in the hr that grows and shrinks. However, when the css3 animation is applied it causes the div to be displaced down and to the right, as if the div's top-left point (which I think is (0,0)) is set to be where the middle was.
I've created a jsfiddle to illustrate what I mean.
Here's my html:
<div id="header">
<h1>Center</h1>
<div id="action-bar">
<hr class="center-line" />
<div class="circle animation"></div>
</div>
</div>
and my css:
div#header {
color: #000;
width: 90%;
text-align: center;
position:absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);
}
div#header h1 {
font-size: 50px;
font-weight: 300;
margin-top: 0px;
margin-bottom: 0px;
}
/* the line beneath h1 */
div #action-bar {
margin: 25px 0;
position: relative;
}
div.circle {
width: 1em;
height: 1em;
background: #000;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
div.circle:hover {
width: 2em;
height: 2em;
background: #000;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
position: absolute;
}
hr.center-line {
border: 0;
height: .25em;
background: #000;
}
/* animation */
#keyframes pulse {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
#-webkit-keyframes pulse {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.animation {
animation: pulse 2s ease-in-out 0s infinite normal none;
-webkit-animation: pulse 2s ease-in-out 0s infinite normal none;
-webkit-backface-visibility: hidden;
}
Can anybody point be in the right direction? I'm looking for a pure-css solution if possible. Thanks!
Add negative margin to your circle element, half of it's width and height:
div.circle {
width: 1em;
height: 1em;
background: #000;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
position: absolute;
margin-left: -0.5em;
margin-top: -0.5em;
}
div.circle:hover {
width: 2em;
height: 2em;
margin-left: -1em;
margin-top: -1em;
}
jsFiddle Demo.
Here is a smooth pulsing option.
http://jsfiddle.net/aLjsut5r/4/
/* animation */
#keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(.8);
}
100% {
transform: scale(1);
}
}
#-webkit-keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(.8);
}
100% {
transform: scale(1);
}
}
.animation {
animation: pulse 2s ease-in-out 0s infinite normal none;
-webkit-animation: pulse 2s ease-in-out 0s infinite normal none;
-webkit-backface-visibility: hidden;
}
.pulsing {
border: 3px solid #999;
-webkit-border-radius: 30px;
height: 18px;
width: 18px;
position: absolute;
left:20px;
top:214px;
-webkit-animation: pulsate 1s ease-out;
-webkit-animation-iteration-count: infinite;
opacity: 0.0;
}
#-webkit-keyframes pulsate {
0% {-webkit-transform: scale(0.5, 0.5); opacity: 0.5;}
50% {opacity: 1.0;}
100% {-webkit-transform: scale(1.2, 1.2); opacity: 0.5;}
}