Center list in div (for navigation bar) - html

Going off of the note at the top of this tutorial (http://css-tricks.com/centering-list-items-horizontally-slightly-trickier-than-you-might-think/), I tried to center a list of two links in the page, but it is centering only the first list item, not the entire list.
li{
font-family: Futura, Arvo, sans-serif;
display: inline-block;
}
ul{
text-align: center;
}
div#nav-list {
left: 0;
right: 0;
margin-right: auto;
margin-left: auto;
width: auto;
height: auto;
background: #E8E8E8;
}
"nav-list" is the container for the navigation bar, shown in the picture colored grey.
Here is the issue- you can see that "about" is centered, but not the entire list.
Thanks in advance!
Edit: Here's the HTML:
<div id="center_content">
<h1 id="page-heading">Title</h1>
<hr id="first-rule"></hr>
<div id="nav-list">
<ul>
<li>about</li> <li>work</li>
</ul>
</div>
<hr></hr>
<p>Here is a paragraph. </p>
</div>

Remove the list padding. By default, list have a 40px padding-left. Try to use ul{ padding:0; }

Try apply these styles :
ul {
margin: 0 auto;
padding: 0;
text-align: center;
width: 170px; /*as per your need*/
}

Hows this for you: http://jsfiddle.net/theStudent/6UnNs/
CSS
li {
font-family: Futura, Arvo, sans-serif;
display: inline-block;
padding: 10px;
}
ul {
text-align: center;
background:red;
width: 25%;
margin:0 auto;
}
ul li:first-child {
background:#ccc;
display:block;
margin:0 0 0 -40px;
}
HTML
<ul>
<li>TITLE</li>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 2</li>
</ul>
Just play with settings I added some color so you can see

Here you go:
1) Set text-align: center; on the nav-list element
2) Set display: inline-block on the list
FIDDLE
li {
font-family: Futura, Arvo, sans-serif;
display:inline-block;
padding: 0 10px;
}
ul{
list-style: none;
display: inline-block;
padding:0;
}
div#nav-list {
text-align: center;
background: #E8E8E8;
}

try this
<div>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
div ul li{float:left;list-style:none;margin:0 10px}
div ul{display: inline-block;margin: 0 auto;}
div{text-align:center}
JSFiddle

Related

How do I make it so I can have social icons on the left, logo in centre and navigation on the right?

Currently I have my website with the logo in the middle and the navigation on the right as shown below. However I want it to have social icons on the left, logo in the middle, and navigation on the right, therefore how do I do this ? I keep trying and failing.
Any help is appreciated, thanks.
HTML
<div id="nav-div">
<ul>
<h1>JOSH TAYLOR</h1>
<li>Contact</li>
<li>CV</li>
<li>Portfolio</li>
<li>Home</li>
</ul>
</div>
CSS
#nav-div {
opacity: 1;
font-size: 15px;
}
#nav-div h1{
color: lightskyblue;
cursor: pointer;
margin-top: 0px;
margin-bottom: 0px;
font-size: 30px;
float: left;
margin-left: 460px;
width: 200px;
}
#nav-div h1:hover{
color: white;
transition:all 0.40s;
}
#nav-div ul{
margin: 0px;
padding: 0px;
width: 100%;
height: 80px;
background: ;
line-height: 80px;
border-bottom: px solid black;
}
#nav-div ul a{
text-decoration: none;
color: lightskyblue;
padding: 25px;
}
#nav-div ul a:hover{
color:white;
transition:all 0.40s;
font-style:italic;
}
#nav-div ul li {
list-style-type: none;
display: inline-block;
float: right;
font-style:normal;
font-size: 13px;
margin-top: 1px;
}
Using flexbox you can have a flex div or header and enclose the three elements within and give it a justify-content:space-around Fiddle the rest is for presentation purpose. Hope this helps.
.flex{
display:flex;
justify-content: space-around;
height: 100px;
align-items: center;
border: 1px solid black;
}
.flex .social{
list-style: none;
padding: 0;
}
.flex .social li{
display: inline-block;
}
.flex nav ul{
list-style: none;
align-items: center;
padding: 0;
}
.flex nav ul li{
display: inline-block;
}
.flex nav ul li a{
text-decoration: none;
}
<header class="flex">
<ul class="social">
<li>SOCIAL</li>
<li>SOCIAL</li>
<li>SOCIAL</li>
</ul>
<img src="http://placehold.it/100x100" alt="">
<nav>
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
</nav>
</header>
You could use Bootstrap and enclose all three in a div with class row and add the class col-md-4 to all sub-divs(if you want a better result for mobile users replace md with xs).

Creating top menu items and right login menu

What is the best way to create top menu items and login text aligned to the right?
CSS
body {
margin: 0px;
}
#header {
height: 25px;
width: 100%;
background-color: rgb(34, 34, 34);
color: #fff;
font-family:'segoe ui', arial, helvetica, sans-serif;
font-size: 13px;
padding: 10px;
}
#header ol {
display: block;
list-style: none;
margin: 0;
padding: 0;
}
#header li {
position: relative;
display: -moz-inline-box;
display: inline-block;
line-height: 27px;
padding: 0;
vertical-align: top;
margin-right: 10px;
}
#login_details {
float: right;
display: inline-block;
}
HTML
<div id="header">
<ol>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
<li>Page 4</li>
</ol>
<div id="login_details">
dd
</div>
</div>
I'm trying to do this at the moment but the text in login_details isn't properly aligning to the right.
http://jsfiddle.net/KqqyY/
Place your login_details div before the list:
<div id="header">
<div id="login_details">dd</div>
<ol>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
<li>Page 4</li>
</ol>
</div>
And change your CSS to include the line-height: 27px; on your login_details div:
#login_details {
float: right;
display: inline-block;
line-height: 27px;
}
jsFiddle example
Since you floated the login details box to the right, it needs to come before the list in order to sit on the same line.
Use the CSS text-align property and make the ol float:left. I've also added some more stuff:
#login_details {
float: right;
display: inline-block;
text-align: right;
}
#header ol {
display: block;
list-style: none;
margin: 0;
padding: 0;
float: left;
}
#header {
height: 25px;
// Get rid of the 100% width to make it fit on screen
background-color: rgb(34, 34, 34);
color: #fff;
font-family:'segoe ui', arial, helvetica, sans-serif;
font-size: 13px;
padding: 10px;
}
Your #header container has a with of 100%, and after that you add padding to it causing it to grow larger than 100%, you could solve this with box-sizing:border-box
#header {
height:45px; /* The original 25px plus the paddings from the top and bottom */
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
}
See this JSFiddle: http://jsfiddle.net/KqqyY/2/

Why won't my unordered list centre properly?

It seemingly attempts to centre, but in actuality is a few pixels off. It's really annoying.
Picture: http://i.imgur.com/X4jhf.png
My code:
HTML:
<body>
<div class="menu-bar">
<ul>
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Contact</li>
</ul>
</div>
<div class="greeting"></div>
</body>
CSS:
.menu-bar {
font-family: 'Lucida Grande';
}
.menu-bar ul {
text-align: center;
list-style-type: none;
}
.menu-bar li {
display: inline;
}
.greeting {
background: url('../media/pic.jpg');
border: 1px solid black;
margin-top: 75px;
margin-left: auto;
margin-right: auto;
width: 500px;
height: 375px;
}
Very frustrating. >_<
Margin and Padding both needs to be set to 0 unless you are using a css reset to avoid browser inconsistencies.
.menu-bar ul {
text-align: center;
list-style-type: none;
margin:0;
padding:0;
}
DEMO.
UL has a default left margin which you haven't dealt with. Just add margin: 0;

How do I center list items inside a UL element?

How do I center list items inside a ul without using extra divs or elements. I have the following. I thought text-align:center would do the trick. I can't seem to figure it out.
<style>
ul {
width:100%;
background:red;
height:20px;
text-align:center;
}
li {
display:block;
float:left;
background:blue;
color:white;
margin-right:10px;
}
</style>
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
Check jsfiddle http://jsfiddle.net/3Ezx2/1/
write display:inline-block instead of float:left.
li {
display:inline-block;
*display:inline; /*IE7*/
*zoom:1; /*IE7*/
background:blue;
color:white;
margin-right:10px;
}
http://jsfiddle.net/3Ezx2/3/
A more modern way is to use flexbox:
ul{
list-style-type:none;
display:flex;
justify-content: center;
}
ul li{
display: list-item;
background: black;
padding: 5px 10px;
color:white;
margin: 0 3px;
}
div{
background: wheat;
}
<div>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
li{
display:table;
margin:0px auto 0px auto;
}
This should work.
Looks like all you need is text-align: center; in ul
I have run into this issue before and found that sometimes padding is the issue.
By removing padding from the ul, any li's set to inline-block will be nicely centred:
* {
box-sizing: border-box;
}
ul {
width: 120px;
margin: auto;
text-align: center;
border: 1px solid black;
}
li {
display: inline-block;
}
ul.no_pad {
padding: 0;
}
p {
margin: auto;
text-align: center;
}
.break {
margin: 50px 10px;
}
<div>
<p>With Padding (Default Style)</p>
<ul class="with_pad">
<li>x</li>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
<div class="break"></div>
<p>No Padding (Padding: 0)</p>
<ul class="no_pad">
<li>x</li>
<li>x</li>
<li>x</li>
<li>x</li>
</ul>
<div>
Hope that helps anyone running into this same issue :)
Cheers,
Jake
Another way to do this:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
ul {
width: auto;
display: table;
margin-left: auto;
margin-right: auto;
}
ul li {
float: left;
list-style: none;
margin-right: 1rem;
}
http://jsfiddle.net/f6qgf24m/
ul {
width: 100%;
background: red;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
}
li {
background: blue;
color: white;
margin-right: 10px;
}
I added the div line and it did the trick:
<div style="text-align:center">
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
</div>
I had a problem slimier to yours I this quick and its the best solution I have found so far.
What the output looks like
Shows what the output of the code looks like
The borders are just to show the spacing and are not needed.
Html:
<div class="center">
<ul class="dots">
<span>
<li></li>
<li></li>
<li></li>
</span>
</ul>
</div>
CSS:
ul {list-style-type: none;}
ul li{
display: inline-block;
padding: 2px;
border: 2px solid black;
border-radius: 5px;}
.center{
width: 100%;
border: 3px solid black;}
.dots{
padding: 0px;
border: 5px solid red;
text-align: center;}
span{
width: 100%;
border: 5px solid blue;}
Not everything here is needed to center the list items.
You can cut the css down to this to get the same effect:
ul {list-style-type: none;}
ul li{display: inline-block;}
.center{width: 100%;}
.dots{
text-align: center;
padding: 0px;}
span{width: 100%;}
The thing is ul tag comes with default margin and padding, therefore all you need to do is:
ul {
margin: 0px;
padding: 0px;
}
Neither text-align:center nor display:inline-block worked for me on their own, but combining both did:
ul {
text-align: center;
}
li {
display: inline-block;
}
In Bootstrap (4) use display: inline-flex, like so:
li {
display: inline-flex;
/* ... */
}
// Ul css
ul{
width: 100%;
justify-content: center;
list-style-type: none;
display: flex;
}
// ul li css
ul li{
display: inline;
}
I overcame the problem with this solution.
HTML:
<div class="list-wrapper">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
</div>
CSS:
.list-wrapper {
text-align: -webkit-center;
}
.list-wrapper ul {
display:block;
}

How to centrally align a float: left ul/li navigation menu with css?

So I have the following CSS in place to display a horizontal navigation bar using:
.navigation ul {
list-style: none;
padding: 0;
margin: 0;
}
.navigation li {
float: left;
margin: 0 1.15em;
/* margin: 0 auto;*/
}
.navigation {
/* width: auto;*/
/* margin: 0 auto;*/
text-align: center;
}
My question is: how do I align the navigation bar centrally above the title?
Here's a solution that doesn't require specifying the width of your ul.
Give your .navigation ul a width and use margin:0 auto;
.navigation ul
{
list-style: none;
padding: 0;
width: 400px;
margin: 0 auto;
}
Well, to use margin:0 auto on something, it must have a defined width. Probably the best workaround is:
ul li {
display: inline;
list-style-type: none;
}
ul {
text-align:center;
}
There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.
.navigation ul
{
list-style: none;
padding: 0;
margin: 0;
text-align: center;/*make this change*/
}
.navigation li
{
float: none;/*make this change*/
display:inline;/*make this change*/
margin: 0 1.15em;
/* margin: 0 auto; */
}
.navigation li a {
display:inline-block;/*make this change*/
}
.navigation
{
/*width: auto;*/
/*margin: 0 auto;*/
text-align: center;
}
You could set the <li>'s to be display: inline, then set text-align: center on the <ul>. Doing that, you can remove the float: left from the list items and you don't need to have a fixed width for the navigation bar as you would if you used margin: 0 auto.
<html>
<head>
<style>
ul {
list-style: none;
text-align: center;
}
li {
display: inline;
margin: 0 1.15em;
}
</style>
</head>
<body>
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</body>
</html>
This one works great with me!
(if I'm correct: IE7+)
Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/
.nav{list-style:none; text-align:center;}
.nav ul{list-style:none;}
.nav li{display:inline;}
.nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
<nav class="nav" role="navigation" aria-label="main navigation">
<ul>
<li>Home</li>
<li>Menu</li>
<li>Onze producten</li>
<li>Impressie</li>
<li>Media</li>
<li>Contact</li>
</ul>
</nav>
ul {
display: inline-block;
text-align: center;
}
.navigation ul
{
list-style-type: none;
padding: 0px;
width: 200px;
margin: 0 auto;
}
If you want to keep using your floated LI in your code, you can use this:
JSFiddle: https://jsfiddle.net/Lvujttw3/
<style>
.wrapper {
width: 100%;
background-color:#80B5EB;
text-align: center;
}
.intWrapper {
display: inline-block;
}
.mainMenu {
padding: 0;
min-height: 40px;
margin:auto;
}
ul {
list-style-type: none;
}
ul li {
float: left;
font-size: 15px;
line-height: 40px;
}
ul li A {
display: block;
color: white;
font-weight: bold;
font-family: Arial;
text-decoration: none;
min-height: 40px;
padding: 0 36px;
}
</style>
<div class="wrapper">
<div class="intWrapper">
<ul class="mainMenu">
<li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
</li><li>ITEM TWO
</li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>
</li>
</ul></div>
</div>
</div>
style="position: absolute; z-index: 1;"