Responsive menu with underline animation - css

I'm working on a menu with a sliding underline with target, I am really close but I can't figure to make it responsive. The "underline" doesn't stick at the center of the link when resizing the window.
Here is a JSFiddle
nav {
margin-top:30px;
font-size: 15pt;
background: #FFF;
position: relative;
width: 100%;
height:50px;
}
nav a {
text-align:center;
background: #FFF;
display: block;
float: left;
padding: 2% 0;
width: 33.33%;
text-decoration: none;
transition: .4s;
color: red;
}
.effect {
position: absolute;
left: 22.5%;
transition: 0.4s ease-in-out;
}
nav a:nth-child(1):target ~ .effect {
left: 22.5%;
/* the middle of the first <a> */
}
nav a:nth-child(2):target~ .effect {
left: 56%;
/* the middle of the second <a> */
}
nav a:nth-child(3):target ~ .effect {
left: 90%;
/* the middle of the third <a> */
}
.ph-line-nav .effect {
width: 34px;
height: 2px;
bottom: 5px;
background: blue;
margin-left:-50px;
}

Each element is 33.33% wide. Divide that in half, that's 16.66%, so that will be the center of the element. Using 16.66% as the default left value will put the left edge of .effect in the center of the first element. To center the .effect in the true center, move it back 50% of it's own with with translateX().
So the first element's left should be 16.66%.
The second element will be 49.99% (99.99 / 2)
The third element will be 83.33% (99.99 - 16.6 or 66.66 + 16.66)
nav {
margin-top:30px;
font-size: 15pt;
background: #FFF;
position: relative;
height:50px;
display: flex;
}
nav a {
text-align:center;
background: #FFF;
display: block;
padding: 2% 0;
flex-basis: 33.33%;
text-decoration: none;
transition: .4s;
color: red;
}
.effect {
position: absolute;
left: 16.66%;
transition: 0.4s ease-in-out;
transform: translateX(-50%);
}
nav a:nth-child(1):target ~ .effect {
left: 16.66%;
/* the middle of the first <a> */
}
nav a:nth-child(2):target~ .effect {
left: 49.99%;
/* the middle of the second <a> */
}
nav a:nth-child(3):target ~ .effect {
left: 83.33%;
/* the middle of the third <a> */
}
.ph-line-nav .effect {
width: 34px;
height: 2px;
bottom: 5px;
background: blue;
}
<nav class="ph-line-nav">
AA
AA
AA
<div class="effect"></div>
</nav>

Related

animating :after pseudo element does not work

I am new to coding, I have tried various solutions from similar questions asked, but nothing seems to work.
I have the following problem:
I made a breadcrumb, including various divs with :before and :after elements - shaping them like connected arrows.
The problem is, I made an animation for the breadcrumb element to fade-in-down. However, no matter in what combination I tried -> The :after element does not animate properly, or at all.
It kinda just pops up after :before and the parent div .breadcrumb have finished sliding in. Can anybody help me here, or has anyone maybe have aa clue?
... I have tried animating the :after element separatly, with transition, with animation-delay etc...nothing seems to work...everything slides in fine from top, excep the :after element, which just pops up ugly after the animation from my ".breadcrumb" is finished.
<div class="breadcrumb_wrapper">
Lasermaschinen
Serien
Unterserien
Produkt
.breadcrumb_wrapper {
position: fixed;
display: flex;
align-items: center;
width: 100%;
height: auto;
margin-top: 120px;
text-align: center;
}
.breadcrumb_element {
padding: 0 2rem 0 2rem;
width: auto;
line-height: 32px;
min-height: 32px;
max-height: 32px;
background: var(--Breadcrumb-gradient);
position: relative;
text-align: center;
margin-left: 0.5rem;
color: var(--nav-text-color-and-general-background);
cursor: pointer;
text-decoration: none;
animation: fade-in-down 0.5s ease-in-out;
}
.breadcrumb_element:before {
content: "";
position: absolute;
top: 4.69px;
left: -11.32px;
width: 22.427px;
height: 22.427px;
background: var(--nav-text-color-and-general-background);
transform: rotate(45deg);
z-index: 1;
border-top: 1px solid #F0F0F0;
border-right: 1px solid #F0F0F0;
border-top-right-radius: 5px;
}
.breadcrumb_element:after {
content: "";
position: absolute;
top: 4.69px;
right: -11.6px;
width: 22.427px;
height: 22.427px;
background: var(--Breadcrumb-arrow-gradient);
transform: rotate(45deg);
z-index: 2;
border-top-right-radius: 5px;
animation: fade-in-down 0.5s ease-in-out;
}
#keyframes fade-in-down {
0% {
opacity: 0;
transform: translateY(-20px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
Without seeing your HTML code I just had to take a guess at how you need this but here is an example with the after pseudo element coming in from the top and sitting on top of the before pseudo. Hope this helps you get what you are looking for. You can have it come in from any direction that you want.
ul {
list-style: none;
display: flex;
overflow: hidden;
}
li {
margin-left: 20px;
margin-right: 20px;
position: relative;
}
li::before {
content: '';
width: 20px;
height: 20px;
background-color: green;
position: absolute;
right: -25px;
}
li::after {
content: '';
width: 20px;
height: 20px;
background-color: red;
position: absolute;
right: -25px;
top: -100px;
transition: all 1s;
}
li:hover:after {
top: 0;
}
<ul>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
</ul>

CSS hover should rotate vertical text to horizontal

I would like a vertically aligned text to rotate in the horizontal direction when it is hovered. As an animation. Unfortunately I can't get any further at the moment. It is about the link on the right ("back"). Who knows how to do it? Thanks for advance!
.container {
display: block;
position: relative;
border: solid 3px #ccc;
padding: 40px 60px;
max-width: 400px;
margin: 100px auto 0;
}
#nav {
position: absolute;
top: 50px;
left: 20px;
}
#nav a {
writing-mode: vertical-rl;
text-orientation: mixed;
transform: rotate(180deg);
font-weight: bold;
color: var(--text-color)
}
#nav a {
writing-mode: vertical-rl;
text-orientation: mixed;
transform: rotate(180deg);
}
#nav a:hover {
writing-mode: vertical-rl;
text-orientation: mixed;
transform: rotate(180deg);
color: red;
}
<div id="nav">
back
</div>
<div class="container">123</div>
The display property has to be changed to block for the transform to work. You don’t need to change the direction when the transform works. You can set the transform to initial on :hover and set transition for the animation.
.container {
display: block;
position: relative;
border: solid 3px #ccc;
padding: 40px 60px;
max-width: 400px;
margin: 100px auto 0;
}
#nav {
position: absolute;
top: 50px;
left: 20px;
}
#nav a {
display: block;
transform: rotate(-90deg);
transition: .3s;
font-weight: bold;
color: var(--text-color)
}
#nav a:hover {
transform: initial;
color: red;
}
<div id="nav">
back
</div>
<div class="container">123</div>

Change animation origin on mouse off event

I've got some css/html code. I wanna improve my "unhover" state by doing following: when I hover over a button there is a before element sliding from left to right. I can easily change it from right to left. However when I do the "unhover" action, before element slides in the opposite direction - from right to left. What I want to achieve is animating it's width from 100% to 0% but from left to right. What should I do to get the result?
https://codepen.io/trueFalse24/pen/YzKNgYm
a{
background: #7f8c8d;
padding: 20px 30px;
text-transform: uppercase;
color: #fff;
position: relative;
&:before{
content: '';
position: absolute;
top: 0;
left: 0;
width: 0%;
height: 100%;
background: rgba(236,240,241, 0.3);
transition: all 0.3s ease;
}
&:hover{
&:before{
width: 100%;
}
}
}
I've modified your pen to get the effect by changing a few usages of the left and right properties. My edits are marked by comments below.
.container{
padding:0;
margin: 0;
box-sizing: border-box;
background: #3498db;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
a{
background: #7f8c8d;
padding: 20px 30px;
text-transform: uppercase;
color: #fff;
position: relative;
&:before{
content: '';
position: absolute;
top: 0;
right: 0; /* Replaced left */
width: 0%;
height: 100%;
background: rgba(236,240,241, 0.3);
transition: all 0.3s ease;
}
&:hover{
&:before{
width: 100%;
right: auto; /* Added */
left: 0; /* Added */
}
}
}
}

Transition with fixed position from right to left

I have a problem when try to make transition effect on fixed element from right to left. When I hover, all li elements transition too.
https://jsfiddle.net/k0fow2jb/
You got it fixed here
https://jsfiddle.net/k0fow2jb/3/
the important thing was to add margin-left:auto to your .side-menu li
.side-menu {
z-index: 999;
overflow: hidden;
position: fixed;
top: 25%;
right: 0;
}
.side-menu li{
cursor: pointer;
display: block;
list-style-type: none;
width: 40px;
height: 40px;
overflow: hidden;
position: relative;
transition: width 0.5s;
margin-left:auto;
}
.side-menu li a{
position: relative;
text-decoration: none;
color: #FFFFFF;
}
.ask-questions {
background: #19b5fe;
}
.ask-questions:hover{
width: 150px;
}
.facebook-link {
background: #3b5998;
}
.side-menu li:hover{
width:150px;
}
.support-box{
background: #dd4b39;
}

div disappears when hovering over it or it leaves a white space

I have asked this question before here div disappears when hovering over it and replacing margin with position relative fixed the problem but now I get white space
http://jsfiddle.net/x3a6frgx/3/ (note the space between div2 and the bottom border) how can I fix that?
css code:
#body {
border-width: 10px;
border-color: red;
border-style: groove;
}
#image {
display: block;
}
#div1 {
transition: opacity 1s;
background-color: red;
opacity: 0;
position: relative;
top: -20px;
width: 100px;
}
#div1:hover {
opacity: 1;
}
#div2 {
background-color: blue;
}
Instead of top, use margin-top for #div1:
#div1 {
transition: opacity 1s;
background-color: red;
opacity: 0;
position: relative;
margin-top: -20px;
width: 100px;
}

Resources