I have the following code to show a division on hover. It is initially hidden and i'm trying to show one division on hover of another element.
.topNav {
padding: 1px 15%;
background: #006cb4;
color: white;
}
.mainMenu {
list-style-type: none;
}
.mainMenu li {
display: inline-block;
padding: 3px 15px;
font-size: 20px;
}
.mainMenu li a {
text-decoration: none;
color: white;
}
#item1 {
display: block;
}
#item1:hover #item1detail {
background: #444;
visibility: visible;
}
#item1detail {
position: absolute;
top: 152px;
left: 250px;
background: #ccc;
width: 750px;
height: 400px;
border: solid 1px black;
border-radius: 0 0 10px 10px;
visibility: hidden;
}
<div class="topNav">
<ul class="mainMenu">
<li><a id="item1" href=""> item 1</a>
</li>
<li> item 3
</li>
<li> item 4
</li>
<li> item 5
</li>
<li> item 6
</li>
<li> item 7
</li>
<li> item 8
</li>
<li> item 9
</li>
</ul>
<div id="item1detail">
Some random content
</div>
</div>
on hover of the list item item1 i want to show the division itemdetail. The above code is not working. What am i doing wrong?
As I see it the only solution to display the given div without touching the HTML would be Javascript... As the others suggested already...
BUT... there's a solution with one slight change to your HTML and CSS each.
The main problem is this CSS-selector:
#item1:hover #item1detail
which would translate to "id item1detail INSIDE of an hovered id item1".
You can fix this by placing the div inside of the li and change the selector to:
#item1:hover + #item1detail
Since the div is positioned absolute anyway it doesn't make a visual difference... at least for your snippet...
Updated version of your snippet:
.topNav
{
padding: 1px 15%;
background: #006cb4;
color: white;
}
.mainMenu
{
list-style-type: none;
}
.mainMenu li
{
display: inline-block;
padding: 3px 15px;
font-size: 20px;
}
.mainMenu li a
{
text-decoration: none;
color: white;
}
#item1
{
display: block;
}
#item1:hover + #item1detail
{
background: #444;
visibility: visible;
}
#item1detail
{
position: absolute;
top: 152px;
left: 250px;background: #ccc;
width: 750px;
height: 400px;
border:solid 1px black;
border-radius: 0 0 10px 10px;
visibility: hidden;
}
<div class="topNav">
<ul class="mainMenu">
<li >
<a id="item1" href=""> item 1</a>
<div id="item1detail">
Some random content
</div>
</li>
<li> item 3</li>
<li> item 4</li>
<li> item 5</li>
<li> item 6</li>
<li> item 7</li>
<li> item 8</li>
<li> item 9</li>
</ul>
</div>
You'll have to use javascript
<script>
function myFunction() {
if (document.getElementById("item1detail").hidden==false){
document.getElementById("item1detail").hidden = true;
}else{
document.getElementById("item1detail").hidden = false;
}
}
</script>
and
<div class="topNav">
<ul class="mainMenu">
<li><a id="item1" onhover="myFunction()" href=""> item 1</a>
</li>
<li> item 3
</li>
<li> item 4
</li>
<li> item 5
</li>
<li> item 6
</li>
<li> item 7
</li>
<li> item 8
</li>
<li> item 9
</li>
</ul>
<div id="item1detail">
Some random content
</div>
</div>
I would do that using jQuery.
$('#item1').hover(function(){
$('#item1detail').show();
}, function(){
$('#item1detail').hide();
});
The reason your CSS isn't working is because you're using this selector:
#item1:hover #item1detail
Which selects the element with id #item1detail occurring within the element with id #item1, if the #item1 element is hovered.
In your current markup, #item1detail is outside #item1, and so does not match the selector. Moving #item1detail should get you the behavior you want. (And there will probably be some layout work to do from that point.)
The #item1detail element is not a sibling of the #item1 element, so that is why the #item1:hover #item1detail CSS rule does not apply as you expect it to.
I believe if this is to work with CSS only (not JavaScript), then you will have to make #item1detail a sibling of #item1.
Related
I am trying to fix the behaviour of a nested navbar I have on my page. The main idea is that I have two levels of <ul>-tags, where on both levels the <li>-tags can contain links (i.e. <a>), so both levels have the ability to redirect the page. Upon hovering the top level <li>, I have the lower level display, otherwise it is hidden. This looks like this:
* {
font-family: Roboto;
}
.menu-container {
height: 29px;
background-color: #dcb400;
}
.menu {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
}
.menu-item {
position: relative;
}
.menu-item:hover {}
.menu-item-div {
display: inline-block;
margin-left: 0;
line-height: 1.5rem;
padding: 3.5px 9.5px;
color: black;
font-weight: 700;
font-size: 13px;
cursor: pointer;
height: 22px;
}
.menu-item:hover .menu-item-div {
background-color: black;
color: white;
}
.submenu {
position: absolute;
top: 29px;
width: auto;
background-color: white;
color: black;
z-index: 100;
padding: 0px;
display: none;
-webkit-box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
box-shadow: 3px 3px 10px rgb(0 0 0 / 50%);
}
.menu-item:hover .submenu {
display: block;
}
.submenu-item {
display: block;
cursor: pointer;
}
.submenu-item:hover {
background-color: #ccc;
}
<body>
<div class='menu-container'>
<ul class='menu'>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 1</div>
</a>
<ul class='submenu'>
<li class='submenu-item'>Submenu item 1</li>
<li class='submenu-item'>Submenu item 2</li>
<li class='submenu-item'>Submenu item 3</li>
</ul>
</li>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 2</div>
</a>
<ul class='submenu'>
<li class='submenu-item'>Submenu item 4</li>
</ul>
</li>
<li class='menu-item'>
<a href='#'>
<div class='menu-item-div'>Menu item 3</div>
</a>
</li>
</ul>
</div>
</body>
Now, this works like a charm on desktop, as my submenus show up as soon as I hover a top level menu item. However, on mobile this doesn't work anymore, as the only (reasonable) way to hover a top level menu item is by clicking it, which results in the page redirecting to the top level <a> target. Is there a way to expand this code to also make it work on mobile, i.e. when tapping the top level menu item on mobile, the submenu should show up instead of the page redirecting?
The solutions I have found so far online are either concerning libraries like bootstrap or are way too clumsy. I am wondering if there is a way to do this with only HTML and CSS, or do we also have to involve some js?
Since you are not using your top level menu items for redirecting, you could abandon your references altogether and use divs instead:
<ul>
<li>
<div>Menu item 1</div>
<ul> <!-- display on hover of top level li -->
<li><a>Submenu item 1</a></li>
...
</ul>
</li>
...
</ul>
Or for your given example: https://jsfiddle.net/0fmbojqk/3/
I'm trying to make a basic navigation bar where a child dropdown appears when hovering over a list item. I want to position this dropdown starting at the right most edge of the list item I am hovering over, but I want it this dropdown be able to scale bigger than the list item you're hovering over.
The trouble is that when I position the parent relative, the dropdown's width is constricted to the width of the list item you're hovering over, when I remove postion relative I lose the ability to position it the way I want it.
When the parent List item doesn't have position relative it looks like this:
But I want the right edge of that dropdown to align with the right side of the list item I'm hovering on. When I add position relative to the list items, the width of the dropdown is contsrained like this:
The markup looks like follows:
<nav>
<ul class="outer-list">
<li>
<a>
Work
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Contact
</a>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Helpdesk
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
<ul class="outer-list">
<li>
<a>
Subscriptions
</a>
<ul class="inner-list">
<li>Sub1</li>
<li>Sub2</li>
<li>Sub3</li>
</ul>
</li>
</ul>
I am not in charge of the markup, but if it needs to change to allow for a solution that is fine.
My CSS is as follows:
.outer-list{
.dropdown{
padding-right: 20px;
a{
color: black;
text-decoration: none;
position: relative;
.icon-dropdown{
position: absolute;
display: inline-block;
width: 6px;
height: 4px;
background-image: url('./Assets/BlueArrowIcon.svg');
background-size: cover;
background-position: center;
top: 50%;
right: -11px;
transform: translateY(-50%)
}
}
.inner-list{
padding: 25px 20px;
display: none;
position: absolute;
background-color: $color-white;
box-shadow: 5px 0px 50px rgba(0,0,0,0.16);
z-index: 1;
max-width: 310px;
li{
margin-bottom: 20px;
&:hover{
a{
color: $color-dark-red;
}
}
}
}
&:hover{
a{
color: $color-blue;
}
.inner-list{
display: block;
a{
color: black;
}
}
}
}
&:last-of-type{
.dropdown{
padding-right: 0px;
}
}
}
If anyone could help me that would be much appreciated.
I have been tasked with styling a website, where I have encountered a hurdle regarding the horizontal alignment of elements inside list items.
I have replicated the structure of the menu in question with this JSFiddle.
I want to know how I can keep the position of the green divs (as shown from the start) when I expand the menu, using the button in the fiddle. I need them to keep horizontal alignment with the first <a> element in their respective <li> element, when the submenus are displayed.
you can do it like this for example:
<html>
<script>
function clickFunction(){
var elements = document.getElementsByClassName("submenu");
for(var i = 0; i < elements.length; i++){
elements[i].classList.toggle("display-sublist");
}
}
</script>
<style>
ul{
list-style-type: none;
margin: 0px;
padding: 0px;
}
ul li{
width: 100%;
background-color: blue;
}
.submenu{
display: none;
}
.display-sublist{
display: block;
}
ul li a{
width: 95%;
background-color: red;
}
.main-test {
display: inline-block;
float: left;
width: 90%;
}
.cancel-test{
display: inline-block;
background-color: green;
float: right;
width: 10%;
}
.expand-button{
clear: both;
display: block;
}
</style>
<body>
<ul>
<li>
<a class="main-test" href="#">Something</a>
<a class="cancel-test">x</div>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
<a class="main-test"href="#">Something</a>
<a class="cancel-test">x</a>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
Something
</li>
<li>
Something
</li>
</ul>
<button onclick="clickFunction()" class="expand-button">Expand</button>
</body>
</html>
I'm trying to use hover property for dropdown but I do not understand
how to use lab1, lab 2, lab 3 ,lab4, lab 5 when they are not defined.
I know how to use hover property but for multiple ul and li is little hard for me to get it.
Dropdowns are not coming down. All my other css properties are fine, I just need help in dropdown property. Any idea?
body {
font: 1.2em Verdana, sans-serif;
background-color: antiquewhite;
}
#header,
#footer {
background-color: #aaa;
padding: 0.5em;
}
#content h1 {
text-align: center;
}
#wrapper {
//background-color: hsl(200, 75%, 40%);
}
#content {
border-left: 8em solid hsl(200, 75%, 40%);
padding: 1em;
background-color: hsl(200, 75%, 60%);
box-sizing: border-box;
}
#leftnav {
float: left;
width: 8em;
box-sizing: border-box;
}
#leftnav ul {
list-style-type: none;
padding-left: 0px;
margin-left: 0px;
}
#leftnav li {
color: white;
background-color: hsl(200, 75%, 30%);
padding: 0.2em 0.5em;
border: solid white;
border-width: 1px 0;
}
#leftnav li:last-child {
border-bottom: none;
}
#leftnav li:first-child {
border-top: none;
}
#leftnav li:hover {
text-decoration: red;
color: red;
font-weight: bold;
text-transform: capitalize;
font-size: 20px;
background-color: white;
}
<div id="nav">
<ul>
<li>Lab 1
<ul>
<li> Basic XHTML </li>
</ul>
</li>
<li>Lab 2
<ul>
<li> Nesting Errors - Fixed </li>
<li> Block and Inline Elements </li>
<li> List of Links </li>
<li> Lab Index </li>
</ul>
</li>
<li>Lab 3
<ul>
<li> Merging Table Cells </li>
<li> Common Page Layout </li>
<li> XHTML Vocabulary </li>
<li> Vocabulary with Examples </li>
</ul>
</li>
<li>Lab 4
<ul>
<li> CSS - HTML Selectors </li>
<li> CSS - More Selectors </li>
<li> Table Styling </li>
<li> CS - Descendant Selectors </li>
</ul>
</li>
<li>Lab 5
<ul>
<li> Body Style </li>
<li> Inheriting Colours </li>
<li> Structuring Text </li>
<li> Another Lab Index </li>
</ul>
</li>
<p class="clearboth"></p>
</ul>
</div>
If i understand correctly, you are making a navbar and try to place the dropdown of the child element when people hover the parent element? The idea of ul-li-ul is that when you hover over a li element, the consequence ul will show up, so you don't actually need to define which lab is going to open which one.
#nav li ul{
display: none;
}
#nav li:hover>ul{
display: block;
position: absolute;
margin-left: 50px;
}
<ul id="nav">
<li>Basic XHTML</li>
<li>LAB 2
<ul>
<li> Nesting Errors - Fixed </li>
<li> Block and Inline Elements </li>
<li> List of Links </li>
<li> Lab Index </li>
</ul>
</li>
<li>LAB 3
<ul>
<li> Merging Table Cells </li>
<li> Common Page Layout </li>
<li> XHTML Vocabulary </li>
<li> Vocabulary with Examples </li>
</ul>
</li>
</ul>
Hope this helps.
You may use select tag. Example:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
The <select> element is used to create a drop-down list.
The <option> tags inside the <select> element define the available options in the list.
Code taken at: w3schools
I'm trying to figure out how to position a flowchart built with ul list growing like a tree, from bottom-up.
It's a genealogical family tree. I've made it from top down using this great code here but I want the first element in the bottom, above it the parents and above it the grandparents and so on...
Here is the code:
HTML
<div class="tree">
<ul>
<li>
<div class="dados_membro">Me</div>
<ul>
<li>
<div class="dados_membro">Father</div>
<ul>
<li>
<div class="dados_membro">Grandfather</div>
</li>
<li>
<div class="dados_membro">Grandmother</div>
</li>
</ul>
</li>
<li>
<div class="dados_membro">Mother</div>
<ul>
<li>
<div class="dados_membro">Grandfather</div>
</li>
<li>
<div class="dados_membro">Grandmother</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS
.tree ul {
padding-top: 20px;
position: relative;
}
.tree li {
float: left;
text-align: center;
list-style-type: none;
position: relative;
padding: 39px 5px 0 5px;
}
.dados_membro {
border: 1px solid;
border-radius: 20px;
width: 300px;
margin: auto;
text-align:left;
padding:10px
}
I'm testing some rules in this fiddle and could position the first node "Me" in the bottom and the other ones up with the CSS bellow, but all the nodes in the same level (parents, grandparents and so on..) get pilled up.
.tree ul {
position: absolute;
bottom:0;
}
.tree ul li ul{
position:relative;
}
.tree li {
float: right;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
margin-top:-120px
}
.dados_membro {
border: 1px solid;
border-radius: 5px;
width: 100px;
margin: auto;
text-align:left;
padding:10px
}
I don't know how big will the tree grow up nor if all the nodes will have elements. Can't use javascript for this. Any ideas?
You could change the order of the elements using the flexbox property flex-direction: column-reverse, if that is an option for your project (see compatibility table).
To revert the order of the list I gave the main container position: absolute with bottom:0, the <ul>s a position:relative and all <li>s position:absolute with bottom:50px, except for the first level that I want to be in the bottom of the container.
This way every <li> is positioned 50px above its parent <ul> that has relative position.
One last tweak to achieve the tree style was adding a class for female and male parents in order to position each one in the left and in the right above its child node. Since I'm generating this tree dinamically I can count how many levels does the tree has and calculate the distance between father/mother nodes. If I could not control the html dinamically, I would need to use javascript to count the nodes and apply the css rules on the fly (maybe I will do ti in the future, just to animate the tree generation).
The new code is here
HTML
<div class="tree">
<ul>
<li>
<div class="dados_membro">Me</div>
<ul>
<li class="f1">
<div class="dados_membro">Father</div>
<ul>
<li class="f2">
<div class="dados_membro">Grandfather f</div>
</li>
<li class="m2">
<div class="dados_membro">Grandmotherf </div>
</li>
</ul>
</li>
<li class="m1">
<div class="dados_membro">Mother</div>
<ul>
<li class="f2">
<div class="dados_membro">Grandfather m</div>
</li>
<li class="m2">
<div class="dados_membro">Grandmother m</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS
.tree {
position:absolute;
bottom: 0;
left:300px
}
.tree ul {
position: relative;
list-style:none;
}
.tree ul li ul li{
position: absolute;
bottom: 50px;
}
.f1{
left:-150px;
}
.m1 {
left:150px;
}
.f2{
left:-80px;
}
.m2 {
left:80px;
}
.dados_membro {
border: 1px solid;
border-radius: 5px;
width: 100px;
margin: auto;
text-align:left;
padding:10px
}