How to display and hide a div with CSS? - css

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>
Here is my JSFiddle of my problem: JSFiddle Link

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.

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.

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/

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;
}

.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>

html code :
<button class="Show">Show</button>
<button class="Hide">Hide</button>
<button class="toggle">Show & Hide</button>
<div id="target"></div>
css code :
#target {
background:#0099cc;
width:300px;
height:300px;
height:160px;
padding:5px;
display:none;
}
.Hide
{
display:none;
}
javascript code :
$('.Show').click(function() {
$('#target').show(200);
$('.Show').hide(0);
$('.Hide').show(0);
});
$('.Hide').click(function() {
$('#target').hide(500);
$('.Show').show(0);
$('.Hide').hide(0);
});
$('.toggle').click(function() {
$('#target').toggle('slow');
});

Related

CSS hover affects another element with pseudo after attribute

This following css works (it makes visible button2 when hovering on button1)
#btn1:hover ~ #btn2 { visibility: initial;}
But if I want to make the same for the pseudo element #btn2:after, the following code doesn't work
#btn1:hover ~ #btn2:after { visibility: initial;}
Is there a reason or a workaround for it ?
It could be that you need to add the content property to btn2 🤔
#btn1:hover ~ #btn2 {
visibility: initial;
background-color:red
}
#btn1:hover ~ #btn2:after{
visibility: initial;
content:' Soy el after';
}
<button id="btn1">Botón 1</button>
<button id="btn2">Botón 2</button>
You can also use display:none;
When you use visibilty:hidden; your buttons will still have width and height in your body element but when using display:none; you can place any element to that position.
body {
position: relative;
height: 100vh;
background-color: bisque;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap:1rem;
}
button{
height: 50px;
width:100px;
}
#btn2::after{
content: '';
width:20px;
height: 20px;
background-color: red;
display: none;
}
#btn1:hover ~ #btn2::after {
display: inline-block;
}
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>
Obviously you are both right. I made some adjustments and it works... can't figure what was wrong
I used visibility parameter

Accessing the checkbox state from not directly after the input using just CSS

I am creating a custom checkbox and want clicking the label text to also toggle the state and show or not show a text depending of the state of the checkbox just using CSS. I wrote this html:
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + .label-text:before{
content: "+";
width: 1em;
height: 1em;
display: inline-block;
margin-right: 5px;
}
input[type="checkbox"]:checked + .label-text:before{
content: "-";
}
label input[type="checkbox"]:checked ~ .elements .is_not_checked{
display: none;
}
label input[type="checkbox"]:not(:checked) ~ .elements .is_checked{
display: none;
}
<label>
<input type="checkbox" name="check"> <span class="label-text">Item Two</span>
<div class="elements">
<div class="is_checked">
Checkbox is checked
</div>
<div class="is_not_checked">
Checkbox is not checked
</div>
</label>
This works. But I am not happy with the checkbox changing state when I click on the element text. However, when I move the elements out of the label I can no longer access the checkbox state. I tried:
(label > input[type="checkbox"]:checked) ~ .elements .is_not_checked{
display: none;
}
But this did not even hide the text. How can I access the state of a checkbox hidden in another element (label in this case) just using CSS?
Use HTML to "connect" the label to the checkbox. This will make a click on the label toggle the state, too. Using the checked state of the checkbox and the sibling selector you can toggle the elements.
<style>
input[type=checkbox] {
display: none;
}
input[type="checkbox"] + label:before {
content: "+";
width: 1em;
height: 1em;
display: inline-block;
margin-right: 5px;
}
input[type="checkbox"]:checked + label:before {
content: "-";
}
input[type=checkbox]:checked ~ .elements div.checked,
input[type=checkbox]:not(:checked) ~ .elements div.unchecked {
display: block;
}
input[type=checkbox]:checked ~ .elements div.unchecked,
input[type=checkbox]:not(:checked) ~ .elements div.checked {
display: none;
}
</style>
<input type="checkbox" id="check" name="check">
<label for="check">My Item</label>
<div class="elements">
<div class="checked">Checkbox is checked.</div>
<div class="unchecked">Checkbox is NOT checked.</div>
</div>
I have rearranged your HTML structure, replacing label with div class="label".
Instead of using display: none, the checkbox is hidden using opacity. You can then control the area it takes up.
.label {
position: relative;
display: inline-block;
}
input[type="checkbox"] {
opacity: 0;
position: absolute;
left: 0;
top: 0;
width: 100%;
}
input[type="checkbox"]+.label-text:before {
content: "+";
width: 1em;
height: 1em;
display: inline-block;
margin-right: 5px;
}
input[type="checkbox"]:checked+.label-text:before {
content: "-";
}
input[type="checkbox"]:checked~.elements .is_not_checked {
display: none;
}
input[type="checkbox"]:not(:checked)~.elements .is_checked {
display: none;
}
<div class="label">
<input type="checkbox" name="check">
<span class="label-text">Item Two</span>
<div class="elements">
<div class="is_checked">
Checkbox is checked
</div>
<div class="is_not_checked">
Checkbox is not checked
</div>
</div>
</div>

Fill the remaining space of a div or h1 tag with a pattern

I'm a big novice in CSS, and I need to fill the remaining space of a div or h1 tag with a pattern (it can be an image or generated with CSS).
This is the effect I need to create: http://es.tinypic.com/r/20gi92/8
Thanks for your help!
I would use it like this: http://jsfiddle.net/maximgladkov/3rNY3/
HTML
<div class="title">
<h1>Test title</h1>
</div>
CSS
.title {
background: url(http://ru.vectorboom.com/Tutorials/FloralPattern/final.png);
}
.title h1 {
background: #fff;
display: inline;
padding: 0 10px 0 0;
}
I assume you mean remaining as in, you already have an img or another element as the main part of the div. So i'd guess adding a background to the div is what you are after.
Example HTML:
<div class="color">
<h1>Some Text</h1>
</div>
<div class="img">
<h1>Some Text</h1>
</div>
CSS for adding color it would be:
.color {
background: yellow;
}
Or an image:
.img {
background: url('http://www.lorempixel.com/200/50');
}
DEMO
I would use a span inside the h1 and a pseudo element.
Codepen Demo(s)
HTML
<div class="wrapper"> /* not required */
<h1 class="left"><span>Some Text</span></h1>
<h1 class="right"><span>Some Much Longer Text</span></h1>
</div>
CSS
h1 {
overflow:hidden; /* hides all extra pixels */
font-size:2em;
}
.right {
text-align:right;
}
h1 > span {
diaplay:inline-block;
background:Navajowhite;
position:relative;
}
h1.left > span:after {
content: "";
position: absolute;
left:100%;
top: 0;
height: 2em; /* same as h1 or same line-height*/
width:2000px; /* some really large number*/
background:red;
margin-left:0.5em; /* or remove and add padding-right to span */
}
h1.right > span:before {
content: "";
position: absolute;
right:100%;
top: 0;
height: 2em; /* same as h1 or same line-height*/
width:2000px; /* some really large number*/
background:red;
margin-right:0.5em; /* or remove and add padding-right to span */
}

Show div on hover of embeded div

I'm trying to display div.overlay on hover of embedded div.box
div.overlay {padding:5px; background:#F00; width:100px; visibility: hidden;}
div.box {display:block; background:#FF0; width:100px; visibility:visible;}
div.box:hover div.overlay { visibility: visible;}
<div class="overlay">
<div class="box">Info about a game</div>
Play
</div>
Thanks for any hint
I suggest you change the markup slightly if possible. It will make it possible to do what you want.
Option 1:
HTML
<div class="box">Info about a game</div>
<div class="overlay">Play</div>
CSS
div.box,
div.overlay {
width: 100px;
background: #FF0; }
div.overlay { display: none; }
div.box:hover + div.overlay { display: block; }
See fiddle: http://jsfiddle.net/3uM49/
Option 2
HTML
<div class="box">
Info about a game
<div class="overlay">Play</div>
</div>
CSS
div.box {
width: 100px;
background: #FF0; }
div.overlay { display: none; }
div.box:hover div.overlay { display: block; }
See fiddle: http://jsfiddle.net/kgXDT/
You could hover on the parent element and that seems to make it work e.g:
div.overlay:hover {
visibility: visible;
}
Fiddle: http://jsfiddle.net/52sM5/
copy and replace your css with my css
div.overlay {padding:5px; background:#F00; width:100px; visibility: hidden;}
div.box {display:block; background:#FF0; width:100px; visibility:visible;}
div.overlay:hover { visibility: visible;}

i want the the text to be shown besides the icon image and not below it

Here, in the following code, the text in <p></p> gets displayed below the icon in the div. I want this <p> to appear within the div beside the icon image. Also, I want the <div> tags to be positioned at the bottom of the page.
Here is my code.
<body>
<input type="date">Date</input>
<input type="number">Fs</input>
<input type="number">PtP</input>
<br/><br/>
<div id="add"><img src="iconic\vector\plus_alt.svg"><p>Add</p></div>
<div id="show"><img src="iconic\vector\eye.svg" title="see"><p>Show</p></div>
<div id="showAll"><img src="iconic\vector\list.svg"><p>Show All</p></div>
<div id="delete"><img src="iconic\vector\trash_stroke.svg"><p>Clear All</p></div>
</body>
Here css
#add
{
margin-left:auto;
margin-right:auto;
margin-bottom:0px
}
#add
{
width: 25%;
height: 40px;
float: left;
background-color:#ffcc00;opacity:0.75;
}
#add p
{font-size:15px;
position: inline;
visibility:hidden
}
#add:hover p
{
visibility:visible;
}
#show
{
width: 25%;
height: 40px;
float: left;
background-color:#ffcc00;opacity:0.75;
}
#show p
{font-size:15px;
position: inline;
visibility:hidden
}
#show:hover p
{
visibility:visible;
}
#showAll
{
width: 25%;
height: 40px;
float: left;
background-color:#ffcc00;opacity:0.75;
}
#showAll p
{font-size:15px;
position: inline;
visibility:hidden
}
#showAll:hover p
{
visibility:visible;
}
#delete
{
width: 25%;
height: 40px;
float: right;
background-color:#ffcc00;opacity:0.75;
}
#delete p
{font-size:15px;
position: relative;
visibility:hidden
}
#delete:hover p
{
visibility:visible;
}
Your css code is rather verbose.
I think this is what you're looking for
FIDDLE
div
{
width: 25%;
height: 40px;
float: left;
background-color:#ffcc00;opacity:0.75;
}
p
{
font-size:15px;
display:inline;
visibility:hidden;
}
div:hover p
{
visibility:visible;
}
At first, there is no need to write the same style for each element using its ID. You can simply give them the same class name and style only using class name. The second, I'm suggesting you to use UL tag to make a navigation menu. And finally, you can achieve the effect you want using float property. I've updated your original code and here is the demo in JSFiddle.
P.S. Ask me, if something is not clear for you.

Resources