I need assistance with changing css of div on hover event. My code:
<div class = "container-main">
<div class = "container-inner">
<ul class = "list">
<li>Option 1</li>
<li>Option 2</li>
</ul>
</div>
</div>
<div class = "container1">
<p>Container text</p>
</div>
<div class = "container2">
<p>Container text</p>
</div>
I need to be able to change classes container1 and container2 when hovering on ul or li in the main container. I only know how to change it with sibling containers but that's not what I need.
.container-main:hover + .container1 {background:red;}
Would appreciate any help in achieving this. Thanks.
Unfortunately, based on your markup heirarchy, what you're looking for isn't possible with just CSS. You can do this pretty simply with javascript though. Here's an example that will add a background-color to .container1 when you hover .container-main ul:
const list = document.querySelector('.container-main ul')
const container1 = document.querySelector('.container1')
list.addEventListener('mouseenter', () => container1.classList.add('red'))
list.addEventListener('mouseleave', () => container1.classList.remove('red'))
.red {
background-color: red;
}
<div class = "container-main">
<div class = "container-inner">
<ul class = "list">
<li>Option 1</li>
<li>Option 2</li>
</ul>
</div>
</div>
<div class = "container1">
<p>Container text</p>
</div>
<div class = "container2">
<p>Container text</p>
</div>
is this what you want?
jQuery solution:
$( ".container-main li" ).hover(
function() {
//do something when hovering
}, function() {
//do something after hovering
$(".container1").css("background", "red");
}
);
Thanks for your answers everyone. I was just unsure whether it was possible or not, I'll try to rearrange my div heirarchy. I know it can be done with jQuery but I have a task where I only need to use CSS without any programming languages.
Once again, thanks a lot. I got a better idea of what needs to be changed.
I don't think css alone can do this.
Do you have access to jquery?
https://jsfiddle.net/ifinto/o2gxgz9r/
$(document).ready(function() {
$(".container-main li").each(function(index) {
$(this).hover(
function() {
div = ".container" + (index + 1);
console.log("in_" + div);
$(div).css("background-color", "red")
},
function() {
div = ".container" + (index + 1);
console.log("out_" + div);
$(div).css("background-color", "white");
});
})
Related
I have a nav-bar which has display:none by default. When a user clicks on a hamburger icon at the top-right corner, I want to display it. What I've done is,
<nav id="navigation-bar">
....
</nav>
Javascript:
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.add('show-btn');
});
In my css, the below style is not working
.show-btn{
display:block
}
But when I add nav. in front of it, it works
nav.show-btn{
display:block
}
ps: I'm using sass
Add a class to your nav tag
So that your Code should look like this
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.add('show-btn');
});
.navbar{
display:none;
}
.show-btn{
display:block
}
<nav id="navigation-bar" class="navbar">
<ul>
<li>Test</li>
</ul>
</nav>
<button id="menu-cta">btn</button>
please don't add css to #navigation-bar{} otherwise it will have the highest specificty.
You can learn more here
CSS Specificity
Another route is to use toggle() so the user can open and close your navigation.
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.toggle('hide');
});
.hide {
display: none
}
<button id="menu-cta">...</button>
<nav id="navigation-bar" class="hide">
<ul>
<li>Test</li>
</ul>
</nav>
I am trying to introduce new class for specific div element using nth-child() pseudo-class but it is not working!
Am I doing something wrong?
Note that menu, container, newClass are already defined in CSS section and I'm not showing them here.
Here is what I am trying:
$(function() {
$("button").click(function {
$("div:nth-child(2)").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<lu>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</lu>
</div>
</div>
There are lots of mistakes in your code. Which div are you want to add a class newClass? I think your code should be like this
$(function() {
$("button").on('click', function() {
$(".menu").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<ul>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</ul>
</div>
</div>
My mistake
I should not use
<script>
$("div:nth-child(2)").toggle Class("newClass");
</script>
this to introduce new class for div element, with the class menu , as it is not a second child of div . What given below is though working fine for me:
Correction
<script>
$("div.menu").toggleClass("newClass");
</script>
That is: to specify div element(whose class is going to be toggled) by tagging its class menu , to call jQuery for div with the class menu .
Also by mistake parentheses () were missing.
I'm using :target in html and I code something like that:
<div class="1">
<div>
<ul>
link to part 2
</ul>
</div>
<div class="ex">
<ul id="2">
<p>hi</p>
</ul>
</div>
and I've done this in css:
.ex ul {
display: none;
}
.ex ul:target {
display: block;
}
I need to make so that when you click on the link (in this case the words 'link to part 2') the #2 ul show, (alredy done this) and the ul whit the link disappears, how can I do?
One way this can be accomplished is with JavaScript. I added the id remove-on-click to your link which you want removed, and then created a JavaScript event listener to alter the style of this item when it is clicked. You can see the code working here.
<div class="1">
<ul>
link to part 2
</ul>
</div>
<div class="ex">
<ul id="2">
<p>hi</p>
</ul>
</div>
<script>
document.getElementById('remove-on-click').addEventListener('click',function(){
this.style.display = "none";
})
</script>
I did not edit any of your other code, but keep in mind that ul tag should be used with li descendants. If you do not have a li descendant, use another tag, such as a div. Also, you may want to become more familiar with proper naming of class and id attributes, especially in regards to not beginning them with a digit:
https://www.w3.org/TR/CSS2/syndata.html#characters
What are valid values for the id attribute in HTML?
The key consideration to note is that you must write the markup in reverse order.
This is because CSS selectors can only select:
an element itself (or a pseudo-element)
an element's descendant elements
an element's subsequent siblings
It cannot select an ancestor element or (in this scenario) a previous sibling.
Once you have written the markup in reverse order, you can achieve the effect you want using CSS.
Working Example:
#part2,
#part3 {
display: none;
}
#part2:target,
#part3:target {
display: block;
}
#part2:target ~ [id^="part"],
#part3:target ~ [id^="part"] {
display: none;
}
<div id="part3">
<p>This is Part 3.</p>
</div>
<div id="part2">
<p>This is Part 2.</p>
<ul>
<li>Link to Part 3</li>
</ul>
</div>
<div id="part1">
<p>This is Part 1.</p>
<ul>
<li>Link to Part 2</li>
</ul>
</div>
BACKGROUND
I would like to be able to hover over a link and a set of text fade out on this action
HTML
<nav class="PageNav">
<ul>
<li id="HomeLink">Home</li>
<li id="OverviewLink">Overview</li>
<li id="ServicesLink">
Mega Services
<ul class="PageSubNav">
<li>Subpage 1</li>
<li>Subpage 2</li>
<li>Subpage 3</li>
<li>Subpage 4</li>
<li>Subpage 5</li>
<li>Subpage 6</li>
</ul>
</li>
<li id="GalleryLink">Gallery</li>
<li id="VideoLink">Video</li>
<li id="ContactLink">Contact</li>
</ul>
</nav>
<article class="ContentText">
<p>text</p>
<p>dummy text</p>
<p>Dummy text, dummy text, dummy text, dummy text, <strong>3 Paragraphs, Roughly 209 Words</strong></p>
</article>
CSS
#ServicesLink:hover .ContentText p {
color: rgba(255,255,255,0.5);
}
I'm having trouble in thinking this one through, here's what I have but to no avail.
The idea being that when one hovers over the "mega services" tab the text contained in the article section fades.
You can do this by declaring opacity:0 for mega services (I suppose id ServicesLink). Or, if you want it to be smoother you can use jQuery:
$("#id_where_you_hover").hover(function(event){
$("#id_that_you_want_to_fade_out").fadeOut(1000, function () {
$(this).html("here you put something to replace if you want").fadeIn(2000);
});
});
What your CSS is doing is targeting .ContentText p inside #ServicesLink -- which doesn't exist.
Your .ContentText is out of the CSS scope. There is no way in vanilla CSS to target selectors outside of it's parent.
JavaScript is your only valuable solution to scope elements on your page.
What is the aim or purpose of this, so we could maybe give you a better answer?
UPDATE: Updated answer.
In JS, you can do the following:
var hoverEl = $('#ServicesLink');
var targetEl = $('.ContentText p');
hoverEl.on('mouseenter', function() {
targetEl.css({'color': 'rgba(255, 255, 255, 0.5)'});
});
hoverEl.on('mouseleave', function() {
targetEl.css({'color': 'rgba(255, 255, 255, 1)'});
});
DEMO
You can't do that with CSS currently.
You will have to use JavaScript.
I finally got Bootstrap tabs to work. I was wondering if there's a way to change the behaviour so instead of clicking just hovering the cursor would show the hidden content?
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs > li > a').hover(function() {
$(this).tab('show');
});
You wont have to modify the core bootstrap code.
Additionally you could do this:
$('.nav-tabs > li ').hover(function() {
if ($(this).hasClass('hoverblock'))
return;
else
$(this).find('a').tab('show');
});
$('.nav-tabs > li').find('a').click(function() {
$(this).parent()
.siblings().addClass('hoverblock');
});
Which will prevent hover selection after the first time a user clicks a tab.
It's better to bind a handler on the document object so it will work with dynamically generated tabs too:
$(document).on('mouseenter', '[data-toggle="tab"]', function () {
$(this).tab('show');
});
Also there is a small Bootstrap plugin which automatically activates tabs on hover: https://github.com/tonystar/bootstrap-hover-tabs.
The complete HTML using this plugin is:
body { padding: 2rem; }
.tab-content { margin-top: 1.5rem; }
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Nav pills -->
<ul class="nav nav-pills">
<li class="active">Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
</ul>
<!-- Tab panes -->
<div class="tab-content well">
<div class="tab-pane active" id="tab-1">Content 1</div>
<div class="tab-pane" id="tab-2">Content 2</div>
<div class="tab-pane" id="tab-3">Content 3</div>
<div class="tab-pane" id="tab-4">Content 4</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="//cdn.rawgit.com/tonystar/bootstrap-hover-tabs/v3.1.1/bootstrap-hover-tabs.js"></script>
JavaScript:
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs[data-toggle="tab-hover"] > li > a').hover( function(){
$(this).tab('show');
});
HTML:
<ul class="nav nav-tabs" data-toggle="tab-hover">
....
</ul>
Modify bootstrap.js (or boostrap-tab.js if you aren't using the full bootstrap.js file)
Where you see:
/* TAB DATA-API
* ============ */
$(function () {
$('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
e.preventDefault()
$(this).tab('show')
})
})
change the 'click.tab.data-api' to 'hover.tab.data-api'
Note: I tested this with Bootstrap v2.0.2
Right from my production code. Orthogonal, unobtrusive, can "unclick" any user interface. Selector can be modified to match many kinds of clickables.
$(document).on('mouseover','.js-mouseover-to-click',function (event) {
$(event.target).trigger('click');
});
i hope it is nice solution
(function ($) {
$(function () {
$(document).off('click.bs.tab.data-api', '[data-hover="tab"]');
$(document).on('mouseenter.bs.tab.data-api', '[data-toggle="tab"], [data-hover="tab"]', function () {
$(this).tab('show');
});
});
})(jQuery);