I have a nav menu that has different size cells for each item. Under each item I want a triangle image to indicate the active menu item.
How can I make the :after reflect the same size as the parent so that I can have the content of the :after centered properly?
Code for the menu:
<div class="nav-collapse collapse">
<ul class="nav nav-pills">
<li>Clients</li>
<li>Solutions</li>
<li>Services</li>
<li>Your Business</li>
<li>Security</li>
<li>Partners</li>
<li>Contact</li>
</ul>
</div>
The way here is use absolute position on the :after element and use the li or a tag parent as relative. Like this:
.nav > li a {
position: relative;
}
.nav > li a:after {
content: "\25B2";
position: absolute;
left: 50%;
transform: translate(-50%, -100%);
}
Check The Snippet
* {
margin: 0;
padding: 0
}
.nav > li {
height: 70px;
line-height: 70px;
background: orange;
list-style: none;
margin: 1px;
float: left;
}
.nav > li a {
position: relative;
padding: 0 15px;
display: block;
color: white;
font-family: 'verdana';
text-decoration: none;
transition: background .3s ease
}
.nav > li a:hover {
background: red;
}
.nav > li a:after {
content: "\25B2";
position: absolute;
color: white;
top: 100%;
line-height: normal;
left: 50%;
transform: translate(-50%, -100%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="nav-collapse collapse">
<ul class="nav nav-pills">
<li>Clients
</li>
<li>Solutions
</li>
<li>Services
</li>
<li>Your Business
</li>
<li>Security
</li>
<li>Partners
</li>
<li>Contact
</li>
</ul>
</div>
Try this css, include text in the content: " " and set the color to transparent via rgba. then set background to an image
.nav-pills {
width: 100%;
display:block;
list-style:none;
}
.nav-pills li{
display:inline;
float:left;
margin: 0 20px;
padding:4px 10px
background:#d7d7d7;
text-align:center;
}
.nav-pills li::after {
content: " Image";
padding: 4px 10px;
background:url(images/image.png);
background-color:orange;
color: rgba(255,255,255,0);
display:block;
}
Check out my codepen here
Related
Hi I can't seem to be removing the background color after shifting elements. While the elements have moved and they are working perfectly but every timeenter image description here I am clicking on the sign in and sign up, the original space seems to show up as the background color.
HTML code:
<nav>
<ul>
<li><a class="active" href="#"><i class="fa fa-home" aria-hidden="true"></i>Home</a></li>
<li>Blog</li>
<li>Categories
<ul>
<li>Romance</li>
<li>Paranormal</li>
<li>Thriller</li>
<li>Horror</li>
</ul>
</li>
<li>Reviews
<ul>
<li>Star Rating 1</li>
<li>Star Rating 2</li>
<li>Star Rating 3</li>
<li>Star Rating 4</li>
<li>Star Rating 5</li>
</ul>
</li>
<li>Contest
<ul>
<li>Giveaways</li>
<li>Free Books for Reviews</li>
</ul>
</li>
<li>Contact Us
<ul>
<li>For Authors</li>
<li>For Bloggers</li>
</ul>
</li>
<li><i class="fa fa-user-plus" aria-hidden="true"></i>Sign Up</li>
<li><i class="fa fa-fw fa-user"></i>Sign In</li>
</ul>
</nav>
CSS code:
/* navigation top menu */
nav{
position: relative;
top:0;
left:0;
padding: 5px 0 0 0;
width: 100%;
font-size: 1em;
}
.active
{
background-color: #F24F1B;
color: white;
}
nav::after{
content:'';
display: block;
clear:both;
}
nav ul{
list-style: none;
margin:0;
padding:0;
position: relative;
right: 200px;
}
nav ul li:hover {
background-color: #F24F1B;
}
nav ul li:hover > ul {
display: block;
}
nav ul li a {
display: inline-block;
color:black;
padding: 10px 20px;
text-decoration: none;
width: 125px;
position: relative;
}
nav ul li a:visited {
color:black;
}
nav ul li a .active{
color: white;
}
nav ul li a:hover {
background-color: #F24F1B;
color: white;
}
nav ul ul {
position: relative;
top: 100%;
background-color: #F24F1B;
display: none;
position: relative;
right: 0px;
}
nav ul ul li {
position: relative;
border-bottom: 2px solid rgb(128, 104, 104);
}
nav ul ul ul {
left: 100%;
top: 0px;
}
/* top level */
nav>ul {
padding-left: 200px;
color:white;
}
nav>ul>li {
float: left;
}
nav>ul>li>a {
width: auto;
padding: 10px 20px 15px 20px;
}
.signed {
position: relative;
left: 300px;
}
Your issue is because your background-color is on the li and not on a.signed but you are moving your a.signed with left: 300px; and not the li.
If you put the class .signed to the list item instead of the link, it will work as you'd expected.
Here's the code with the fix: https://codepen.io/MateoStabio/pen/rNWeYXd
I do recommend looking into restructuring your navigation entirely so that the right nav doesn't jump when you hover items on the left navigation.
.signed {
background-color: transparent;
position: relative;
left: 300px;
}
Try This one.
I am trying to make a hoverable dropdown menu and the items of the dropdown are overlapped. I don't know how the CSS should be, but I tried to modify each class, but still doesn't work.
I also tried to modify the display of links, but that doesn't work. Here is the code I made:
<style>
#menu
{
margin:0;
font-size: 30px;
border-bottom: 1px solid;
text-align: center;
}
#menu a
{
color:#900;
text-decoration:none;
}
#menu .subitem a{
display: block;
padding: 12px 16px;
z-index: 1;
background-color: #f1f1f1;
border-bottom: 1px solid;
}
#menu a:hover
{
text-decoration:underline;
}
.item{
position: relative;
display: inline-block;
border-right: 0.5px solid;
padding-right: 30px;
padding-left: 30px;
}
.subitem{
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
left: 0;
z-index: 1;
}
#menu .item:hover .subitem{
display: block;
}
</style>
<div id="navWrapper">
<ul id="menu">
<li class="item">Small Things
<ul class="submenu">
<li class="subitem">Gnomes</li>
<li class="subitem">Fairies</li>
<li class="subitem">Elves</li>
<li class="subitem">Leprechauns</li>
</ul>
</li>
<li class="item">Big Things
<ul class="submenu">
<li class="subitem">Loch Ness Monster</li>
<li class="subitem">Ogres</li>
<li class="subitem">Giants</li>
<li class="subitem">Dragons</li>
</ul>
</li>
</ul>
</div>
I want to display properly each item when I hover with the mouse like it is in this
image.
You confused your .submenu with .subitem:
#menu
{
margin:0;
font-size: 30px;
border-bottom: 1px solid;
text-align: center;
}
#menu a
{
color:#900;
text-decoration:none;
}
#menu .subitem a{
display: block;
padding: 12px 16px;
z-index: 1;
background-color: #f1f1f1;
border-bottom: 1px solid;
}
#menu a:hover
{
text-decoration:underline;
}
.item{
position: relative;
display: inline-block;
border-right: 0.5px solid;
padding-right: 30px;
padding-left: 30px;
}
.subitem {
/* display: none; <- get rid of that */
/* position: absolute; <- get rid of that, otherwise all your subitems will be in the top left corner */
background-color: #f1f1f1;
min-width: 160px;
left: 0;
z-index: 1;
}
.submenu {
position: absolute; /* <- put it here */
display: none; /* <- put it here */
list-style-type: none;
left:-40px;
top: 30px;
}
#menu .item:hover .submenu {
display: block;
}
<div id="navWrapper">
<ul id="menu">
<li class="item">Small Things
<ul class="submenu">
<li class="subitem">Gnomes</li>
<li class="subitem">Fairies</li>
<li class="subitem">Elves</li>
<li class="subitem">Leprechauns</li>
</ul>
</li>
<li class="item">Big Things
<ul class="submenu">
<li class="subitem">Loch Ness Monster</li>
<li class="subitem">Ogres</li>
<li class="subitem">Giants</li>
<li class="subitem">Dragons</li>
</ul>
</li>
</ul>
</div>
I'm trying to create a basic drop down menu. I'm having issues with the alignment. How can I get sub menu items to be directly underneath one another? For example, I am trying to get twitter, instagram, and facebook underneath "Social".
Thanks.
https://jsfiddle.net/xalxnder/ostaqgyk/
HTML
<body>
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
</body>
CSS
body {
background: #000000;
color: white;
}
.main-nav {
float: left;
}
.main-nav li {
font-size: 10px;
display: inline;
padding-right: 10px;
}
.main-nav .sub {
color: pink;
float: none;
z-index: -200;
}
//** .sub{display: none;
}
.dropdown:hover > .sub {
display: block;
position: absolute;
}
Here's the basics of it.
JSFiddle
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: black;
}
ul {
list-style-type: none;
color: white;
}
.main-nav >li {
display: inline-block;
position: relative;
padding-right: 10px;
}
.sub {
position: absolute;
top: 100%;
left: 0;
color: pink;
/* display:none */ /* disabled for demo */
}
.dropdown:hover .sub {
display:block;
}
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
Since block-level elements take up their own line, this rule should hold you:
.sub li {
display: block;
}
Add to .main-nav .sub:
position: absolute;
padding: 0;
body {
background: #000000;
color: white;
}
.main-nav {
float: left;
}
.main-nav li {
font-size: 10px;
display: inline-block;
padding-right: 10px;
}
.main-nav .sub {
position: absolute;
padding: 0;
color: pink;
float: none;
z-index: -200;
}
.sub li {
display: block;
}
<body>
<ul class="main-nav">
<li>Home</li>
<li>Projects</li>
<li class="dropdown">
Social
<ul class="sub">
<li>Twitter</li>
<li>Facebook</li>
<li>Instagram</li>
</ul>
</li>
</ul>
</body>
I have this class:
.top-level-menu {
list-style: none;
padding: 0;
margin-bottom: 12px;
width: 100%;
}
...which is applied to this:
<template name="mnuScheduler">
<ul class="top-level-menu">
...yet the menu bar (comprised of uls and lis) sits right on top of the element below it (an HTML Table), and a Template that's loaded dynamically lines up to its right.
Based on the CSS, there should be some space between the menu bar and the table, and the Template should display below the menu bar, as the HTML Table does before it's displaced (using Meteor's Template.dynamic) by the other Template, which for now is just a placeholder.
Here's what it looks like:
So why is an element which should be 100% in width not, and which should have a margin along the bottom doesn't?
UPDATE
Here is the pertinent CSS and HTML (this is from a Meteor app, so the HTML has Spacebars code (template language) mixed in).
CSS:
/* Menu-specific styles/rules */
.third-level-menu {
position: absolute;
top: 0;
right: -150px;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.third-level-menu > li {
height: 30px;
background: #999999;
}
.third-level-menu > li:hover {
background: #CCCCCC;
}
.second-level-menu {
position: absolute;
top: 30px;
left: 0;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.second-level-menu > li {
position: relative;
height: 30px;
background: navy;
}
.second-level-menu > li:hover {
background: #CCCCCC;
}
.top-level-menu {
list-style: none;
padding: 0;
position: absolute;
top: 0;
width: 60%;
}
.top-level-menu > li {
position: relative;
float: left;
height: 30px;
//width: 150px;
width: 20%;
background: black;
}
.top-level-menu > li:hover {
background: #CCCCCC;
}
.top-level-menu li:hover > ul {
/* On hover, display the next level's menu */
display: inline;
}
/* Menu Link Styles */
.top-level-menu a
/* Apply to all links inside the multi-level menu */
{
font: bold 16px Candara, Calibri, 'Segoe UI', serif;
color: #FFFFFF;
text-decoration: none;
padding: 0 0 0 10px;
/* Make the link cover the entire list item-container */
display: block;
line-height: 30px;
margin-bottom: 12px;
}
.top-level-menu a:hover {
color: #000000;
}
/* End of Menu-specific Styles */
HTML:
<head>
<TITLE>Crew Scheduler</TITLE>
</head>
<body TEXT="#000000">
<div class="container">
{{> mnuScheduler}}
{{> Template.dynamic template=currentTemplate}}
</div>
</body>
<template name="mnuScheduler">
<ul class="top-level-menu">
<li> Schedules
<ul class="second-level-menu">
<li name="mniOpenExisting" id="mniOpenExisting">Open Existing</li>
<li>Create New...
<ul class="third-level-menu">
<li name="mniCreateNewScheduleBasedOnExisting" id="mniCreateNewScheduleBasedOnExisting">Based on Existing</li>
<li name="mniCreateNewScheduleFromScratch" id="mniCreateNewScheduleFromScratch">From Scratch</li>
</ul>
</li>
<li name="mniSaveCurrentSchedule" id="mniSaveCurrentSchedule">Save Current</li>
<li name="mniEmailCurrentSchedule" id="mniEmailCurrentSchedule">Email Current</li>
<li name="mniPrintCurrentSchedule" id="mniPrintCurrentSchedule">Print Current</li>
</ul>
</li>
<li>Job/Locations
<ul class="second-level-menu">
<li name="mniAddNewJobLoc" id="mniAddNewJobLoc">Add New</li>
<li name="mniViewOrEditJobLoc" id="mniViewOrEditJobLoc">View or Edit</li>
</ul>
</li>
<li>Workers
<ul class="second-level-menu">
<li name="mniAddNewWorker" id="mniAddNewWorker">Add New</li>
<li name="mniViewOrEditWorker" id="mniViewOrEditWorker">View or Edit</li>
<li name="mniWorkerPreferences" id="mniWorkerPreferences">Preferences</li>
</ul>
</li>
<li>Rules
<ul class="second-level-menu">
<li name="mniSetRules" id="mniSetRules">Establish/Maintain</li>
</ul>
</li>
<li>Help
<ul class="second-level-menu">
<li name="mniAbout" id="mniAbout">About</li>
<li name="mniHowTo" id="mniHowTo">How To...</li>
<li name="mniContact" id="mniContact">Contact Us</li>
<li name="mniAcquireLicense" id="mniAcquireLicense">Acquire License</li>
</ul>
</li>
</ul>
</template>
.top-level-menu{
width:100%;
display:block;
}
you modify css class .top-level-menu
Width or height in % is usually in relation to the parent element. In this case name="mnuScheduler" probably has a width that is less than 100% of the viewport width which is why the child also has a width less than 100% of the viewport width.
You could potentially solve this issue by using width: 100vw; on .top-level-menu.
That said a closer look at the markup and styling and maybe a fiddle wouldn't hurt.
I've been trying to work on this with no success, for some reason the sub list is just not showing up! I've tried: nav > ul > li:hover > ul{} but that seems to break functionality of the code. I'm sure this is a pretty simple issue I'm having.
nav > ul {
list-style: none;
}
nav > ul > li {
padding: 20px;
float: left;
}
nav > ul > li {
background-color: #fff;
}
nav > ul > ul {
display: none;
position: absolute;
top: 100%;
left: 0;
padding: 0;
}
nav > ul > ul > li {
float: none;
width: 200px;
}
nav > ul > li:hover {
color: #4169E1;
display: block;
visibility: visible;
}
body {
background: black;
}
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Secure</li>
<ul>
<li>How secure are we?</li>
<li>We are not secure enough!!</li>
</ul>
<li>Mad</li>
</ul>
</nav>
Simplify your selectors (nav ul ul) is fine
Make the parent li's position: relative so that the position: absolute dropdowns are positioned in relation to them. Use an appropriate top value
In your example, visibility: visible is not doing anything. display: none and display: block are used to hide / show
Nest your lists properly. This is the correct way:
<ul>
<li>Top Menu Item
<ul>
<li>Sub-menu Item</li>
</ul>
</li>
</ul>
Read more: Nested lists on w3.org
CSS / HTML / Demo
* {
margin: 0;
padding: 0;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
padding: 20px;
float: left;
background-color: #fff;
position: relative;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
padding: 0;
}
nav ul ul li {
width: 200px;
background: #FFF;
padding: 10px;
}
nav ul li:hover ul {
color: #4169E1;
display: block;
}
body {
background: black;
}
<nav>
<ul>
<li>Home</li>
<li>About Us
<ul>
<li>This is us!</li>
<li>This is us!</li>
<li>This is us!</li>
<li>This is us!</li>
</ul>
</li>
<li>Secure
<ul>
<li>How secure are we?</li>
<li>We are not secure enough!!</li>
</ul>
</li>
<li>Mad</li>
</ul>
</nav>
To Point out one of the Mistakes you have
<ul>
<li>Home</li>
<li>About Us</li>
<li>Secure
<ul> **--> this should be inside li**
<li>How secure are we?</li>
<li>We are not secure enough!!</li>
</ul>
</li>
<li>Mad</li>
</ul>
and your css
add this
nav > ul > li:hover > ul{
display: block;
opacity: 1;
visibility: visible;
}
check the fiddle
http://jsfiddle.net/ruchan/4fk6y2wu/
Use some more css3 power!
See Demo here
See Fullscreen
<nav>
<ul id="menu">
<li class="category top_level"><a class="selected" href="#">Home</a></li>
<li class="category top_level">About</li>
<li class="category top_level">Secure
<ul class="dropdown">
<li class="item">How secure are we?</li>
<li class="item">We are not secure enough!!</li>
</ul>
</li>
<li class="category top_level last">Mad
</li>
</ul>
</nav>
css
body {
font-family:'Montserrat', sans-serif;
background:#000;
}
ul {
list-style-type: none;
}
#menu a {
text-decoration: none;
white-space: nowrap;
color: #222;
background-color: #fff;
}
#menu li.top_level {
vertical-align: top;
zoom: 1;
display: block;
float: left;
width: 16.5%;
margin-right: 0.2%;
position: relative;
text-align:center;
}
#menu li.last {
margin-right: 0px;
}
#menu .dropdown {
float: none;
z-index: 100;
position: absolute;
width: 100%;
height: 0;
overflow: hidden;
}
#menu .category:hover .dropdown, #menu .category:focus .dropdown {
height:auto;
}
#menu .item a, #menu .category a, #menu .category a:visited, #menu .item a:visited {
line-height:2em;
display:block;
padding:.6em;
border-top: #ffffff 2px solid;
}
#menu .item a:hover, #menu .category a:hover, #menu .item a:focus, #menu .category a:focus {
background:#007dac;
-webkit-transition: background-color 940ms;
-moz-transition: background-color 940ms;
}
#menu a.selected {
color: #ffffff;
background-color: #007dac;
}