How to display and hide a div with CSS?

css show hide div on click
onclick show div and hide other div
hide and show div using javascript with example
hide div jquery
javascript hide element by class
display: none vs hidden
onclick show hide div jquery demo
div display: none

In my script there are three divs. I want to display div with class="ab" when I hover on first line and display div with class="abc", when hover on second line. Otherwise I want to display div with class="a" by default.

But it never displays the div with class="a".

.abc,.ab {
    display: none;
}
#f:hover ~ .ab {
    display: block;

}
#f:hover ~ .abc,.a {
    display: none;

}
#s:hover ~ .abc {
    display: block;

}
#s:hover ~ .ab,.a {
    display: none;
}
<a id="f">Show First content!</a>
<br/>
<a id="s">Show Second content!!</a>
<div class="a">Default Content</div>
<div class="ab">First content</div>
<div class="abc">Second content</div>

You need

.abc,.ab {
    display: none;
}

#f:hover ~ .ab {
    display: block;
}

#s:hover ~ .abc {
    display: block;
}

#s:hover ~ .a,
#f:hover ~ .a{
    display: none;
}

Updated demo at http://jsfiddle.net/gaby/n5fzB/2/


The problem in your original CSS was that the , in css selectors starts a completely new selector. it is not combined.. so #f:hover ~ .abc,.a means #f:hover ~ .abc and .a. You set that to display:none so it was always set to be hidden for all .a elements.

CSS Layout - The display Property, Hide an Element - display:none or visibility:hidden? display:none. Italy. Remove. visibility:hidden. Forest. The default display value for most elements is block or inline. Click to show panel. This panel contains a <div> element, which is hidden by default ( display: none ). It is styled with CSS, and we use JavaScript to show it (change it to ( display: block ).

To hide an element, use:

display: none;
visibility: hidden;

To show an element, use:

display: block;
visibility: visible;

The difference is:

Visibility handles the visibility of the tag, the display handles space it occupies on the page.

If you set the visibility and do not change the display, even if the tags are not seen, it still occupies space.

How To Toggle Between Hiding And Showing an Element, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  To get this to work, we will add two divs, one inside the other. The outer div will always be displayed, along with a title wrapped inside an h3 heading. The inner div will be hidden by default and once the outer div is clicked, the inner div will be shown. Feel free to alter the code to your needs.

you can use any of the following five ways to hide element, depends upon your requirements.

Opacity
.hide {
  opacity: 0;
}
Visibility
.hide {
   visibility: hidden;
}
Display
.hide {
   display: none;
}
Position
.hide {
   position: absolute;
   top: -9999px;
   left: -9999px;
}
clip-path
.hide {
  clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);
}

To show use any of the following: opacity: 1; visibility: visible; display: block;

Source : https://www.sitepoint.com/five-ways-to-hide-elements-in-css/

10 Ways to Hide Elements in CSS, There are multiple ways to hide an element in CSS, but they differ in the way .​hide { display: flex; justify-content: center; list-style-type: none; If you click the show menu link after that, the jQuery query code will add the display: initialvalue. This will make the menu visible again and links will move down. A menu demo with visibility property. I will use the same example as above, but this time using the CSS visibility property to show and hide the menu.

Html Code :

    <a id="f">Show First content!</a>
    <br/>
    <a id="s">Show Second content!!</a>
    <div class="a">Default Content</div>
    <div class="ab hideDiv">First content</div>
    <div class="abc hideDiv">Second content</div>

Script code:

$(document).ready(function() {
    $("#f").mouseover(function(){
        $('.a,.abc').addClass('hideDiv');
        $('.ab').removeClass('hideDiv');
    }).mouseout(function() {
        $('.a').removeClass('hideDiv');
        $('.ab,.abc').addClass('hideDiv');
    });

    $("#s").mouseover(function(){
        $('.a,.ab').addClass('hideDiv');
        $('.abc').removeClass('hideDiv');
    }).mouseout(function() {
        $('.a').removeClass('hideDiv');
        $('.ab,.abc').addClass('hideDiv');
    });
});

css code:

.hideDiv
{
    display:none;
}

Hide Show content-list with only CSS, no javascript used, DEMO http://jsfiddle.net/6W7XD/1/. CSS body { display: block; } .span3:focus ~ .​alert { display: This way the text is only hidden on click of the hide element. import CSS (display, displayNone) import Halogen.HTML as HH import Halogen.HTML.CSS as CSS render state = HH.div [ CSS.style $ display displayNone ] [ HH.text "Hi there!" If you "inspect element", you'll see something like:

Toggle (Show/Hide) Element, Inline usage: Click here to toggle visibility of element #foo This is foo. The CSS properties for "display" and "visibility" both allow you to hide elements in a page's HTML, but they differ in their implications for its appearance and function. Visibility: hidden hides the tag, but it still takes up space and affects the page.

Hide or show elements in HTML using display property , Hide or show elements in HTML using display property. Style display To hide an element, set the style display property to “none”. <style type= "text/css" >. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. w3schools .com THE WORLD'S LARGEST WEB DEVELOPER SITE

Show/Hide Script-Free (Which Means CSS Only), There are many ways to hide and show content with a click (or tap or poke or key-​press or …). You can mitigate that JavaScript failure with a little CSS-only progressive <div class="trviewcat expand_block closed"> <h3  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Comments
  • Ahhh that's what he meant. He could have just said "Hide .a when one of the others is hovered over".
  • Thanks ! It work ! But still I don't understand, why my script never display div with class a. :(
  • @mridul, because the , in css selectors starts a completely new selector. it is not combined.. so #f:hover ~ .abc,.a means #f:hover ~ .abc and .a. You set that to display:none so it was always set to be hidden
  • Thanks . This worked. I needed to hide the jumbotron class from bootstrap in one of the pages. This is how I did it. <style> .jumbotron { display: none; visibility: hidden; } </style>
  • why wouldn't one use just display: none; ?
  • The visibility rule is indeed redundant when you set display:none. Note that a toggling display may cause surrounding elements to move as a result of space reallocation. If this "jumping" effect is undesired, one might want to use visibility instead.