Weird quick switching between normal and hover state CSS - html

I just created a custom styled button with an effect when hovering. However if you are unlucky and find a specific spot while hovering on the button it quickly switches between the normal and hover state.
My code:
button {
height: 34px;
padding: 0 16px;
border-radius: 4px;
margin: 8px;
border: none;
background: red;
color: #fff;
transition: background .2s, transform .2s;
outline: none;
cursor: pointer;
}
button:hover {
box-shadow: 4px 4px 0 0 blue;
transform: translate(-1px, -1px);
}
button:active {
background: green;
box-shadow: 2px 2px 0 0 blue;
}
<button>I am a button</button>
Code
Is there a trick to solve this issue?

The cause is the transform on hover. As soon as you stop hovering, the button goes back to place. If you've only moved one pixel away, you are now hovering the button again, which moves 1px away, and now is not hovered, and so on.
The best solution would be to remove the transform effect.
If you can't, a simple solution would be to add a transparent border around the button when it's hovered. You'll have to go another pixel to stop hovering the button, and when the hover stops you'll be outside of the new button position.
button {
height: 34px;
padding: 0 16px;
border-radius: 4px;
margin: 8px;
border: none;
background: red;
color: #fff;
transition: background .2s, transform .2s;
outline: none;
cursor: pointer;
}
button:hover {
box-shadow: 4px 4px 0 0 blue;
transform: translate(-1px, -1px);
border: 1px solid transparent;
}
button:active {
background: green;
box-shadow: 2px 2px 0 0 blue;
}
<button>I am a button</button>

A solution could be to add a container with a small padding, and check the hover on it instead of the button. Since it doesn't move, you shouldn't have the problem.
#buttonContainer {
display: inline-block;
padding: 2px;
}
button {
height: 34px;
padding: 0 16px;
border-radius: 4px;
margin: 8px;
border: none;
background: red;
color: #fff;
transition: background .2s, transform .2s;
outline: none;
cursor: pointer;
}
#buttonContainer:hover button{
box-shadow: 4px 4px 0 0 blue;
transform: translate(-1px, -1px);
}
button:active {
background: green;
box-shadow: 2px 2px 0 0 blue;
}
<div id="buttonContainer">
<button>I am a button</button>
</div>

Related

How can ı resolve my nested div hover problem in css

#main_div {
background-color: #333;
margin-top: 5%;
border-radius: 10px;
box-shadow: 12px 12px 18px #000;
-moz-box-shadow: 12px 12px 18px #000;
-webkit-box-shadow: 12px 12px 18px #000;
border-left: 5px solid orange;
border-top: 5px solid orange;
height: 800px;
}
a {
background-color: orange !important;
}
a:hover {
border-left: 5px solid darkorange !important;
border-top: 5px solid darkorange !important;
transition: 0.4s;
-webkit-transform: scale(1.1);
-ms-transform: scale(1.1);
transform: scale(1.1);
}
<div id="main_div">
<a href"#">
<div><span>Hello World!</span></div>
</a>
</div>
I am trying to hover div. "a" is growing in hover position. But I have a problem As a grows, main_div grows with it. How do I prevent main_div from growing.
As commented above, try giving the border already some width and simply change its color on hover.
#main_div {
background: black;
padding: 1rem;
text-align: center;
}
a {
display: inline-block;
color: #fff;
border-top: 5px solid transparent;
border-left: 5px solid transparent;
padding: 1rem;
transition: all .3s ease;
}
a:hover {
transform: scale(1.1);
border-color: orange;
}
<div id="main_div">
<a href"#">
<div><span>Hello World!</span></div>
</a>
</div>

Is it possible for the color to 'erase' the background in CSS?

I really doubt what I am asking is possible but it's still worth a try.
I am trying to create a button that normally has background-color: transparent; color: white; and when you hover over it, those properties should swap. The problem is that if you just swap them then all you see is a white button. If you know the background colour of the containing element then you can get the colour from there but If the button is over an image or a canvas then this won't work.
This is how I've been doing it so far
html,
body {
height: 100%;
}
#container {
background-color: #38404D;
height: 100%;
}
.ghost-button {
background-color: transparent;
border: 1px solid #ffffff;
outline: none !important;
transition: all 0.8s;
margin: 10px 10px;
padding: 6px 7px;
cursor: pointer;
color: #ffffff;
}
.ghost-button:hover {
background-color: #ffffff;
color: #38404D;
}
.ghost-button:active {
box-shadow: inset 0 0 8px 0px #888888;
}
<div id="container">
<button class="ghost-button">Hover Here</button>
</div>
UPDATE
It seems that quite a few people were confused by the question. I am asking if there is a way to do the exact same thing I've done above but on top of an image or a canvas (instead of a solid colour). See example below
html,
body {
height: 100%;
}
#container {
background-image: url("http://www.freegreatpicture.com/files/147/17878-hd-color-background-wallpaper.jpg");
height: 100%;
}
.ghost-button {
background-color: transparent;
border: 1px solid #ffffff;
outline: none !important;
transition: all 0.8s;
margin: 10px 10px;
padding: 6px 7px;
cursor: pointer;
color: #ffffff;
}
.ghost-button:hover {
background-color: #ffffff;
color: #38404D;
}
.ghost-button:active {
box-shadow: inset 0 0 8px 0px #888888;
}
<div id="container">
<button class="ghost-button">Hover Here</button>
</div>
Yes, it IS possible in CSS with mix-blend-mode.
Answer's update in April 2021: Currently it have a very solid support (95% globally) although Safari doesn't have hue, saturation, color, and luminosity blend modes. Of course, IE isn't a considerable thing if you expect to use it (like many of other cool CSS features of the last years).
.ghost-button {
/* Important part */
mix-blend-mode: screen;
background: #000;
color: #fff;
/* Button cosmetics */
border: .125em solid #fff;
font: 2em/1 Cursive;
letter-spacing: 1px;
outline: none !important;
transition: all .8s;
padding: .5em 1em;
cursor: pointer;
}
.ghost-button:hover {
/* Important part */
background: #fff;
color: #000;
}
#container {
background: url('http://www.freegreatpicture.com/files/147/17878-hd-color-background-wallpaper.jpg') center/cover;
/* Also works with background-color or gradients: */
/* background: linear-gradient(to right, red, yellow); */
/* Container positioning */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
html, body {
margin: 0;
}
<div id="container">
<button class="ghost-button">Hover Here</button>
</div>
As you can see, the secret here is using mix-blend-mode: screen along with the black color for the "erased" part, since black is mixed with the background when using this screen mode.
No, it isn't possible in CSS! You could try to set the color with JS to mimic this effect.
body {
height: 100%;
}
#container {
background-color: #38404D;
height: 100%;
}
.ghost-button {
background-color: transparent;
border: 1px solid #ffffff;
outline: none !important;
transition: all 0.8s;
margin: 10px 10px;
padding: 6px 7px;
cursor: pointer;
color: #ffffff;
}
.ghost-button:hover {
background-color: none;
color: red;
}
.ghost-button:active {
box-shadow: inset 0 0 8px 0px #888888;
}
<div id="container">
<button class="ghost-button">Hover Here</button>
</div>
hover color is set to red you can update it.

Make custom tooltips compatible with changing widths

I have a custom tooltip code for my forum but due to its nature, I can't seem to make it compatible with different [tip] ranges.
See below for what I mean:
span.tooltip {
border-bottom: 1px dashed black;
}
span.tooltip span {
display: inline;
z-index: 10;
opacity: 0;
position: absolute;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
margin-left: -100px;
margin-top: 27px;
-webkit-box-shadow: 1px 1px 7px transparent;
-moz-box-shadow: 1px 1px 7px transparent;
box-shadow: 1px 1px 7px transparent;
border: 1px solid #444;
}
span.tooltip:hover span {
display: inline;
opacity: 1;
position: absolute;
background: #f4f4f4;
}
span.tooltip>span:hover {
display: inline;
position: absolute;
border: 1px solid #444;
background: #f4f4f4;
}
span.tooltip>span {
max-width: 200px;
padding: 10px 12px;
opacity: 0;
visibility: hidden;
z-index: 10;
position: absolute;
font-size: 12px;
font-style: normal;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 1px 1px 7px #6B151F;
-moz-box-shadow: 1px 1px 7px #6B151F;
box-shadow: 1px 1px 7px #6B151F;
}
span.tooltip:hover>span {
opacity: 1;
text-decoration: none;
visibility: visible;
overflow: visible;
display: inline;
}
span.tooltip span>b:first-child {
width: 15px;
height: 15px;
margin-left: 20px;
margin-top: -19px;
display: block;
position: absolute;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-box-shadow: inset -1px 1px 0 #fff;
-moz-box-shadow: inset 0 1px 0 #fff;
-o-box-shadow: inset 0 1px 0 #fff;
box-shadow: inset 0 1px 0 #fff;
display: none0/;
*display: none;
background: #f4f4f4;
border-top: 1px solid #333;
border-right: 1px solid #333;
}
<span class="tooltip">works fine with long tips<span><b></b>parameter</span></span>
<br /><br />
<span class="tooltip">not<span><b></b>parameter</span></span> so much with shorter ones
<br /><br />
not even when it's <span class="tooltip">indented<span><b></b>parameter</span></span> into the paragraph
So uh... Yeah. I'm not sure how to make it compatible with smaller tips. Percent widths and using left do not work. I have already tried. Please don't suggest I use that unless you found a way how to make it work.
If it's not possible to make it work then that's all right, just lemme know.
There are a few problems with the tooltip you are using. First of all tooltips must be fluid and flexible, meaning they need to position themselves according to the element they are tipping. Also it's width should be decided by the ammount of text inside it. I have created a Tooltip javascript file that can help you accomplish that. But if you prefer fixing your current one you can do the following:
Add padding to your tooltip
span.tooltip span {/* all your previous code */
padding: 6px 10px; }
add a minimum width to your tooltip
min-width: 20px;
if you do decide to implement my tooltip library send me a message or comment on my post.

Clickable search icon in background

I have a search field with a search icon in the background. Here's the CSS:
input[type=search] {
outline: none;
-webkit-appearance: textfield;
-webkit-box-sizing: content-box;
font-family: inherit;
font-size: 100%;
background: #ededed url('$http://www.$domain/images/search-icon.png') no-repeat right 5px top 5px;
padding-left: 8px;
padding-right: 24px;
border: solid 1px #ccc;
width: 122px;
font-size:16px;
font-family: \"Lato\";
font-weight: 300;
-webkit-border-radius: 10em;
-moz-border-radius: 10em;
border-radius: 10em;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
}
input[type=search]:focus {
width: 230px;
background-color: #fff;
border-color: #7486BA;
-webkit-box-shadow: 0 0 5px #7486BA;
-moz-box-shadow: 0 0 5px #7486BA;
box-shadow: 0 0 5px #7486BA;
}
#media screen and (max-width: 800px) {
input[type=search]:focus {
width: 122px;
}
}
input[type=search]:-moz-placeholder {
color: #DEDEDE;
}
input[type=search]::-webkit-input-placeholder {
color: #DEDEDE;
}
and here's the HTML:
<form id=\"search_form\" method=\"GET\" action=\"$http://www.$domain/\">
<input type=\"search\" id=\"search\" name=\"search\" placeholder=\"$search_placeholder\" class=\"awesomplete\" autocomplete=\"off\">
</form>
The form can only be submit by pressing enter button. What would be the easiest way to make the search icon clickable ?
I tried making a 30x30 anchor with relative positionning on top of the search icon but it wasn't working well

Adding a border to input with Bootstrap

I am currently working on a website with a complex CSS file. I have added a new feature, but I can't seem to edit an input tab that I have due to other styling affecting it. Essentially I am trying to over-ride a certain property.
CSS
textarea, input[type="number"]{
background-color: #ffffff;
border: 0 solid #CCCCCC;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.035) inset;
transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
}
select, input[type="number"] {
border-radius: 2px 2px 2px 2px;
color: #555555;
display: inline-block;
height: 37px;
font-size: 14px;
line-height: 15px;
margin-bottom: 0px;
padding: 2px 6px;
vertical-align: middle;
}
select, textarea, input[type="number"]
color: #626c72;
display: inline-block;
font-size: 14px;
line-height: 20px;
margin-bottom: 10px;
padding: 4px 6px;
vertical-align: middle;
box-shadow: 1px 1px 8px rgba(0,0,0,0.05);
width: 100%;
}
.target {
border: 0;
}
HTML
<div class="container">
<div class="row">
<div class="form-group col-sm-6">
<label for="Label1">Label1:</label>
<input class="form-control target" step="any" type="number" min="0" max="24"></input>
</div>
</div>
</div>
What I am trying to do is have is override border: 0 solid #CCCCCC; from the first selector and make it look like the default bootstrap input for the .target input . I don't want it to affect all other inputs in my application. I only want it to affect the html you see above. I thought my last styling .target selector would do the trick, but it doesn't. My jsFiddle is here. I want the default bootstrap border/outline for my input. As you can tell its not there right now.
You can use the CSS :not selector if you don't want your custom CSS to apply to that specific input:
textarea, input[type="number"]:not(.target) {
background-color: #ffffff;
border: 0 solid #CCCCCC;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.035) inset;
transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
}
select, input[type="number"]:not(.target) {
border-radius: 2px 2px 2px 2px;
color: #555555;
display: inline-block;
height: 37px;
font-size: 14px;
line-height: 15px;
margin-bottom: 0px;
padding: 2px 6px;
vertical-align: middle;
}
Bootply
You can also use:
input.target {
border: 0;
}
or
input[type="number"].target {
border: 0;
}