I'm trying to create a CSS3 only dropdown menu with transitions but it's not working 100% as I would like it to. I want the underline hover state to be the same even when you hover over the expanding dropdown menu.
Here's my first try:
http://codepen.io/Winterfox/pen/wGmEbY
ul {
text-align: center;
li {
display:inline-block;
margin-right: 15px;
transition:all 0.3s ease-in-out;
&:hover {
.submenu {
height: 85px;
}
}
.submenu {
overflow:hidden;
position:absolute;
left: 0;
width: 100%;
background-color: #3862a0;
height: 0;
margin-top: 10px;
line-height: 40px;
box-sizing:border-box;
transition:height 0.3s ease-in-out;
transition-delay: 0.1s;
a {
color: #fff;
margin-top: 20px;
font-size: 16px;
&:hover {
color: #fff;
text-decoration:underline;
}
}
}
a {
color: #999;
display: block;
padding: 0 7px 0 7px;
margin: 0 0 0 0;
text-decoration: none;
position: relative;
&:hover {
color: #3862a0;
&::before {
visibility: visible;
transform: scale(1, 1);
}
}
&::before {
content: "";
position: absolute;
width: 100%;
height: 3px;
bottom: -10px;
left: 0px;
background-color: #3862a0;
transition: all 0.2s ease-in-out;
transform: scale(0, 0);
visibility: hidden;
}
}
}
}
As you can see the hover state is removed when hovering the expanded dropdown menu. I tried to fix this by moving the transitions to the li element as shown here:
http://codepen.io/Winterfox/pen/ZWxqvp
But then I got the problem that 1# when changing the position on the hover state the dropdown doesn't look good when it closes again. 2# The blue underline for the menu text now shows at the bottom of the page.
Any suggestions how to fix this with CSS3 only?
I think your thought was going in the right direction but instead of applying the styles to the li you could just move the anchor inside the li:hover block, as in:
li {
...
&:hover {
...
a {
color: #3862a0;
&::before {
visibility: visible;
transform: scale(1, 1);
}
}
}
}
Full code here: http://codepen.io/anon/pen/VaxrwE
Related
.free-btn {
a {
position: relative;
.fbtn-bg {
display: inline-block;
font-size:20px;
font-weight: 600;
color:#000;
padding:20px 85px;
background: #f4f4f4;
border-radius: 14px;
z-index: 100;
position: relative;
transition: all .5s ease-in-out;
&:hover {
top:10px;
right: -10px;
transition: all .5s ease-in-out;
}
}
}
}
I want to transition for buttun but it is not working. Where is my mistake, can u help me guys?
instead of using right and left you should use translate().
the hover selector should look like this:
:hover {
transform: translate(10px , 10px);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I hover over word it should shows an appropriate pict as background
Considering to make it with js dataset but it still won't work
<ul>
<li>word</li>
<li>word2</li>
<li>word3</li>
<li>word4</li>
</ul>
li a {
display: block;
text-decoration: none;
font-size: 3.3rem;
color: $dark-clr;
padding: 10px;
transition: all 0.4s ease;
font-weight: bold;
letter-spacing: 1.2px;
text-transform: capitalize;
&:hover {
color: $icons-clr;
}
}
li a::before {
content: url(/pict/1.jpg);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
z-index: -1;
transition: all 0.4s ease;
}
li a:hover::before {
opacity: 0.5;
}
Can't use attr src
Apply the image as a background-image on the pseudo element (:before). You will also need to apply position: relative to the anchors so the background element is positioned accordingly. Then you can apply unique background by giving each item in the list a unique class or id.
li a {
position: relative; /* so the absolutely positioned pseudo element works */
display: block;
text-decoration: none;
font-size: 3.3rem;
color: $dark-clr;
padding: 10px;
transition: all 0.4s ease;
font-weight: bold;
letter-spacing: 1.2px;
text-transform: capitalize;
&:hover {
color: $icons-clr;
}
}
li a::before {
content: ''; /* empty content */
background-size: cover; /* make sure the backgrounds cover the element */
position: absolute;
top: 0;
left: 0;
width: 100%; /* cover the full width of the anchor */
height: 100%; /* cover the full height of the anchor */
opacity: 0;
z-index: -1;
transition: all 0.4s ease;
}
li a:hover::before {
opacity: 0.5;
}
/* unique backgrounds for each list item anchor */
li a#item1::before {
background-image: url('https://picsum.photos/250/50');
}
li a#item2::before {
background-image: url('https://picsum.photos/200/50');
}
li a#item3::before {
background-image: url('https://picsum.photos/220/50');
}
li a#item4::before {
background-image: url('https://picsum.photos/240/50');
}
<ul>
<li>word</li>
<li>word2</li>
<li>word3</li>
<li>word4</li>
</ul>
See the following button animation:
html {
background: white;
font-family: Arial;
}
.button {
position: relative;
display: inline-block;
color: #000;
border: 2px solid #000;
padding: 10px 24px;
font-size: 20px;
font-weight: 600;
text-align: center;
text-decoration: none;
transition-property: color, background, border-color;
transition-duration: 0.3s;
}
.button:hover {
color: #fff;
}
.button:hover ._background:after {
transform: translateX(0);
animation: fill-horizontal 0.3s linear 0s 1;
}
.button ._background {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: -1;
overflow: hidden;
}
.button ._background:after {
content: "";
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #000;
transform: translateX(100%);
transition: transform .3s;
}
#keyframes fill-horizontal {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
<a class="button" href="javascript:">
<div class="_background"></div>
Button
</a>
The intended animation is to sweep the ._background:after element in from the left, and then out to the right like so:
translateX(-100%)
translateX(0) - Hover
translateX(100%) - Remove Hover
Whilst the animation works as intended when the user hovers for the duration of the CSS animation (.3s), it looks terrible if the user 'unhovers' before the CSS animation completes.
I would like the transition to translateX(100%) to continue from where the animation finished. Is this even possible?
NOTE - I am aware that the div._background element is not necessary, this has additional functionality that is not relevant to this question.
You can consider the same effect differently in order to avoid this bad effect:
Here is an idea using background animation where the trick is to change the position only after the size has changed.
.button {
position: relative;
display: inline-block;
color: #000;
border: 2px solid #000;
padding: 10px 24px;
font-size: 20px;
font-weight: 600;
text-align: center;
text-decoration: none;
background-image:linear-gradient(#000,#000);
background-size:0% 100%;
background-position:left;
background-repeat:no-repeat;
background-origin:border-box;
transition:color 0.3s, background-size 0.3s, background-position 0s 0.3s;
}
.button:hover {
color:#fff;
background-size:100% 100%;
background-position:right;
}
<div class="button">Some text</div>
Using this method, you will have a transition back in case you unhover rapidly.
A hacky idea to force the animation to complete is to consider a pseudo element that will make the hover area bigger and be sure you will keep the hover until the end:
.button {
position: relative;
display: inline-block;
color: #000;
border: 2px solid #000;
padding: 10px 24px;
font-size: 20px;
font-weight: 600;
text-align: center;
text-decoration: none;
background-image:linear-gradient(#000,#000);
background-size:0% 100%;
background-position:left;
background-repeat:no-repeat;
background-origin:border-box;
transition:color 0.3s, background-size 0.3s, background-position 0s 0.3s;
}
.button:hover {
color:#fff;
background-size:100% 100%;
background-position:right;
}
.button:hover:before {
content:"";
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
z-index:99;
animation:remove 0s 0.3s forwards;
}
#keyframes remove {
to {
top:100%;
}
}
<div class="button">Some text</div>
This is my modification of someone else's hover effect. So I am not familiar with the working of btn-2 class.(I don't know the syntax used)
Here is my CSS code:
* {
box-sizing: inherit;
transition-property: all;
transition-duration: .6s;
transition-timing-function: ease;
}
body {
background-color: black;
}
a {
text-decoration: none;
color: tomato;
font-size: 50px;
font-family: 'Oswald', sans-serif;
}
.container {
padding: 1em;
text-align: center;
vertical-align: middle;
}
.btn-2 {
letter-spacing: 10px;
}
.btn-2:hover,
.btn-2:active {
letter-spacing: 30px;
}
.btn-2:after,
.btn-2:before {
border: 1px solid rgba(tomato, 0);
bottom: 2px;
top: 2px;
content: " ";
display: block;
margin: 0 auto;
position: relative;
transition: all 280ms ease-in-out;
width: 0;
}
.btn-2:hover:after,
.btn-2:hover:before {
backface-visibility: hidden;
border-color: tomato;
transition: width 350ms ease-in-out;
width: 50%;
}
.btn-2:hover:before {
bottom: auto;
top: 0;
width: 50%;
}
I want to use the effect for button in my navigation bar. But I have 3 problems to solve:
I want the lines above and below the word that appear when you hover it to be of the same width as the word.
I want the word to be centered relative to the line. That is, the line should grow out from the middle point of the word.
The lines isn't going where the word is going.
Some discoveries I make, which I don't know the cause of:
The 2 lines will be longer when .comtainer{padding=1em} than 5em.
When I delete text-align and vertical-align in the .container class, the hovering lines stay centered, but the word goes to the left of the window.
I'm not sure how good I understand you, but here some example I made
a {
color: #333;
display: inline-block;
padding: 15px 0;
position: relative;
text-align: center;
text-decoration: none;
transition: all 250ms ease-in-out;
&:before,
&:after{
content: '';
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 0;
height: 2px;
background-color: #333;
transition: all 250ms ease-in-out;
}
&:before {
top:0;
}
&:after {
bottom: 0;
}
&:hover {
letter-spacing: 5px;
&:before,
&:after {
width: 100%;
}
}
//Trick is here
span {
&:before {
content:attr(title);;
letter-spacing: 5px;
display:block;
height:1px;
color:transparent;
overflow:hidden;
visibility:hidden;
margin-bottom:-1px;
}
}
}
<span title="Hover Me">Hover Me</span>
You can check my example here
My animation does not seem to want to go both on and off the hover. I have tried putting the animation line on the LI:before itself and swapping the 0% and 100% but then nothing happens at all. I've been messing around with it for hours to no avail.
EDIT: Updated link, code
JSFiddle
ul {
display: flex;
flex-direction: row;
align-items: center;
list-style-type: none;
background-color: $base-gray-t;
margin: 1em;
padding: .25em 0 0 0;
height: 3em;
border-bottom: 0.375em solid $secondary-color;
overflow: hidden;
box-shadow: .05em .05em 1em 0;
}
li {
position: relative;
color: $base-gray-light;
padding: 0.40em;
font-size: 1.5em;
font-family: 'Montserrat', sans-serif;
cursor: pointer;
z-index: 2;
}
li:not(.active):not(:first-child):before {
content: '';
display: block;
position: absolute;
bottom: .01em;
width: 100%;
height: 2em;
margin-left: -.4em;
border-radius: .25em .25em 0 0;
z-index: -1;
}
li:hover:before {
background: $primary-color;
animation: splash .3s ease;
bottom: .01em
}
#keyframes splash {
0% {
bottom: -2em;
border-radius: 100%;
}
100% {
bottom: .01em;
}
}
If you are trying to complete the animation which you created on hover then for that add forwards along-with your animation properties as below,
li:hover:before {
background: $primary-color;
animation: splash .3s ease forwards;
bottom: .01em
}
Check this working jsfiddle link.
The transition method is much less involved and has less scope for problems so i edited your fiddle to use that instead:
https://jsfiddle.net/6t8xLssv/1/
I simply transitioned the :before element on hover, except in the case of the active li.
li:before{
content:'';
display:inline-block;
position:absolute;
bottom:0;
right:0;
left:0;
height:0;
transition:height 0.2s linear;
background:pink;
border-radius:5px 5px 0 0;
z-index:-1;
}
li:hover:before{
transition:height 0.2s linear;
height: 2em;
}
li.active:before{
display:none;
}
I hope this helps