Display div when hovering over button using CSS/HTML only - html

Here is my code:
a) I have a row of buttons at the top formatted horizontally as such:
HTML:
<ul class="nav">
Work
Volunteer
Education
Skills
References
Images
</ul>
b) I have div blocks each displaying a paragraph:
<div class="jobs">
<h2>text</h2>
<h3>text</h3>
<h4>text</h4>
</div>
c) I want the CSS to not display the jobs div yet:
.jobs {
display: none;
}
d) Now that I hover over the first button I want the jobs div to display:
.button1:hover+.jobs {
display: block
}
e) Repeat for all other div sections
.volunteer {
display: none;
}
.button2:hover+.volunteer {
display:block
}

You will need to markup HTML differently.
.jobs, .volunteer {
display: none;
}
.button1:hover+.jobs, .button2:hover+.volunteer {
display: block;
/* position the divs under the navigation links */
position: absolute;
top: 120px;
}
<ul class="nav">
<li>
Work
<div class="jobs">
<h2>h2 jobs</h2>
<h3>h3 jobs</h3>
<h4>h4 jobs</h4>
</div>
</li>
<li>
Volunteer
<div class="volunteer">
<h2>h2 volunteer</h2>
<h3>h3 volunteer</h3>
<h4>h4 volunteer</h4>
</div>
</li>
<li> Education</li>
<li> Skills</li>
<li> References</li>
<li> Images</li>
</ul>

This is impossible, as described, with your current HTML, with only HTML and CSS (though only perhaps until the reference and :matches() pseudo-selectors arrive). However, if, rather than :hover you'd be willing to work with clicks on the list-elements, it can be done (without JavaScript). Given the corrected HTML:
<ul class="nav">
<li>Work
</li>
<li> Volunteer
</li>
<!-- and so on... -->
</ul>
<div id="details">
<div id="jobs"></div>
<div id="volunteer"></div>
<!-- and so on... -->
</div>
The following CSS will show the relevant div element once the <a> element has been clicked on (note that the use of an id is essential for this to work):
#details > div {
/* to hide the eleemnt(s) initially: */
display: none;
}
#details > div:target {
/* to show the relevant element once the relevant link is clicked: */
display: block;
}
#details > div[id]::after {
content: attr(id);
}
#details > div {
display: none;
}
#details > div:target {
display: block;
}
<ul class="nav">
<li>Work
</li>
<li> Volunteer
</li>
<li> Education
</li>
<li> Skills
</li>
<li> References
</li>
<li> Images
</li>
</ul>
<div id="details">
<div id="jobs"></div>
<div id="volunteer"></div>
<div id="education"></div>
<div id="skills"></div>
<div id="references"></div>
<div id="images"></div>
</div>
With plain JavaScript, on the other hand, it can be achieved with:
// the 'e' argument is automatically to the function by addEventListener():
function toggleRelevant (e) {
// caching the 'this' element:
var self = this,
// finding the div element with a class equal to the href of the 'a' element
// (though we're stripping off the leading '#':
relevantElement = document.querySelector('div.' + self.getAttribute('href').substring(1) );
// if the event we're responding to is 'mouseover' we set the display of the
// found div to 'block', otherwise we set it to 'none':
relevantElement.style.display = e.type === 'mouseover' ? 'block' : 'none';
}
// finding all the a elements that are in li elements:
var links = document.querySelectorAll('li a');
// iterating over those a elements, using Array.prototype.forEach:
[].forEach.call(links, function(linkElem){
// adding the same event-handler for both mouseover and mouseout:
linkElem.addEventListener('mouseover', toggleRelevant);
linkElem.addEventListener('mouseout', toggleRelevant);
});
function toggleRelevant(e) {
var self = this,
relevantElement = document.querySelector('div.' + self.getAttribute('href').substring(1));
relevantElement.style.display = e.type === 'mouseover' ? 'block' : 'none';
}
var links = document.querySelectorAll('li a');
[].forEach.call(links, function(linkElem) {
linkElem.addEventListener('mouseover', toggleRelevant);
linkElem.addEventListener('mouseout', toggleRelevant);
});
div[class] {
display: none;
}
div[class]::before {
content: attr(class);
color: #f00;
border: 1px solid #f00;
padding: 0.2em;
}
<ul class="nav">
<li>Work
</li>
<li> Volunteer
</li>
<!-- and so on... -->
</ul>
<div class="jobs">
<h2>text</h2>
<h3>text</h3>
<h4>text</h4>
</div>
<div class="volunteer">
<h2>text</h2>
<h3>text</h3>
<h4>text</h4>
</div>
<!-- and so on... -->

I don't think this is do able in css since display blocks (job, volonteer, ...) and button are not parent. But in jQuery this is fairly simple :
$('.buttonX').hover(
function() {
// Styles to show the box
$('.boxX').css(...);
},
function () {
// Styles to hide the box
$('.boxX').css(...);
}
);

It sounds like you're trying to do some kind of a tab menu where pressing a specific button shows a different content. Here's a SO page that describes how it's done: How to make UL Tabs with only HTML CSS

Related

Expanding Div CSS

I'm redesigning the fixtures section of our football teams page at the moment and would like to add functionality for clicking on the fixutre (most likely the date) and it then 'expands' and shows details of the fixture.
The details to be shown include kickoff time, sponsors and scorers etc....
I will be modifying the previous code for the actual expanding but for the life of me I can't work out why the Div to display this is being confinded to the width of the first div within the li tag.
The div is after the closing of the li so I though that would have stopped that happening. Hopefully makes sense, code below:
<ul>
<li>
<div class="cell"><span><a>July 7th</a></span></div>
<div class="cell">
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
</div>
<div class="cell"><span>Friendly</span></div>
</li>
<div class = "fixture-info">
<div class = "kickoff">Kickoff: <span>1930</span></div>
<div class = "match-sponsor">....</div>
.........
</div>
<!--Next Fixture-->
<li>....</li>
What I would like it to look like would be:
(No expanded)
July 7th Nairn County 1 Inverness 3 Friendly
July 14th Nairn County 1 Nairn St Ninian 2
(Expanded)
July 7th Nairn County 1 Inverness 3 Friendly
Kickoff: 1930
Match Sponsor: ......
....
July 14th Nairn County 1 Nairn St Ninan 2
July 15th ...............................
.......
How it currently looks.
July 7th Nairn ..........
Kickoff:1
930
Match Spo
nsor(s): M
adeup spon
sor
I would like the fixture info div to be the same width as the li not the first within.
Any help appreciated.
UPDATE
Have moved the div within the li tag. I am getting very different results from the suggested answers. I think this is because the previous div's are being display as table-cell to keep everything the same, inline-block knocks this out.
I've uploaded images of what is happening.
1st is inline-block styling.
2nd is the details moved inside the li tag.
3rd is how I would like it to be and how it is normally. I would like the
fixture info to the same with as the who fixture (date, fixture and type).
Simple solution:
use display: inline-block; to force those divs in the same line. Since div by default have display: block which will take the whole line.
.cell {
display: inline-block;
}
use toggleClass from jQuery:
$(document).ready(function() {
// Slide
$('ul > li').click(function() {
$(this).next('.fixture-info').toggleClass('collapsed').slideToggle('fast');
});
});
And hide all sub section first using css:
.fixture-info {
background-color: lightgray;
display: none;
}
$(document).ready(function() {
// Slide
$('ul > li').click(function() {
$(this).next('.fixture-info').toggleClass('collapsed').slideToggle('fast');
});
});
.fixture-info {
background-color: lightgray;
display: none;
}
.cell {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<div class="cell"><span><a>July 7th</a></span></div>
<div class="cell">
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
</div>
<div class="cell"><span>Friendly</span></div>
</li>
<div class="fixture-info">
<div class="kickoff">Kickoff: <span>1930</span></div>
<div class="match-sponsor">....</div>
111.........
</div>
<!--Next Fixture-->
<li>
<div class="cell"><span><a> 2222 July 7th</a></span></div>
<div class="cell">
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
</div>
<div class="cell"><span>222 Friendly</span></div>
</li>
<div class="fixture-info">
<div class="kickoff">222 Kickoff: <span>1930</span></div>
<div class="match-sponsor">....</div>
2222.........
</div>
</ul>
Move the
<div class = "fixture-info">
<div class = "kickoff">Kickoff: <span>1930</span></div>
<div class = "match-sponsor">....</div>
.........
</div>
inside the li
then use something like
var li = document.getElementsByTagName('li');
li.addEventListener('click', expandFunction);
function expandFunction() {
this.classList.add('expanded');
}
//SCSS
li {
height: auto;
.fixture-info {
height: 0;
transition: all 0.25s ease-out;
}
&.expanded {
.fixture-info {
height: 80px; //height value of expanded box
}
}
}
Here's a pure html+css solution if you're not too familiar with javascript. The only downside to using the show/hide checkbox maneuver is having to label each one of your label/inputs so they don't have to toggle one another ( see html, toggle1, toggle2, etc).
Outside of this I'd recommend the the 'toggleClass' jQuery solution from the other answer.
ul {
list-style: none;
margin: 0 0 20px;
}
ul li {
color: #2B91AF;
margin: 0 0 10px;
}
ul li span a,
ul li span[class*='-score'] {
color: red;
}
ul li .cell {
display: inline-block;
}
input[type=checkbox],
ul li .fixture-info {
display: none;
}
input[type=checkbox]:checked+.fixture-info {
display: block;
}
<ul>
<li>
<label for="toggle1">
<div class="cell"><span><a>July 7th</a></span></div>
<div class="cell">
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
</div>
<div class="cell"><span>Friendly</span></div>
</label>
<input id="toggle1" type="checkbox">
<div class="fixture-info">
<div class="kickoff">Kickoff: <span>1930</span></div>
<div class="match-sponsor">Match Sponsor: ....</div>
</div>
<li>
<li>
<label for="toggle2">
<div class="cell"><span><a>July 7th</a></span></div>
<div class="cell">
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
</div>
<div class="cell"><span>Friendly</span></div>
</label>
<input id="toggle2" type="checkbox">
<div class="fixture-info">
<div class="kickoff">Kickoff: <span>1930</span></div>
<div class="match-sponsor">Match Sponsor: ....</div>
</div>
<li>
</ul>
Don't put any elements between the "li" tags
Here is a possible way of organizing the HTML
<ul>
<li>
<div class="cell"><span><a>July 7th</a></span>
<span>Nairn County</span>
<span class="h-score">0</span>
<span>Inverness Caledonian Thistle</span>
<span class="a-score">3</span>
<span>Friendly</span>
</div>
<ul>
<li>
<div class = "fixture-info">
<div class = "kickoff">Kickoff: <span>1930</span></div>
<div class = "match-sponsor">....</div>
.........
</div>
</li>
</ul>
</li>
<!--Next Fixture-->
<li>....</li>
<ul>

Select item from unordered list in html

I have a list of items vertically. I want to select item from the list. Also, selected item will get green or any color. At a time, only one item can be selected from list. I can create the list. But, no idea how to make it selective and change color after selection by clicking mouse. Do I need to use any CSS for that?
<div class="items">
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<ul>
</div>
Give each li element a tabindex, and add this CSS:
li:focus {
background: lightgreen;
}
<div class="items">
<ul>
<li tabindex="1">Item1</li>
<li tabindex="1">Item2</li>
<li tabindex="1">Item3</li>
<ul>
</div>
To do this in plain JavaScript:
Add a click event listener to the ul element.
If the event's target is an li element:
2a. If there's an existing li that's selected, remove its class.
2b. Add a selected class to the event's target.
document.querySelector('ul').addEventListener('click', function(e) { // 1.
var selected;
if(e.target.tagName === 'LI') { // 2.
selected= document.querySelector('li.selected'); // 2a.
if(selected) selected.className= ''; // "
e.target.className= 'selected'; // 2b.
}
});
.selected {
background: lightgreen;
}
<div class="items">
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<ul>
</div>
Note that LI must be capitalized in e.target.tagName === 'LI'.
The HTML
<div class="items">
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<ul>
</div>
And the Jquery
$(".items ul li").click(function(){
$(".items ul li").css("color","inherit");
$(this).css("color","green");
});
http://jsfiddle.net/74g21org/1/
You can use jquery as such:
$("ul li").on("click", function () {
$("ul li").removeClass('selected');
$(this).attr('class', 'selected');
});
.selected {
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
</ul>
You can do it with plain html and css like this:
.items li:active {
color: green;
}
http://jsfiddle.net/b8jwnfgp/1/
To expand on Bak's response, you will need to use Javascript to apply the styles such as color.
jQuery is probably the easiest way to go about doing as he suggested. This is a good place to start: https://learn.jquery.com/about-jquery/how-jquery-works/
Also, don't forget to close your list item tags:
<li>Item1< / li>

<div> section to be trigger of show/hide content by click html/css only

:)
I started making a website few days ago and I'm stuck at this step:
http://jsfiddle.net/r6uhczks/
CSS:
/* This section to be trigger of dropdown process */
.parent_style { background: -webkit-linear-gradient(left,rgba(0,0,0,0.5),transparent); width: 325px; height: 21px; }
.parent_style:hover { background: -webkit-linear-gradient(left,rgba(242,182,0,0.7),transparent); }
.parent_style .textP {}
.parent_style:hover .textP {color: red;}
/* This section to be shown by clicking on ↑ and hidden by clicking for second time */
.child_style { background: rgba(0, 0, 0, 0.5); width: 325px; height; auto; }
.child_style:hover { background: rgba(242,182,0,0.7);}
.child_style .textC {color: white;}
.child_style:hover .textC {color: black;}
As you can see I created two styles for dropdown menu,
.parent_style
defines trigger of show/hide content defined by
.child_style
Simplier I want to make a class="parent_style" to be show/hide trigger of shown/hidden class="child_style"
The problem is that I don't know how to create this action by click with CSS/HTML only, I know only by hover.
HTML:
<div class="parent_style">
<p class="textP">Something</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else 2</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<br>
EDIT: and also I would like to know how to edit spaces between these elements, best to remove them.
You can add state to CSS and HTML by using checkboxes (persistent) or focus (temporary):
http://jsfiddle.net/rudiedirkx/L40zcjfc/
HTML
<button>hold down here</button>
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
<br />
<br />
<button><label for="cb1">click here</label></button>
<input id="cb1" type="checkbox" />
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
CSS
input,
ul {
display: none;
}
button:active + ul,
input:checked + ul {
display: block;
}
The persistence comes from the checkbox's :checked state. You can toggle the checkbox with a <label> so you don't have to know it's there.
The temporariness comes from the :active state of a button. Maybe you can use a normal link, but that might keep focus and/or active.
If you can use custom html and css, i'm sure you can stick a custom script at the bottom of the page if it doesn't allow you to add to the head (keep in mind this script requires the page to be loaded first).
Because the elements are not paired together in individual divs, the javascript is a bit longer but nothing too complicated. This script I created will scan through all the divs on the page and will allow the onclick function to determine what the next three divs are. This means that the script is custom to your layout of navigation so you'll need to change the javascript with a layout change.
Here is a working jsfiddle: http://jsfiddle.net/sLc4svam/
<script>
var divs = document.getElementsByTagName("div");
var parents = document.getElementsByClassName("parent_style");
for (var i=0; i<parents.length; i++) {
parents[i].onclick = function() { toggleChildren(this); };
}
function toggleChildren(elem) {
for (var i=0; i<divs.length;i++) {
if (divs[i] == elem) {
for (var ii=1; ii<=3; ii++) { // The 3 is how many children it will toggle
if (divs[i+ii].style.display == "none") {
divs[i+ii].style.display = "block";
} else {
divs[i+ii].style.display = "none";
}
}
}
}
}
</script>

How to disable div or jstree inside div?

Can anyone tell me how to disable a div or elements inside a div?
I have a jstree in my div and I want to disable the div/jstree.
Thanks!
I made a simple example for you here
Basically, it creates a div over your jstree's one so that it is disabled from user interactions.
I guess you can make it visually better, but i think this gives you the idea.
I also checked that there is no strigth way to disable a jstree, even if it could be usefull.
Maybe you'd want to ask the dev in google group...
HTML Code:
<button id="disable">Disable</button>
<button id="enable">Enable</button>
<div id="jstree-wrapper">
<div id="demo" style="height:100px;">
<ul>
<li id="node_1_id">
<a>Root node 1</a>
<ul>
<li id="child_node_1_id">
<a>Child node 1</a>
</li>
<li id="child_node_2_id">
<a>Child node 2</a>
</li>
</ul>
</li>
</ul>
<ul>
<li><a>Team A's Projects</a>
<ul>
<li><a>Iteration 1</a>
<ul>
<li><a>Story A</a></li>
<li><a>Story B</a></li>
<li><a>Story C</a></li>
</ul>
</li>
<li><a>Iteration 2</a>
<ul>
<li><a>Story D</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div id="disabler"></div>
</div>​
CSS code:
#jstree-wrapper {
position: relative;
}
#disabler {
position: absolute;
top: 0px;
left: 0px;
background-color: black;
opacity: 0.5;
}​
JS Code:
$(document).ready(function() {
$("#demo").jstree();
$("#disable").on("click", function() {
$("#disabler").css("width", $("#demo").width());
$("#disabler").css("height", $("#demo").height());
});
$("#enable").on("click", function() {
$("#disabler").css("width", "0px");
$("#disabler").css("height", "0px");
});
});
Here's a very good example I didn't find done so simple and good anywhere else. In this example you can simulate the 'disabled' attribute only by adding CSS style as I entered in the code snippet. It disables by using the CSS "visible:hidden", and adds a translucent mask to cover the whole div area and disable anything inside it. You can choose to comment out the 'Visibility:hidden' to be able to see the elements behind the mask, but then they will be tabable, if you don't mind them hidden then uncomment that style.
function disable(elementId, enabling) {
el = document.getElementById(elementId);
if (enabling) {
el.classList.remove("masked");
} else
{
el.classList.add("masked");
}
}
.masked {
position: relative;
pointer-events: none;
display: inline-block;
//visibility:hidden; /* Uncomment this for complete disabling */
}
.masked::before {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
visibility: visible;
opacity: 0.2;
background-color: black;
content: "";
}
<button onclick="alert('Now, click \'OK\' then \'Tab\' key to focus next button.\nThen click \'Enter\' to activate it.');">Test</button>
<div id="div1" style="display:inline-block" class="masked">
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<br/>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
</div>
<button>Dummy</button>
<br/>
<br/>
<br/>
<button id="enableBtn" onclick="disable('div1',true);disable('enableBtn',false);disable('disableBtn',true);">Enable</button>
<button id="disableBtn" onclick="disable('div1',false);disable('enableBtn',true);disable('disableBtn',false);" class="masked">Disable</button>

How to make UL Tabs with only HTML CSS

Trying to figure out how to do this. I have the style but I'd like something to happen after I click the tabs. I would like the div with the tab class names to show and hide when i click the tabs. I'm assuming how that would work. Right now when I click the tabs nothing happens.
Here's my HTML
<style type="text/css">
ul.tabs {
display: table;
list-style-type: none;
margin: 0;
padding: 0;
}
ul.tabs>li {
float: left;
padding: 10px;
background-color: lightgray;
}
ul.tabs>li:hover {
background-color: lightgray;
}
ul.tabs>li.selected {
background-color: lightgray;
}
div.content {
border: 1px solid black;
}
ul { overflow: auto; }
div.content { clear: both; }
</style>
<body>
<ul class="tabs">
<li>Description</li>
<li>Specs</li>
</ul>
<div class="pane">
<div class="tab1">
<div><h2>Hello</h2></div>
<div />
<div>Hello hello hello.</div>
<div />
<div>Goodbye goodbye, goodbye</div>
<div />
<div />
</div>
<div class="tab2" style="display:none;">
<div><h2>Hello2</h2></div>
<div />
<div>Hello2 hello2 hello2.</div>
<div />
<div>Goodbye2 goodbye2, goodbye2</div>
<div />
</div>
</div>
<div class="content">
This should really appear on a new line.
</div>
</body>
Standard answer: you can't. There is no way to do this with purely HTML/CSS2, unfortunately. We can make drop-downs in CSS with the :hover psuedo-class, but there's no equivalent for clicks. Look into one of these Javascript-based solutions.
Secret answer: CSS3 [kind of] supports this. But you have to create radio buttons [weird], and it's not supported in IE7/8. If you dare...
And if you don't mind using Javascript, here's a quick solution. Reformatted your HTML, first of all. No need to put <h2>s in <div>s, and use <br /> for breaks—that's what it's there for. Also, I changed the tab <div>s to use id's instead of classes. If you have unique identifiers for an element, use id.
<ul class="tabs">
<li>Description</li>
<li>Specs</li>
</ul>
<div class="pane">
<div id="tab1">
<h2>Hello</h2>
<p>Hello hello hello.</p>
<p>Goodbye goodbye, goodbye</p>
</div>
<div id="tab2" style="display:none;">
<h2>Hello2</h2>
<p>Hello2 hello2 hello2.</p>
<p>Goodbye2 goodbye2, goodbye2</p>
</div>
</div>
<div class="content">This should really appear on a new line.</div>
Didn't touch your CSS.
For Javascript, I recommend using jQuery. It really simplifies things.
All you need are these lines of code:
$(document).ready(function() {
$("ul.tabs a").click(function() {
$(".pane div").hide();
$($(this).attr("href")).show();
});
})
Basically, once the page is ready [has loaded], look for every link that's a child of a tabs ul. Attach a function that runs each time this link is clicked. When said link is clicked, hide all the tabs in the .pane div. Then, use the link's href to find the proper tab div and show it.
fiddle: http://jsfiddle.net/uFALn/18/
Because of the floated <li> elements your <ul> element is zero height.
Try adding ul { overflow: auto; } and div.content { clear: both; } to your CSS
Thanks benesch. It helped me too.
One can also add return false to prevent that jerky jump to the anchor. For instance:
$("ul.tabs a").click(function() {
$(".pane div").hide();
$($(this).attr("href")).show();
return false;
});