Menu list items: Full width (border-bottom)? - css

I have a menu from a list. On small widths I want the border-bottom to use the full width:
HTML:
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2 with Text</li>
<li>Item 3 with more Text</li>
<li>Item 4</li>
<li>Item 5 with Text</li>
</ul>
</div>
CSS:
#menu li {
display: inline-block;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li a {
padding: .5em 1em;
box-sizing: border-box;
display: block;
}
Test: Fiddle

Without a background image, you're going to have to get creative. How about this...
var li = $("#menu li");
li.click(function () {
li.removeClass("active");
$(this).addClass("active");
});
body {
background-color: #eee;
}
#wrapper {
width: 300px;
padding: 1em;
background-color: #fff;
}
a {
color:inherit;
text-decoration: none;
}
#menu {
overflow: hidden;
list-style:inline;
padding: 0 0 2px;
-webkit-margin-before: 0;
-webkit-margin-after: 0;
margin: 0 1em 1em;
}
#menu li {
display: inline-block;
margin-top: 2px;
margin-bottom: -2px;
position: relative;
z-index: 2;
}
#menu li:after {
content: " ";
position: absolute;
bottom: -2px;
right: -1000px;
width: 1000px;
height: 0;
border-bottom: 2px solid #ccc;
z-index: 1;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li:not(:last-of-type) {
margin-right: -.275em;
}
#menu li a {
padding: .5em 1em;
box-sizing: border-box;
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2 with Text</li>
<li>Item 3 with more Text</li>
<li>Item 4</li>
<li>Item 5 with Text</li>
</ul>
</div>

flexbox (as mention by Luis P.A.) can help here by making the li fill with full width of the ul and wrapping as required.
Flexbox Support is IE10 and up.
var li = $("#menu li");
li.click(function() {
li.removeClass("active");
$(this).addClass("active");
});
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #eee;
}
#wrapper {
width: 300px;
padding: 1em;
background-color: #fff;
}
a {
color: inherit;
text-decoration: none;
}
#menu {
list-style: none;
padding: 0;
-webkit-margin-before: 0;
-webkit-margin-after: 0;
margin: 0 1em 1em;
overflow: hidden;
display: flex;
flex-wrap: wrap;
}
#menu li {
flex: 1 1 auto;
position: relative;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li:not(:last-of-type) {} #menu li a {
padding: .5em 0;
box-sizing: border-box;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1
</li>
<li>Item 2 with Text
</li>
<li>Item 3 with more Text
</li>
<li>Item 4
</li>
<li>Item 5 with Text
</li>
</ul>
</div>

Related

why Increased Height item Contact when hover on it?

why Increased Height item Contact when hover on it? how fix it?
This is My code :
ul {
padding: 0;
}
#nav ul {
display: none;
}
#nav li:hover > ul {
display: block;
}
#nav > li {
float: left;
}
#nav li {
list-style: none;
width: 150px;
position: relative;
border: 1px solid red;
box-sizing: border-box;
}
#nav a {
display: block;
background-color: #000;
color: red;
text-decoration: none;
padding: 10px 20px;
text-align: center;
box-sizing: border-box;
border: 2px solid transparent;
}
#nav ul ul {
position: absolute;
left: 150px;
top: 0;
}
#nav li:hover > a {
color: orange;
}
#nav li:hover > a:after {
content:'\25B6';
color: red;
margin-left: 5px;
padding: 0;
}
#nav > li:hover > a:after {
content: '\25BE';
color: red;
margin-left: 5px;
padding: 0;
}
<div id="wrapper">
<ul id="nav">
<li>Home</li>
<li>Products
<ul>
<li>Product 1</li>
<li>Product 2
<ul>
<li>Model 1</li>
<li>Model 2</li>
<li>Model 3</li>
</ul>
</li>
<li>Product 3</li>
</ul>
</li>
<li>Services
<ul>
<li>Service 1</li>
<li>Service 2</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
See This Image :
When you hover over the list items - you're adding an arrow to the anchor tag via generated content.
Thant's what's causing the increase in height.
To fix this - just set position:absolute on the generated content.
ul {
padding: 0;
}
#nav ul {
display: none;
}
#nav li:hover > ul {
display: block;
}
#nav > li {
float: left;
}
#nav li {
list-style: none;
width: 150px;
position: relative;
border: 1px solid red;
box-sizing: border-box;
}
#nav a {
display: block;
background-color: #000;
color: red;
text-decoration: none;
padding: 10px 20px;
text-align: center;
box-sizing: border-box;
border: 2px solid transparent;
}
#nav ul ul {
position: absolute;
left: 150px;
top: 0;
}
#nav li:hover > a {
color: orange;
}
#nav li:hover > a:after {
content:'\25B6';
color: red;
margin-left: 5px;
padding: 0;
position:absolute;
}
#nav > li:hover > a:after {
content: '\25BE';
color: red;
margin-left: 5px;
padding: 0;
}
<div id="wrapper">
<ul id="nav">
<li>Home</li>
<li>Products
<ul>
<li>Product 1</li>
<li>Product 2
<ul>
<li>Model 1</li>
<li>Model 2</li>
<li>Model 3</li>
</ul>
</li>
<li>Product 3</li>
</ul>
</li>
<li>Services
<ul>
<li>Service 1</li>
<li>Service 2</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
Because of
#nav > li:hover > a:after {
content: '\25BE';
}
the height (font size) is to high of this content.
maybe you should try a background-img.
Or position it absolute and the a-tag relative.
Its because of the :after content attribute (#nav > li:hover > a:after) you are adding on hover(the small arrow icon)
To avoid this, applying line-height: 0 is an efficient way of doing it.
#nav > li:hover > a:after {
content: '\25BE';
color: red;
margin-left: 5px;
padding: 0;
line-height: 0; //fix
}
Also you have this code twice, make sure you remove the redundant one.
It simple line height issue.
ul {
padding: 0;
}
#nav ul {
display: none;
}
#nav li:hover > ul {
display: block;
}
#nav > li {
float: left;
}
#nav li {
list-style: none;
width: 150px;
position: relative;
border: 1px solid red;
box-sizing: border-box;
}
#nav a {
display: block;
background-color: #000;
color: red;
text-decoration: none;
padding: 10px 20px;
text-align: center;
box-sizing: border-box;
border: 2px solid transparent;
line-height: 20px;
}
#nav ul ul {
position: absolute;
left: 150px;
top: 0;
}
#nav li:hover > a {
color: orange;
}
#nav li:hover > a:after {
content:'\25B6';
color: red;
margin-left: 5px;
padding: 0;
}
#nav > li:hover > a:after {
content: '\25BE';
color: red;
margin-left: 5px;
padding: 0;
}
<div id="wrapper">
<ul id="nav">
<li>Home</li>
<li>Products
<ul>
<li>Product 1</li>
<li>Product 2
<ul>
<li>Model 1</li>
<li>Model 2</li>
<li>Model 3</li>
</ul>
</li>
<li>Product 3</li>
</ul>
</li>
<li>Services
<ul>
<li>Service 1</li>
<li>Service 2</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>

CSS drop-up menu with second row on top-left

I have menu position on botoom of the page.
And I need to have three level menu, when each level is in row and located to the left.
My CSS Code:
.primary-navigation {
clear: both;
float: left;
display: block;
position: relative;
width: 100%;
top: 80px;
border: 2px solid red;
}
.primary-navigation ul {
list-style: none;
margin: 0;
padding-left: 0;
}
.primary-navigation ul > li,
.primary-navigation ul > li > ul > li,
{
position: relative;
float: left;
padding: 3px 5px;
margin: 10px 5px;
cursor: pointer;
}
.primary-navigation ul ul ul li {
float: none;
}
.primary-navigation li > ul {
display: none;
position:absolute;
bottom: 100%;
left: -50%;
border: 2px solid red;
}
.primary-navigation li:hover > ul {
position: absolute;
display: block;
width: 600px;
}
.primary-navigation li {
float: left;
position: relative;
font-size: 20px;
}
.primary-navigation li ul li {
float: left;
position: relative;
font-size: 15px;
}
.primary-navigation a {
color: #FFF;
display: block;
line-height: 16px;
padding: 9px 15px;
text-decoration: none;
}
<nav class="primary-navigation">
<ul>
<li>Ahoj</li>
<li>Test A
<ul>
<li>Test 1</li>
<li>Test 2
<ul>
<li>Test A</li>
<li>Test B</li>
<li>Test C</li>
</ul></li>
<li>Test 3</li>
</ul></li>
<li>Test B
<ul>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
</ul></li>
</ul>
</nav>
I know, that there is a problem in li > ul part, when is
position:absolute;
bottom: 100%;
left: -50%;
I know, that "left" give me a horizontal position.
The problem is, how to get this second row to the left and make it wide full length
Here you go, you have to delete the position: relative out of your children <li> elements, because this is the reason the children of those will align absolute to it's parent (<li> and not the <nav> itself.) Further, you've to do a little trick with the border, because it affects the absolute positioning. So here I use box-shadow on the left side of your elements to achieve the same effect without a border. border-bottom is not needed, because you stack the elements anyway.
CSS for border "trick"
border-right: 2px solid red;
border-top: 2px solid red;
box-shadow: inset 2px 0 0 red //imitates a border-left
SNIPPET
html * {
box-sizing: border-box;
}
.primary-navigation {
clear: both;
float: left;
display: block;
position: relative;
width: 100%;
top: 80px;
border: 0;
box-shadow: inset 0 0 0 2px red;
}
.primary-navigation ul {
list-style: none;
margin: 0;
padding-left: 0;
}
.primary-navigation ul > li,
.primary-navigation ul > li > ul > li,
{
float: left;
padding: 3px 5px;
margin: 10px 5px;
cursor: pointer;
}
.primary-navigation ul ul ul li {
float: none;
}
.primary-navigation li > ul {
display: none;
position:absolute;
bottom: 100%;
left: 0;
border-right: 2px solid red;
border-top: 2px solid red;
box-shadow: inset 2px 0 0 red
}
.primary-navigation li:hover > ul {
position: absolute;
display: block;
width: 600px;
}
.primary-navigation li {
float: left;
font-size: 20px;
}
.primary-navigation li ul li {
float: left;
font-size: 15px;
}
.primary-navigation a {
color: #FFF;
display: block;
line-height: 16px;
padding: 9px 15px;
text-decoration: none;
}
<nav class="primary-navigation">
<ul>
<li>Ahoj</li>
<li>Test A
<ul>
<li>Test 1</li>
<li>Test 2
<ul>
<li>Test A</li>
<li>Test B</li>
<li>Test C</li>
</ul></li>
<li>Test 3</li>
</ul></li>
<li>Test B
<ul>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
</ul></li>
</ul>
</nav>

CSS3 Onclick Menu

I used the following code to create my menu, for learning purposes:
http://koen.kivits.com/articles/pure-css-menu/
My CSS:
html, body {
margin: 0;
padding: 0;
min-width: 1920;
}
.container {
position: relative;
display: inline-block;
width: 1920;
height: 20;
background: linear-gradient(#858585, #636263);
border-top: 1px solid #656565;
border-bottom: 1px solid #3663ab;
box-shadow: inset 0 1px 0 #a8a8a8;
}
.wrapper {
position: relative;
display: inline-block;
width: 1920;
height: 24;
background: linear-gradient(#c8bfb0, #f5efe6);
border-bottom: 1px solid #d3c7b6;
}
.seperator {
position: absolute;
display: inline;
width: 2;
height: 22;
border: 2px solid #fff;
background-color: #000;
}
.onclick-menu-red {
font-family: verdana;
font-size: 11px;
color: #eaeaea;
background: linear-gradient(#858585, #636263);
box-shadow: inset 0 1px 0 #a8a8a8;
position: relative;
display: inline-block;
cursor: pointer;
}
.onclick-menu-red:hover {
color: #fff;
}
.onclick-menu-red:active {
background-color: red;
}
.onclick-menu-red:after {
background-color: red;
}
.onclick-menu-red:focus {
pointer-events: none;
background-color: red;
}
.onclick-menu-red:focus .onclick-menu-content {
opacity: 1;
visibility: visible;
}
.onclick-menu-content {
font-family: verdana;
pointer-events: auto;
position: absolute;
z-index: 1;
opacity: 0;
visibility: hidden;
transition: visibility 0.5s;
float: left;
}
.onclick-menu-red.no-pointer-events {
pointer-events: auto !important;
}
.onclick-menu-red.no-visibility .onclick-menu-content {
visibility: visible !important;
display: none;
}
.onclick-menu-red.no-visibility:focus .onclick-menu-content {
display: block;
}
.onclick-menu-red.no-opacity .onclick-menu-content {
opacity: 1 !important;
}
.onclick-menu-red {
padding: 3 27 4 27;
margin: 0 0 1em 0;
outline: 0;
}
.onclick-menu-red:before {
padding: 5px 10px;
background-color: #94a4a5;
}
.onclick-menu-content {
display: inline-block;
background: linear-gradient(#c8bfb0, #f5efe6);
width: 600;
margin-top: 5px;
margin-left: 0;
padding: 5px;
float: left;
}
.onclick-menu-content ul {
display: inline;
}
.onclick-menu-content li {
display: inline;
color: #f2f5e9;
list-style-type: none;
white-space: nowrap;
}
a:link {
color: #000;
text-decoration: none;
}
a:visited {
color: #000;
text-decoration: none;
}
a:hover {
color: red;
text-decoration: none;
}
a:active {
color: #fff;
font-weight: bold;
text-decoration: none;
}
My HTML:
<div class="container">
<div tabindex="0" class="onclick-menu-red">Home
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Settings
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Expenses
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Incomes
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
</div>
<div class="wrapper"></div>
My issue:
I did manage to fix a few issues I had, but my main issue right now is that I want the buttons to change to "red" once clicked.
This is a simple issue which comes from the use of the property background for elements basically but only using background-color when overwriting property.
You might change background-color: red to background: red to fix this issue.

Nested lists with full-width border and indentation

I'm trying to achieve the following result:
So far, I've written the following:
a {
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
li {
border-top: 1px solid;
border-bottom: 1px solid;
padding-left: 1em;
line-height: 2em;
}
li li {
margin-left: -1em;
padding-left: 2em;
border-bottom: none;
}
li li li {
margin-left: -2em;
padding-left: 3em;
border-bottom: none;
}
Demo: https://jsfiddle.net/9h891a0s/1/
However, I am looking for a solution that would allow for infinite depth. Is there a clean solution for this?
Take a look of this by using the position:absolute tricks for the borders.
body {
margin: 0;
padding: 1em;
}
body > ul {
border-bottom: 1px solid black;
overflow: hidden;
}
a {
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
li {
line-height: 2em;
position: relative;
}
li ul {
padding-left: 1em;
}
li:after {
content:"\00A0";
position: absolute;
width: 200%;
left: -100%;
top: 0;
border-top: 1px solid black;
z-index: -1;
}
<ul>
<li>Level 1
<ul>
<li>Level 2</li>
<li>
Level 2
<ul>
<li>Level 3</li>
<li>Level 3</li>
<li>Level 3</li>
</ul>
</li>
<li>Level 2</li>
</ul>
</li>
</ul>
Fiddle Demo: http://jsfiddle.net/Lr5cmoo6/
You could fake the borders by applying a repeating linear-gradient as the background of the top level ul. Then you'd need to just need a single rule for your list items to set your padding.
Here's an example:
body{
font-family:arial;
font-size:14px;
margin:0;
}
body>ul{
background:linear-gradient(0deg,#000 3.333%,#fff 3.333%);
background-size:100% 30px;
}
ul{
margin:0;
padding:0;
list-style-type:none;
}
li{
line-height:30px;
padding:0 1em;
}
<ul>
<li>Level 1
<ul>
<li>Level 2</li>
<li>
Level 2
<ul>
<li>Level 3</li>
<li>Level 3</li>
<li>Level 3</li>
</ul>
</li>
<li>Level 2</li>
</ul>
</li>
</ul>
There's no way to increase the margin/padding of an element by the nesting.
May this could help you:
a {
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
li {
border-top: 1px solid;
border-bottom: 1px solid;
line-height: 2em;
}
li li {
border-bottom: none;
}
li li a {
margin-left: 1em;
}
li li li a {
margin-left: 2em;
}
li li li li a {
margin-left: 3em;
}

Centering Submenu

I've read all the questions concerning centering submenus. But I don't get my problem solved.
I have a simple navigation bar with 2 submenus.
You can find it here: Fiddle.
ul#nav, ul#sub1, ul#sub2 {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul#sub1 a, ul#sub2 a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul#sub1, ul#sub2 {
display: none;
position: absolute;
left: 0px;
}
ul#nav li:hover ul#sub1 {
display: block;
}
ul#sub1 li:hover ul#sub2 {
display: block;
}
<nav>
<ul id="nav">
<li>Reisen
<ul id="sub1">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul id="sub2">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul id="sub1">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
</nav>
I want the submenu centered. When I hover over "Reisen" the submenu gets the same width like the main menu.
When I hover over "Karriere", I want the submenu centered under "Karriere" and not positioned left under "Reisen".
I was thinking of a span-element to the button "Karriere" but I couldn't solve it.
Thanks for your help.
I don't really now if this is what you're looking for or not, but maybe something like this?
Note: I made a few changes to your CSS and HTML, mainly changing everything to use classes instead of IDs
JS Fiddle Example
HTML
<nav>
<ul id="nav">
<li>Reisen
<ul class="sub">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul class="sub-second">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul class="sub">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
CSS
ul#nav, ul.sub {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
position: relative;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul.sub a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul.sub {
display: none;
position: absolute;
left: 0px;
padding-left: 0;
}
ul.sub-second {
display: none;
list-style: none;
left:100px;
top: 0;
position: absolute;
}
ul#nav li:hover ul.sub {
display: block;
}
ul#nav li:hover ul.sub li:hover ul.sub-second {
display:block;
}
}

Resources