I have a dropdown menu in my website which I want to be exactly in line with the "Resources" tab (which opens up the dropdown menu when I hover over it). I have tried adding margins to the ul inside the "Resources" li, but it isn't changing the styling the way I want it to. Am I entering the CSS in the wrong selector or even using the wrong properties?
Here is my HTML and CSS:
* {
margin: 0;
padding: 0;
}
body {
background-color: orange;
}
nav {
width: 100%;
height: 60px;
background-color: black;
display: flex;
}
nav p {
text-align: center;
font-family: arial;
color: white;
font-size: 24px;
line-height: 55px;
float: left;
padding: 0px 20px;
flex: 1 0 auto;
}
nav ul {
position: absolute;
right: 0;
}
nav ul li {
float: left;
list-style: none;
position: relative; /* we can add absolute position in subcategories */
padding-right: 1em;
}
nav ul li a {
display: block;
font-family: arial;
color: white;
font-size: 14px;
padding: 22px 14px;
text-decoration: none;
}
nav ul li ul {
display: none;
position: absolute;
background-color: black;
padding: 5px; /* Spacing so that hover color does not take up entire chunk */
border-radius: 0px 0px 4px 4px;
}
nav ul li:hover ul {
/* This means when li is hovered, we want the unordered list inside list item to do something. */
display: block;
}
nav ul li ul li{
width: 130px; /* increases width so that all text can be fit */
border-radius: 4px;
}
nav ul li ul li a:hover {
background-color: #ADD8E6;
}
<nav>
<p> The Novel Column </p>
<ul>
<li> Resources
<ul>
<li> Book Reviews </li>
<li> Quotes and Principles </li>
<li> Community Aid </li>
</ul>
</li>
<li> About Us </li>
</ul>
</nav>
Add
nav ul li ul {
left: 50%;
right: auto;
transform: translateX(-50%);
}
left: 50% positions the left edge of the dropdown in the center of its parent. Then translateX(-50%) moves it to the left by half of the dropdown's width. Lastly right: auto ensures that the dropdown's width doesn't get messed up.
Demo:
https://jsfiddle.net/7Lzb8u6t/
Add translateX() CSS property to move your box, you can play with transform value and match your exact location,
for more information 1 rem = 16px;
* {
margin: 0;
padding: 0;
}
body {
background-color: orange;
}
nav {
width: 100%;
height: 60px;
background-color: black;
display: flex;
}
nav p {
text-align: center;
font-family: arial;
color: white;
font-size: 24px;
line-height: 55px;
float: left;
padding: 0px 20px;
flex: 1 0 auto;
}
nav ul {
position: absolute;
right: 0;
}
nav ul li {
float: left;
list-style: none;
position: relative; /* we can add absolute position in subcategories */
padding-right: 1em;
}
nav ul li a {
display: block;
font-family: arial;
color: white;
font-size: 14px;
padding: 22px 14px;
text-decoration: none;
}
nav ul li ul {
display: none;
position: absolute;
background-color: black;
padding: 5px; /* Spacing so that hover color does not take up entire chunk */
border-radius: 0px 0px 4px 4px;
transform: translateX(3rem);
}
nav ul li:hover ul {
/* This means when li is hovered, we want the unordered list inside list item to do something. */
display: block;
}
nav ul li ul li{
width: 130px; /* increases width so that all text can be fit */
border-radius: 4px;
}
nav ul li ul li a:hover {
background-color: #ADD8E6;
}
<nav>
<p> The Novel Column </p>
<ul>
<li> Resources
<ul>
<li> Book Reviews </li>
<li> Quotes and Principles </li>
<li> Community Aid </li>
</ul>
</li>
<li> About Us </li>
</ul>
</nav>
Related
I have a simple navbar and i have few menu items via ul and what I'm trying to do is that whenever i hover over the li a border-bottom appears in center of the text but im having a few problems first is that the border-bottom's width is leaving the text and sometimes it doesn't it cover the full width of the text above it
body{
margin: 0px;
padding: 0px;
background: #2c3e50;
}
.header__middle ul{
list-style-type: none;
display: flex;
}
.header__middle ul li a{
margin-right: 10px;
text-decoration: none;
font-size: 1.2em;
color: black;
text-align: center;
}
.header__middle ul li {
position: relative;
}
.header__middle ul li:hover ::after{
content: "";
position: absolute;
left: 4%;
bottom: -13%;
width: 40px;
border-bottom: 2px solid black;
}
<div class="header__middle">
<ul>
<li >Home</li>
<li>Join</li>
<li>FAQs</li>
<li>About Us</li>
</ul>
</div>
HTML
Here, try this. The only issue I see is that your 40px border is too wide for some items like "Join". (Notes are in the code)
body {
margin: 0px;
padding: 0px;
background: #2c3e50;
}
.header__middle ul{
list-style-type: none;
display: flex;
}
/* Remove your margin here and re-add it as a padding on the parent li */
.header__middle ul li a{
text-decoration: none;
font-size: 1.2em;
color: black;
text-align: center;
}
.header__middle ul li {
position: relative;
padding-right: 10px;
}
/* Change to display block and position relative */
.header__middle ul li:hover ::after{
content: "";
display: block;
position: relative;
left: calc(50% - 20px); /* To center, set your left for 50% minus half of your bar's width */
width: 40px;
border-bottom: 2px solid black;
}
<div class="header__middle">
<ul>
<li >Home</li>
<li>Join</li>
<li>FAQs</li>
<li>About Us</li>
</ul>
</div>
Edit: Sorry, I may have misunderstood. I thought you were wanting your 40px bar to be centered on each object.
If you want your bar to be the same width as each item, just remove the left and width properties from my code above, as shown here:
.header__middle ul li:hover ::after{
content: "";
display: block;
position: relative;
border-bottom: 2px solid black;
}
So I've created menu on my website, but when I resize windows it is going into other divs in same line. I tried various things but I didn't had any luck. My nav for menu is in another div for header with 2 more divs, for left, and right. I also pasted CSS of those stuff down there, they have class .icons and .logo, and main holder with .menuHolder.
Code
#primary_nav_wrap {
margin: 0.85% 0% 0% 26%;
width: 60%;
padding: 10px;
position: absolute;
min-width: 772px;
}
#primary_nav_wrap ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul a {
display: block;
color: #333;
text-decoration: none;
font-weight: 700;
font-size: 12px;
line-height: 32px;
padding: 0 15px;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul li:hover {
background: #ddd;
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0
}
#primary_nav_wrap ul ul li {
float: none;
width: 200px
}
#primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px
}
#primary_nav_wrap ul ul ul {
top: 0;
left: 100%
}
#primary_nav_wrap ul li:hover > ul {
display: block
}
.icons {
float: right;
margin-top: 1.3%;
margin-right: 1.57%;
position: apsolute;
min-width: 53px;
}
.logo {
float: left;
position: apsolute;
/* min-width */
}
.menuHolder {
background-color: #fff;
width: 100%;
text-align: center;
margin: 0px auto;
padding: 0;
border-style: solid;
border-color: #CC0000;
border-width: 1px 0px 2px 0px;
height: 83px;
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Index
</li>
<li>Blahblah
<ul>
<li>Blahblah2
</li>
<li>Blahblah3
</li>
<li>Blahblah4
</li>
</ul>
</li>
<li>Blahblah5
</li>
<li>Blahblah6
</li>
<li>Vaša pitanja i odgovori
<ul>
<li>Blahblah7
</li>
<li>Blahblah8
</li>
</ul>
</li>
<li>Blahblah9
</li>
<li>Blahblah10
</li>
<li>Blahblah11
</li>
</ul>
</nav>
if you want to adjust to the width of the window on window size change you have to use SASS, LESS and bootstarp. when you resize the window all that css needs to be rendered again so you would need to literally write a compiler for css and part of the bootstrap manually! therefore using bootstrap and media query would be easier and they are easy to learn.
It also adds value to you as a programmer.
Here's a nav I use on my web page. I set up a side bar to place my menu's in. It stays the same size whenever I resize my page. It might be a starting place for you.
nav {
position: fixed;
left: 0;
top: 0;
bottom: 0;
background: #E5E4E2;
width:275px;
padding: 10px;
}
As per title, I have got the code almost working I think, but due to having limited knowledge on CSS, I am making stupid mistakes/assumptions. Any help with explanation would be much appreciated.
The fiddle link is here
ul {
padding: 0;
list-style: none;
background: #ffffff;
}
ul li {
color: #0000ff;
display: inline-block;
position: relative;
line-height: 21px;
text-align: left;
}
ul li a {
display: inline-block;
padding: 8px 25px;
background-color: #FFFFFF;
text-decoration: none;
width: 140px;
}
ul li a:hover {
background-color: #FFFFFF;
}
ul li ul.dropdown {
min-width: 125px;
/* Set width of the dropdown */
background: #f2f2f2;
display: none;
position: absolute;
z-index: 999;
left: 500;
}
ul li:hover ul.dropdown {
border: 2px solid #0000ff;
display: block;
padding-left: 50px;
background-color: #ffffff;
}
ul li:hover a:hover ul.dropdown {}
<div id="mylinks">
<ul id="mylists" href="#">
<li>MY LIST MENU
<ul class="dropdown">
<li><span id="level1" onclick="location.href='http://www.google.co.uk/'" title="Go Google"> First item<span>
</li>
<li> Second item
</li>
</ul>
</li>
</ul>
</div>
You need to set a correct position for the dropdown list:
ul li ul.dropdown {
min-width: 125px;
background: #f2f2f2;
display: none;
position: absolute;
z-index: 999;
left: 150px;
top: 0;
}
Notice that left was set to 150px and top was set to 0.
I'm using a fairly typical nested UL setup to create a dropdown menu, however I can't get the anchorlinks inside the li to expand to their height.
The HTML
<div id="navbar-container">
<ul id="navbar">
<li>Home</li>
<li>Lessons</li>
<ul>
<li>sub item1sdfsdfsdfsdfsdf</li>
<li>sub item2</li>
<li>sub item3</li>
</ul>
</li>
<li>Custom Fitting</li>
</ul>
</div>
In the CSS I'm using display:block on the anchor tags which does make them expand to the width of the li but not the height. I have tried using padding but it does not work correctly across all browsers. #navbar is using display: table and the children lis are using display: table-cell. This is so the navbar can expand and contract to fit the screen size. I suspect display: table-cell may have something to do with the anchors not expand vertically.
Here is a JSFiddle so you can see what I'm talking about.
The CSS
#navbar-container {
min-width: 768px;
height: 32px;
position: relative;
background-color: #bb4212;
}
#navbar {
list-style-type: none;
display: table;
width: 100%;
font : 14px"Arial", sans-serif;
height: 100%;
}
#navbar li {
text-transform:uppercase;
display: table-cell;
text-align: center;
}
#navbar li a {
color: #f2f2f2;
display: block;
border-left: 1px solid #c17455;
}
#navbar > li:first-child a {
border: 0;
}
#navbar li ul {
list-style-type: none;
background-color: #f2f2f2;
position: absolute;
right: -9999px;
top: 32px;
margin-left: 1px;
-moz-box-shadow: 0px 6px 4px 0px #898989;
-webkit-box-shadow: 0px 6px 4px 0px #898989;
box-shadow: 0px 6px 4px 0px #898989;
}
#navbar li ul li:hover {
background-color: #bb4212;
}
#navbar li ul a:hover {
color: #f2f2f2;
}
#navbar li:hover {
background-color: #f2f2f2;
}
#navbar li:hover a {
color: #000;
}
#navbar li:hover ul {
right: auto;
}
#navbar li ul li {
display: block;
text-align: left;
}
#navbar li ul li a {
border: 0;
white-space:nowrap;
margin: 0 5px;
text-decoration: none;
display: block;
}
My favorite technique for filling up a parent container 100% width and height is to use absolute positioning:
parent {
position: relative; /* unless it's already positioned */
}
child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Here it is applied to your JSFiddle: http://jsfiddle.net/Kgz5p/
In my set up I have my navigation bar set horizontally and contained within my header div like this:
<div id="header-section">
<div id="main-menu-wrapper">
<ul id="main-menu">
<li>Home</li>
<li>Services
<ul id="sub-men">
<li>Service 1</li>
<li>Service 2</li>
<li>Service 3</li>
</ul>
</li>
</ul>
<div class="clear"></div>
</div>
</div>
My problem is that the sub-menu is not showing because the height on "main-menu-wrapper" is set to auto. The sub-menu is showing when I set a height like 100px. When I set the position on the sub-menu to static instead of absolute, it expands the entire main-menu-wrapper. How can I get the sub-menu to show properly?
Here's the CSS portion for my whole header section:
#header-section {
position: relative;
width: 100%;
padding: 5px 0px;
background: #740600;
}
#main-menu-wrapper {
position: relative;
width: 74%;
min-width: 600px;
height: auto;
margin: 0% auto;
}
#main-menu {
list-style: none;
font-weight: bold;
line-height: 150%;
}
#main-menu li {
position: relative;
float: right;
margin: 0px 5px;
}
#main-menu a {
padding: 3px;
color: #ffffff;
background: #740600;
text-decoration: none;
border-radius: 5px;
}
#main-menu a:hover {
padding: 3px;
color: #740600;
background: #ffffff;
text-decoration: none;
}
#main-menu li ul {
position: absolute;
display: none;
}
#main-menu li ul li{
float: none;
}
#main-menu li:hover ul {
display: block;
}
#main-menu li ul a {
padding: 3px;
color: #ccc;
background: #740600;
text-decoration: none;
border-radius: 5px;
}
#main-menu li ul a:hover {
padding: 3px;
color: #740600;
background: #ccc;
text-decoration: none;
}
#banner-wrapper {
position: relative;
padding: 5px 0 5px;
}
#banner {
position: relative;
max-width: 75%;
min-width: 600px;
margin: 0% auto;
background: #ffffff;
}
#logo {
max-width: 600px;
height: auto;
}
I'm a little confused by what you're asking here, but I created a fiddle where your menu shows.
I deleted the styles for #main-menu-wrapper and I removed the background color on #header-section.
Hopefully this can be a decent starting point for you: http://jsfiddle.net/44vRN/
#header-section {
position: relative;
width: 100%;
padding: 5px 0px;
}
You could try to use absolute positioning on the submenu to remove it from the document flow.