Hello I am trying to create menu with absolute position CSS rule for each list item. display: inline-block for parent doesn't work. Here's an example of markup.
.menu-items{
}
.main-menu{
position: relative;
list-style:none;
}
.main-menu li {
position: absolute;
float:left;
}
.main-menu li a{
font-size: 12px;
color: rgba(1, 1, 1, 1);
}
.main-menu li a:hover{
font-size: 21px;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can use transform:scal(x); , it won't disturb the layout.
.menu-items {} .main-menu {
list-style: none;
}
.main-menu li {
display: inline-block;/*modified*/
padding-left: 10px;/* moved here*/
}
.main-menu li a {
font-size: 12px;
color: rgba(1, 1, 1, 1);
display: inline-block;/*added*/
background:white;/*added*/
transition: 0.25s;/*added*/
}
.main-menu li a:hover {
transform: scale(1.75);/*modified*/
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index
</li>
<li>shop
</li>
<li>about
</li>
<li>projects
</li>
<li>contacts
</li>
</ul>
</div>
</div>
.menu-items {}
.main-menu {
position: relative;
list-style: none;
}
.main-menu li {
position: relative;
float: left;
width: 100px;
}
.main-menu li a {
font-size: 12px;
padding-left: 10px;
color: rgba(1, 1, 1, 1);
}
.main-menu li a:hover {
font-size: 21px;
overflow: hidden;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can use position:absolute, too, but on a elements:
.menu-items{
}
.main-menu{
list-style:none;
}
.main-menu li {
position:relative;
display:inline-block;
padding:30px;
}
.main-menu a {
font-size: 12px;
color: rgba(1, 1, 1, 1);
position:absolute;
width:100%;
height:100%;
left:0;
top:0;
}
.main-menu a:hover{
font-size: 21px;
color: rgba(1, 1, 1, 1);
}
<div class="row">
<div class="menu-items">
<ul class="main-menu">
<li>index</li>
<li>shop</li>
<li>about</li>
<li>projects</li>
<li>contacts</li>
</ul>
</div>
</div>
You can hardcode a "left: XXXpx" for each li but then you will have issues based on screen sizes. If you really want that for some reason though let me know and I will send an example of that.
Another option is to use a flex box (assuming you do not need this to work with older versions of IE) like below. The spacing between items will change, but it will be responsive to the size of the text and screen , versus just pushing everything to the right.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
padding: 5px;
margin-top: 10px;
color: white;
text-align: center;
font-size: 12px;
color: rgba(1, 1, 1, 1);
}
.flex-item:hover {
font-size: 21px;
}
<div class="row">
<div class="menu-items">
<ul class="flex-container">
<li class="flex-item">index</li>
<li class="flex-item">shop</li>
<li class="flex-item">about</li>
<li class="flex-item">projects</li>
<li class="flex-item">contacts</li>
</ul>
</div>
</div>
Related
Hi, if you look at the above screenshot you can see I have added in code to make a small bar transition across when hovering over items in the navigation bar.
However, this bar also appears when I hover over items in the drop down menu. For example, when hovering over "Stock 1" the bar appears and obviously looks awful.
How can I make it so that this bar only appears on the items in the main menu and NOT in the drop down menu's.
Please see HTML code below:
<header>
<div class="container-fluid">
<div class="logo">
<img src="images/japan_flag.gif" alt="" height="31" width="41">
</div>
<div class="container">
<nav role="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>About Us</li>
<li>Stock
<ul class="dropdown">
<li>Stock 1</li>
<li>Stock 2</li>
</ul>
</li>
<li>Enquiries</li>
<li>Contacts</li>
</ul>
</nav>
</div>
<div id="mobile-menu-wrap"></div>
</div>
</header>
Please see CSS below:
nav a {
text-decoration: none;
text-transform: uppercase;
font-family: "Oswald", sans-serif;
}
nav {
font-family: monospace;
}
nav ul {
list-style: none;
margin: 0;
margin-top: 30px;
padding-left: 45%;
}
nav ul li {
color: #fff;
display: block;
float: left;
padding: 1rem;
position: relative;
text-decoration: none;
transition-duration: 0.5s;
}
nav ul li a {
color: #fff;
}
nav ul li a:hover {
cursor: pointer;
color: rgb(199, 50, 13);
transition-duration: 0.3s;
}
nav ul li ul {
visibility: hidden;
background: rgb(199, 50, 13);
opacity: 0;
min-width: 100%;
position: absolute;
transition: all 0.5s ease;
margin-top: 1rem;
display: block;
min-width: 100%;
white-space: nowrap;
z-index: 100;
padding: 9% 9%;
text-align: left;
line-height: 10px;
padding-top: 8px;
}
nav ul li:hover > ul,
nav ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
nav ul li ul li a:hover {
color: black;
}
nav ul li ul li {
clear: both;
width: 100%;
}
nav ul li a::after {
content: '';
display: block;
height: 5px;
background-color: rgb(199, 50, 13);
position: relative;
bottom: 0;
width: 0%;
transition: all ease-in-out 250ms;
}
nav ul li a:hover::after {
width:100%;
}
Any help with this would be hugely appreciated as I have tried for ages to research and figure this out. I am very new to HTML, CSS and any sort of coding in general so apologies.
You can give class to <a> on which you want to apply style..
You can do like this.
<header>
<div class="container-fluid">
<div class="logo">
<img src="images/japan_flag.gif" alt="" height="31" width="41">
</div>
<div class="container">
<nav role="navigation">
<ul class="mainmenu">
<li><a class="abc" href="#">Home</a></li>
<li><a class="abc" href="#">About Us</a></li>
<li><a class="abc" href="#">Stock</a>
<ul class="dropdown">
<li>Stock 1</li>
<li>Stock 2</li>
</ul>
</li>
<li>Enquiries</li>
<li>Contacts</li>
</ul>
</nav>
</div>
<div id="mobile-menu-wrap"></div>
</div>
And in css file you can do like this:
.abc:hover{
cursor: pointer;
color: rgb(199, 50, 13);
transition-duration: 0.3s;
}
I want my nav bar to have the navigation buttons on the left & my signup/login to the right. I've managed to have a main nav bar, and a separated navbar for the login/signup, but that messes with my code.
I've tried to do something with the ':last-child' but it doesn't seem to work...
.nav-wrapper ul {
float: left;
padding-left: 50px;
}
.nav-wrapper ul li {
display: inline-block;
}
.nav-wrapper ul li:not(:first-child) {
margin-left: 36px;
}
.nav-wrapper ul li:last-child {
margin-right: 18px;
float: right;
}
.nav-wrapper ul li a {
display: inline-block;
outline: none;
color: #333333;
text-transform: uppercase;
text-decoration: none;
font-size: 14px;
letter-spacing: 1.24px;
font-family: 'Montserrat', sans-serif;
font-weight: 800;
font-style: normal;
}
<nav class="nav-bar">
<input type="checkbox" id="nav" class="hidden">
<label for="nav" class="nav-btn">
<i></i>
<i></i>
<i></i>
</label>
<div class="logo">
"logo"
</div>
<div class="nav-wrapper">
<ul class="list">
<li>Home</li>
<li>Collections</li>
<li>Contact</li>
<li>Log In</li>
<li>Sign Up</li>
</ul>
</div>
</nav>
in my opinion you can have a header element (the topbar), with two nav inside, each with its own alignment
<header class="topbar">
<div class="logo">
My logo
</div>
<nav class="navigation">
<ul>
<li>Home</li>
<li>Collections</li>
<li>Contact</li>
</ul>
</nav>
<nav class="user">
<ul>
<li>Log In</li>
<li>Sign Up</li>
</ul>
</nav>
</header>
and then
header.topbar {
position: relative;
display: block;
float: left;
width: 100%;
}
.logo {
position: relative;
display: block;
float: left;
}
nav.navigation {
position: relative;
display: block;
float: left;
}
nav.user {
position: relative;
display: block;
float: right;
}
nav ul {
position: relative;
display: block;
float: left;
list-style: none;
}
nav ul li {
position: relative;
display: block;
float: left;
margin: 0px 12px;
}
You can use flexbox on your ul element and move its last child element to the right by applying margin-left: auto to it (no floats needed in this case):
.nav-wrapper ul {
display: flex;
padding-left: 50px;
list-style: none;
}
.nav-wrapper ul li:not(:first-child) {
margin-left: 36px;
}
.nav-wrapper ul li:last-child {
margin-right: 18px;
margin-left: auto;
}
.nav-wrapper ul li a {
display: inline-block;
outline: none;
color: #333333;
text-transform: uppercase;
text-decoration: none;
font-size: 14px;
letter-spacing: 1.24px;
font-family: 'Montserrat', sans-serif;
font-weight: 800;
font-style: normal;
}
<nav class="nav-bar">
<input type="checkbox" id="nav" class="hidden">
<label for="nav" class="nav-btn">
<i></i>
<i></i>
<i></i>
</label>
<div class="logo">
"logo"
</div>
<div class="nav-wrapper">
<ul class="list">
<li>Home</li>
<li>Collections</li>
<li>Contact</li>
<li>Log In</li>
<li>Sign Up</li>'
</ul>
</div>
</nav>
Regarding your code, it will work if you remove float: left; from .nav-wrapper ul.
In my opinion, it is more intuitive to use CSS grid, and divide your ul into two logical units (main ul for the left side, and secondary ul for the right side):
.nav-wrapper {
display: grid;
grid-template-columns: 1fr max-content;
}
.nav-wrapper ul.list {
display: flex;
list-style-type: none;
padding: 0;
}
.nav-wrapper ul li:not(:first-child) {
margin-left: 36px;
}
.nav-wrapper ul li a {
display: inline-block;
outline: none;
color: #333333;
text-transform: uppercase;
text-decoration: none;
font-size: 14px;
letter-spacing: 1.24px;
font-family: 'Montserrat', sans-serif;
font-weight: 800;
font-style: normal;
}
<div class="nav-wrapper">
<ul class="list list-main">
<li>Home</li>
<li>Collections</li>
<li>Contact</li>
</ul>
<ul class="list list-secondary">
<li>Log In</li>
<li>Sign Up</li>
</ul>
</div>
I have been checking on other questions related to mine, and most of the time "text-align:center" is suggested. However, even if I implement it to my code, the navbar still doesn't center... Here's my code:
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
float:left;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.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;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
Thanks for any suggestions in advance!
remove float:left on your navli class ang set it to display:inline-block and add text-align:center on your #nav, and to center your #navigation add margin:0 auto;
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
margin : 0 auto;
}
#nav {
width: auto;
text-align : center;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
.navli {
/* float:left;*/
display : inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.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;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</div>
You've used float:left on the li, which aligns them to left. I've removed it and added text-align:center to the ul
Is this what you're looking for?
/* NAVIGATION BAR */
#navigation {
width:1300px;
height:auto;
}
#nav {
width: auto;
}
.ulnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align:center;
}
.navli {
display:inline-block;
}
.navli a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 25px;
text-decoration: none;
}
.navli a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.navli.dropdown {
display: inline-block;
}
.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;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display:block;
}
.active {
background-color:green;
}
<div id="navigation">
<div id="nav">
<ul class="ulnav">
<li class="navli"><a class="active" href="#home">Home</a></li>
<li class="navli">Link</li>
<li class="dropdown navli">
Dropdown
<div class="dropdown-content">
Droplink 1
Droplink 2
</div>
</li>
<li class="dropdown navli">
Dropdown 2
<div class="dropdown-content">
Droplink 2.1
Droplink 2.2
Droplink 2.3
</div>
</li>
<li class="dropdown navli">
Dropdown 3
<div class="dropdown-content">
Droplink3.1
Droplink3.2
Droplink3.3
</div>
</li>
<li class="navli">Link 2</li>
</ul>
</div>
</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>
Screenshot of the header that's needing this work:
As you can see, the menu is below the logo. I was wanting the menu beside it, to the right of the logo. I don't know if it's possible, but if it is, I could use some help, please.
Here's the code for everything, separated for your convenience.
The menu and logo in their divs:
<div id="wrapper">
<div id="body-wrapper">
<div class="head">
<div class="head-wrapper">
<div class="logo">
<img src="http://i.imgur.com/sDnntOE.png">
<ul>
<li>Home</li>
<li>About Us
<ul>
<li>The Team</li>
<li>History</li>
</ul>
</li>
<li>Products
<ul>
<li>Chaotix Browser</li>
<li>Useful Beta 1.7.5</li>
<li>Chaotix Cleaner 1.4</li>
<li>Forum</li>
<li>CDev</li>
<li>Infinite-PVP</li>
<li>Ulta-Craft</li>
</ul>
</li>
<li>Contact Us
<ul>
<li>E-Mail</li>
<li>News Letter</li>
<li>Social Mediar</li>
</ul>
</li>
<li>Divisions
<ul>
<li>Gaming</li>
<li>Films</li>
</ul>
</li>
<li>Chaotix! Forum</li>
<li>Partnerships
<ul>
<li>GameFanShop</li>
<li>Forumotion</li>
</ul>
</li>
</ul>
The CSS:
<style>
*{
background-image:url('http://i.imgur.com/0u7sBsT.png');
background-color:#999999;
font-family:Tahoma;color:white;
}
div.head{
text-align:Center;
padding-top:10px;
}
div.body{
padding-top:100px;
padding-left:300px;
padding-right:300px;
text-align:center;
}
div.logo{
float:left;
}
a{
color:white;
}
a:hover{
color:gray;
}
/* Main menu
------------------------------------------*/
ul{
font-family: Lato,Tahoma,Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
padding-left:25px;
}
ul li{
display: block;
position: relative;
float: left;
}
li ul{
display: none;
}
ul li a{
display: block;
text-decoration: none;
color: #FFFFFF;
border-top: 1px solid #000000;
padding: 5px 15px 5px 15px;
background: #000000;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover{
background: #999999;
}
li:hover ul{
display: block;
position: absolute;
}
li:hover li{
float: none;
font-size: 11px;
}
li:hover a{
background: #000000;
}
li:hover li a:hover{
background: #999999;
}
</style>
Add your css
.logo img
{
float:left;
}
.logo ul
{
float:left;
}
It working ok. Hope this help!