Using a checkbox for a css toggle, this code works... The div disappears when you click the checkbox...
input:checked ~ #test{display:none}
<div id="main">
<input type="checkbox">Click Me
<div id="test">This is a test</div>
</div>
But here, it doesn't work, because the TEST div is not a sibling, right? It's outside of the main div...
input:checked ~ #test{display:none}
<div id="main">
<input type="checkbox">Click Me
</div>
<div id="test">This is a test</div>
Is there a way to make this work with just css? I thought if I remove the ~ then any div with an ID = TEST would disappear no matter where it is, but that's not the case.
well this kind of works
the trigger is inside your main but the actual checkbox must be on the same level as "test"
#main {white-space:nowrap;}
#check {
display:none;
}
p {display:inline-block;}
.fake{
display:inline-block;
width: 10px;
height: 10px;
background:lightgray;
}
#check[type=checkbox]:checked + #main + #test {
display:none;
}
#check[type=checkbox]:checked + #main > label {
background: gray;
}
<input id="check" type="checkbox">
<div id="main">
<label class="fake" for="check"></label>
<p>Click Me</p>
</div>
<div id="test">This is a test</div>
Related
I want to split my site in two vertical sections. If you click on a left button (width:50%), the content for this button should appear below with width:100% and if you click on the right button the same but of course with content 2.
Is it even possible with pure css? Because I don't know java :/ and I think it's a quite simple problem, isn't it?
#content_button_left,
content_button_right {
display: none
}
#button_left:active~#content_button_left {
display: inherit
}
#button_right:active~#content_button_right {
display: inherit
}
<div>
<div style="display:flex">
<div id="button_left" style="flex:1">Menu left</div>
<div id="button_right" style="flex:1">Menu right</div>
</div>
<div id="content_button_left" style="width:100%">
blabla 1
</div>
<div id="content_button_right" style="width:100%">
blabla 2
</div>
</div>
You can use :target CSS selector to fake the click event but for that you have to convert your div to anchor tag, below is CSS
#content_button_left, #content_button_right{
display:none
}
#content_button_left:target {
display:block;
}
#content_button_right:target{
display:block;
}
Updated HTML
<div style="display:flex">
<a id="button_left" href="#content_button_left" style="flex:1">Menu left</a>
<a id="button_right" href="#content_button_right" style="flex:1">Menu right</a>
</div>
<div id="content_button_left" style="width:100%">
blabla 1
</div>
<div id="content_button_right" style="width:100%">
blabla 2
</div>
I would use radio buttons next to the content and labels for your button targeting the radios. This way you can use the adjacent sibling selector to only show the content next to a checked radio:
/* hide radio and content */
.radio,
.content {
display: none;
}
/* show content if it directly follows a checked radio */
.radio:checked + .content {
display: block;
}
<div>
<div style="display:flex">
<label id="button_left" style="flex:1" for="left-input">Menu left</label>
<label id="button_right" style="flex:1" for="right-input">Menu right</label>
</div>
<input type="radio" name="show-content-radio" id="left-input" class="radio">
<div id="content_button_left" style="width:100%" class="content">
blabla 1
</div>
<input type="radio" name="show-content-radio" id="right-input" class="radio">
<div id="content_button_right" style="width:100%" class="content">
blabla 2
</div>
</div>
Is it possible to select a specific <div> when another <div> which is not a parent is :hover?
All that in HTML5/CSS3 only, without JS.
<section>
<div id=first></div>
</section>
<section>
<div class=second></div>
</section>
As an example, i want <div class=second> to show when <div id=first> is :hover.
This is possible, but only if the two elements have the same parent.
Using the element1 ~ element2 selector. For example:
HTML:
<div class="first">
<!-- content -->
</div>
<span class="example-element"></span>
<div class="second">
<!-- content -->
</div>
CSS:
.first:hover ~ .second {
/* styles */
}
If you need to select an element that does not have the same parent, you need to use javascript.
this is two ways to achive that, with click adding an a tag or with hover that its a little tricky
.second{
display:none;
}
#second:target {
display:block;
}
#first a{
text-decoration:none;
color:black;
}
.disp1:hover + .disp2{
display:block;
}
.disp2{
display:none;
}
<section>
<div id="first"><a href="#second" >div one</a></div>
</section>
<section>
<div id="second" class="second">div two</div>
</section>
<div class="disp1">first div</div>
<div class="disp1 disp2">second div</div>
I need to add pipe between links except after the last . My html is rendered in the following way , html is rendered with same class.
<!DOCTYPE html>
<html>
<head>
<style>
p:last-of-type {
background: #ff0000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
</body>
</html>
I need to add Pipe but after last link no pipe should be displayed . How can i do it for this case .
Fiddle Link
P.S. : This can be done easily when we have different links with different class . In my case links are dynamic and after last link no pipe should be displayed
Use this selector .sample:not(:last-of-type), which will target all but the last item.
Do note, when combining a class name with last-of-type, it will target any element type not being the last (if there is more than 1) having the given class.
Updated fiddle (added the pipe to the div so it won't pick up the color from the p)
.sample:not(:last-of-type) > div::after {
content: ' | ';
}
Another option is last-child, .sample:not(:last-child), which also will target all but the last.
Do note, when using last-child, it means the last no matter class or element type, so if you have another element coming after the last sample, that will count as the last, and here is a fiddle sample showing how this rule will fail in cases like that.
Updated fiddle (added the pipe to the div so it won't pick up the color from the p)
.sample:not(:last-child) > div::after {
content: ' | ';
}
A third option is to use the immediate sibling selecor +, which target all sibling elements with the given class but the first.
p:last-of-type {
background: #ff0000;
display: inline-block;
}
.sample + .sample > div::before {
content: ' | ';
}
<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
Here's another way:
'Add a pipe before each link - only if it comes after another one'
.sample + .sample .para:before {
content: "|";
margin-right: 10px;
color: red;
}
.sample + .sample .para:before {
content: "|";
margin-right: 10px;
color: red;
}
<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
</div>
</div>
I think you want like this.
.sample:last-child div {
border: none;
}
.sample div {
border-right: 1px solid #000;
margin-right: 10px;
padding-right: 10px;
}
<h1>This is a heading</h1>
<div>
<div class="sample" style="display:inline-block">
<div>
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div>
<p class="para">link1</p>
</div>
</div>
<div class="sample" style="display:inline-block">
<div>
<p class="para">link1</p>
</div>
</div>
</div>
One way to do it is to add this CSS rule:
.sample:not(:last-of-type) {
border-right: 1px solid black;
}
https://jsfiddle.net/9c8szuc5/2/
The selector #div1 p:hover + #div2 img is not working and I can not understand why.
Trying to make hover over an object in div 1 affect the CSS settings of the objects in div2 with only CSS. Please help.
I am using Chrome.
#div1 #x:hover + #div2 #y {
width: 200px;
}
<div id="content">
<div id="div1">
<p id="x">1234</p>
</div>
<div id="div2">
<img id="y" src="http://img3.sprzedajemy.pl/540x405_obrazek-szyty-2901508.jpg" />
</div>
</div>
JSBin Demo
This does not work because your #div2 is not an adjacent sibling (actually not even a sibling) of the p tag. It is a sibling of the #div1.
The below is how your selector would be interpreted.
#div1 #x:hover + #div2 #y
When mouse is over (or hover) on an element with id='x' which is a child of an element with id='div1' select the element with id='y' under another element with id='div2' which is an adjacent sibling of the element with id='x'.
If you want to make the above selector work, your HTML should be changed like in the below snippet. Notice how in this example, the #div2 is an adjacent sibling of the #x.
#div1 #x:hover + #div2 #y {
width: 200px;
}
<div id="content">
<div id="div1">
<p id="x">1234</p>
<div id="div2">
<img id="y" src="http://img3.sprzedajemy.pl/540x405_obrazek-szyty-2901508.jpg" />
</div>
</div>
</div>
Another option (if your #div1 has no elements other than #x) would be to put the hover on the #div1 itself like in the following snippet.
#div1:hover + #div2 #y {
width: 200px;
}
<div id="content">
<div id="div1">
<p id="x">1234</p>
</div>
<div id="div2">
<img id="y" src="http://img3.sprzedajemy.pl/540x405_obrazek-szyty-2901508.jpg" />
</div>
</div>
If neither of the above can be applied then you would have to use JavaScript (or one of the various JS libraries like jQuery) to achieve what you need. Below is a sample with JavaScript (uses HTML5 classList feature but can easily be modified for non HTML5 compliant browser also).
In this sample, we add handlers for the mouseover and mouseout events on the #x element and toggle the .test class (which changes the width) of the #y element.
document.getElementById('x').onmouseover = function() {
document.getElementById('y').classList.add('test');
}
document.getElementById('x').onmouseout = function() {
document.getElementById('y').classList.remove('test');
}
.test {
width: 200px;
}
<div id="content">
<div id="div1">
<p id="x">1234</p>
</div>
<div id="div2">
<img id="y" src="http://img3.sprzedajemy.pl/540x405_obrazek-szyty-2901508.jpg" />
</div>
</div>
I am trying to have a display that changes when hovering a div class. The idea is to have one div disappear when hovering another. I have tried using general sibling selectors to make the display change from inline to none. The CSS is as follows:
#Inicio {width: 100%;
height: 100%;
background-color: yellow;
position: absolute;
display: inline;
}
.buttons:hover ~ #Inicio {display: none;}
.buttons {width: 80%;
margin-left: auto;
margin-right: auto;
position: static;
margin-left: 10%;
font-size: 22px;
border-top: 1px solid white;
padding-top: 20px;
padding-bottom: 20px; }
.buttons:hover {font-size: 24px;
transition: all .5s ;}
And the HTML:
<div id="wrapper">
<div id="menubar">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
Inicio
</div>
<div class="buttons">
Productos
</div>
<div class="buttons">
Localizacion
</div>
<div class="buttons">
El equipo
</div>
<div class="buttons">
Ideas
</div>
<div class="buttons">
La pagina
</div>
</div>
<div id="content">
<div id="inicio"></div>
</div>
</div>
First of all, your id names doesn't match, its case sensitive, you #inicio and #Inicio are completely two different things..
And as I commented, the issue is that you cannot pop out of the element using CSS means you cannot select the parent element and than go ahead and select the parents sibling element, so you need to change your DOM, you are trying to select an element which is adjacent to the buttons parent element and not the button itself, so the best you can do is this
.buttons:hover ~ #content > #inicio {
display: none;
}
Demo
Altered DOM, you need to bring the elements on the same level, if #inicio is nested, it's fine, but to select it's parent, bring the elements adjacent to each other on the same level so that all are direct child to an element having an id of #wrapper
<div id="wrapper">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
Inicio
</div>
<div class="buttons">
Productos
</div>
<div id="content">
<div id="inicio">Disappear this</div>
</div>
</div>
As #enguerranws commented, I thought to put a compatibility table as well,
Credits - Support Table
Maybe because it's #inicio, not #Inicio ?
Then you need to change your DOM. You have to put #inicio in .buttons div. Or :
.buttons:hover ~ #Inicio
Won't work.
<div id="wrapper">
<div id="menubar">
<div id="menu">
<h1>Menu</h1>
</div>
<div class="buttons">
<span>Inicio</span>
<div id="inicio"></div>
</div>
</div>
<div id="content">
</div>
</div>
You should use that structure. Btw, I added a span to wrap your text, as it's not valid to put text directly in block element (here: div).