I have a Nav with some unordered elements displayed horizontally and then I have a submenu from one of those elements of the nav, so far I'm displaying all the elements inline but I would like to display them side by side, just two of them per line.
How it actually looks:
And how I want it to be:
Just two columns displaying elements side by side, aligned to left side (of 50% width each side).
But I can't get it done, here's what I got so far
HTML
<nav id="menu">
<ul>
<li class="menu1">HOME</li>
<li class="left">NEWS</li>
<li class="left">VIDEOS</li>
<li class="left">STYLE</li>
<li class="left">BEAUTY</li>
<li class="left">MOMS</li>
<li class="left">ENTERTAINMENT</li>
<li class="left">PETS</li>
<li class="left">NEWS
<ul>
<li>Content1</li>
<li>Content2</li>
<li>Content3</li>
<li>Content4</li>
<li>Content5</li>
<li>Content6</li>
<li>Content7</li>
<li>Content8</li>
</ul>
</li>
</nav>
CSS
#menu {
position: absolute;
left:80px;
top:50%;
transform: translate(0%,-50%);
-webkit-transform: translate(0%,-50%);
}
#menu ul {
list-style:none;
position:relative;
float:left;
margin:0;
padding:0;
top:50%;
}
#menu ul li {
display: inline;
position: relative;
}
#menu ul li a {
color: #fff;
text-decoration: none;
font-family: 'Cabin', sans-serif;
background-color: ;
position: relative;
padding-top: 12px;
padding-bottom: 10px;
padding-right: 10px;
padding-left: 10px;
transition: background-color 0.2s ease-in-out;
}
#menu ul li a.news {
color:#ec008c;
transition: color 0.2s ease-in-out;
}
#menu ul li a:hover.news {
color: #fff;
}
#menu ul li a:hover {
background-color: #ec008c;
text-decoration: none;
border-bottom: 2px solid #bf1b33;
color: #fff;
}
#menu ul ul
{
display:none;
position:absolute;
top:100%;
right: 0%;
margin-left:0px;
text-align: center;
width: 350px;
line-height: 60px;
margin-top:12px;
background-color: #243641;
}
#menu ul ul.longer {
width: 150px;
}
#menu ul ul li
{
float:none;
width:100%;
background-color: ;
}
#menu ul ul a
{
line-height: 0px;
padding:0px 5px;
width: 100%;
background-color: ;
}
#menu ul li:hover > ul
{
display:block;
}
You can do something like this :
#menu ul ul li
{
float: left;
width: 50%;
}
See working demo here
You can use display: flex on the last ul and set a flex-basis width on the children (li). See my example below.
#menu ul li:hover > ul {
display: flex;
flex-flow: row wrap;
}
#menu ul ul li {
flex-basis: 50%;
}
This is all the extra code that's needed. I also fixed your code. because the <ul> didn't have closed tag (</ul>).
#menu {
position: absolute;
left: 80px;
top: 50%;
transform: translate(0%, -50%);
-webkit-transform: translate(0%, -50%);
}
#menu ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0;
top: 50%;
}
#menu ul li {
display: inline;
position: relative;
}
#menu ul li a {
color: #fff;
text-decoration: none;
font-family: 'Cabin', sans-serif;
background-color: ;
position: relative;
padding-top: 12px;
padding-bottom: 10px;
padding-right: 10px;
padding-left: 10px;
transition: background-color 0.2s ease-in-out;
}
#menu ul li a.news {
color: #ec008c;
transition: color 0.2s ease-in-out;
}
#menu ul li a:hover.news {
color: #fff;
}
#menu ul li a:hover {
background-color: #ec008c;
text-decoration: none;
border-bottom: 2px solid #bf1b33;
color: #fff;
}
#menu ul ul {
display: none;
position: absolute;
top: 100%;
right: 0%;
margin-left: 0px;
text-align: center;
width: 350px;
line-height: 60px;
margin-top: 12px;
background-color: #243641;
}
#menu ul ul.longer {
width: 150px;
}
#menu ul ul li {
float: none;
width: 100%;
background-color: ;
}
#menu ul ul a {
line-height: 0px;
padding: 0px 5px;
width: 100%;
background-color: ;
}
#menu ul li:hover > ul {
display: flex;
flex-flow: row wrap;
}
#menu ul ul li {
flex-basis: 50%;
}
<nav id="menu">
<ul>
<li class="menu1">HOME</li>
<li class="left">NEWS</li>
<li class="left">VIDEOS</li>
<li class="left">STYLE</li>
<li class="left">BEAUTY</li>
<li class="left">MOMS</li>
<li class="left">ENTERTAINMENT</li>
<li class="left">PETS</li>
<li class="left">NEWS
<ul>
<li>Content1</li>
<li>Content2</li>
<li>Content3</li>
<li>Content4</li>
<li>Content5</li>
<li>Content6</li>
<li>Content7</li>
<li>Content8</li>
</ul>
</li>
</ul>
</nav>
You should use display: inline-block; width width: 50% on #menu ul ul li elements:
#menu ul ul {
display:none;
position:absolute;
top:100%;
right: 0%;
margin-left:0px;
text-align: center;
width: 350px;
line-height: 60px;
margin-top:12px;
background-color: #243641;
font-size: 0; /* white spaces fix */
}
#menu ul ul li {
font-size: 1rem; /* white spaces fix */
float: none;
width: 50%;
display: inline-block;
background-color: ;
}
#menu ul ul a {
display: block;
padding: 0px 5px;
width: 100%;
background-color: ;
}
JSfiddle here.
This might be useful for your needs.
I don't know if you are using any framework or not, but this should work for you :)
http://alijafarian.com/bootstrap-multi-column-dropdown-menu/
Related
I've basically copy/pasted the code from Drop-down menu that opens up/upward with pure css
body {
padding: 3em;
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu {
float: center;
text-align: center;
line-height: 10px;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu" style="text-align:center; float: center; width: 100%">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
No matter what I do, I cannot align the menu to sit in the center of the web page.
Your help would be highly appreciated.
Thanks in advance.
Like this? I added the following css:
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
body {
padding: 3em;
}
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
You can horizontally center #menu by following CSS rules:
define #menu's display as inline-block
Add a container div outside of #menu and make its text-align as center.
I've made a fiddle for your code, please check.
Update your HTML code as below.
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel = "stylesheet" type = "text/css" href = "menu.css"/>
<title>Frames Test</title>
</head>
<body>
<iframe style="width:100%">padding</iframe>
<div class="container" style="text-align:center">
<div id="menu" style="text-align:center; float: center;display:inline-block">
<ul>
<li><center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
</div>
</body>
</html>
I am using simple HTML and CSS to create horizontal menu using unordered list.
This is HTML & CSS code:
.nav_bar{
margin-left: 700px;
margin-top: 40px;
position: absolute;
}
.nav_bar ul{
list-style-type: none;
}
.nav_bar ul li{
display: inline-block;
text-align: center;
float: left;
}
.nav_bar ul li a{
text-decoration: none;
padding: 12px;
margin: 8px;
font-size: 20px;
color: #fff;
}
.nav_bar ul li a::after{
content: '';
display: inline-block;
width: 0px;
height: 4px;
background: #ff6600;
transition: width .4s;
}
.nav_bar ul li a:hover::after{
width: 100%;
}
<div class="nav_bar">
<ul>
<li>Services</li>
<li>Solution</li>
<li>Support</li>
<li>Partners</li>
<li>Contacts</li>
</ul>
</div>
I don't know where is the problem and why it is occurring?
Please help me out with good reason..
Ive fixed your code and ive got something like this.
.nav_bar ul li a{
text-decoration: none;
padding: 12px;
margin: 8px;
font-size: 20px;
color: orange;
position: relative;
}
.nav_bar ul li a::after{
content: '';
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin: auto;
height: 4px;
background: #ff6600;
transition: width .4s;
width: 0;
}
the problem is your after element is displayed inline-block so it rendered inline with your achor tag. and since your achor tag doesnt have enough width to display the ::after inline, your ::after element wrapped after your anchor tag.
see this: https://jsfiddle.net/pm68Lzmv/
Try this:
Add position:absolute; to :after selector for a tag
Remove margin-left from .nav_bar and add right:0
.nav_bar{
margin-top: 40px;
position: absolute;
right:0;
}
.nav_bar ul{
list-style-type: none;
}
.nav_bar ul li{
display: inline-block;
text-align: center;
float: left;
}
.nav_bar ul li a{
text-decoration: none;
padding: 12px;
margin: 0;
font-size: 20px;
color: #aaa;
display:block;
position:relative;
}
.nav_bar ul li a::after{
content: '';
display: inline-block;
width: 0px;
height: 4px;
background: #ff6600;
transition: width .4s;
position:absolute;
left:50%;
bottom:0;
transform: translateX(-50%);
}
.nav_bar ul li a:hover::after{
width: 100%;
}
<div class="nav_bar">
<ul>
<li>Services</li>
<li>Solution</li>
<li>Support</li>
<li>Partners</li>
<li>Contacts</li>
</ul>
</div>
Remove .nav_bar{ margin-left:0px}
Add .nav_bar ul li a position:relative;
add .nav_bar ul li a:after position:absolute;
.nav_bar ul{
list-style-type: none;
}
.nav_bar ul li{
display: inline-block;
text-align: center;
}
.nav_bar ul li a{
text-decoration: none;
padding: 12px;
margin: 0;
font-size: 20px;
color: #aaa;
display:block;
position:relative;
}
.nav_bar ul li a::after{
content: '';
display: inline-block;
width: 0px;
height: 4px;
background: #ff6600;
transition: width .4s;
position:absolute;
left:50%;
bottom:0;
transform: translateX(-50%);
}
.nav_bar ul li a:hover::after{
width: 100%;
}
<div class="nav_bar">
<ul>
<li>Services</li>
<li>Solution</li>
<li>Support</li>
<li>Partners</li>
<li>Contacts</li>
</ul>
</div>
I am trying to make horizontal menu navigation. I have several links in navigation, and I would like to have equal horizontal space between them.
How to make links in horizontal menu with equal space between them?
HTML:
<div id="header">
<div class="secondary-navigation">
<div itemscope itemtype="http://schema.org/SiteNavigationElement">
<nav id="navigation">
<ul id="menu-main" class="menu">
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
</ul>
Menu
</nav>
</div>
</div>
</div>
CSS:
#header {
position: relative;
float: left;
padding: 0 0 0 0;
clear: both;
}
/*-----------------------------------------------
/* Header navigation
/*---------------------------------------------*/
.secondary-navigation {
display: block;
width: 100%;
float: left;
}
.secondary-navigation a {
vertical-align: top;
color: #F1F1F1;
font-weight: bold;
margin-top: 8px;
margin-bottom: 4px;
line-height:18px;
font-size: 15px;
border-bottom: 2px solid #333888;
}
.secondary-navigation a:hover, .secondary-navigation .sfHover {
color: #F1F1F1;
border-bottom: 2px solid #F1F1F1;
}
.secondary-navigation li li a { line-height: 1 }
.secondary-navigation a .sub {
font-size: 12px;
font-weight: normal;
color: #CFCFCF;
text-transform: none;
}
.menu-item-has-children > a:after {
content: "▼";
font-size: 10px;
color: #F1F1F1;
position: absolute;
right: 12px;
top: 22px;
}
.footer-navigation .menu-item-has-children > a:after { display: none }
.sub-menu .menu-item-has-children>a:after {
right: 0;
top: 17px;
}
.menu .current-menu-item > a { background: #fff }
.menu .current-menu-item > a:after {
content: "";
position: absolute;
width: 100%;
height: 1px;
background: #fff;
bottom: 0px;
left: 0;
z-index: 1;
}
#navigation {
margin: 0 auto;
font-size: 13px;
width: 100%;
float: left;
}
#navigation ul {
margin: 0 auto;
list-style: none; /*Added*/}
#navigation .menu { float: left; }
#navigation ul li {
float: left;
position: relative;
margin-left: 0;
}
#navigation > ul li:first-child a { }
#navigation > ul li:last-child a { border-right: 0 }
#navigation ul .header-search { float: right }
#navigation > ul > li:last-child { border-right: none }
#navigation ul li a, #navigation ul li a:link, #navigation ul li a:visited { display: block }
#navigation > ul > .current a {
background: transparent;
color: #555 !important;
}
#navigation li:hover ul, #navigation li li:hover ul, #navigation li li li:hover ul, #navigation li li li li:hover ul {
opacity: 1;
left: -228px;
top: 0;
}
#navigation ul ul {
position: absolute;
width: 226px;
z-index: 400;
font-size: 12px;
color: #333888;
border: 1px solid #F1F1F1;
background: #FFFFFF;
padding: 0;
}
#navigation ul ul li {
margin-left: 0;
padding: 0 10%;
width: 80%;
color: #333;
}
#navigation ul ul li:hover { background: #F1F1F1 }
#navigation ul ul a, #navigation ul ul a:link, #navigation ul ul a:visited {
padding: 12px 0;
position: relative;
border-left: 0;
background: transparent;
border-right: 0;
text-transform: none;
line-height: 1.4;
margin-right: 0;
min-height: 100%;
}
#navigation ul ul li:last-child a { border-bottom: none }
#navigation ul ul {
opacity: 0;
left: -999em;
}
#navigation ul li:hover ul {
left: -1px;
opacity: 1;
top: 81px;
}
#navigation ul ul li:hover ul {
top: -1px;
left: -228px;
padding-top: 0;
}
#navigation ul ul ul:after { border-color: transparent }
I tried something like this, but it does not work for me.
You can use justify-content: space-between or justify-content: space-around flexbox property
ul {
display: flex;
justify-content: space-between;
border: 1px solid black;
list-style-type: none;
padding: 10px;
margin: 0;
}
<ul>
<li>Random Link</li>
<li>Random Link</li>
<li>Random Link</li>
<li>Random Link</li>
</ul>
I have a vertical list of navigation icons, with a sub-menu (under the News tab). I want this submenu to transition from a width 0 to its full width (150px) when I hover over its parent option on the main navigation menu. Currently, the transition is not happening - it is going straight to the full width with no transition.
I pasted all the relevant code (parts of the css that are important for the transition are at the bottom below the marked point).
HTML:
<div id="nav">
<ul>
<li>Home</li>
<li>News
<ul>
<li>Current</li>
<li>Archive</li>
</ul></li>
<li>History</li>
</ul>
</div>
CSS:
#nav {
height: auto;
width: 150px;
float: left;
}
#nav a {
display: block;
padding: 6px 0px;
text-indent: 5px;
color: #ffffff;
font-size: 12px;
text-decoration: none;
border: 3px solid;
border-color: #9c6741 #663918 #572e11 #96643f;
}
#nav ul {
padding: 0px;
margin-top: 0px;
}
#nav li {
list-style: none;
float: left;
width: 150px; /*150-2 for border*/
background-color: #8f5f3c;
}
#nav ul li:hover {
background-color: #7d5334;
}
#nav ul li:hover a {
border-color: #8f5e39 #663918 #572e11 #8a5b38;
}
#nav ul li ul {
display: none;
position: absolute;
margin-top: -33px;
margin-left: 150px;
padding: 0;
}
#nav ul li:hover ul {
display: block;
}
/*code for transition begins here*/
#nav ul li ul li {
float: none;
overflow: hidden;
width: 0px;
transition: width 1s ease-in;
}
#nav ul li:hover ul li {
width: 148px;
}
You can't call an animation on something that is invisible.
https://jsfiddle.net/scLhp54w/1/
#nav ul li ul {
/*display: none; */
position: absolute;
margin-top: -33px;
margin-left: 150px;
padding: 0;
}
I'm having troubles with my horizontal menu-bar list items overlapping.
So the ul ul li of the nav div elements overlap, what can I do to stop that?
The jsfiddle
CSS:
body, html{
padding: 0px;
margin: 0px;
}
.nav {
font-family: 'PT Sans', sans-serif;
width: 100%;
color: #F5F5F5;
background: #1565c0;
display: flex;
height: 40px;
padding: 0;
margin: 0;
border: 0;
}
.nav ul, .nav ul li, .nav{
list-style: none;
margin: 0;
padding: 0;
border-bottom: 3px solid #0d47a1;
}
.nav ul li{
background: #1565c0;
width:auto;
position: relative;
transition: background 250ms ease-in;
line-height: 40px;
display:block;
}
.nav ul li:hover{
background: #0d47a1;
}
.nav ul ul{
overflow:visible;
position: absolute;
visibility: hidden;
top: 100%;
width: 0%;
}
.nav ul ul li{
border:none;
display:block;
position: absolute;
left:0;
margin:0px;
}
.nav ul li:hover>ul{
visibility: visible;
}
.nav a{
text-decoration: none;
color: inherit;
margin: 0px 20px;
}
.nav ul ul li:last-child{
border-bottom: 3px solid #0d47a1;
}
<div class="nav">
<ul>
<li><a>Hi there</a><ul><li><a>Here is the awesomeness</a></li>
<li><a>Awesome</a></li>
</ul>
</div>
fiddle Here are some modification in your code:
.nav ul ul li {
border: medium none;
display: block;
/*left: 0;*/
margin: 0;
/*position: absolute;*/
}
.nav ul ul {
overflow: visible;
position: absolute;
top: 100%;
visibility: hidden;
/*width: 0;*/
}
you can add below css not to wrap your text in 2 lines:
.nav ul ul {
white-space: nowrap;
}
Try like this Demo
CSS:
.nav ul li > ul {
position: relative;
display:none;
}
.nav ul ul li {
border:none;
display:block;
position: relative;
left:0;
margin:0px;
}
.nav ul li:hover>ul {
display:block;
}
body, html {
padding: 0px;
margin: 0px;
}
.nav {
font-family:'PT Sans', sans-serif;
width: 100%;
color: #F5F5F5;
background: #1565c0;
display: flex;
height: 40px;
padding: 0;
margin: 0;
border: 0;
}
.nav ul, .nav ul li, .nav {
list-style: none;
margin: 0;
padding: 0;
border-bottom: 3px solid #0d47a1;
}
.nav ul li {
background: #1565c0;
width:auto;
position: relative;
transition: background 250ms ease-in;
line-height: 40px;
display:block;
}
.nav ul li:hover {
background: #0d47a1;
}
.nav ul li > ul {
position: relative;
display:none;
}
.nav ul ul li {
border:none;
display:block;
position: relative;
left:0;
margin:0px;
}
.nav ul li:hover>ul {
display:block;
}
.nav a {
text-decoration: none;
color: inherit;
margin: 0px 20px;
}
.nav ul ul li:last-child {
border-bottom: 3px solid #0d47a1;
}
<div class="nav">
<ul>
<li><a>Hi there</a>
<ul>
<li><a>Here is the awesomeness</a>
</li>
<li><a>Awesome</a>
</li>
</ul>
</li>
</ul>
</div>