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>
Related
So I'm working on this small frontend project with some jQuery I wanted to do something where there is a list of names and if you click it, a class of "selected" should be added, but I want to remove that class if some other option is selected.
So suppose Name-1 is selected It gets a background Color Change, but If Name-2 is selected right after the background color for Name-1 should go away and add to Name-2, and I have multiple names so I can't make a function for every single option.
I was wondering if there is a easier way to do it.
HTML
<ul class="names">
<li class="name selected">Name-1</li>
<li class="name">Name-2</li>
<li class="name">Name-3</li>
</ul>
CSS
.selected {
background-color: rgba(0, 0, 0, 0.3);
}
you can do it like this:
$("ul.names li").on("click", function(){
$("ul.names li").removeClass("selected");
$(this).addClass("selected");
})
EDIT - explanation
you select all the <li> tags by this selector ul.names li and manipulate them together, first you remove the selected class from all li tags by this $("ul.names li").removeClass("selected"); and then you add selected class to the chosen li tag represented by the this key word
$("ul.names li").on("click", function() {
$("ul.names li").removeClass("selected");
$(this).addClass("selected");
})
.selected {
background-color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="names">
<li class="name selected">Name-1</li>
<li class="name">Name-2</li>
<li class="name">Name-3</li>
</ul>
Something like this should work.
$('.name').on('click', function() {
$('.name').removeClass('selected').css('background','none');
$(this).addClass('selected').css('background',$(this).data('bg'));
});
$('.name.selected').css('background',$('.name.selected').data('bg'));
.selected {
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="names">
<li class="name selected" data-bg="green">Name-1</li>
<li class="name" data-bg="red">Name-2</li>
<li class="name" data-bg="blue">Name-3</li>
</ul>
I've been banging my head against this for a couple hours now. Maybe it can't be done, maybe I've missed something obvious.
I need to highlight the li I'm hovering over, and each li after that but not before, and only if I'm also hovering over a specific child of that li.
So in the code example, if I hover over the second li's div.doAThing, the second through to last li should highlight. I'm not sure how to get the selection working.
li
{
background-color: #3e3e3e;
}
li:hover, li:hover ~ li
{
background-color: #aaf500;
}
<ul id="theList">
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
</ul>
As far I know, it can't be done with only CSS. You need some way to target the parent, which isn't really possible with only CSS. I don't know if your able to use JavaScript/jQuery in your project, but I've put together an example of how you could do it. Basically, when you hover a .doAThing element, we add a class hover to its parent (the li). And then we can easily target those specific elements in a similar way you did in the code you provided. Hope it helps. Please see the following SO links for further information:
Is there a CSS parent selector?
Complex CSS selector for parent of active child
$('.doAThing').hover(function() {
$(this).parent().addClass('hover');
}, function () {
$(this).parent().removeClass('hover');
});
li {
background-color: #3e3e3e;
}
.hover, .hover ~ li {
background-color: #aaf500;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="theList">
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
<li>
<div>Data</div>
<div>One</div>
<div class='doAThing'>All</div>
</li>
</ul>
I had the same issue, i just used child of
li:hover > * {
font-size: 17px;
}
Hope it helps
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
I got a menu and I would like to display the current page with different color of text, or a border bottom.
I know how to do this with different html files, though, the current website im working on, is on a single file, index.html. When I click on menu, it will scroll down to the specific tab.
Does anyone know any way of styling menu in a single file?
HTML:
<div id="menu">
<ul class="nav">
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</div>
CSS:
#menu ul li .a:hover {
color: #6D6D6D;
border-bottom: 2px solid #fcd017;
}
Currently I'm able to change it while hovering, but I would like to change it while i remain in the #section1 in this case.
<script type="text/javascript">
var backcolor = [
"#aaaaaa",
"#bbbbbb",
"#cccccc",
];
function changeBGcolor(whichcolor)
{
if (document.body)
{
document.getElementById('menu').style.backgroundcolor = '(' + backImage[whichcolor] + ')';
}
}
</script>
<div id="menu">
<ul class="nav">
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</div>
Try this sometimes it will be work
Is there a way in HTML/CSS to change color of just one list item not all of them? I just want to change the color of each of them so the user knows what page he is on. for now I managed only to do the a:hover but I cant figure out how could I do it so the color would stay.
HTML:
<div id="nav">
<div class="wrapper">
<ul id="buttons">
<li>| About</li>
<li>| Gallery</li>
<li>| Prices</li>
<li>| FAQ</li>
<li>| Contact Us</li>
<div class="clear"></div>
</ul>
</div>
</div>
CSS:
#buttons {
background-color: black;
}
.clear { clear: both; }
#buttons li a
{
position:block;
color:#fff;
padding:1em;
text-decoration:none;
float:left;
width:95px;
}
#buttons li a:hover
{
background-color:#bc1b32;
}
Look: http://jsfiddle.net/qzXgJ/
Detail:
HTML
<div>
<div class="wrapper">
<ul>
<li><a id="index" href="#">| About</a></li>
<li><a id="gallery" href="#">| Gallery</a></li>
<li><a id="prices" href="#">| Prices</a></li>
<li><a id="faq" href="#">| FAQ</a></li>
<li><a id="contact_us" href="#">| Contact Us</a></li>
<div class="clear"></div>
</ul>
</div>
</div>
JS
$(".wrapper li a").click(function () {
$('.wrapper li a').each(function() {
$(this).removeClass('selected');
});
$(this).addClass('selected');
$(".wrapper ul li a").click(function () {
$('.wrapper ul li a').each(function() {
$(this).removeClass('selected');
});
$(this).addClass('selected');
console.log($(this).attr('id'));
/* Get html by jQuery */
$.get($(this).attr('id'),function(data){
$('#result').html(data);
});
return false;
});
CSS
.selected
{
background-color:#b51ba2;
}
easiest way would be to just create a class, lets say selected, add it to the a tag you'd like to change the color of, and that's it:
.selected { background-color: green; }
then your link would be:
<li><a class='selected' href="index.html">| About</a></li>
I've used this answer for the same question with good results: http://hicksdesign.co.uk/journal/highlighting-current-page-with-css
The question is a little vague; to indicate which page the user is on you could add a class to that particular list item that houses the link they last clicked:
<li class="current">
| Prices
</li>
You then provide specific styles for that:
li.current {
background-color: #f1f1f1;
}