Selecting parent menu should show child menu - css

I am developing a Wordpress site and trying to display my menu as shown in image. How can I display the sub menu when the parent menu is selected?

You can create your main top links with secondary nested like so
<ul class="primary">
<li>Tutorial</li>
<ul class="secondary">
<li>Photoshop</li>
<li>Illustrator</li>
<li>Flash</li>
<li>HTML</li>
<li>PHP</li>
<li>Wordpress</li>
<li>jQuery</li>
<li>more...</li>
</ul>
<li>Wallpaper</li>
<li>Get A Quote</li>
<li>Photography</li>
<li>Freelance</li>
</ul>
then your styling would be like so ( this is using just CSS3, not JS )
<style>
ul.primary {
width: -- ;
height: -- ;
margin: -- ;
overflow: hidden;
}
ul.primary > li {
width: -- ;
height: -- ;
margin: -- ;
float: left;
list-style: none;
}
ul.seconday {
opacity: 0;
width: -- ;
height: -- ;
margin: -- ; /* when this is used with position: relative you can adjust where the drop down is placed. */
position: relative; /* need to set this to relative to position properly */
/* css3 transition */
transition: all .5s ease-in;
-webkit-transition: all .5s ease-in;
-moz-transition: all .5s ease-in;
-ms-transition: all .5s ease-in;
-o-transition: all .5s ease-in;
}
ul.primary > li:hover ul.secondary {
opacity: 1;
}
</style>

Related

css content property transition in animation keyframe [duplicate]

Please view this code jsfiddle:
http://jsfiddle.net/rflfn/6wCp6/
<div id="menu">
<ul>
<li>Link #1</li>
<li>Link #2</li>
<li>Link #3</li>
<li>Link #4</li>
</ul>
</div>
CSS:
*{
margin: 0;
padding: 0;
text-decoration: none;
font-family: arial;
font-size: 16px;
}
a{
color: #000000;
}
a:hover{
color: #860000;
}
#menu{
margin: 15px auto;
padding: 20px;
width: 300px;
background: #DDDDDD;
border-radius: 5px;
box-shadow: 0 0 5px #000000;
}
#menu ul{
list-style: none;
}
#menu li:before{
margin-right: 10px;
content: url("http://st.deviantart.net/emoticons/s/smile.gif");
transition: all 0.5s ease 0s;
/* transition: content 0.5s ease 0s; */
}
#menu li:hover:before{
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
}
I have one image on tag 'li' and other image on 'li:hover', is possible make transition with fade only using css?
You can do this by using both pseudo elements :before/after and using the CSS3 transition to animate the opacity of both on hover. This will create a fade transition between both images.
DEMO
CSS :
*{
margin: 0;
padding: 0;
text-decoration: none;
font-family: arial;
font-size: 16px;
}
a{
color: #000000;
}
a:hover{
color: #860000;
}
#menu{
margin: 15px auto;
padding: 20px;
width: 300px;
background: #DDDDDD;
border-radius: 5px;
box-shadow: 0 0 5px #000000;
}
#menu ul{
list-style: none;
}
#menu ul li{
position:relative;
}
#menu li:before{
margin-right: 10px;
content: url("http://st.deviantart.net/emoticons/s/smile.gif");
transition: opacity 0.5s ease;
-webkit-transition: opacity 0.5s ease;
}
#menu li:after{
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
position:absolute;
left:0;
opacity:0;
transition: opacity 0.5s ease;
-webkit-transition: opacity 0.5s ease;
}
#menu li:hover:after{
opacity:1;
}
#menu li:hover:before{
opacity:0;
}
EDIT :
Even better, you can position both images one on top of the other and with z-index and css transition animate the opacity of ony one image :
DEMO
Your case in particular
Not sure why're you trying to put images in content,
you could simply add that image as a background-image to the :before,
set it size & display: inline-block; and you would just animate the background-image like so: http://jsfiddle.net/7f695m1q/ , since background-image transition is supported :)
TL;DR: Can content CSS property be animated? No.
MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
OUTDATED w3.org official document: https://www.w3.org/TR/2017/WD-css-transitions-1-20171130/#animatable-css
This may however change in future because this list is missing in current official sources:
missing in working draft https://www.w3.org/TR/css-transitions-1/
missing in editor's draft https://drafts.csswg.org/css-transitions/
So in theory there is a possibility this list will be updated and changes made in future browser versions, but currently it doesn't work.
Is content affected by other animations? Yes.
Content not being animatable in itself doesn't mean it's not affected by other animations like opacity, visibility etc. It can be leveraged in at least 2 simple ways:
a) answer by #web-tiki provides a smooth&always visible fade effect, however, you have to sacrifce both :after and :before to it
b) if fadeOut => fadeIn is an option for you you can levarage CSS animations & #keyframes
#keyframes changeContent {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
content: "See? I've changed seemlessly!";
}
}
div:before{
content: "HOVER OVER ME!";
background: green;
transition: all 1s linear;
animation-direction: reverse;
}
div:hover:before{
animation-fill-mode: forwards;
animation: changeContent 3s linear forwards;
background: orange;
}
<div />
Important here is to put the "forward" - it's an animation fill mode that basically says "stop the animation at it's end", otherwise it'd jump back.
There is also one drawback - it's not easily animatable on hover-out - if I find some reasonable way I will edit this answer or please comment if you know.
I think #web-tiki 's answer suits your needs and it's simpler. But just to show another possible solution:
You can separate the icons in two elements, each with its own content. Then, apply the transition on the li:hover event, setting the element's opacity inverted. Like this example:
FIDDLE: http://jsfiddle.net/2J7b9/1/
<ul>
<li>
<div class="img1"></div>
<div class="img2"></div>
test
</li>
</ul>
CSS:
li {
position: relative;
padding-left: 30px;
}
li .img1, li .img2 {
position: absolute;
top: 0;
left: 0;
}
li .img1 {
opacity: 1;
transition: all .25s ease-in-out;
}
li .img2 {
opacity: 0;
transition: all .25s ease-in-out;
}
li .img1:before {
content: url("http://st.deviantart.net/emoticons/s/smile.gif");;
}
li .img2:before {
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
}
li:hover .img1 {
opacity: 0;
}
li:hover .img2 {
opacity: 1;
}

Transitioning opacity from 0 to .9 works with toggling visibility from hidden to visible, but not the other way around

I have a sub-menu that appears when I hover over an item in the main menu:
I have a transition effect whereby the sub-menu transitions from 0 opacity to .9 opacity in .5 seconds. However, I also have to toggle the visibility from hidden to visible in order for this to work.
Here's the html:
<li style="position: relative;" onmouseover="showLegalMenu()" onmouseout="hideLegalMenu()">
<a>Legal</a>
<div id="legal-menu" class="legal">
<ul>
#if (termsOfUse != null)
{
<li>#termsOfUse.Name</li>
}
#if (privacyAndSecurity != null)
{
<li>#privacyAndSecurity.Name</li>
}
#if (refundPolicy != null)
{
<li>#refundPolicy.Name</li>
}
</ul>
</div>
</li>
Here's the Javascript:
function showLegalMenu() {
$("#legal-menu").addClass("legal-show");
}
function hideLegalMenu() {
$("#legal-menu").removeClass("legal-show");
}
Here's the CSS:
.legal {
visibility: hidden;
background-color: #383838;
opacity: 0;
padding: 5px 0;
z-index: 10;
position: absolute;
top: 15px;
left: 10px;
width: 150px;
transition: opacity .5s linear;
}
.legal-show {
visibility: visible;
opacity: .9;
}
Transitioning in works fine. The legal-show class is added, it is set to visible, and it transitions from 0 opacity to .9 opacity.
It's transitioning out that's the problem. The legal-show class is removed, causing the sub-menu to become invisible immediately (no transition). The sub-menu items still transition from .9 opacity to 0 opacity somehow (even though the div they are contained in is supposedly invisible at this time), but I would like for the sub-menu div to also transition to 0 opacity like this as well.
If I could just set the visibility to hidden at the end of the transition rather than right away, I believe this would work. How does one does this? Thanks.
No need to complicate it using JavaScript. This can be easily achieved by CSS only.
.legal {
visibility: hidden;
background-color: #383838;
opacity: 0;
padding: 5px 0;
z-index: 10;
position: absolute;
top: 15px;
left: 10px;
width: 150px;
transition: all 0.5s ease-in-out;
}
.parent-li:hover .legal {
visibility: visible;
opacity: .9;
}
<li class="parent-li">
<a>Legal</a>
<div id="legal-menu" class="legal">
<ul>
<li>#termsOfUse.Name</li>
<li>#privacyAndSecurity.Name</li>
<li>#refundPolicy.Name</li>
</ul>
</div>
</li>
.to-toggle {
max-height: 0;
overflow: hidden;
opacity: 0.9;
background: red;
transition: max-height .5s ease-out;
}
.to-toggle.active {
height: auto;
max-height: 500px;
transition: max-height .5s ease-in;
}
<div class="legal">
<label id="hoverable">Hover me</label>
<div class="to-toggle">
<ul>
<li>show1</li>
<li>show2</li>
<li>show3</li>
</ul>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(() => {
$('#hoverable').hover(function() {
$('.to-toggle').addClass('active')
console.log('asdasd')
}, function() {
$('.to-toggle').css({'transition': 'opacity height 5s ease-out'}).removeClass('active')
})
})
</script>
Instead of only transitionning opacity, do it also for visibility :
transition: opacity .5s linear, visibility .5s;
visibility is a "0" or "1" state (no intermediate values) so it will change its value after .5s, giving time for your opacity transition to take effect for the fade out.
Edit : for now I don't understand why it also work for the fade in.
.legal {
visibility: hidden;
background-color: #383838;
opacity: 0;
padding: 5px 0;
z-index: 10;
position: relative;
top: 15px;
left: 10px;
width: 150px;
transition: opacity .5s linear, visibility .5s;
}
.legal-show {
visibility: visible;
opacity: .9;
}
li.legal-container:hover #legal-menu,
#legal:hover {
visibility: visible;
opacity: .9;
}
<ul>
<li style="position: relative;" class="legal-container">
<a>Legal</a>
<ul id="legal-menu" class="legal">
<li>test3</li>
<li>test</li>
<li>test2</li>
</ul>
</li>
</ul>

Is possible use fade transition on tag 'content' only with css?

Please view this code jsfiddle:
http://jsfiddle.net/rflfn/6wCp6/
<div id="menu">
<ul>
<li>Link #1</li>
<li>Link #2</li>
<li>Link #3</li>
<li>Link #4</li>
</ul>
</div>
CSS:
*{
margin: 0;
padding: 0;
text-decoration: none;
font-family: arial;
font-size: 16px;
}
a{
color: #000000;
}
a:hover{
color: #860000;
}
#menu{
margin: 15px auto;
padding: 20px;
width: 300px;
background: #DDDDDD;
border-radius: 5px;
box-shadow: 0 0 5px #000000;
}
#menu ul{
list-style: none;
}
#menu li:before{
margin-right: 10px;
content: url("http://st.deviantart.net/emoticons/s/smile.gif");
transition: all 0.5s ease 0s;
/* transition: content 0.5s ease 0s; */
}
#menu li:hover:before{
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
}
I have one image on tag 'li' and other image on 'li:hover', is possible make transition with fade only using css?
You can do this by using both pseudo elements :before/after and using the CSS3 transition to animate the opacity of both on hover. This will create a fade transition between both images.
DEMO
CSS :
*{
margin: 0;
padding: 0;
text-decoration: none;
font-family: arial;
font-size: 16px;
}
a{
color: #000000;
}
a:hover{
color: #860000;
}
#menu{
margin: 15px auto;
padding: 20px;
width: 300px;
background: #DDDDDD;
border-radius: 5px;
box-shadow: 0 0 5px #000000;
}
#menu ul{
list-style: none;
}
#menu ul li{
position:relative;
}
#menu li:before{
margin-right: 10px;
content: url("http://st.deviantart.net/emoticons/s/smile.gif");
transition: opacity 0.5s ease;
-webkit-transition: opacity 0.5s ease;
}
#menu li:after{
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
position:absolute;
left:0;
opacity:0;
transition: opacity 0.5s ease;
-webkit-transition: opacity 0.5s ease;
}
#menu li:hover:after{
opacity:1;
}
#menu li:hover:before{
opacity:0;
}
EDIT :
Even better, you can position both images one on top of the other and with z-index and css transition animate the opacity of ony one image :
DEMO
Your case in particular
Not sure why're you trying to put images in content,
you could simply add that image as a background-image to the :before,
set it size & display: inline-block; and you would just animate the background-image like so: http://jsfiddle.net/7f695m1q/ , since background-image transition is supported :)
TL;DR: Can content CSS property be animated? No.
MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
OUTDATED w3.org official document: https://www.w3.org/TR/2017/WD-css-transitions-1-20171130/#animatable-css
This may however change in future because this list is missing in current official sources:
missing in working draft https://www.w3.org/TR/css-transitions-1/
missing in editor's draft https://drafts.csswg.org/css-transitions/
So in theory there is a possibility this list will be updated and changes made in future browser versions, but currently it doesn't work.
Is content affected by other animations? Yes.
Content not being animatable in itself doesn't mean it's not affected by other animations like opacity, visibility etc. It can be leveraged in at least 2 simple ways:
a) answer by #web-tiki provides a smooth&always visible fade effect, however, you have to sacrifce both :after and :before to it
b) if fadeOut => fadeIn is an option for you you can levarage CSS animations & #keyframes
#keyframes changeContent {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
content: "See? I've changed seemlessly!";
}
}
div:before{
content: "HOVER OVER ME!";
background: green;
transition: all 1s linear;
animation-direction: reverse;
}
div:hover:before{
animation-fill-mode: forwards;
animation: changeContent 3s linear forwards;
background: orange;
}
<div />
Important here is to put the "forward" - it's an animation fill mode that basically says "stop the animation at it's end", otherwise it'd jump back.
There is also one drawback - it's not easily animatable on hover-out - if I find some reasonable way I will edit this answer or please comment if you know.
I think #web-tiki 's answer suits your needs and it's simpler. But just to show another possible solution:
You can separate the icons in two elements, each with its own content. Then, apply the transition on the li:hover event, setting the element's opacity inverted. Like this example:
FIDDLE: http://jsfiddle.net/2J7b9/1/
<ul>
<li>
<div class="img1"></div>
<div class="img2"></div>
test
</li>
</ul>
CSS:
li {
position: relative;
padding-left: 30px;
}
li .img1, li .img2 {
position: absolute;
top: 0;
left: 0;
}
li .img1 {
opacity: 1;
transition: all .25s ease-in-out;
}
li .img2 {
opacity: 0;
transition: all .25s ease-in-out;
}
li .img1:before {
content: url("http://st.deviantart.net/emoticons/s/smile.gif");;
}
li .img2:before {
content: url("http://st.deviantart.net/emoticons/r/razz.gif");
}
li:hover .img1 {
opacity: 0;
}
li:hover .img2 {
opacity: 1;
}

CSS Dropdown menu with animation (no js)

Trying to create an animated dropdown menu using CSS animation, without any JS. Thought I've been barking up the right tree but can't see where I'm going wrong, for this simplified menu item...
<div class="menu">Menu Item
<ul>
<li>Dropdown 1</li>
<li>Dropdown 2</li>
<li>Dropdown 3</li>
<li>Dropdown 4</li>
<li>Dropdown 5</li>
</ul>
</div>
And the following CSS;
.menu ul {
height: 0px;
overflow: hidden;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.menu:hover ul {
height: auto;
}
Thought that should successfully result in a scroll down of the div, but it just keeps appearing. Any thoughts? Cheers
See this topic for reference: How can I transition height: 0; to height: auto; using CSS?
To put it simply, you can not animate to height: auto;. It is not supported. If you have a pre-determined, fixed height, you can do it by animating to that specific value. 0px to 100px for instance. Auto however is not supported.
The first answer in the link above links to another article in which a sort of work-around is given. You may explore that for implementation on your site.
Can you use CSS3 to transition from height:0 to the variable height of content?
You can't use CSS transitions with height:auto, only with specific values.
.menu:hover ul {
height: 100px;
}
http://jsfiddle.net/mblase75/cWZMu/
The animation for a dropdown can be implemented with pure CSS:
ul {
overflow: hidden;
transition: all .3s ease;
}
ul.folded {
max-height: 0;
}
ul.unfolded {
max-height: 300px; //value of max-height should always bigger than actual sum of all li's height
}
I was able to build a solution similar to #artcher, but instead I used max-height: 100%; and that worked perfectly:
ul.submenu {
overflow: hidden;
transition: all .3s ease;
max-height: 0;
}
.top-level-item:hover {
.submenu {
max-height: 100%;
}
}

CSS3 transition

I have this page here (work in progress) http://kimwilddesigns.com/index_new.htm
In this section, I want to be able to hover on the li, have the background image fade out and the h2 to fade in. Is this possible with transitions? I might not be setting it up correctly but I wanted to see if this kind of effect is even possible.
<div id="categories-wrapper">
<ul>
<li class="fine-art"><img src="pics/hp_icon_fine-art.jpg" alt="fine art" width="290" height="240" border="0">
<h2>fine art work</h2>
</li>
<li class="gd">graphic design work</li>
<li class="students">my students' work</li>
</ul>
</div>
Yes, this works nicely with CSS transitions, something like that:
#categories-wrapper li a {
position:relative;
display: block
}
#categories-wrapper li h2 {
position: absolute;
left: 20px;
top: 20px;
opacity: 0;
}
#categories-wrapper li h2,
#categories-wrapper li img {
-webkit-transition: opacity .3s ease-in-out;
-moz-transition: opacity .3s ease-in-out;
-o-transition: opacity .3s ease-in-out;
transition: opacity .3s ease-in-out;
}
#categories-wrapper li:hover h2 {
opacity: 1
}
#categories-wrapper li:hover img {
opacity: 0
}
See the Fiddle for this, slightly changed your markup by putting the h2 inside of the a tag.

Resources