CSS transition on vertical-align property - css

How can I make a div that has vertical align:top, change it's position to vertical-align: middle, but have a smooth animated transition through those states?
I can't get it to work, is vertical-align non transitionable?
Code:
.outer{
width:200px;
height:200px;
background:red;
display:table;
text-align:center;
}
.inner{
width:100px;
height:100px;
vertical-align:bottom;
display:table-cell;
transition:all 2s;
}
.inner:hover{
vertical-align:middle;
}
Here's the fiddle: https://jsfiddle.net/7amp783t/

Instead of changing the vertical-align property, use transform: translateY() to move up and down.
I've had a fiddle and got this code to work:
.outer{
width:200px;
height:200px;
background:red;
display:table;
text-align:center;
}
.inner{
width:100px;
height:100px;
display:table-cell;
vertical-align: bottom;
transition: all 2s ease-in-out;
}
.inner:hover{
transform: translateY(-50%);
}
The translateY(-50%) moves the text "up" (hence the -) by 50% of the height of its parent box.
EDIT You don't need to worry about setting the position: relative of the parent div. I've removed it for clarity.
I've updated your JSFiddle, also.

if you want to use vertical-align, you should not use inside a table-cell but with 2 elements side by side, where you can set values around the line-height they produce. In this case, vertical-align can take lots predefined values or numerics values.
https://www.w3.org/wiki/CSS/Properties/vertical-align#Values
Because your example gives an height of 200px, we may play around this : https://jsfiddle.net/7amp783t/5/
.outer{
font-size:18px;/* whatever */
width:200px;
height:200px;
background:red;
text-align:center;
}
.outer:before {
content:'';
display:inline-block;
height:100%;/* here the line is 100% height of container: set to 200px */
vertical-align:calc(-200px + 1em); /*chrome instead bottom */
}
.inner{
vertical-align:0;/* from bottom : calc(-200px + 2em) go all the way down minus room for text */;
display:inline-block;
transition:all 2s;
}
.outer:hover .inner{/* outer, else you need to follow inner */
vertical-align:calc(-100px + 1em);/* lets go half way */
}
<div class="outer">
<div class="inner">
Hover me box
</div>
</div>

CSS Transitions defines vertical-align as animatable, but only when interpolating between two length values.
┌────────────────┬───────────┐
│ Property Name │ Type │
├────────────────┼───────────┤
│ vertical-align │ as length │
└────────────────┴───────────┘
Therefore, transitions between bottom and middle won't be smooth.
If you were using vertical-align to align an inline-level element relatively to its line box, you could try using length values.
However, you are using it on a table cell, which has a different behavior, as defined in Table height algorithms. In particular, length values don't apply to table cells.

I think you have to use Jquery to create a sliding animation. You can use jQuery's toogle function to do so.
http://api.jquery.com/toggle/
http://api.jquery.com/slidetoggle/

Try transform instead of vertical aligning it to middle. This code will do same what you are trying to accomplish. Change your hover state to include translateY(-50%)
Working Demo
.outer{
width:200px;
height:200px;
background:red;
display:table;
text-align:center;
}
.inner{
width:100px;
height:100px;
vertical-align:bottom;
display:table-cell;
transition:all 2s;
}
.inner:hover{
transform: translateY(-50%);
}

u can try this sample:
using the transform: translateY(-50%); method
css vertical align animation
.block {
background: orange;
height: 500px;
}
.centered {
text-align: center;
background: pink;
position: relative;
top: 0;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
.block:hover .centered {
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
top: 50%;
}
let me know if u need further help

position:relative can be an easy way:
.outer {
width: 200px;
height: 200px;
background: red;
display: table;
text-align: center;
}
.inner {
position: relative;
top: 0;
display: table-cell;
transition: all 2s;
}
.inner:hover {
top: 50%;
}
<div class="outer">
<div class="inner">
Hover me
</div>
</div>

Related

Transform:scale issue

I have a div and im trying to scale it(just the Y) on hover.The problem is that it works well without transition.When i use transition,the div scales at top a bit and then goes down,check the fiddle.The question is how to prevent div from scalling like that?I want it to scale straight down,without this bouncing to top.
http://jsfiddle.net/q9akawr6/21/
HTML
<div></div>
CSS
div{
width:100px;
height:100px;
margin:100px;
background-color:red;
transition:.2s;
}
div:hover
{
transition:.2s;
transform-origin:top;
transform:scaleY(2.0);
}
As Niet says, or just put this in the div{} rule, rather than div:hover{}:
transition:.2s;
transform-origin:top;
for the same reason (to avoid a transition on transform-origin at the wrong time).
the div bouncing due to you margin area. I try to change your code. check the following
HTML:
<div class="container">
<div class="demo"></div>
</div>
CSS:
div.container
{
width:300px;
height:300px;
border:1px solid gray;
}
div.demo{
width:100px;
height:50px;
margin: 100px auto;
background-color:red;
transition:.2s;
transform-origin: left top;
}
div.demo:hover
{
transition:all .2s linear;
transform-origin: top;
transform:scaleY(2.0);
}

CSS animation moving and changing color

I am not that familiar with CSS animations. My client want to achieve the following result when hovering the contact button:
so to be clear:
the square's move from left to right and vice versa
when the square moves, the line underneath it changes color
the top image it the start state, the middle is during the effect (50%) and the bottom image is the end stage.
Is this achievable with only CSS or do I need JS as well?
How would I approach this?
I created a quick and dirty JSFiddle here: https://jsfiddle.net/x0b397pb/
As you can see, it is possible with just CSS. In this example I used pseudo elements (::before and ::after) to create most of the elements.
You mentioned "Im not that familiar with CSS animations". For this I used transitions.
transition: left 1000ms, right 1000ms, box-shadow 1000ms;
Each comma separated element is a value that will transition between 2 points. This transition happens on a change of the div, this can be on a hover, but also when applying another div (Through JS).
To created the effect of the lines gradually shifting in color I used another element that slides on top of the original two lines. The new lines originally have 0 width, but on hover they gain 100% width. With a transition transition: width 1000ms; this happens gradually.
Try not to use my code as your final example, as it is somewhat ugly. But I hope it gets the point across.
Here is a small demonstration of css transition:
Consider this HTML:
<div class="container">
<div class="box"></div>
</div>
With this CSS:
.container {
position: relative;
width: 400px;
height: 400px;
border: solid 1px black;
}
.box {
position: absolute;
width: 40px;
height: 40px;
top: 10px;
left: 10px;
background-color: red;
transition: all 1s;
}
.container:hover {
border-color: blue;
.box {
top: 200px;
left: 200px;
width: 160px;
height: 160px;
background-color: blue;
}
}
Or, check it on JsFiddle: https://jsfiddle.net/ronency/75ozjq3s/
.box {
background: linear-gradient(80deg, #f3efef, #90009f, #01060d);
background-size: 600% 600%;
animation: AnimationName 29s ease infinite;
}
#keyframes AnimationName {
0%{background-position:0% 51%}
50%{background-position:100% 50%}
100%{background-position:0% 51%}
}

triggering a transition of a element when hovered on another element

i have a div which is box and a 'p' element whose opacity is set to 0. when i hover over the div i want the 'p' elements opacity to change to 1. i have the following code . its looks proper to me but its not working. i could not figure out the problem with it. can some one help me. thanks in advance.
HTML:
<p class="se">Hover over the div element below</p>
<div class="box"></div>
css:
.se{
position: relative;
color:red;
opacity:0;
}
.se{
-webkit-transition: opacity 2s;
transition: opacity 2s;
}
.box{
position: relative;
left: 400px;
width: 100px;
height: 100px;
background: red;
}
box:hover + .se{
opacity:0;
}
jsFiddle http://jsfiddle.net/2f1k5yq4/
CSS selector +
Any element that is the next sibling of a previous element (that is: the
next child of the same parent)
.box {
position: relative;
left: 400px;
width: 100px;
height: 100px;
background: red;
}
.se {
color: red;
opacity: 0;
position: relative;
transition: 1s linear;
}
.box:hover + .se {
opacity: 1;
}
<div class="parent">
<div class="child">
<div class="box"></div>
<p class="se">Hover over the div element below</p>
</div>
</div>
This is actually a kind of annoying problem with just CSS. I'd prefer using Javascript, especially if you already have jQuery loaded into your site.
But, that said, this can sort of be done in CSS. I'm going to assume you just want to show/hide an element, so I'll use display for the example rather than opacity, so modify as needed to use a transition.
The first problem you have is that you box:hover rather than .box.hover. The second problem is a bit more annoying. Abhitalks is correct that +, the adjacent sibling selector, only selects the next sibling, so you can't have the text above the div that will show it. If, however, you switch the order, moving the div above the text, it works. You could use some clever positioning, floating, etc. in order to make the order of their appearance different than their order in the markup, if you wanted.
.se{
display: none
}
.box{
width: 100px;
height: 100px;
background: red;
}
.box:hover + .se{
display: block;
}
<div class="box"></div>
<p class="se">Hover over the div element below</p>

CSS Transform-Origin and image alignment

Hi I am trying to rotate an image when i hover over a div using Rotate and Transform-Origin. It was works when the image is centered in the div but when i change the vertical position of the image and make the relevant changes to the Transform-origin values the image still rotates but is slightly off center. Any ideas would be appreciated.
jsfiddle here (only seems to work in firefox)
http://jsfiddle.net/boyle/U3yLk/
html
<div class="box">
<div class="c">
<img class="pic" src="p.png"/>
</div>
css
.box{
width: 200px;
height: 300px;
background-color: #4781AA;
display:block;
}
.c{
width:200px;
height:300px;
display:block;
transition: all 1s ease;
}
.c:hover{
transform: rotate(180deg);
transform-origin: 100px 150px;
}
.pic {
position: relative;
left:50px;
top: 100px;
}
This also only seems to work in Firefox!
Cheers
try this
.transform{
transform:rotate(60deg);
-ms-transform:rotate(60deg);
-moz-transform:rotate(60deg);
-webkit-transform:rotate(60deg);
-o-transform:rotate(60deg);
}

When using CSS transformation, how to avoid an element to overflow its parent?

I'm using CSS transformation to rotate some elements. However, rotated elements may render partially outside its parent.
Is it possible to avoid this behavior?
To illustrate my thoughts, here is a jsfiddle that highlight this.
There is two divs :
<div id="g">
<div id="inner"></div>
</div>
and few css rules:
#g {
background:silver;
width:400px;
height:400px;
margin:100px 100px 100px 100px;
border:solid 6px green
}
#inner {
background:blue;
width:100px;
height:100px;
transform:rotate(45deg);
-webkit-transform:rotate(45deg);
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
}
In fact, the blue rectangle should not render outside the gray rectangle.
Actual:
Expected:
The main idea behind this question, is to add some kind of viewport with interactivity, but never outside the viewport.
Add overflow: hidden; to #g. See this: http://jsfiddle.net/Z5TZ3/5/.
It should look like this:
#g {
background:silver;
width:400px;
height:400px;
margin:100px 100px 100px 100px;
border:solid 6px green;
overflow: hidden;
}
#inner {
background:blue;
width:100px;
height:100px;
transform:rotate(45deg);
-webkit-transform:rotate(45deg);
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
}
That'll give you:

Resources