This question already has answers here:
How can I transition height: 0; to height: auto; using CSS?
(41 answers)
Closed 8 years ago.
I have a website, and I decided to replace the jquery based toggle boxes with pure CSS snippets. When I use fixed height value for the transition (last lines of the CSS), it works well, but with the auto value, the animation is missing, only the height change has an effect!
Is there a way to use this with auto value? I would like to use variable texts and no scripts.
.ac-container{
width: 400px;
margin: 10px auto 30px auto;
text-align: left;
}
.ac-container label{
font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
padding: 5px 20px;
position: relative;
z-index: 20;
display: block;
height: 30px;
cursor: pointer;
color: #777;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
line-height: 33px;
font-size: 19px;
background: #ffffff;
background: -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
background: -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -ms-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: linear-gradient(top, #ffffff 1%,#eaeaea 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
1px 0px 0px 0px rgba(255,255,255,0.9) inset,
0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container label:hover{
background: #fff;
}
.ac-container input:checked + label,
.ac-container input:checked + label:hover{
background: #c6e1ec;
color: #3d7489;
text-shadow: 0px 1px 1px rgba(255,255,255, 0.6);
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container input{
display: none;
}
.ac-container section{
background: rgba(255, 255, 255, 0.5);
margin-top: -1px;
overflow: hidden;
height: 0px;
position: relative;
z-index: 10;
-webkit-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-moz-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-o-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-ms-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
}
.ac-container section p{
font-style: italic;
color: #777;
line-height: 23px;
font-size: 14px;
padding: 20px;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}
.ac-container input:checked ~ section{
-webkit-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
-moz-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
-o-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
-ms-transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
transition: height 0.5s ease-in-out, box-shadow 0.1s linear;
box-shadow: 0px 0px 0px 1px rgba(155,155,155,0.3);
}
.ac-container input:checked ~ section.ac-small{
height: 120px; /*auto*/
}
<div class="ac-container">
<div>
<input id="ac-1" name="accordion-1" type="checkbox" />
<section class="ac-small">
<p>Some content... </p>
</section>
<label for="ac-1">About us</label>
</div>
<div>
<input id="ac-2" name="accordion-2" type="checkbox" />
<section class="ac-small">
<p>Some content... </p>
</section>
<label for="ac-2">About us</label>
</div>
</div>
One solution if you just want to use CSS is to transition max-height instead of height and set it to something greater than it will ever get ...
Here's a DEMO
You will need to tweek the speed of the transition a bit, but at least the example gives you an idea on how it can be done. Don't forget to change the property in your transition as well. From transition: height 0.5s; to transition: max-height 0.5s;.
.ac-container{
width: 400px;
margin: 10px auto 30px auto;
text-align: left;
}
.ac-container label{
font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;
padding: 5px 20px;
position: relative;
z-index: 20;
display: block;
height: 30px;
cursor: pointer;
color: #777;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
line-height: 33px;
font-size: 19px;
background: #ffffff;
background: -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
background: -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -ms-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: linear-gradient(top, #ffffff 1%,#eaeaea 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
1px 0px 0px 0px rgba(255,255,255,0.9) inset,
0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container label:hover{
background: #fff;
}
.ac-container input:checked + label,
.ac-container input:checked + label:hover{
background: #c6e1ec;
color: #3d7489;
text-shadow: 0px 1px 1px rgba(255,255,255, 0.6);
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container input{
display: none;
}
.ac-container section{
background: rgba(255, 255, 255, 0.5);
margin-top: -1px;
overflow: hidden;
max-height: 0px;
position: relative;
z-index: 10;
-webkit-transition: max-height 0.3s ease-in-out, box-shadow 0.6s linear;
-moz-transition: max-height 0.3s ease-in-out, box-shadow 0.6s linear;
-o-transition: max-height 0.3s ease-in-out, box-shadow 0.6s linear;
-ms-transition: max-height 0.3s ease-in-out, box-shadow 0.6s linear;
transition: max-height 0.3s ease-in-out, box-shadow 0.6s linear;
}
.ac-container section p{
font-style: italic;
color: #777;
line-height: 23px;
font-size: 14px;
padding: 20px;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}
.ac-container input:checked ~ section{
-webkit-transition: max-height 0.5s ease-in-out, box-shadow 0.1s linear;
-moz-transition: max-height 0.5s ease-in-out, box-shadow 0.1s linear;
-o-transition: max-height 0.5s ease-in-out, box-shadow 0.1s linear;
-ms-transition: max-height 0.5s ease-in-out, box-shadow 0.1s linear;
transition: max-height 0.5s ease-in-out, box-shadow 0.1s linear;
box-shadow: 0px 0px 0px 1px rgba(155,155,155,0.3);
}
.ac-container input:checked ~ section.ac-small{
max-height: 500px; /*auto*/
}
<div class="ac-container">
<div>
<input id="ac-1" name="accordion-1" type="checkbox" />
<section class="ac-small">
<p>Some content...Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... Some content... </p>
</section>
<label for="ac-1">About us</label>
</div>
<div>
<input id="ac-2" name="accordion-2" type="checkbox" />
<section class="ac-small">
<p>Some content... </p>
</section>
<label for="ac-2">About us</label>
</div>
</div>
You can't animate to or from a dimension of "auto" (unfortunately). My usual approach for this is to animate the height of an outer DIV which has a single child which is a style-less DIV used only for measuring the content height.
function growDiv() {
var growDiv = document.getElementById('grow');
if (growDiv.clientHeight) {
growDiv.style.height = 0;
} else {
var wrapper = document.querySelector('.measuringWrapper');
growDiv.style.height = wrapper.clientHeight + "px";
}
}
#grow {
-moz-transition: height .5s;
-ms-transition: height .5s;
-o-transition: height .5s;
-webkit-transition: height .5s;
transition: height .5s;
height: 0;
overflow: hidden;
outline: 1px solid red;
}
<input type="button" onclick="growDiv()" value="grow">
<div id='grow'>
<div class='measuringWrapper'>
<div>
The contents of my div.
</div>
<div>
The contents of my div.
</div>
<div>
The contents of my div.
</div>
<div>
The contents of my div.
</div>
<div>
The contents of my div.
</div>
<div>
The contents of my div.
</div>
</div>
</div>
You should use scaleY.
ul {
background-color: #eee;
transform: scaleY(0);
transform-origin: top;
transition: transform 0.3s ease-in-out;
}
p:hover ~ ul {
transform: scaleY(1);
}
<p>Here (scaleY(1))</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Or you can use clip to cut the list.
ul {
clip: rect(auto, auto, 0, auto);
position: absolute;
margin: 0;
padding: .5rem;
color: white;
background-color: rgba(0, 0, 0, 0.8);
transition-delay: 0.29s;
transition-property: clip;
transition-duration: 0.5s;
transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
h3:hover ~ ul,
h3:active ~ ul {
clip: rect(auto, auto, 10rem, auto);
}
<h3>Hover here</h3>
<ul>
<li>This list</li>
<li>is clipped.</li>
<li>A clip transition</li>
<li>will show it</li>
</ul>
<p>
Some text...
</p>
CSS transition doesn't work with auto value. Get the scroll height with JavaScript el.scrollHeight or use max-height instead.
auto isn't an appropriate type for an animatable property, see CSS Transitions: 7. Animatable properties. You need either a length (px,em,...) or percentage (13.37%).
Thus a CSS only solution isn't possible, as long as auto isn't added to the list. You'll need to use JavaScript or a specific length value.
Related
I'm using this accordion for a menu but it has a panel which is open automatically, I'm wondering how I can have it so no panels are open until it is clicked. This is a pure css menu but I can't figure out why it's open automatically.
body {background: #e0e3ec url(http://netcribe.com/example/bgnoise_lg.jpg) repeat top left;}
.ac-container{
width: 400px;
margin: 10px auto 30px auto;
}
.ac-container label{
font-family: 'Arial Narrow', Arial, sans-serif;
padding: 5px 20px;
position: relative;
z-index: 20;
display: block;
height: 30px;
cursor: pointer;
color: #777;
text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
line-height: 33px;
font-size: 19px;
background: -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
background: -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: -ms-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
background: linear-gradient(top, #ffffff 1%,#eaeaea 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
1px 0px 0px 0px rgba(255,255,255,0.9) inset,
0px 2px 2px rgba(0,0,0,0.1);
}
}
.ac-container label:hover{
background: #fff;
}
.ac-container input:checked + label,
.ac-container input:checked + label:hover{
background: #c6e1ec;
color: #3d7489;
text-shadow: 0px 1px 1px rgba(255,255,255, 0.6);
box-shadow:
0px 0px 0px 1px rgba(155,155,155,0.3),
0px 2px 2px rgba(0,0,0,0.1);
}
.ac-container label:hover:after,
.ac-container input:checked + label:hover:after{
content: '';
position: absolute;
width: 24px;
height: 24px;
right: 13px;
top: 7px;
background: transparent url(http://netcribe.com/example/arrow_down.png) no-repeat center center;
}
.ac-container input:checked + label:hover:after {
background-image: url(http://netcribe.com/example/arrow_up.png);
}
.ac-container input{
display: none;
}
.ac-container article{
background: rgba(255, 255, 255, 0.5);
margin-top: -1px;
overflow: hidden;
height: 0px;
position: relative;
z-index: 10;
-webkit-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-moz-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-o-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-ms-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
}
.ac-container input:checked ~ article {
-webkit-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-moz-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-o-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
-ms-transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
transition: height 0.3s ease-in-out, box-shadow 0.6s linear;
box-shadow: 0px 0px 0px 1px rgba(155,155,155,0.3);
height: 100px;
}
.ac-container article p{
font-style: italic;
color: #777;
line-height: 23px;
font-size: 14px;
padding: 20px;
}
change <input id="ac-1" name="accordion-1" type="checkbox" checked />
to <input id="ac-1" name="accordion-1" type="checkbox" />
Remove the checked. It seems the accordion relies on these checkboxes being checked. Remove the checked and it won't open.
i've got following problem:
when im using transform scale for hover the image shows judder.
Here my Code:
<div class="item">
</div>
.item {
background: transparent url("http://s14.directupload.net/images/141019/s3r8avxj.jpg") no-repeat 0 0 / 176px 176px;
width: 176px;
height: 176px;
display: inline-block;
font-family: Arial;
margin: 0px 10px 10px 0px;
border: 5px solid #fff;
-webkit-box-shadow: 0 0 2px 1px rgba(0,0,0,0.4);
box-shadow: 0 0 2px 1px rgba(0,0,0,0.4);
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
-ms-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
}
.item:hover {
transform: scale(1.05);
border: 5px solid #fff;
cursor: default;
}
Fullscreen view: http://jsfiddle.net/excsa15w/embedded/result/
Code: http://jsfiddle.net/excsa15w/
Is there a possibility to remove the judding?
Thanks in advance
Change property
background: transparent url("http://s14.directupload.net/images/141019/s3r8avxj.jpg") no-repeat 0 0 / 176px 176px;
To
background: transparent url("http://s14.directupload.net/images/141019/s3r8avxj.jpg") no-repeat;
jsfiddle
Using CSS I would like to place a hollow circle behind a social icon and when I hover over the social icon the hollow circle is then filled, when I attempt to do this the circle is squished to the right of my social icon (fontawesome). I want the icon to be centered within the circle, also I would like to know the best way of going about this if I wanted to have have several social icons aligned horizontally each within a circle a distance of 100px apart. Thanks.
My CSS code:
.circle {
border: 2px solid #666;
border-radius: 50%;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
opacity: 1;
-webkit-box-shadow: 0 0 1px 0px rgb( 255, 255, 255);
box-shadow: 0 0 1px 0px rgb( 255, 255, 255);
width: 80px;
height: 80px;
z-index: 86;
-webkit-transition: background-color .5s ease-in-out;
-moz-transition: background-color .5s ease-in-out;
-o-transition: background-color .5s ease-in-out;
-ms-transition: background-color .5s ease-in-out;
transition: background-color .5s ease-in-out;
}
.circle:hover {
background-color: #666;
}
.fa-twitter {
color: #fff;
z-index: 99;
}
Wordpress - Header.php
<div class='header-social-icons'>
<div class='twitter'>
<i class='fa fa-twitter'></i>
<i class='circle'></i>
</div>
</div>
Just wrap your social icon inside the circle div.
HTML:
<div class='header-social-icons'>
<div class='twitter circle'>
<i class='fa fa-twitter'></i>
</div>
</div>
Updated CSS:
.fa-twitter { display:block; }
.circle { width: 50px; height: 50px; padding: 15px;}
DEMO
Updated HTML:
<div class='header-social-icons'>
<div class='twitter'>
<i class='fa fa-twitter'></i>
<i class='circle'>Twitter</i>
<i class='circle'>Facebook</i>
<i class='circle'>Google+</i>
<i class='circle'>Reddit</i>
</div>
</div>
Updated CSS:
.circle {
display: inline-block;
margin-right: 100px;
text-align: center;
line-height: 75px;
border: 20px solid #666;
border-radius: 50%;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
opacity: 1;
-webkit-box-shadow: 0 0 1px 0px rgb( 255, 255, 255);
box-shadow: 0 0 1px 0px rgb( 255, 255, 255);
width: 80px;
height: 80px;
z-index: 86;
-webkit-transition: background-color .5s ease-in-out;
-moz-transition: background-color .5s ease-in-out;
-o-transition: background-color .5s ease-in-out;
-ms-transition: background-color .5s ease-in-out;
transition: background-color .5s ease-in-out;
}
Demo.
I hope this helped. Updated to align text's width and height.
I use some pretty straightforward css to show a larger image on hover. This is the HTML structure:
<div class="Enlarge">
<img src="small.jpg" />
<span><img src="large.jpg" /></span>
</div>
And here's the CSS:
.Enlarge {
position:relative;
}
.Enlarge span {
position:absolute;
left: -9999px;
}
.Enlarge span img {
margin-bottom:5px;
}
div.Enlarge:hover{
z-index: 999;
cursor:pointer;
}
div.Enlarge:hover span{
top: 110px;
left: 0px;
z-index: 999;
width:500px;
height:300px;
padding: 10px;
background:#eae9d4;
-webkit-box-shadow: 0 0 20px rgba(0,0,0, .75));
-moz-box-shadow: 0 0 20px rgba(0,0,0, .75);
box-shadow: 0 0 20px rgba(0,0,0, .75);
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius:8px;
font-family: 'Droid Sans', sans-serif;
font-size:12px;
line-height:18px;
text-align: center;
color: #495a62;
padding-bottom:20px;
}
However, I would like to add an ease in/out effect to the larger image. I couldn't work that out. If I apply the transition to the , the image will slide in from the left side. This is not what I want. If I apply the effect to the image, it won't work.
Here's the example: Example
Thanks in advance for your input!
Using visibility and opacity you can achieve a fade effect.
JSFiddle Demo
Add these styles:
.Enlarge span {
position:absolute;
left: -9999px;
visibility: hidden;
opacity: 0;
-webkit-transition: opacity 0.5s ease;
-moz-transition: opacity 0.5s ease;
-ms-transition: opacity 0.5s ease;
-o-transition: opacity 0.5s ease;
transition: opacity 0.5s ease;
}
div.Enlarge:hover span {
visibility: visible;
opacity: 1;
/* rest of your styles below */
I'm trying to make a menu with definitions on the left and links on the right - but I can't get all the box to be a link. I could solve it with table I guess, but I hope there is a smoother solution.
So I want to float left one side of the line and right the other.
Here's my CSS:
li {
margin: 0px 10px 0px 100px;
padding: 30px 5px 0px 5px;
border-bottom: 2px solid black;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
display: block;
-moz-transition: background-color .3s ease-in;
-webkit-transition: background-color .3s ease-in;
-o-transition: background-color .3s ease-in;
transition: background-color .3s ease-in;
}
li:hover {
background-color: #CCC;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
a {
float: right;
color: black;
text-decoration: none;
}
And the HTML:
<ul>
<li>Blog#.blogspot.com</li>
<li>Twitter##</li>
<li>Google+Google+</li>
<li>Contact me##gmail.com</li>
</ul>
Or much better, here: http://jsfiddle.net/hJRdN/
It is done here on jsfiddle
just replace html and css as in js fiddle
<ul>
<li>Blog<span>#.blogspot.com</span></li>
<li><span>Twitter</span>##</li>
<li>Google+<span>Google+</span></li>
<li>Contact me<span>##gmail.com</span></li>
</ul>
CSS
li {
float:left;
width:400px;
margin: 0px 10px 0px 100px;
border-bottom: 2px solid black;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
display: block;
-moz-transition: background-color .3s ease-in;
-webkit-transition: background-color .3s ease-in;
-o-transition: background-color .3s ease-in;
transition: background-color .3s ease-in;
}
li:hover {
background-color: #CCC;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
a {
display:block;
padding: 30px 5px 0px 5px;
color: black;
text-decoration: none;
width:390px;
}
a span{ float:right}
Is it necesary to have unordered list? If not you could easily achieve that with just using floated divs.
<a href="#">
<div class="row">
<div class="left">blog</div><div class="right">blogspot.com</div>
</div>
</a>
<a href="#">
<div class="row">
<div class="left">twitter</div><div class="right">twitter.com</div>
</div>
</a>
And css:
.row { width: 100%; float: left; }
.row:hover { background-color: #e3e3e3; }
.left { float: left; }
.right { float: right; }
.row .left { color: #000; }
.row .right { color: #000; }
Check out this fiddle: http://jsfiddle.net/dejvidpi/7a6mp/