Translate after on hover using CSS - html

I'm designing the front end of an e-commerce website and while looking through some inspiration I found a really nice effect involving a button and a after on that button, that when hovering over it, the text of the button would go up and at the same time an icon would replace it. You can see what I mean here. I probably won't use this on the project but I got really confused trying to mimic this effect while using Dev Tools, and just ending up with a cart icon on the bottom of the page and would love to know how to create something similar to this.
This is the final result
I almost got to something but I can't seem to make the text and the icon move at the same time, sometimes the icon wouldn't move at all and just the whole button would do, and not the text.
Any ideas on how this could be achieved with CSS? I already went through CodePen to find something similar and I'm not really sure how this effect is called to google it
EDIT: Already tried this code on an with a button class.
.button {
background: none;
font-weight: 600;
line-height: inherit;
margin: 0;
padding: 0 15px;
margin-top: 0;
position: relative;
text-align: center;
vertical-align: top;
-webkit-transition: color 0.15s linear 0s,-webkit-transform 0.3s linear 0s;
text-transform: uppercase;
transition: color 0.15s linear 0s,transform 0.3s linear 0s;
}
.button:hover {
-webkit-transform: translateY(-100%);
-moz-transform: translateY(-100%);
-ms-transform: translateY(-100%);
-o-transform: translateY(-100%);
transform: translateY(-100%);
}
.button:after {
background-color: inherit;
border-color: inherit;
border-style: inherit;
border-width: inherit;
font-size: 18px;
font-weight: 400;
height: auto;
margin: 0;
position: absolute;
left: 0;
top: 100%;
text-align: center;
width: 100%;
-webkit-transform: translateY(0);
transform: translateY(0);
-webkit-animation: none;
animation: none;
}
.button:hover:after {
top: 150%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}

Here is a simple example using a psuedo element and font-awesome icon
.btn {
background-color: turquoise;
border-radius: 10px;
color: white;
padding: 5px 10px;
position: relative;
overflow: hidden;
height: 20px;
display: inline-block;
transition: all .3s;
}
.btn span {
position: relative;
top: 0;
transition: all .3s;
}
.btn::after {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f217";
position: absolute;
left: 50%;
transform: translatex(-50%);
top: 40px;
transition: all .3s;
font-size: 20px;
}
.btn:hover {
background-color: blue;
}
.btn:hover span {
top: -30px;
}
.btn:hover::after {
top: 5px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" rel="stylesheet"/>
<a href="#" class="btn">
<span>Add to cart</span>
</a>

Here is a slightly simpler example using two different p tags instead of text/svg. It shouldn't be too much trouble to convert:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
button {
width: 100px;
height: 40px;
overflow: hidden;
border: unset;
border-radius: 10px;
background-color: turquoise;
transition: all 200ms;
}
button:hover {
background-color: blue;
}
div {
height: 80px;
transform: translateY(0%);
transition: inherit;
}
div:hover {
transform: translateY(-50%)
}
p {
display: flex;
align-items: center;
justify-content: center;
color: white;
height: 50%;
font-size: 18px;
font-weight: 600;
}
<button>
<div>
<p class="one">text one</p>
<p class="two">text two</p>
</div>
</button>

Related

Hover animation not smooth

I'm dipping my toes into web design for now and I'm trying to replicate the "buy more" button animation from here( https://themes.getbootstrap.com/preview/?theme_id=67539 ). While I did find a solution for the border, I can't make a smooth animation for the icon(I am using font awesome), instead it just jumps directly.
Thanks in advance for the help
My CSS is this for the button is:
and the html for it is `Buy now <i class="fas fa-level-down-alt navbar__btn--i"></i>`
.navbar__btn {
position: relative;
font-size: 1.2rem;
color: #fff;
display: inline-block;
margin-right: 7rem;
padding-top: .5rem;
}
.navbar__btn:after {
margin-top: .3rem;
display: block;
content: " ";
border-bottom: solid 2px #fff;
transform: scaleX(0);
transform-origin: 0% 100%;
transition: all 1s ease-in-out;
}
.navbar__btn:hover:after {
transform:scaleX(1.2);
}
.navbar__btn:hover .navbar__btn--i {
left: 7rem;
}
.navbar__btn--i {
position: absolute;
top: 5px;
transition: all 1s linear 1s;
}
And the html is
Buy now <i class="fas fa-level-down-alt navbar__btn--i"></i>
Replace the appropriate icon. I designed it according to the link. You can adjust the delays to your liking
.navbar__btn {
position: relative;
font-size: 1.2rem;
color: #fff;
display: inline-block;
margin-right: 7rem;
padding-top: .5rem;
color:black;
text-decoration:none;
color:white;
}
.navbar__btn:after {
margin-top: .3rem;
display: block;
content: " ";
border-bottom: solid 2px #fff;
transform: scaleX(0);
transform-origin: 0% 100%;
transition: all .5s ease-in-out;
}
.navbar__btn:hover:after {
transform:scaleX(1.3);
}
.navbar__btn:hover .navbar__btn--i {
right: -20px;
}
.navbar__btn--i {
position: absolute;
top: 5px;
right:-10px;
line-height:0px;
transition: all .3s linear 0s;
vertical-align:middle;
margin-top:15px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div style="heigth:100px;width:500px;background:red;padding:5px;">
Buy now <i class="fas fa-arrow-right navbar__btn--i"></i>
</div>

Over image text that only appears when hovering over the image

I got some simple code to make text appear when I hover over an image, the problem I run into is that the hover hitbox is bigger than the image. I looked at my CSS and tried a lot but I can't find the right bit of code that I need to change.
CSS:
.container {
position: relative;
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 30%;
height: 183px;
transition: .5s ease;
backface-visibility: hidden;
}
.middle {
transition: .5s ease;
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
.over_image_text {
background-color: #6552c7;
color: white;
font-size: 16px;
padding: 16px 32px;
}
html:
<div class='container'>
<img class="projects" src="../../images/spaceinvaders.jpg" alt="spaceinvaders"></a>
<div class="middle">
<div class="over_image_text">Read more about this project</div>
</div>
</div>
The code you are looking for is this class in your CSS:
.over_image_text {
background-color: #6552c7;
color: white;
font-size: 16px;
padding: 16px 32px;
}
The part which is interesting for you is the last line padding: 16px 32px;.
Try decreasing the pixel values to the size which would fit for you. If that's not enough, then you can also decrease the font-size here.

Icon animation when hovered

I wan the arrows on my navbar to produce the animation I've created when I hover the options on top of them,
When my mouse hovers "contact", "register" or "login", the arrows under them should move down indicating that they are dropdown buttons, how can I do this? I already have the code of the animation but I don't know how to sync this code with my buttons, I would appreciate any help. Here's the code:
#arrow1 {
position: absolute;
top: 11%;
left: 83.5%;
transform: translate(-50%, -50%);
width: 15px;
height: 100px;
text-align: center;
line-height: 110px;
color: white;
font-size: 30px;
overflow: hidden;
color: yellow;
}
#arrow1 {
margin: 0;
padding: 0;
animation: animate 1s;
right: 0;
pointer-events: none;
}
#keyframes animate {
50% {
transform: translateX(-8px);
margin-top: -35px;
}
}
#arrow2 {
position: absolute;
top: 11%;
left: 66%;
transform: translate(-50%, -50%);
width: 15px;
height: 100px;
text-align: center;
line-height: 110px;
color: white;
font-size: 30px;
overflow: hidden;
color: yellow;
}
#arro2 {
margin: 0;
padding: 0;
animation: animate 1s;
right: 0;
pointer-events: none;
}
#keyframes animate {
50% {
transform: translateX(-8px);
margin-top: -35px;
}
}
#arrow3 {
position: absolute;
top: 11%;
left: 48.7%;
transform: translate(-50%, -50%);
width: 15px;
height: 100px;
text-align: center;
line-height: 110px;
color: white;
font-size: 30px;
overflow: hidden;
color: yellow;
}
#arrow3 {
margin: 0;
padding: 0;
animation: animate 1s;
right: 0;
pointer-events: none;
}
#keyframes animate {
50% {
transform: translateX(-8px);
margin-top: -35px;
}
}
<ul>
<li id="inicio">INICIO</li>
<li id="contacto">CONTACTO</li>
<li id="registrate">REGISTRATE</li>
<li id="ingresar">INGRESAR</li>
<div class="circle">
<i class="fas fa-angle-down" id="arrow1"></i>
<i class="fas fa-angle-down" id="arrow2"></i>
<i class="fas fa-angle-down" id="arrow3"></i>
</div>
</ul>
Since you have your animation assigned to each arrow ID, that animation is playing on page load.
To have the animation play only when the cursor is hovering over the element, you need to add a :hover pseudo-class to each arrow and add the animation within that block.
#arrow1:hover {
animation: animate 1s;
}
After doing that, you should have something like this:
#container {
width: 100%;
height: 80px;
background: #151515;
}
#container,
.navItem {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
transform: translateZ(0);
}
.navItem {
cursor: pointer;
}
.navItem:hover .arrow {
animation: animate 1s;
}
.navText {
font-family: 'Arial', sans-serif;
color: #fff;
text-transform: uppercase;
letter-spacing: 1px;
}
.arrow {
position: absolute;
color: #ffff4c;
bottom: -15px;
}
#keyframes animate {
50% {
transform: translateY(8px);
margin-top: -35px;
}
}
<script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
<div id="container">
<div class="navItem">
<div class="navText">
Contacto
</div>
<i class="fas fa-angle-down arrow"></i>
</div>
</div>
You may notice that I also edited the HTML and CSS for that example, but the idea is the same. This brings me to my next point, and something that may help you.
Classes
Using ID's is a perfectly acceptable way to write your markup and use CSS to style; however, using id as opposed to class can be cumbersome. Since you have multiple arrows, I would advise using classes as opposed to ID's.
So, all of your arrows would be assigned a class. In my example, I used a simple .arrow class. This is proper CSS styling. ID's are unique and should only be used sparingly, usually only once in a document. Classes are reused.
I should also note that this is purely a syntactical thing. CSS will treat ID's and classes similarly if you require it.
Transitions
You may have also noticed that the animation in my example stops if you hover away from the navItem. This is expected behavior if using a :hover pseudo-class on an element. A workaround is substituting an animation for a transition.
Transitions are an alternate way to apply animated properties to elements. They will animate 'forward' and 'backward' on, say, pseudo-classes like :hover and :focus.
Here's what that example looks like with a transition in place of an animation.
#container {
width: 100%;
height: 80px;
background: #151515;
}
#container,
.navItem {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
transform: translateZ(0);
}
.navItem {
cursor: pointer;
}
.navItem:hover .arrow {
transform: translateY(8px);
}
.navText {
font-family: 'Arial', sans-serif;
color: #fff;
text-transform: uppercase;
letter-spacing: 1px;
}
.arrow {
position: absolute;
color: #ffff4c;
bottom: -15px;
transition: all 1s;
}
<script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
<div id="container">
<div class="navItem">
<div class="navText">
Contacto
</div>
<i class="fas fa-angle-down arrow"></i>
</div>
</div>
So this gives you a much smoother interaction when hovering over a menu.
Pseudo Elements
Lastly, I want to touch up on using Pseudo Elements for things like icons. They're useful for adding additional children to a parent element, without affecting your written markup.
We can replace the arrows with a pseudo-element to clean up our HTML, as well as creating a more manageable workspace. Here's that example, again, but using a pseudo-element in place of HTML for the arrow.
#container {
width: 100%;
height: 80px;
background: #151515;
}
#container,
.navItem {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: center;
transform: translateZ(0);
}
.navItem {
cursor: pointer;
}
.navItem:hover .navText::after {
transform: translateY(8px);
}
.navText {
font-family: 'Arial', sans-serif;
color: #fff;
text-transform: uppercase;
letter-spacing: 1px;
text-align: center;
}
.navText::after {
content: "\f107";
font-family: 'FontAwesome';
color: #FFFF00;
position: absolute;
transition: all 1s;
}
<script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
<div id="container">
<div class="navItem">
<div class="navText">
Contacto
</div>
</div>
</div>
Conclusion
Should you have any further questions, I'd be happy to answer them and go more in depth.
I also leave you with this final example via Codepen:
https://codepen.io/jeffheral/pen/NYKbZq
The HTML tags may not be what you want, but feel free to change them. The important thing is we have a solid setup of ID's and classes. If you want to add more navigation items, you only need to add more HTML to your document.
Instead of targetting every arrow individually, you can use a more generic solution:
.menu {
display: flex;
list-style: none;
background-color: #444;
}
.menu li {
padding: 10px 15px;
cursor: pointer;
padding-bottom: 20px;
position: relative;
margin: 5px;
color: white;
font-weight: bold;
font-family: sans-serif;
}
.menu li:after {
font-family: FontAwesome;
font-size: 20px;
font-weight: bold;
content: '\f107';
display: inline-block;
color: yellow;
position: absolute;
bottom: 0;
left: 50%;
transform: translate(-50%, 0);
transition: transform 0.3s ease;
}
.menu li:hover {
background-color: rgba(0, 0, 0, 0.25);
}
.menu li:hover:after {
transform: translate(-50%, 5px);
}
<ul class="menu">
<li>INICIO</li>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>

Create 'X' with using CSS transform-origin and rotate

I have a small issue, I need to rotate 2 spans and create a perfect 'X'.
I've created a JSfiddle for this. Can you guys help me?
I can't seem to get my head around how much should i transform origin...
HTML
<div class="toggle-btn">
<span></span>
<span></span>
</div>
SASS
.toggle-btn {
width: 38px;
height: 19px;
cursor: pointer;
position: relative;
margin-top: 18px;
text-align: center;
&:after {
content: '';
display: block;
clear: both;
}
span {
height: 2px;
margin: 5px 0px;
background: #333;
width: 100%;
display: block;
transition: .15s ease-in;
text-align: center;
&.toggled {
&:nth-of-type(1) {
transform: rotate(-45deg);
transform-origin: 22px 9px;
}
&:nth-of-type(2) {
transform: rotate(45deg);
transform-origin: 20px -5px;
}
}
}
}
and JS
$('.toggle-btn').on('click',function(){
$(this).find('span').toggleClass('toggled');
});
This appears to work. I had to adjust the margin to 4px. The rotation needs to come after the x/y translation because translating after the rotation appears to change the point of origin on which the element is rotating instead of origin of the element. You can observe this behavior by trying a large (100px) x-translation after the rotation.
.toggle-btn {
width: 38px;
height: 19px;
cursor: pointer;
position: relative;
margin-top: 18px;
text-align: center;
overflow: show;
&:after {
content: '';
display: block;
clear: both;
}
span {
height: 2px;
margin: 4px 0px;
background: #333;
width: 38px;
display: block;
transition: .15s ease-in;
text-align: center;
&.toggled {
&:nth-of-type(1) {
transform: translate(0px, 3px) rotate(-45deg);
}
&:nth-of-type(2) {
transform: translate(0px, -3px) rotate(45deg);
}
}
}
}

Text is not rotating with button

I need help rotating text at the same time that the button rotates. For some reason the text is currently disappearing when I hover the mouse over the button. The text should not be disappearing; it should be rotating with the button I'm using Chrome for this project.
http://codepen.io/matosmtz/pen/oXBaQE
HTML
<body>
<div class = "container">
<section class="3d-button">
<h2>Animated Button</h2>
<p class="btn_perspective">
<button class="btn btn-3d btn-3da">Submit</button>
</p>
</section>
</div>
</body>
CSS
html, body {
font-size: 100%;
padding: 0;
margin: 0;
height: 100%;
border: 0;
}
body {
font-family: Lekton;
background: rgb(245,245,245);
}
a {
color: #888;
text-decoration: none;
}
.container {
padding: 0;
margin: 0;
height: 100%;
background: #fff;
position: relative;
}
.container > section {
margin: 0 auto;
padding: 6em 3em;
text-align: center;
}
h2 {
margin: 20px;
text-align: center;
text-transform:uppercase;
letter-spacing: 1px;
font-size: 2em;
}
.btn {
border:none;
position: relative;
background: rgb(245,245,245);
padding: 15px 40px;
display: inline-block;
text-transform: uppercase;
margin: 15px 30px;
color: inherit;
letter-spacing: 2px;
font-size: .9em;
outline: none;
-webkit-transition: all 0.4s;
transition: all 0.4s;
cursor: pointer;
}
.btn:after {
content: "";
position: absolute;
z-index: -1;
-webkit-transition: all 0.4s;
transition: all 0.4s;
}
.btn_perspective {
-webkit-perspective: 800px;
perspective: 800px;
display: inline-block;
}
.btn-3d {
display: block;
background: #5cbcf6;
color: white;
outline: 1px solid transparent;
transform-style: preserve-3d;
}
.btn-3d:active {
background: #55b7f3;
}
.btn-3da:after {
width: 100%;
height: 42%;
left: 0;
top: -40%;
background: #53a6d7;
transform-origin: 0% 100%;
transform: rotateX(90deg);
}
.btn-3da:hover {
transform: rotateX(-45deg);
}
Here's the updated codepen: http://codepen.io/anon/pen/KpaGYd
Added the following code to your button:
.btn-3da:after {
z-index:1;
}
.btn-3da:hover {
position:relative;
z-index:2;
}
You can take a look into this pure CSS3 solution (works in all major web browsers: DEMO):
/*ROTATE*/
.divRotate
{
-moz-transition : all 0.8s;
-webkit-transition : all 0.8s;
-o-transition : all 0.8s;
transition : all 0.8s;
}
.divRotate:hover
{
-moz-transform : rotate(360deg);
-webkit-transform : rotate(360deg);
-o-transform : rotate(360deg);
transform : rotate(360deg);
}
Add the div element to your HTML and refer the class = 'divRotate'.
Hope this may help. Best regards,