what goes wrong with my code? can anyone help me a bit?
want to show a div onclick other div.
nav div#showdiv {
width: 100px;
height: 100px;
background-color: #999;
outline: none;
cursor: pointer;
}
#hiddendiv {
display: none;
height: 200px;
background-color: #666;
color: #FFF;
}
nav div#showdiv:focus+section#hiddendiv {
display: block;
}
<nav>
<div id="showdiv">Click me</div>
</nav>
<section id="hiddendiv">
<div>Text</div>
</section>
I think I have some wrong selector code in the css. or is it not possible
to toggle a div in other container / div / section etc.
You cannot with just CSS.
This selector
nav div#showdiv:focus+section#hiddendiv
will not work because #hiddendiv is not a sibling of #showdiv, so you cannot target it with a +.
Also, clicking will not focus the element (unless it is an interactive element, like a button, or a input)
You can accomplish what you want by adding some Javascript that, as an example, add/remove a class after the click event.
document
.getElementById('showdiv')
.addEventListener('click', function() {
const hidden = document.getElementById('hiddendiv')
if (hidden.classList.contains('show')) {
hidden.classList.remove('show');
} else {
hidden.classList.add('show');
}
})
nav div#showdiv {
width: 100px;
height: 100px;
background-color: #999;
outline: none;
cursor: pointer;
}
#hiddendiv {
display: none;
height: 200px;
background-color: #666;
color: #FFF;
}
#hiddendiv.show {
display: block;
}
<nav>
<div id="showdiv">Click me</div>
</nav>
<section id="hiddendiv">
<div>Text</div>
</section>
You can use the jQuery toggle() method to toggle div on click.
$("#showdiv").click(function(){
$("#hiddendiv").toggle();
});
nav div#showdiv {
width: 100px;
height: 100px;
background-color: #999;
outline: none;
cursor: pointer;
}
#hiddendiv {
display: none;
height: 200px;
background-color: #666;
color: #FFF;
}
nav div#showdiv:focus+section#hiddendiv {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
<div id="showdiv">Click me</div>
</nav>
<section id="hiddendiv">
<div>Text</div>
</section>
You can't use :focus ond div elements. Focus can only be used on elements that accept keyboard events or other user inputs.
You may want to try using javascript (jQuery) to do that:
$('#showdiv').click(function(e) {
$('#hiddendiv').show();
})
You can use .toggle() instead of .show() to switch between display: block; and display: none;
Related
I am trying to toggle the display of two sibling elements when I hover on the first sibling. I want to change the display of the hovered first sibling element from block to none while its immediate or next sibling element's style changes to block from none. I was able to write a css code for that but on hover, the elements constantly flicker and alternate in a never ending loop. Below is my html and css code:
.box {
display: inline-block;
width: 300px;
height: 150px;
margin: 20px 40px;
border-radius: 4px;
cursor: pointer;
}
.box-one {
background: green;
}
.box-two {
background: whitesmoke;
display: none;
}
.box-one:hover {
display: none;
}
.box-one:hover+.box-two {
display: block;
}
<div class="box box-one"></div>
<div class="box box-two"></div>
Wrap the two boxes with another div (.container) and toggle them when hovering the container:
.box {
display: inline-block;
width: 300px;
height: 150px;
margin: 20px 40px;
border-radius: 4px;
cursor: pointer;
}
.box-one {
background: green;
}
.box-two {
background: whitesmoke;
}
.container {
width: fit-content;
}
.container:hover>.box-one {
display: none;
}
.container:not(:hover)>.box-two {
display: none;
}
<div class="container">
<div class="box box-one"></div>
<div class="box box-two"></div>
</div>
Using JS mouseenter and mouseleave events, you can add the event listener with the once: true option only to the current item. As soon as the mouse enter, you show the new item, and only when the user leaves the current item, you add the new mouseenter event listener to the current item, and so on...
const showNext = evt => {
const current = evt.target
const next = current.nextElementSibling
if(!next || !next.classList.contains('box')) return
current.classList.remove('show')
next.classList.add('show')
next.addEventListener('mouseleave', () => {
next.addEventListener('mouseenter', showNext, { once: true })
}, { once: true })
}
document.querySelector('.box')
.addEventListener('mouseenter', showNext, { once: true })
.box {
display: none;
width: 300px;
height: 150px;
margin: 20px 40px;
border-radius: 4px;
cursor: pointer;
}
.show {
display: inline-block;
}
.box-one {
background: green;
}
.box-two {
background: whitesmoke;
}
.box-three {
background: red;
}
.box-four {
background: blue;
}
.container {
width: fit-content;
}
<div class="box box-one show"></div>
<div class="box box-two"></div>
<div class="box box-three"></div>
<div class="box box-four"></div>
Let's say I have a container, which leads to the other page when I click it. However, I want some elements inside this container to be disabled, so when I click on them the link won't work. How do I do that?
For example, here I want to disable the red side of the container.
a {
text-decoration: none;
color: white;
}
.container {
display: flex;
width: 400px;
height: 100px;
background-color: #ddd;
box-sizing: border-box;
padding: 5px;
}
.block-1 {
width: 50%;
height: 100%;
background-color: #3e3e3e;
text-align: center;
box-sizing: border-box;
padding-top: 20px;
}
.block-2 {
width: 50%;
height: 100%;
background-color: red;
text-align: center;
box-sizing: border-box;
padding-top: 20px;
}
<a href="#">
<div class="container">
<div class="block-1">Active</div>
<div class="block-2">Disabled</div>
</div>
</a>
Everything you put in <a> tag will be clickable, because "click" event is triggered actually on parent (<a> tag) and not on what's inside it.
You need to separate this - simply make one div a link and another not.
a {
text-decoration: none;
color: white;
}
.container {
display: flex;
width: 400px;
height: 100px;
background-color: #ddd;
box-sizing: border-box;
padding: 5px;
}
.block-1 {
width: 50%;
height: 100%;
background-color: #3e3e3e;
text-align: center;
box-sizing: border-box;
padding-top: 20px;
}
.block-2 {
width: 50%;
height: 100%;
background-color: red;
text-align: center;
box-sizing: border-box;
padding-top: 20px;
}
<div class="container">
Active
<div class="block-2">Disabled</div>
</div>
<!DOCTYPE html>
<html>
<head>
<title>Disable Link using CSS</title>
<style type="text/css">
.not-active {
pointer-events: none;
cursor: default;
}
</style>
</head>
<body>
<center>
<h1 style="color: green;">GeeksforGeeks</h1>
<h3>A Computer Science Portal for Geeks</h3>
<b>Disabled link:</b> To visit us
<a href="www.geeksforgeeks.org"
class="not-active">Click Here</a>
<br>
<br>
<b>Enabled link:</b> To use our ide
<a href=
"https://ide.geeksforgeeks.org/tryit.php">
Click Here</a>
</center>
</body>
</html>
One option would be to add a click handler. Like this post talks about, you can tell the browser to follow or not to follow the link by calling e.preventDefault().
In the code below, change disabled and you turn on/off the link.
I should add, though, that while I am not an accessibility expert, this is not very "semantic" and I'm guessing probably doesn't work well with screen readers... Maybe something like aria-disabled could be used to fix that, but I'm not familiar enough with it to say.
var link = document.querySelector('a');
var disabled = true;
link.addEventListener('click', (e) => {
if (disabled) {
console.log('disabled');
e.preventDefault();
}
});
My link
There is an issue with dropdown, when hover it.
Instead of normal opening, the dropdown menu expands header.
Where is an error in the code?
I wrote this code from example on w3school()Code example, by which this code is written
There are also several attempts to do the same:
first attempt
second attempt
And in all of these attempts i do the same error, but cannot find where
exactly.
Can somebody show where are the errors?
* {
margin: 0;
padding: 0;
overflow-x: hidden;
}
header, nav {
background-color: aliceblue;
color: darkcyan;
font-family: Arial, Helvetica, sans-serif;
width: 100%;
height: auto;
text-align: center;
padding: 10px;
}
a {
text-decoration: none;
color: darkcyan;
padding: 10px;
display: inline-block;
}
.active {
padding-left: 0;
}
a, .dropdown {
display: inline-block;
width: 100px;
padding: 10px;
}
.dropdown .dropbutton {
border: none;
outline: none;
color: inherit;
background-color: inherit;
font-size: inherit;
}
.dropdown-content {
display: none;
position: absolute;
background-color: whitesmoke;
color: black;
width: auto;
}
.dropdown-content a {
float: none;
padding: 10px;
display: block;
text-align: center;
}
.dropdown:hover .dropdown-content {
display: block;
position: relative;
width: auto;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title>Dropdown Third Version</title>
</head>
<body>
<header>
<h1>The Homework</h1>
<nav>
Home
Hobbies
Third Page
<div class="dropdown">
<button class="dropbutton">Dropdown</button>
<div class="dropdown-content">
One
Two
Three
</div>
</div>
</nav>
</header>
</body>
</html>
Make a simple change to position in css:
.dropdown:hover .dropdown-content {
display: block;
position: absolute;----------------------This One
width: auto;
text-align: center;
}
Use this link to understand: Difference Between relative and absolute
This is because you have given position:relative in css instead give position:absolute for following selector .inline:hover .dropdown
.inline:hover .dropdown {
display: block;
position: absolute;
padding: 10px;
background-color: white;
color: dimgray;
}
And one more modification is that for all sub menu you have mentioned the class dropdown instead of that under one div which is having class dropdown
<div class="inline">The Page
<div class="dropdown">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
</div>
here is working example : https://liveweave.com/NpUwOZ
.dropdown:hover .dropdown-content {
display: block;
position: absolute;
width: auto;
text-align: center;
}
just delete the position line or change position in to absolute
You forgot to link the CSS file in your HTML code. Make sure you do that first.
Othetwise it won't do anything.
I have created a Pagination which will show/hide div based on the active Page as here https://jsfiddle.net/bogaso/qh7cpxzv/11/
However, I failed to apply style on the Navigation bar. Particularly I want to apply below two styles at the minimum level:
I want the Navigation bar will stay at the center of the page, with specific margin at top
Furthermore I want to apply border around each page number as in https://www.w3schools.com/css/tryit.asp?filename=trycss_ex_pagination_border_round
However when I try to wrap the <a> tag with some <div>, I lost all control in the navigation, i.e. below code fails to apply any style
<div class = 'Top'>
1
2
3
4
7
6
</div>
Any help on how to apply Style in the navigation bar would be highly appreciated.
Also is it possible to implement the same based solely on HTML + CSS?
The main problem with your CSS code is at the beginning.
You are adding a rule to hide all div elements, and this is causing the problem when you wrap the <a> tags with a div.
The solution is to apply the CSS hiding only for the page elements.
So, remove the CSS rule at the top and style the AAA class instead:
.AAA {
display: none;
height: 120px;
width: 120px;
}
.AAA.active { display: block; }
To center the pagination controls, I recommend using the Flexible Box Layout Module, aka flex.
This would be the CSS rule to center the pagination and keep its margin:
.Top {
display: flex;
margin: 10px 0;
justify-content: center;
}
As for your question, it isn't possible to do this using only CSS and HTML, at least not in a good way. CSS shouldn't dictate the behaviour of a webpage.
For your development, I really suggest learning proper HTML style guide, and especially trying to be consistent. I noticed sometimes you use single quotes other times double quotes fort HTML attributes. It should always be double quotes without any spaces betweens the equal sign and the quotes :)
In the end, the code will be:
$('a.A1').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('#' + $(this).attr('rel')).addClass('active');
return false;
});
.A1 {
background: rgba(0,0,0,.1);
margin: 0;
padding: 0;
height: 10px;
width: 10px;
}
.AAA {
display: none;
height: 120px;
width: 120px;
}
.AAA.active { display: block; }
.Top {
display: flex;
margin: 10px 0;
justify-content: center;
}
.Top a {
color: black;
float: left;
padding: 8px 16px 16px;
text-decoration: none;
border: 1px solid #ddd;
}
.Top a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
.Top a:hover:not(.active) {background-color: #ddd;}
.Top a:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.Top a:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="page-1" class="active AAA">p1</div>
<div id="page-2" class="AAA">p2</div>
<div id="page-3" class="AAA">p3</div>
<div id="page-4" class="AAA">p4</div>
<div id="page-5" class="AAA">p5</div>
<div id="page-6" class="AAA">p6</div>
<div class="Top">
1
2
3
4
7
6
</div>
Here's a working js fiddle:
https://jsfiddle.net/ovitrif/o8h3nrjb/
Please try with below HTML, CSS and JS.
$('.Top a').click(function() {
$('.active').removeClass('active');
$(this).addClass('active');
$('#' + $(this).attr('rel')).addClass('active');
return false;
});
div.AAA {
display: none
}
div.active {
display: block;
}
.Top {
display: inline-block;
}
.Top a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
border: 1px solid #ddd;
background: rgba(0, 0, 0, .1);
}
.Top a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
.Top a:hover:not(.active) {
background-color: #ddd;
}
.Top a:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.Top a:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
.AAA {
height: 120px;
width: 120px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="page-1" class="active AAA">p1</div>
<div id="page-2" class='AAA'>p2</div>
<div id="page-3" class='AAA'>p3</div>
<div id="page-4" class='AAA'>p4</div>
<div id="page-5" class='AAA'>p5</div>
<div id="page-6" class='AAA'>p6</div>
<div class='Top'>
1
2
3
4
7
6
</div>
I am using a React component that injects a <div tabindex="-1"> on my HTML. Then, every time I click an inner object it adds a blue border on the element, like the example below:
https://jsfiddle.net/zdtw7uq0/
Is it possible to remove this border?
You can use an attribute selector to turn the behavior off.
For all element with tabindex:
[tabindex] {
outline: none;
}
Only for elements with tabindex="-1":
[tabindex="-1"] {
outline: none;
}
.container {
width: 200px;
height: 100px;
padding: 100px;
background-color: #fff;
}
[tabindex="-1"] {
outline: none;
}
<div tabindex="-1">
<div class="container">
Something
</div>
</div>
Try this:
Code:
<div tabindex="-1" class="tab">
<div class="container">
Something
</div>
</div>
Style:
.container {
width: 400px;
height: 200px;
padding: 200px;
background-color: #fff;
outline:none;
}
.tab {
outline:none;
}