I'm trying to create an CSS button hover effect. But I didn't manage to fill the element with a slanted shape.
How the hover effect was planned:
Screenshot 1: How it looks actually.
Screenshot 2: How I want the hover effect to look like with slanted side.
.button_sliding_bg {
color: #31302B;
background: #FFF;
padding: 12px 17px;
margin: 25px;
font-family: 'OpenSansBold', sans-serif;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
box-shadow: inset 0 0 0 0 #31302B;
-webkit-transition: all ease 0.8s;
-moz-transition: all ease 0.8s;
transition: all ease 0.8s;
}
.button_sliding_bg:hover {
box-shadow: inset 200px 0 0 0 #31302B;
color: #FFF;
}
<button class="button_sliding_bg">Buttontext</button>
You can use the technique described in this answer : Fill element from center on hover and skew the pseudo element so it fills the button with a slant :
div {
position: relative;
display: inline-block;
padding: 15px 70px;
border: 5px solid #B17461;
color: #B17461;
font-size: 30px;
font-family: arial;
transition: color .5s;
overflow:hidden;
}
div:before {
content: '';
position: absolute;
top: 0; left: 0;
width: 130%; height: 100%;
background: #B17461;
z-index: -1;
transform-origin:0 0 ;
transform:translateX(-100%) skewX(-45deg);
transition: transform .5s;
}
div:hover {
color: #fff;
}
div:hover:before {
transform: translateX(0) skewX(-45deg);
}
<div>BUTTON</div>
Don't forget to add vendor prefixes for browser support (see canIuse for more info).
I believe that you are actually looking for the end state to fill the entire element with background color and not leave the gap. You could also do it with linear-gradient background images and transition their background-size and background-position like in the below snippet.
One disadvantage of using linear-gradient over pseudo-elements or transforms is that the browser support is lower but it doesn't need extra pseudo-elements and so can leave them spare for other use.
.button_sliding_bg {
color: #31302B;
background: #FFF;
padding: 12px 17px;
margin: 25px;
font-family: 'OpenSansBold', sans-serif;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
background-image: linear-gradient(135deg, #31302B 50%, transparent 51%);
background-size: 100px 100px; /* some initial size to get the slanted appearance */
background-position: -50px -50px; /* negative positioning to hide it initially */
background-repeat: no-repeat;
transition: all ease 0.8s;
}
.button_sliding_bg:hover {
background-size: 200% 200%; /* 200% because gradient is colored only for 50% */
background-position: 0px 0px; /* bring it fully into view */
color: #FFF;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<button class="button_sliding_bg">Buttontext</button>
<button class="button_sliding_bg">Button text lengthy</button>
<button class="button_sliding_bg">Button text <br> with line break</button>
<button class="button_sliding_bg">Button text <br> with <br> multiple <br> line <br>breaks</button>
You can use css :after.
Jsfiddle
.button_sliding_bg {
color: #31302B;
background: #FFF;
padding: 12px 17px;
margin: 25px;
font-family:'OpenSansBold', sans-serif;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
box-shadow: inset 0 0 0 0 #31302B;
-webkit-transition: all ease 0.8s;
-moz-transition: all ease 0.8s;
transition: all ease 0.8s;
position: relative;
}
.button_sliding_bg:hover {
box-shadow: inset 200px 0 0 0 #31302B;
color: #FFF;
}
.button_sliding_bg:after {
content:'';
display: block;
position: absolute;
right: 0;
bottom: 0;
width: 0;
height: 0;
border-width: 0 0 0 0;
border-color: transparent transparent #fff transparent;
border-style: solid;
border-width: 0 0 32px 30px;
-webkit-transition: all ease 0.8s;
-moz-transition: all ease 0.8s;
transition: all ease 0.8s;
}
<button class="button_sliding_bg">Buttontext</button>
Related
I have an animation that wipes from left to right on hover here. I want to have an exit animation that also goes from left to right (not right to left as shown in the demo). How can I achieve this?
.btn {
color: #31302B;
background: #FFF;
margin: 25px;
padding: 10px 0;
width: 240px;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
box-shadow: inset 0 0 0 0 #6a0dad;
-webkit-transition: all ease 0.8s;
-moz-transition: all ease 0.8s;
transition: all ease 0.8s;
}
.btn:hover {
box-shadow: inset 240px 0 0 0 #6a0dad;
color: #fff;
}
<div class="btn">CONTAINER</div>
You could use #keyframes:
.btn {
color: #31302B;
background: #FFF;
margin: 25px;
padding: 10px 0;
width: 240px;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
animation: out 0.8s ease;
}
#keyframes in {
from {
box-shadow: inset 0 0 0 0 #6a0dad;
color: black;
}
to {
box-shadow: inset 240px 0 0 0 #6a0dad;
color: white;
}
}
#keyframes out {
from {
box-shadow: inset -240px 0 0 0 #6a0dad;
color: white;
}
to {
box-shadow: inset 0 0 0 0 #6a0dad;
color: black;
}
}
.btn:hover {
animation: in 0.8s ease;
box-shadow: inset 240px 0 0 0 #6a0dad;
color: white;
}
<div class="btn">CONTAINER</div>
I would suggest using pseudo-element ::after and transform-origin for this.
.btn::after {
z-index: -1;
content: '';
position: absolute;
left: 0;
top: 0;
background: #6a0dad;
height: 100%;
width: 100%;
transform: scaleX(0);
transition: all ease 0.8s;
transform-origin: left;
}
.btn:hover {
color: white;
}
.btn:hover::after {
transform: scaleX(1);
transform-origin: right;
}
Add position: relative; z-index: 1; to .btn class
A simple background transition can easily do this without keyframes:
.btn {
color: #31302B;
background: #FFF;
margin: 25px;
padding: 10px 0;
width: 240px;
border: 3px solid #31302B;
font-size: 14px;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 2px;
display: inline-block;
text-align: center;
cursor: pointer;
background:linear-gradient(#6a0dad,#6a0dad) left/0% 100% no-repeat;
transition: all ease 0.5s,background-position 0s 0.5s;
}
.btn:hover {
background-size:100% 100%;
background-position:right;
color: #fff;
}
<div class="btn">CONTAINER</div>
Related: How to animate underline from left to right?
I have an element with background set as color
On hover background is set as radial-gradient
I want to make transition between colors on hover but it creates weird effect where my element disappear for a second.
Here is link
Link
Is it possible to switch between color and gradient without this problem?
.link {
display: block;
position: absolute;
bottom: 20%;
padding: 0 25px;
height: 42px;
line-height: 42px;
border-radius: 8px;
font-size: 15px;
font-weight: 700;
background: red;
color: white;
transition: all 0.3s ease-in-out;
}
.link:hover {
text-decoration: none;
background: radial-gradient(98px 98px at center center, red 0%, #0088b5 100%);
}
You can play with background-size:
.link {
display: inline-block;
padding: 0 25px;
line-height: 42px;
border-radius: 8px;
font-size: 15px;
font-weight: 700;
background-image: radial-gradient(circle,red 0%, #0088b5 100%);
background-position:center;
background-size:600% 600%; /*a big size to see only the red*/
color: white;
text-decoration: none;
transition: all 0.3s ease-in-out;
}
.link:hover {
background-size:150% 150%; /* reduce the size to see the gadient effect*/
}
<div class="link">Link</div>
You can use the :before pseudo-element along with a transition on the opacity of the background color to get this effect.
Credit: Dave Lunny.
Also, check out this previous question.
.link {
display: block;
position: absolute;
bottom: 20%;
padding: 0 25px;
height: 42px;
line-height: 42px;
border-radius: 8px;
font-size: 15px;
font-weight: 700;
background: rgba(255, 0, 0, 1);
color: white;
opacity: 1;
transition: all 0.3s ease-in-out;
}
.link:before {
border-radius: inherit;
content: '';
display: block;
height: 100%;
position: absolute;
top: 0; left: 0;
width: 100%;
z-index: -100;
background: radial-gradient(98px 98px at center center, red 0%, #0088b5 100%);
}
.link:hover {
text-decoration: none;
background: rgba(255, 0, 0, 0);
}
<a class="link" href="#">Hover Me!</a>
I need to create button like this
You can see a rounded borders on center of sides. I'm tried to do it with after and before classes, but it was tricky. Which solution is the cleanest? Also I'm done on dev resizeble button and it'll be better if this can be done as one figure, without absolute positioning or smth like that
body {
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
}
button {
text-transform: uppercase;
background-color: #F9EFCA;
border: none;
padding: 20px 100px;
cursor: pointer;
letter-spacing: 1px;
border-bottom: 10px solid #ae9e5c !important;
box-shadow: inset 0 -1px 1px 0 rgba(0, 0, 0, .12), 0 10px 20px -5px rgba(0, 0, 0, .25);
font-size: 50px;
transition: .2s all;
position: relative;
border-radius: 10px;
}
button:hover,
button:active {
transition: .2s all;
border-bottom: none !important;
}
button:before,
button:after {
content: '';
position: absolute;
top: 9%;
bottom: 0;
height: 91%;
background: #F9EFCA;
width: 10px;
border-radius: 100%;
}
button:before {
left: -4px;
}
button:after {
right: -4px;
}
button:active:before,
button:active:after,
button:hover:before,
button:hover:after {
top: 9%;
bottom: 0;
height: 82%;
}
<button>Call me</button>
Codepen example
create a new button class and try this in your CSS:
.button_costum
{
margin: 10px auto;
font-size: 2.0rem;
padding: 1.25rem 2.5rem;
display: block;
background-color: // choose what you want
border: 1px solid transparent;
color: // choose what you want
font-weight: 300;
-webkit-border-radius: 3px;
border-radius: 20px; // in your case it shout be 25 or 30
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
try using the property on button
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
This would help you target the button corners without distorting the shape of the button itself.
When I hover over the corner of the element I am getting a weird constant hover and hover out effect, how can I fix this?
HTML
<h2>
HELLO
</h2>
CSS
h2 {
position: absolute;
background: rgba(2,35,64,0.58);
width: calc(100% - 97px);
height: calc(100% - 88px);
line-height: calc(100% + 70px);
top: 0;
left: 0;
display: block;
font-size: 18px;
text-transform: uppercase;
text-align: center;
color: #FFF;
border: solid 20px rgba(2,35,64,0);
padding: 20px;
margin: 0px;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
cursor: pointer;
}
h2:hover {
border: solid 5px #FFF;
padding: 10px;
margin: 25px;
background: transparent;
}
h2.woocommerce-loop-category__title:hover {
border: solid 5px #FFF;
padding: 10px;
margin: 25px;
background: transparent;
}
http://jsfiddle.net/xc7vjstn/1/
change the background from transparent to what ever color you want and you will see the content plus if you want to keep it online like small box then margin and padding is what you need to play with.
h2:hover {
border: solid 5px #FFF;
padding: 10px;
margin: 25px;
background: blue;
}
The margin in h2:hover is causing the behavior removing the margin fixed the problem
h2:hover {
border: solid 5px #FFF;
padding: 10px;
/*margin: 25px; */
background: transparent;
}
Here is my solution, please check the live example below:
h2 {
position: absolute;
background: rgba(2, 35, 64, 0.58);
width: calc(100% - 97px);
height: calc(100% - 88px);
line-height: calc(100% + 70px);
top: 0;
left: 0;
font-size: 18px;
text-transform: uppercase;
text-align: center;
color: #FFF;
border: solid 20px rgba(2, 35, 64, 0);
padding: 20px;
margin: 0 auto;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
cursor: pointer;
}
h2:hover {
background: transparent;
-ms-transform: scale(0.9);
-webkit-transform: scale(0.9);
transform: scale(0.9);
}
<h2>
HELLO
</h2>
I have the following button.
The CSS for the button above is this:
.cta-btn {
display: inline-block;
margin: 20px 0 0 20px;
color: #fff;
background-color: #FF8F1B;
background-image: linear-gradient(to right, #2ab3ff, #ff2d00);
box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
font-size: 21px;
border-radius: 30px;
padding: 12px 21px;
font-family: Montserrat;
}
click me
I want the button to change gradient color smoothly when I hover over it. I do not want the gradient color to just snap onto the button when I hover it. This is my attempt at a smooth gradient color transition:
a.cta-btn:hover {
background-image: linear-gradient(to right,#FF2A67,#FF5D3A);
color: #fff;
box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
transition: background-image .3s linear;
transition: box-shadow .3s linear;
}
Any help is much appreciated.
Short answer, you can't using just background. However, you can achieve a similar effect using other elements (or pseudo elements) inside and fading them in on hover.
The following example uses two pseudo-elements as the two background states. On hover, we simply fade-in the new background giving a similar transition effect that would happen if gradients were transition-able.
NOTE: Not all browsers support transitions on pseudo elements, so you may need to add empty elements to achieve the same effect on older/unsupported browsers.
.cta-btn {
position: relative;
display: inline-block;
margin: 20px 0 0 20px;
color: #fff;
box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
font-size: 21px;
border-radius: 30px;
overflow: hidden;
padding: 12px 21px;
font-family: Montserrat;
transition: box-shadow.3s ease-in-out;
text-decoration: none;
}
/* These are the two backgrounds, absolutely positioned to cover. */
.cta-btn::before,
.cta-btn::after {
content: '';
display: block;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
background-image: linear-gradient(to right, #2ab3ff, #ff2d00);
border-radius: 30px;
z-index: -1;
}
.cta-btn::after {
opacity: 0;
background-image: linear-gradient(to right,#FF2A67,#FF5D3A);
transition: opacity.3s ease-in-out;
}
/* On hover, transtiion the shadow of the anchor, and fade in the after element to show the new background. */
.cta-btn:hover {
box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
}
.cta-btn:hover::after {
opacity: 1;
}
click me
I have try all your answers, and i prefer this :
It's lightly and working perfect with only background-size property for the hover
and Work with Chrome IE and ff
Enjoy
.ex-button-0 {
transition: all ease 0.5s;
cursor: pointer;
padding: 10.5px 25px;
border: none;
border-radius: 35px;
background-image: linear-gradient(to left, black, blue, yellow, orange);
background-size:300%;
background-position: 0 0;
-webkit-appearance: none !important;
color: #000;
text-decoration:none
}
.ex-button-0:hover {
background-position: 100% 0;
color:#fff;
}
<a class="ex-button-0" href="">Exemple</a>
Though still able to see background decreasing and increasing in dimensions, this is partially possible using multiple background properties at same element, toggling background-size property.
.cta-btn {
color: #fff;
background: linear-gradient(to right, #2ab3ff, #ff2d00)
, linear-gradient(to right,#FF2A67,#FF5D3A);
background-size:100% 100%, 0% 0%;
background-origin: border-box, border-box;
box-shadow: 4px 5px 27px 4px rgba(220, 120, 184, 0.85);
font-size: 21px;
border-radius: 30px;
padding: 12px 21px;
font-family: Montserrat;
transition: background .3s linear;
}
.cta-btn:hover {
background-size:0% 0%, 100% 100%;
box-shadow: 4px 5px 27px 4px rgba(255,45,45,0.85);
}
click me
Probably a little late to the party, but I did manage to get a gradient transition into a solid color, which is what I needed for my project.
Here is the codepen for proof of concept.
https://codepen.io/etc-umbrella/pen/pXremq
<button class="ui-button">This is a button</button>
<h2>Creating an aninmated gradient background button using only SCSS. Worked pretty good. Didn't have to use any crazy javascript</h2>
.ui-button {
position: relative;
overflow: hidden;
border-radius: 6px;
cursor: pointer;
padding: 12px 18px;
border: 1px solid aqua;
background-color: white;
color: #ffffff;
font-family: raleway;
font-weight: bold;
font-size: 16px;
z-index: 1;
transition: all 800ms ease-in;
&:after{
content: '';
position: absolute;
left: -200%;
top: 0px;
width: 400%;
height: 100%;
background: rgb(33,209,159);
background: linear-gradient(45deg, rgba(33,209,159,1) 0%, rgba(34,44,64,1) 50%, rgba(21,65,153,1) 100%);
z-index: -1;
transition: all 800ms ease-in;
}
&:hover{
color: #ffffff;
background-color: #21d19f;
}
&:hover:after{
left: 0%;
opacity: 0;
}
}
#import url('https://fonts.googleapis.com/css?family=Montserrat:500');
html,body {
font-family: 'Montserrat', sans-serif;
margin:0;
padding:0;
background: linear-gradient(to right, #c9d6ff, #e2e2e2);
}
div {
height: 100vh;
display: flex;
align-items:center;
justify-content:center;
}
h1 {
font-size: 42px;
background-size:200%;
padding:15px;
border-radius:5px;
background-image: linear-gradient(to top left, #fe87c3 0%, #D38312 50%, #A83279 100%);
transition: .3s ease;
cursor: pointer;
}
h1:hover {
background-position: 90%;
color: #202020;
}
.home {
background-size: 200%
}**strong text**
Here is a demo
https://codepen.io/Mikeytown19/pen/aLpNZa