I was playing around with CSS animations to get some eye candy - and I found following result pretty nice:
body {
font-size: xx-large;
font-family: 'Courier New';
color: white;
background: black;
}
a {
text-decoration: none;
position: relative;
}
a:visited {
color: white;
}
a::before,
a::after {
content: '';
position: absolute;
bottom: 0;
right: 50%;
width: 0;
border-bottom: 2px solid blue;
transition: 400ms 200ms;
}
a::after {
left: 50%;
right: 0;
}
a:hover::before,
a:hover::after {
width: 50%;
}
This is a test. This line has to be a bit longer to see the effect.
The problem with it, is its behaviour on line breaks... Can this be fixed? I would already be happy, if the animation is only on the hovered clientRect and once it is finished, all other clientRects get just underlined. A CSS-only solution (if there is one) would be highly appreciated.
I could find a solution by modifying Nicky Meuleman's example (thanks to webdev-dan for providing that link):
body {
background-color: black;
font-size: 3rem;
}
a {
color: blue;
text-decoration: none;
background-image: linear-gradient(blue, blue), linear-gradient(blue, blue);
background-size: 0 2px, 0 2px;
background-position: 50% 100%, 50% 100%;
background-repeat: no-repeat;
transition: background-size 400ms linear, background-position 400ms linear;
transition-delay: 200ms;
}
a:hover {
background-size: 50% 2px, 100% 2px;
background-position: 0 100%, 50% 100%;
}
a:visited {
color: white;
}
Short link test.
This link is a bit longer to see the multiline behaviour.
Hope this is useful to others too. :-)
Related
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>
In short, I have a button with a darker bottom border, and, on hover, I want the button to move down a little and shrink the border (to give the effect of pressing it).
However, it jitters slightly as it animates, completely killing the feel of the button "pressing".
Here's a Fiddle demonstrating the issue, along with the CSS in question:
.btn {
color: grey;
background: lightgrey;
border: 0 rgba(0, 0, 0, .1) solid;
border-bottom-width: 4px;
font-family: sans-serif;
padding: 10px;
position: relative;
text-decoration: none;
top: 0;
transition: all 250ms;
}
.btn:hover {
top: 2px;
border-bottom-width: 2px;
}
I'm not using the prefixes like -webkit here for the sake of simplicity.
The main issue is the bottom border. The button slides down fine, but the border appears to be doing something different (and isn't smooth).
Any ideas here?
Try to replace the top transition with transform: translateY(2px), also replace the bottom border with a ::before pseudo element and animate it's scale property:
https://jsfiddle.net/qjfstq1c/1/
.btn, .btn:before {
transition: all 250ms;
transform-origin: top center;
}
.btn {
display: inline-block;
color: grey;
background: lightgrey;
font-family: sans-serif;
padding: 10px;
position: relative;
text-decoration: none;
}
.btn:before {
content: '';
position: absolute;
top: 100%; left: 0; right: 0;
border-top: 4px solid #bebebe;
}
.btn:hover {
transform: translateY(2px);
}
.btn:hover:before {
transform: scaleY(.5);
}
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
I add a ":after" element to all links (simulate a "border-bottom") so on ":hover" i can animate this pseudo element ("height: 100%"). This works fine, but when the link is split with a line-break the pseudo element is broken after the line break.
a {
color: red;
position: relative;
text-decoration: none;
&:after {
transition: height .1s;
background-color: red;
bottom: -3px;
content: '';
display: inline;
height: 3px;
left: 0;
right: 0;
position: absolute;
width: 100%;
z-index: -1;
}
&:hover:after {
height: calc(100% + 4px);
}
&:hover {
color: white;
}
}
Here is a pen:
http://codepen.io/herrfischer/pen/YWKmQJ
Any idea what I am doing wrong?
For an inline element, background will be more efficient: http://codepen.io/gc-nomade/pen/pbzMYP
a {
color: red;
position: relative;
text-decoration: none;
background:linear-gradient(red,red) bottom repeat-x;
background-size:3px 3px;
transition:1s;
&:hover {
background-size:100% 100%;
color: white;
}
}
Stolen from another site - it works with animated background gradient :)
a {
background-image: linear-gradient(red 0%, red 100%);
background-position: bottom;
background-repeat: repeat-x;
background-size: 0 0;
border-bottom: 3px solid red;
color: red;
position: relative;
text-decoration: none;
transition: 150ms ease;
&:hover {
color: white;
background-size: 1em 1.5em;
}
}
Updated the pen.
I defined my "default" checkbox style on globals.css, I file that I include on every page
input[type=checkbox] {
display:none;
}
input[type=checkbox]+label {
width:auto;
display:inline-block;
cursor:pointer;
position:relative;
line-height:20px;
padding-left:22px;
}
input[type=checkbox]+label:before {
content:"";
display:inline-block;
width:16px;
height:16px;
position:absolute;
left:0;
background-color:#F5F5F5;
border-width:1px;
border-style:solid;
border-radius:3px;
}
input[type=checkbox]:checked+label {
outline:0;
}
input[type=checkbox]:checked+label:before {
content:'\2713';
font-size:16px;
line-height:16px;
text-align:center;
}
Then on a page I need this type of checkbox as "default" and another checkbox (on-off switch) only in one place. The problem is that the switch take also the CSS style of the normal checkbox of globals.css. Is the only way to solve this problem apply !important to each?? (or most of) the lines of the on-off switch? Or is there a way to reset CSS to normal checkbox for this container and the apply new styles?
On-off CSS
.checkbox_onoff {
float:left;
width:60%;
position: relative; width: 60px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.checkbox_onoff-checkbox {
display: none;
}
.checkbox_onoff-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #666666; border-radius: 0px;
}
.checkbox_onoff-inner {
width: 200%; margin-left: -100%;
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.checkbox_onoff-inner:before, .checkbox_onoff-inner:after {
float: left; width: 50%; height: 20px; padding: 0; line-height: 20px;
font-size: 10px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.checkbox_onoff-inner:before {
content: "ON";
padding-left: 10px;
background-color: #6194FD; color: #FFFFFF;
}
.checkbox_onoff-inner:after {
content: "OFF";
padding-right: 10px;
background-color: #F8F8F8; color: #666666;
text-align: right;
}
.checkbox_onoff-switch {
height:20px;
width:20px; margin: 0px;
background: #FFFFFF;
border: 2px solid #666666; border-radius: 0px;
position: absolute; top: 0; bottom: 0; right: 36px;
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
background-image: -moz-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: -webkit-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: -o-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
}
.checkbox_onoff-checkbox:checked + .checkbox_onoff-label .checkbox_onoff-inner {
margin-left: 0;
}
.checkbox_onoff-checkbox:checked + .checkbox_onoff-label .checkbox_onoff-switch {
right: 0px;
}
You can use !important or you can do what is done in this post
What are the implications of using "!important" in CSS?
I also found this other post very helpful: http://css-tricks.com/when-using-important-is-the-right-choice/
Make sure your global css declaration is before your second css declaration.
There are a few ways to achieve what you want, !important is one of them and you can use it, no problem. It's not the best practice, but since you mention that it would be a limited use, in a controlled environment, there should be no problem.
Still, it would be much better if you can do some changes like add a class to the element you want to control in that page and modify elements with that class.
Or you can load the css and after that add a style section on the head of the document to modify the effect of the css file.
Finally, you can add a style attribute on the tag itself, although considering your code, it looks like too much for readability in a tag.