How to center a ul CSS - html

I have a ul that I am using for my page navigation and I would like to center it but I can't figure out how to do it. Here is my code:
<ul id="nav">
<li>Home</li>
<li>What Are Drones</li>
<li>How Do Drones Work</li>
<li>Buying a Drone</li>
<li>About Us</li>
</ul>
#nav { list-style-type: none; height: 100px; padding: 0; margin: 0; }
#nav { list-style-type: none; height: 100px; padding: 0; margin: 0; }
#nav li { display:inline-block; position: relative; padding: 0; line-height: 100px; background: #666 url(nav-bg.png) repeat-x 0 0; }
#nav li:hover { background-position: 0 -40px; }
#nav li a { display: block; padding: 0 15px; color: #fff; text-decoration: none; font-size: 50px; font-family: 'Cuprum', sans-serif; }
#nav li a:hover { color:#0F0 }
#nav li ul { opacity: 0; position: absolute; left: 0; width: 8em; background: #63867f; list-style-type: none; padding: 0; margin: 0; }
#nav li:hover ul { opacity: 1; }
#nav li ul li { float: none; position: static; height: 0; line-height: 0; background: none; }
#nav li:hover ul li { height: 30px; line-height: 30px; }
#nav li ul li a { background:#666 }
#nav li ul li a:hover { background:#666 }
#nav { background-color:#666; }

It really depends on what you're trying to do, your constraints and how you are trying to build your site but for me setting text-align:center; on your #nav element centers all of the individual menu elements relative to which row they appear on.
You can read more here if you need to get into the finer arts of it: https://css-tricks.com/centering-list-items-horizontally-slightly-trickier-than-you-might-think/
But as they say this is pretty simple in modern HTML. It's only if you need to support older browsers for some reason or some other quirky setup that it gets more complex.

Here is the simplest solution to center a list:
#nav {
display: table;
margin: auto;
}
#nav > li {
display: block;
}
<ul id="nav">
<li>Home</li>
<li>What Are Drones</li>
<li>How Do Drones Work</li>
<li>Buying a Drone</li>
<li>About Us</li>
</ul>
I used display: table because:
It fits to its content, like display: inline-block
It allows me to center it without using an extra element.

Related

CSS responsive menu width

I'm making a simple website and I'm struggling one one thing...
Exactly, it's a menu navbar.
I've done it, but it's only positioning in left corner. What I want to do is to continue the color strip (#333) to the very end of the screen.
Here is the code:
#primary_nav_wrap {
margin: 0;
float: left;
}
#primary_nav_wrap ul {
background-color: #333;
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0;
}
#rectangle {
float: right;
position: relative;
width: 1200px;
overflow: hidden;
background: #333;
}
#primary_nav_wrap ul a {
display: block;
color: #fff;
text-decoration: none;
font-weight: 500;
line-height: 50px;
padding: 0 20px;
font-family: 'Open Sans', "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul li.current-menu-item {
background: #4CAF50
}
#primary_nav_wrap ul li:hover {
background: #282828
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #333;
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
}
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>Portfolio
<ul>
<li>About me</li>
<li>Menu1</li>
<li>MenuMenu</li>
<li>MenuMenuMenu</li>
</ul>
<li>Menu2</li>
<li>Menu3
<ul>
<li>MenuMenuMenu</li>
<li>MenuMenu</li>
<li>MenuMenuMenu</li>
<li>MenuMenuMenu</li>
<li>MenuMenu</li>
<li>Menu</li>
<li>WutWut</li>
</ul>
</li>
<li>Contact</li>
</ul>
<!--
<div id="rectangle"></div>
-->
</nav>
I am still learning CSS, HTML and JS and this is my first attempts... Some code are mine, some not, but hey.. I'm trying to learn :)
The struggle is to continue menu background color (#333) to the end of the right side. As you can see I was trying to do that using a rectangle (do not kill me for that, please) and didn't worked out properly, ofc...
Help :(
#primary_nav_wrap {
width: 100%;
background:#333;
}
Just give 100% width and color the background
Simply set the width as 100% to #primary_nav_wrap and #primary_nav_wrap ul.
#primary_nav_wrap {
margin: 0;
float: left;
width: 100%;
}
#primary_nav_wrap ul {
background-color: #333;
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0;
width: 100%;
}
#rectangle {
float: right;
position: relative;
width: 1200px;
overflow: hidden;
background: #333;
}
#primary_nav_wrap ul a {
display: block;
color: #fff;
text-decoration: none;
font-weight: 500;
line-height: 50px;
padding: 0 20px;
font-family: 'Open Sans', "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul li.current-menu-item {
background: #4CAF50
}
#primary_nav_wrap ul li:hover {
background: #282828
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #333;
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
}
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>Portfolio
<ul>
<li>About me</li>
<li>Menu1</li>
<li>MenuMenu</li>
<li>MenuMenuMenu</li>
</ul>
<li>Menu2</li>
<li>Menu3
<ul>
<li>MenuMenuMenu</li>
<li>MenuMenu</li>
<li>MenuMenuMenu</li>
<li>MenuMenuMenu</li>
<li>MenuMenu</li>
<li>Menu</li>
<li>WutWut</li>
</ul>
</li>
<li>Contact</li>
</ul>
<!--
<div id="rectangle"></div>
-->
</nav>

CSS Hover State in Drop Down Navigation

I can't figure out why the hover state in my css does not work. I've been successful in hiding the sub navigation, but can't get it to reappear on rollover. I Any help would be appreciated. Thanks.
Here's the html:
<nav>
<ul>
<li>writings</li>
<ul>
<li>devotionals</li>
<li>published</li>
<li>articles</li>
</ul>
<li>fundraising</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
and the css:
.nav {
position: relative;
margin-left: 200px;
margin-top: -50px;
}
.nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.nav > ul > li {
float: left;
font-size: 30px;
}
.nav ul::after {
content:'';
display: block;
clear: both;
}
.nav ul li:hover > ul {
display: block;
}
.nav ul ul {
float: left;
display: block;
position: absolute;
top: 90%;
display: none;
line-height: 5px;
}
nav ul li a {
display: inline-block;
color: rgb(92,178,227);
font-family: 'Josefin Sans', sans-serif;
padding: 10px 20px;
text-decoration: none;
}
You need the sub nav inside of the list item:
<li>writings
<ul>
<li>devotionals</li>
<li>published</li>
<li>articles</li>
</ul>
</li>

Match text height with image height in menu

I have a menu, for which I put at the end 3 images, measuring 30x30 px each. After the images being inserted, it appeared a space between the bottom line of the menu and only the text. For the image there is no space between the bottom of the menu and the images. What I would like now to do, would be to have the text having the exact size (I managed to fix this space by increasing font size, but this is not a solution for me) as now and get rid of that space. Again, the space is only below the text and not the images. *Check with full-screen the menu.
#nav {
background-color: #e26a63;
padding: 0;
margin: 0;
font-family: FONT;
font-size: 20px;
}
#wrap {
padding-left: 60px;
height: 100px;
width: 100%;
margin: 0 auto;
display: table-cell;
vertical-align: bottom;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
min-width: 245px;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #cb5f59;
}
#nav ul li:after {
content: "";
font-size: 0;
display: block;
height: 5px;
}
#nav ul li:hover:after {
background: #9e4a45;
}
#nav ul ul li:hover:after {
background: transparent;
}
#nav ul li a, visited {
color: white;
display: block;
padding: 15px;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #e26a63;
border-top: 0;
margin-top: 5px;
z-index: 100;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color: white;
}
#nav {
display: table;
width: 100%;
}
.alignright { float: right; }
<div id="nav">
<div id="wrap">
<ul>
<li>Home</li><li>
Study
<ul>
<li>Present Simple</li><li>
Possesives</li><li>
Articles</li><li>
Modal Verbs</li><li>
Prepositions</li><li>
Plural of nouns</li><li>
Countability</li>
</ul>
</li><li>
Games
<ul>
<li>Riddles</li><li>
Flip card game</li><li>
Spot the mistake</li><li>
Multiple choice</li>
</ul>
</li><li>
Shop</li><li>
Contact</li><li>
About Us</li>
<li style="float:right"><a><img src="gmail.png" height="30px" width="30px"></a></li> <li style="float:right"><a><img src="twitter.png" height="30px" width="30px"></a></li><li style="float:right"><a><img src="facebook.png" height="30px" width="30px"></a></li>
</ul>
</div>
</div>
The simplest would be to set the same height to your texts as you have on your images
#nav ul li a, visited {
color: white;
display: block;
padding: 15px;
height: 30px; /* added */
text-decoration: none;
}
Stack snippet
#nav {
background-color: #e26a63;
padding: 0;
margin: 0;
font-family: FONT;
font-size: 20px;
}
#wrap {
padding-left: 60px;
height: 100px;
width: 100%;
margin: 0 auto;
display: table-cell;
vertical-align: bottom;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
min-width: 245px;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #cb5f59;
}
#nav ul li:after {
content: "";
font-size: 0;
display: block;
height: 5px;
}
#nav ul li:hover:after {
background: #9e4a45;
}
#nav ul ul li:hover:after {
background: transparent;
}
#nav ul li a, visited {
color: white;
display: block;
padding: 15px;
height: 30px; /* added */
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #e26a63;
border-top: 0;
margin-top: 5px;
z-index: 100;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color: white;
}
#nav {
display: table;
width: 100%;
}
.alignright { float: right; }
<div id="nav">
<div id="wrap">
<ul>
<li>Home</li><li>
Study
<ul>
<li>Present Simple</li><li>
Possesives</li><li>
Articles</li><li>
Modal Verbs</li><li>
Prepositions</li><li>
Plural of nouns</li><li>
Countability</li>
</ul>
</li><li>
Games
<ul>
<li>Riddles</li><li>
Flip card game</li><li>
Spot the mistake</li><li>
Multiple choice</li>
</ul>
</li><li>
Shop</li><li>
Contact</li><li>
About Us</li>
<li style="float:right"><a><img src="gmail.png" height="30px" width="30px"></a></li> <li style="float:right"><a><img src="twitter.png" height="30px" width="30px"></a></li><li style="float:right"><a><img src="facebook.png" height="30px" width="30px"></a></li>
</ul>
</div>
</div>

dropdown menu: whole menu comes down

I'm trying to make a basic dropdown menu with css and html. However when I hover on the li that has to drop down, my whole menu comes down with it.
This is my code:
<nav class="icons">
<ul>
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
<li>Account
<ul id="login">
<li>Change password</li>
<li>Update details</li>
<li>Logout</li>
</ul>
</li>
</ul>
</nav>
And the CSS
nav {
height: 70px;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
ul {
margin: 0;
padding: 0;
list-style: none;
margin-left: 5%;
}
ul li {
display: inline-block;
width: 15%;
text-align: center;
line-height: 70px;
}
ul li a {
text-decoration: none;
color: #fff;
font-size: 2em;
display: block;
}
ul li a:hover {
border-bottom: solid black 1px;
}
ul#login{
margin: 0;
padding: 0;
list-style: none;
}
ul#login li {
display: block;
width: 30%;
text-align: center;
line-height: 70px;
}
ul#login li a {
text-decoration: none;
color: #fff;
font-size: 2em;
display: block;
}
ul#login li ul li{
width: 20%;
}
ul#login li ul li a {
text-decoration: none;
color: #fff;
font-size: 0.7em;
display: block;
}
ul#login li a:hover {
border-bottom: solid black 1px;
}
I know it's a lot of CSS for such a basic menu but I don't know how to make it more compact.
Can someone please tell me what I'm doing wrong with the dropdown menu?
Add this css
ul li{
position:relative;
}
#login{
position:absolute;
top:71px;
left:0;
}
FIDDLE

How do I make my sub-links/text horizontal when being diplayed?

I have a nav bar with a drop down that is working beautifully. The only thing I am trying to change, is when you hover over the main links, the sub-links/text are on top of eachother. I want them to display horizontally. I have tried all sorts of floats and display inline etc...
Here is a portion of the HTML:
<ul id="top_main_nav" style="float:left;">
<li>Me
<ul>
<li><?php echo ($auth->first_name); ?> <?php if(strlen($auth->last_name) > 6) { echo substr(strtoupper($auth->last_name),0,6) . "..."; }else{ echo ($auth->last_name); } ?></li>
<li>Edit My Profile</li>
<li>Settings</li>
<li>Email Settings</li>
</ul>
</li>
</ul>
Here is the css:
#user_nav_future {
margin: 0 auto;
font: bold 12px Arial,Helvetica, sans-serif;
color: #fff;
}
#user_nav_future ul li {
list-style: none;
float: left;
}
#user_nav_future ul li a {
color: #fff;
padding: 10px;
display: block;
text-decoration: none;
}
#user_nav_future li ul {
display: none;
position: absolute;
top: 35px;
right: 160px;
z-index: 1001;
margin: 0;
padding: 0;
}
#user_nav_future li ul a {
color: #666;
display: inline;
float: left;
}
#user_nav_future ul li a:hover {
color: #FF4800;
}
#user_nav_future ul {
padding:0;
margin:0;
list-style:none;
}
#user_nav_future li {
float:left;
position:relative;
text-align:center;
}
#user_nav_future li ul {
display:none;
position:absolute;
top:34px;
left:0;
}
#user_nav_future li ul li {
width:160px;
}
#user_nav_future li:hover ul,#user_nav li.over ul {
display:block;
}
These changes should do the trick:
#user_nav_future {
margin: 0 auto;
font: bold 12px Arial,Helvetica, sans-serif;
color: #fff;
}
#user_nav_future ul {
/* Reset padding / margins on <ul>. Add back in as necessary. */
padding: 0;
margin: 0;
list-style: none;
}
#user_nav_future ul li {
/* Take <li> out of the picture - everything is now being dictated by nested <a> */
padding: 0;
margin: 0;
display: inline;
}
#user_nav_future ul li a {
/* whatever width you want each link to be. Since you've got 10px of left/right padding, true element width will be 180px */
width: 160px;
padding: 10px;
float: left;
position: relative;
text-decoration: none;
text-align: center;
color: #fff;
}
#user_nav_future li ul a {
color: #666;
}
#user_nav_future ul li a:hover {
color: #FF4800;
}
#user_nav_future li ul {
display: none;
position: absolute;
top: 34px;
left: 0;
}
#user_nav_future li:hover ul,#user_nav li.over ul {
display: block;
}
What they're doing is setting all the block elements that are causing your drop downs to go down to inline elements that float so the menu is horizontal.
I've also transferred positioning control over to the <a> elements. That way the entire area of the link will be clickable, rather than just the text.
If the above doesn't work for you, post some of your HTML or a dev link so we can see what's going on.