CSS on hover stay (with pre-structured CSS) - css

I'm new up here, and I tried ti find an answer to my question on existing forums. I tried some and couldn't resolve the issue.
I want to make the hover effect to stay after mouseout on this pre-structured code.
Someone have an idea ?
Thanks much !!!
#search_block_top {
position:absolute;
right: 0;
top: 120px;
z-index: 99;
}
#search_block_top p {padding:0;}
#search_block_top #search_query_top {
border: medium none;
height: 31px;
padding: 0;
width: 1px;
background:#FFF;
box-shadow:none;
line-height: 33px\9;
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
}
#search_block_top .button {
background: url("img/bg-search.png") no-repeat scroll right center transparent;
border: medium none;
border-radius: 0 0 0 0;
height: 33px;
line-height: 33px;
margin: 10px -5px 0 0;
padding: 0;
text-indent: -9999px;
width: 57px;
}
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox:hover #search_query_top{
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
#searchbox{}
#searchbox label{
text-indent: -9999px;
}
#searchbox #search_query_block{
border: 1px solid #CCCCCC;
-webkit-border-radius:3px !important;
-moz-border-radius:3px !important;
border-radius:3px !important;
height: 18px;
margin-top:10px;
}
#searchbox #search_button{padding: 1px 4px;}

#badAdviceGuy is right on this one. There's no way in pure css to make the change stay there after you've hovered off of it.
If you do want to venture into jQuery territory however, it would look something like this:
$(document).on('mouseenter', '#hover-element', function(){
$(this).addClass('highlight');
});
Then you would just add whatever changes you want to take place to the highlight class in css and voila!
hope that helps

Well it's best practice to put the jQuery code into a separate js file, but it seems like you'd rather just get it to work so here's the quick and dirty way:
using the css you added above, your new css will look like this:
Change this:
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox:hover #search_query_top{
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
To be this:
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox.highlight #search_query_top {
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
the html you copied in won't need to be changed, don't worry about that. Then you'll want to add this code into your header before the closing </head> tag
<script type="text/javascript">
$(document).ready(function(){
$(document).on('mouseenter', '#searchbox', function(){
$(this).addClass('highlight');
});
});
</script>
The only issue with this is that you may not have already included the jQuery library. If you put this code in and it doesn't work, add the following code into the header right before the js code you just put in:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Let me know if that helps, to answer your second question, no, css won't be able to override the styling on mouseleave.
Keep in mind also that if you're using something like wordpress, then the rules are different and none of this code will work.
good luck!

Related

CSS hover triggered vertical sliding animated header

I wanting a header bar that slides vertically into view from negative top.
Rather than simply appears as if being behind a curtain.
The following is animated using height :-
https://jsfiddle.net/AaronNGray/kf0br46u/31/
HTML
<div id="box">
<div id="content">AaronNGray</div>
</div>
CSS
body {
margin: 0px;
padding: 0px;
}
#box {
height: 100px;
width: auto;
background: transparent;
transition: all 0.4s ease-in-out;
}
#content {
overflow: hidden;
width: auto;
background: white;
height: 0px;
transition: all 0.4s ease-in-out;
border-bottom: 2px solid black;
-webkit-transition: all .8s ease;
-moz-transition: all .8s ease;
-ms-transition: all .8s ease;
-o-transition: all .8s ease;
transition: all .8s ease;
}
#box:hover > #content {
height: 50px;
top: 0px;
}
What I need is to be able to animate top so the content div slides downwards from off the top of the screen.
This is what I have tried but it does not work :-
https://jsfiddle.net/AaronNGray/kf0br46u/40/
body {
margin: 0px;
padding: 0px;
}
#box {
height: 100px;
top: -50px;
width: auto;
background: transparent;
transition: top 0.4s ease-in-out;
}
#content {
overflow: hidden;
width: auto;
background: white;
top: -50px;
height: 50px;
transition: top 0.4s ease-in-out;
border-bottom: 2px solid black;
-webkit-transition: top .8s ease;
-moz-transition: top .8s ease;
-ms-transition: top .8s ease;
-o-transition: top .8s ease;
}
#box:hover > #content {
top: 0px;
}
Hope you can help and its probably something simple I am missing, usually is :)
There are a couple of problems.
First, positioning with e.g. top does not work if the element's position is not defined (and if it is, the positioning is in relation to the first ancestor which itself is positioned).
Second, the box element is positioned at -50px (half its height) which is fine, but the content is put -50px which would put it at -100px (if it were positioned at all).
Here's a snippet with your code with these two things altered:
body {
margin: 0px;
padding: 0px;
position: relative;
}
#box {
height: 100px;
top: -50px;
width: auto;
transition: top 0.4s ease-in-out;
position: relative;
}
#content {
overflow: hidden;
position: relative;
top: 0px;
width: auto;
height: 50px;
transition: top 0.4s ease-in-out;
border-bottom: 2px solid black;
}
#box:hover #content {
top: 50px;
}
<div id="box">
<div id="content">AaronNGray</div>
</div>
StackOverflow has a monopoly on Google and the Internet and is abusing this by stopping people asking questions that they really need to ask in order to do their work. You may regard this question as stupid but theres no where else you cn get CSS answers anymore you have killed off all the other CSS forums !!!!!

How to apply transform and transition effect on a div

I was practicing css on an example i found. I tried to show the submenu above the nav with transition effects. I can change the position of the submenu on hover :
nav li:hover .menu-sub {
display: block;
transform: translateY(-100%);
}
I also modified the code to add a transition effect:
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
display: none;
color: #fff;
padding: 2em;
-webkit-transition: -webkit-transform 1.5s ease;
-moz-transition: -moz-transform 1.5s ease;
-o-transition: -o-transform 1.5s ease;
transition: transform 1.5s ease;
}
The position changed but I don't see any transition effect at all. What am i doing wrong ?
Please modify the transition to shown below, it was written wrong.
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
opacity:0;
overflow:hidden;
box-sizing:border-box;
height:0px;
color: #fff;
-webkit-transition: opacity 1.5s ease-out;
-moz-transition: opacity 1.5s ease-out;
-o-transition: opacity 1.5s ease-out;
transition: opacity 1.5s ease-out;
}
Transition does not work with display, instead use the below effect.
Codepen Demo
Where we can toggle the height from 0px to auto(full height) and opacity from 0(invisible) to 1(visible). You can see that we only see the animation on opacity, this will produce the best effect.
Use visibility:hidden then visible
display: none disables it in the active DOM and such elements with this CSS can't be selected for stuffs like animations.
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
visibility: hidden;
color: #fff;
padding: 2em;
transition: transform 1.5s ease;
}
nav li:hover .menu-sub {
visibility: visible;
transform: translateY(-100%);
}

button width css transition doesn't work

I'm having a issue with a transition css property in a button tag.
When I hover the button I supposed it will go smoothly to width:auto, but it jump directly.
This is the code, what did I miss?
<button>Hello</button>
button {
padding: 10px;
width: 30px;
overflow:hidden;
transition: width 400ms ease-in-out;
-webkit-transition: width 400ms ease-in-out;
-moz-transition: width 400ms ease-in-out;
}
button:hover {
width: auto;
}
max-width is your friend
Demo
button {
padding: 10px;
width: auto;
max-width: 30px;
overflow:hidden;
transition: max-width 400ms ease-in-out;
-webkit-transition: max-width 400ms ease-in-out;
-moz-transition: max-width 400ms ease-in-out;
}
button:hover {
max-width: 100%;
}
Transition won't work with width: auto; you have to give a value in px, ems, percentage or whatever...

CSS3 transition for opacity on hover does not work

I am building a navigation that's going to fade in and out a dropdown sub menu. Because I want to build it in pure CSS, I am trying to use a transition. But whatever I do, it does not work, though it seems to be the correct way.
I define the hidden, normal version of the sub list to be display none and opacity 0, telling it to use transitions. And then, on hover of its parent it shall be display-block'd and the opacity should be transitioned.
Where's the problem here?
CSS:
#nav-main>ul>li>ul {
display: none;
opacity: 0;
position: absolute;
top: 2rem;
background-color: #fef1a3;
margin: 0;
padding: 0.25rem 0;
text-align: center;
list-style: none;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.25);
-o-transition: all 0.5s linear;
-moz-transition: all 0.5s linear;
-khtml-transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
-ms-transition: all 0.5s linear;
transition: all 0.5s linear
}
#nav-main>ul>li:hover>ul {
display: block;
opacity: 1
}
HTML:
<nav id="nav-main">
<ul>
<li>
Link 1
<ul>
<li>
Link 1.1
</li>
</ul>
</li>
</ul>
</nav>
For your reference, here's a fiddle (don't mind the call for transitioning "all" instead of just "opacity", tried both :-) ):
https://jsfiddle.net/hzhnqx1r/
Tried it in Firefox and Safari so far. Both have the same problem.
Thanks for your help!
Transitions will not work if you toggle the display property to/from none.
(MDN: Which CSS properties are animatable)
A trick is to move it in/out of sight as well.
A simplified example (of your code)
#nav-main>ul>li>ul {
opacity: 0;
position: absolute;
top: 1rem;
left:-9999rem;
transition: opacity 0.5s linear 0s, left 0s linear 0.5s;
}
#nav-main>ul>li:hover>ul {
opacity: 1;
left:0;
transition: opacity 0.5s linear;
}
What we do here is we initially place the ul way off the screen (left:-9999rem and set it to 0 on :hover
Using a delay on the left property animation when un-hovering to allow the fadeout to be visible.
Demo at https://jsfiddle.net/hzhnqx1r/3/
Then you don't need to display: none and display: block on hover.
like this
#nav-main>ul>li>ul {
opacity: 0;
position: absolute;
top: 2rem;
background-color: #fef1a3;
margin: 0;
padding: 0.25rem 0;
text-align: center;
list-style: none;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.25);
-o-transition: all 0.5s linear;
-moz-transition: all 0.5s linear;
-khtml-transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
-ms-transition: all 0.5s linear;
transition: all 0.5s linear
}
#nav-main>ul>li:hover>ul {
opacity: 1
}
For your knowledge opacity property value work with in 0-0.9
I also updated your fiddle reference for your better understanding
https://jsfiddle.net/hzhnqx1r/2/

CSS3 transform and transition doesn't work properly on firefox

I'm trying to make an affect on a box to drop 5px down when hovering.
It does work smoothly on Chrome but on firefox it's doesn't do the transition.
Please have a look at the next codepen using firefox and using chrome
<div class="test"></div>
.test {
background-color:blue;
width: 200px;
height: 200px;
#include transition(transform .3s 0 ease);
#include transform(translateY(0));
&:hover {
#include transform(translateY(5px));
}
}
Using Padding
Here's my preferred method using only padding:
JSFiddle DEMO
CSS:
body {
margin: 0;
padding: 0;
}
.test {
background-color:blue;
width: 200px;
height: 200px;
}
.test:hover {
margin-top: 10px;
}
.transition {
-webkit-transition: margin 0.5s ease-out;
-moz-transition: margin 0.5s ease-out;
-o-transition: margin 0.5s ease-out;
transition: margin 0.5s ease-out;
}
Using Transform
Or if you still want to use transform:
JSFiddle DEMO
CSS:
body {
margin: 0;
padding: 0;
}
.test {
background-color:blue;
width: 200px;
height: 200px;
}
.test:hover {
-webkit-transform: translateY(10px);
-moz-transform: translateY(10px);
-ms-transform: translateY(10p));
-o-transform: translateY(10px);
transform: translateY(10px);
}
.transition {
-webkit-transition: -webkit-transform 0.5s ease-out;
-moz-transition: -moz-transform 0.5s ease-out;
-o-transition: -o-transform 0.5s ease-out;
transition: transform 0.5s ease-out;
}
As Kiran said already, each browser has varying support for directly using transform and transition. You can check who can use transforms here and transitions here.
Also take note that the transition wasn't applied to the :hover. It needs to be called at the base level (in this case at the div level).
Hi i guess will might help you out http://codepen.io/anon/pen/dHBni
check below css to find transitions property for different browsers
.box {
width: 150px;
height: 150px;
background: red;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
-webkit-transition: background-color 2s ease-out;
-moz-transition: background-color 2s ease-out;
-o-transition: background-color 2s ease-out;
transition: background-color 2s ease-out;
cursor: pointer;
}
.box:hover {
background-color: green;
}
for more information about transition http://css3.bradshawenterprises.com/transitions/

Resources