list item highlighted but not the entire row - html

My current fiddle is working fine apart from one thing. When I hover over the button I see the list displayed. Then when I hover over a list item the background changes colour which is all fine. However when the list item changes colour there is almost a box to the left of the item which is not highlighted and can't seem to get rid of it?
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content li:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #4C66E9;}

If i understand you. i believe you are looking to remove ui margin. you can do that using
ul{
padding: 0;
margin: 0;
}

By default most of HTML tags has some default styling, same is here in ul tag and that's the reason that when you hover li tags the left-side is not highlighted.
Default ul styling,
ul{
display: block;
list-style-type: disc;
margin-top: 1em;
margin-bottom: 1 em;
margin-left: 0;
margin-right: 0;
padding-left: 40px;
}
$(document).ready(function() {
var $region = $('#regionList');
$region.append('<li id="Europe">Europe</li>');
$region.append('<li id="Japan">Japan</li>');
$region.append('<li id="North America">North America</a></li>');
$("#regionList li").click(function() {
alert('Clicked list. ' + this.id);
});
})
/* Dropdown Button */
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
ul {
padding: 0px;
margin: 0px;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content li {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content li:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #4C66E9;
}
.selected {
background: #FF00FF;
}
/*Add this*/
ul{
padding-left:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Regions</button>
<div class="dropdown-content">
<ul id="regionList"></ul>
</div>
</div>

I believe below approach solves your problem. It was caused by ul element having default padding. I also added box-sizing: border-box to list elements, so padding won't make li elements stick out of ul.
I added explanation below snippet.
Solution key part
.dropdown-content #regionList {
padding: 0;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
box-sizing: border-box;
text-decoration: none;
display: block;
}
Snippet
$(document).ready(function() {
var $region = $('#regionList');
$region.append('<li id="Europe">Europe</li>');
$region.append('<li id="Japan">Japan</li>');
$region.append('<li id="North America">North America</a></li>');
$("#regionList li").click(function() {
alert('Clicked list. ' + this.id);
});
})
/* Dropdown Button */
.dropbtn {
background-color: #9FACEC;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content #regionList {
padding: 0;
}
.dropdown-content li {
color: black;
padding: 12px 16px;
box-sizing: border-box;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content li:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #4C66E9;
}
.selected {
background: #FF00FF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Regions</button>
<div class="dropdown-content">
<ul id="regionList"></ul>
</div>
</div>
Explanation
Problem is caused by browser's default CSS rules, which are for Chrome:
ul, menu, dir {
display: block;
list-style-type: disc;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start: 40px;
}
You can see more here.

The other answers are correct. I just wanted to explain what's going on.
Your user agent stylesheet contains -webkit-padding-start: 40px; for ul elements.
Represented here in green.
As other answers said, override the padding style for ul elements set by your user agent stylesheet.

Just add padding 0 to your regionList
#regionList{
padding : 0;
}

Related

resize dropdown menu to match link width

I would like to resize my dropdown menu to match the link width. Here is a picture of what I currently have:
Here are the relevant parts of my HTML page:
<body>
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
Here are the most relevant parts of my CSS stylesheet:
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: #272424;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
width: 25%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
I tried adding width: 100% to .dropdown-content but the width was too large and extended beyond the writing link to the right.
EDIT: As an extra feature, I would like my links to be appear as "boxes" that are connected without any spaces between them instead of having them appear as on one continuous band of black. How can I do that?
I have make some modification related to the css.
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
background-color: #272424;
font-family: Arial;
height:50px
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
width: 25%;
position: relative;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 100%;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: -webkit-fill-available;
}
Setting width to -webkit-fill-available will do the change.
If you want to appear as seperate boxes. You need to use flex feature.
Check out my code :I solved that .dropdown-content issue and Also added extra feature as per u r requirement i.e (spaces between them instead of having them appear as on one continuous band of black)
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: transparent;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
border-radius:4px;
margin:6.2px;
background-color: black;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 23.5%;
}
/* The dropdown container */
.dropdown {
float: left;
margin:6px;
border-radius:4px;
overflow: hidden;
background-color: black;
width: 24%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
width:23.5%;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
background-color:white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
#media only screen and (max-width: 900px){
.navbar a {
width:22%;
}
.dropdown{
width:22%;
}
.dropdown-content{
width:22%;
}
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/ Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>

nested dropdown menu using css

Trying out nested drop down menu using simple css.I am Trying to make navigation menu. All seems to work fine except that when I hover over the ABOUT US section under the NEW HERE, it shows me correct result. but the problem is it doesn't go away when i hover to the next section below it. how can i possibly fix that ? here's The Link For the snippet for you to consider, kindly tell me where i am doing it wrong.
here's the code for you to see..
.dropbtn2
{
position: relative;
display: none;
}
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 169px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
font-size:20px;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
transition:none;
}
.dropdown-content a:hover {
padding-top:15px;
transition:ease-in-out .5s;
}
.dropdown:hover .dropbtn
{
background-color: #8300ff;
color: #182318;
padding-top: 500px;
padding-bottom: 30px;
padding-right: 20px;
padding-left: 20px;
transition: ease-out 0.5s;
text-decoration: none;
box-shadow:none;
}
/* End*/
.dropdown-content2 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width:170px;
left:170px;
top:0;
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content2 a {
color: black;
font-size:20px;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content2 a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown-content:hover .dropdown-content2 {
display: block;
transition:none;
}
.dropdown-content2 a:hover {
padding-top:15px;
transition:ease-in-out .5s;
}
.dropdown a:hover .dropbtn2
{
padding-bottom: 30px;
padding-right: 20px;
padding-left: 20px;
transition: ease-out 0.5s;
text-decoration: none;
box-shadow:none;
}
/*TEST*/
You need to change the element to hover in order to display your dropdown-content only when hovering your first link.
The value to replace is at line 182 of your codepen. Replace dropdown-content:hover .dropdown-content2 by .dropdown2:hover .dropdown-content2.
Lines 181-185:
/* Show the dropdown menu on hover */
.dropdown2:hover .dropdown-content2 { /* change the hover element here */
display: block;
transition:none;
}

Align html dropdown menu left (under the title)

I am currently struggling to align the dropdown menu I have created. I need it to align to the left as it is currently aligning to the centre. I have created a jsfiddle to demonstrate my current code and the issue.
JSFiddle - https://jsfiddle.net/wu9gnsj5/
.menu {
float: right;
margin-top: 22px;
margin-left: 45px;
margin-right: 140px;
padding:0;
list-style: none;
font-weight: 700;
}
.menu div {
float: left;
margin-left: 30px;
}
.menu div a {
color: #42494e;
}
.header div.menu div a:hover {
color: #e60000;
}
.dropbtn {
color: black;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
padding-bottom: 20px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
top:100%;
left: 0%;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
display: block;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {}
Any help would be greatly appreciated.
You have a css class that targets all .menu > div elements with margin-left: 30px;, including your dropdown.
You can override this by resetting the margin in your dropdown class.
.dropdown-content {
margin-left: 0;
}

Dropdown in navbar not showing list of elements(Angular 6)

I write navbar component in my Angular 6 app.
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
and .css file:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
but it's not working.
When I use the mouse, dropdown does not display anything.. It's difficult to say but I use https://www.w3schools.com/css/css_dropdowns.asp .
Do not I have any libraries?
And other question. Will I be able to use in dropdown later? Beacuse I use RoutingModule.
Very thanks for all answers.
EDIT:
add left navbar
<ul>
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>
ul {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
You need to remove the overflow: hidden rule from ul, otherwise the dropdown won't be visible.
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%;
z-index: 5;
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
position: relative;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
ul.side {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
z-index: 1;
}
.side li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
.side li a:hover {
background-color: #555;
color: white;
}
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
<ul class="side">
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>

Button dropdown menu disappears

I added the following code for my button drop-down for my page http://test.hkkkki.eu/ and here it is the code snippet:
.dropbtn {
background-color: #ff4e39;
color: white;
padding: 0px 16px 0px 6.4px;
font-size: 16px;
border: none;
font-family: 'Josefin Sans',Helvetica,Arial,Lucida,sans-serif;
transition: all 0.5s ease;
}
.dropdown {
position: relative;
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #fff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color:#fff;
background-color:#ff4e39;
text-decoration: none;
display: block;
}
.dropdown-content a:hover { color:#000; transition: all 0.5s ease;}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #ff4e39;}
<div class="dropdown">
<button class="dropbtn">press</button>
<div class="dropdown-content">
priopćenja
foto
video
iz medija
</div>
</div>
The result is not good, as the drop-down disappears when trying to pick a sub-menu option. And it is also not on top of the following content. It's probably due to Z-index, but I tried more than a couple of places to put it, and it didn't work. Any help is greatly appreciated.
I can see what you mean when I go to your website. I can't see the problem exactly in your code but it seems like the drop down entitled 'press' works fine, are you using the same classes to reference the dropdowns? It would be nice to see the way you created the 'press' dropdown so we can compare and see what went wrong.
Regardless here is the best I can do. w3schools has a very handy tutorial on everything you need to make a hoverable drop down. Here is a snippet to attempt to answer your question:
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>