I am trying to have some LIs within a UL align left, right, and center within a page. For the life of me, I can't figure out how to keep something "centered" on the same line as a left and right aligned LI.
ul {
margin:1em 0;
padding:0
}
ul li{
display:inline-block;
white-space:nowrap;
margin:5px
}
ul li.left{
float: left;
text-align:left;
}
ul li.center{
float:left;
text-align: center;
}
ul li.right{
float: right;
text-align:right;
}
<ul>
<li class="left">left</li>
<li class="center">center</li>
<li class="right">right</li>
</ul>
<ul>
<li class="left">left</li>
<li class="right">right</li>
</ul>
<ul>
<li class="left">left</li>
</ul>
Can anyone help? BTW, I've trying to avoid DIVs.
Thanks!
If you want each to share screen space equally, you can do this:
<style>
.split { width: 33%; float: left; }
</style>
<ul>
<li class="split">left</li>
<li class="split">center</li>
<li class="split">right</li>
</ul>
You'll want to move your styles to an external stylesheet, though.
You can definitely do this with only one thing being floated.
ul li { float:right; }
If you float them all to the left, then you will get (with three LI elements) a right, center, and left.
<ul><li>right</li><li>center</li><li>left</li></ul>
A good way to think of this is thinking of what you want to happen to each individual LI element: you want each one to be moved to the right of the other. This is the most common method of making horizontal navigation with a list structure.
Your li elements will only be as wide as the text that it contains since you are floating them and are not specifying width. Do you want your center element to be fluid? If I'm not mistake., it sounds like you're going for a fluid three-column layout? There are plenty of examples of these on the net if that's what you're going for.
Thanks to mitch and everyone else, this is the solution I came up with and works for me.
<style>
ul {
margin: 1em 0;
padding: 0;
list-style-type:none;
}
li.three {
width: 33%;
}
li.two {
width: 50%;
}
li.one {
width: 100%;
}
li.left {
float: left;
text-align: left;
}
li.center {
width: 33%;
float: left;
text-align: center;
}
li.right {
width: 33%;
float: right;
text-align: right;
}
</style>
<ul>
<li class="three left">left</li>
<li class="three center">center</li>
<li class="three right">right</li>
</ul>
<ul>
<li class="two left">left</li>
<li class="two right">right</li>
</ul>
<ul>
<li class="one left">left</li>
</ul>
Related
I'm trying to right-align the nav without using float. I read somewhere online that I could set the parent element to text-align: right, but that didn't work when I tried it. I appreciate any help for what I know is an easy problem but one that has confounded me most of the day.
header {
background-color: #AEB7CC;
padding: 0 10px;
}
nav {
display: inline-block;
}
nav li {
display: inline;
}
<header>
hi
<nav>
<ul>
<li>Home
</li>
<li>bop</li>
<li>bop</li>
<li>bop</li>
</ul>
</nav>
</header>
https://jsfiddle.net/a6a367vp/
The solution is that you have to add text-align: right; to the parent element which is the header.
header {
text-align: right;
...
}
Then add display: inline-block; to the children (nav) and the children will be aligned right.
Another solution would be to set a width to nav and a:
header nav {
with: 80%;
}
header a {
width: 20%;
}
header nav ul {
text-align: right;
}
header nav ul li {
display: inline-block;
}
if you want the nav to move to the right, simple method is float:right but since you don't want to use this then text-align:right would place it on right if nav has 100% width for that row.
Therefore, you can do either of these two things.
right now using CSS you can make
nav{width:98%; text-align:right};
or for better results, use JS
var anchorTopWidth = $("a").width() / $('a').parent().width() * 100;
requiredWidth = 100-anchorTopWidth;
$('nav').css({'width': requiredWidth+'%', 'text-align':'right'});
Fiddle: https://jsfiddle.net/a6a367vp/3/
One possible and recommended solution is as follows:
Put the <nav> in a <div> element and then use the text-align property for the specified div. like this:
Html:
<div class="nav_menu">
<nav>
<ul>
<li>Home</li>
<li>bop</li>
<li>bop</li>
<li>bop</li>
</ul>
</nav>
</div>
CSS:
.nav_menu{
text-align: right;
}
Update:
Put the <a> tag in another div and then specify the width for the selected divs. Your final code should be like this:
HTML:
<header>
<div class="atag_div">
hi
</div>
<div class="nav_menu">
<nav>
<ul>
<li>Home</li>
<li>bop</li>
<li>bop</li>
<li>bop</li>
</ul>
</nav>
</div>
</header>
CSS:
header {
background-color: #AEB7CC;
padding: 0 10px;
}
nav li {
display: inline;
}
.atag_div{
width: 20%;
display: inline-block;
}
.nav_menu{
width: 78%;
display: inline-block;
text-align: right;
}
jsfiddle:
https://jsfiddle.net/pokies/7Lnfq7es/
I'm trying to make a navigation bar at the top of my site. But the items wont align horizontally properly. I used display: inline. That should make things align side by side right? Well... Once I set display to inline, the width of the item seems to be set to 100%, so the items are not allowed to go side by side. When I set the display to inline-block, the height seems to triple, but is the items are set side by side. Though, I don't want it to be higher than it is because then the appearance isn't what I want.
How can I make the items align properly!? Please help and thank you if you do.
html code
<div id="Bars">
<ul>
<li><p style="width: 50px">Home</p></li>
<li><p style="width: 73px">Software</p></li>
<li><p style="width: 62px">Gallery</p></li>
</ul>
</div>
css code
#Bars{
height: 30px;
width: 100%;
background-color: #000099;
border-radius: 10px;
}
#Bars ul li p{
color: black;
font-size: 20px;
}
#Bars ul li{
list-style: none;
display: inline;
}
Add below css to your style sheet and set inline-block to li, in your case predefined margin and padding are applying
* {
margin:0;
padding:0;
}
#Bars ul li{
list-style: none;
display: inline-block;
}
Use Fiddle
HTML:
<div id="nav">
<ul>
<li class="current_page_item">[LINK]</li>
<li class="page_item">[LINK]</li>
<li class="page_item">[LINK]</li>
<li class="page_item">[LINK]</li>
<li class="page_item">[LINK]</li>
<li class="page_item">[LINK]</li>
</ul>
</div>
CSS:
#nav ul {
list-style: none;
height:16px;
background: #ccc
}
#nav ul li {
position: relative;
display: inline-block;
}
#nav {
position: relative;
text-align: center;
}
use can use display: inline-block; and need to adjust the margin.
js fiddle link :
`https://jsfiddle.net/2pr2pq78/`
JS Fiddle example
I need these boxes in example to be placed the same size out of each other on the same line. How it could be possible to make?
Also, if you resize window and one box drops down, then it would be in the center like it is now in the start of the example.
<div id='wrapper'>
<ul>
<li>
</li>
<li>
</li>
</ul>
</div>
EDIT:
First, I would suggest using divs instead of <ul> and <li> otherwise you will have to reset styles on them (for example, chrome adds -webkit-margin-before/after and other css properties that can mess up your layout).
Fiddle with divs.
If you really need to keep you HTML markup, here is a
FIDDLE taht works in chrome.
HTML :
<div id='wrapper'>
<ul>
<li><span></span>
</li>
<li><span></span>
</li>
<li><span></span>
</li>
</ul>
</div>
CSS :
html, body {
width:100%;
margin:0;
overflow: hidden;
}
#wrapper {
width: 100%;
height: 220px;
background-color: #000;
}
ul {
-webkit-margin-before: 0;
-webkit-margin-after: 0;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start:0;
}
ul > li {
float:left;
display:block;
width:33.33%;
height: 220px;
list-style-type: none;
}
ul > li > span {
background-color: red;
display:block;
margin:0 auto;
width:200px;
height:100%;
}
I've got an issue that I can't resolve.
I have a list of lists, like this one:
<ul id="nav">
<li>
<div class="tabquad">
Item 1
</div>
<ul>
<li>
<a class="item" href="index.jsp?quad=1">
<div class="tabquad">
1.1
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=2">
<div class="tabquad">
1.2
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=3">
<div class="tabquad">
1.3
</div>
</a>
</li>
</ul>
</li>
<li>
<div class="tabquad">
Item 2
</div>
<ul>
<li>
<a class="item" href="index.jsp?quad=7">
<div class="tabquad">
2.1
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=8">
<div class="tabquad">
2.2
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=9">
<div class="tabquad">
2.3
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=10">
<div class="tabquad">
2.4
</div>
</a>
</li>
<li>
<a class="item" href="index.jsp?quad=11">
<div class="tabquad">
2.5
</div>
</a>
</li>
</ul>
</li>
</ul>
with this CSS:
#nav, #nav ul {
padding: 0;
margin: 0 auto;
list-style: none;
}
#nav {
width: 45em;
margin: 0 auto;
}
#nav a {
display: block;
width: 15em;
}
#nav li {
float: left;
width: 15em;
}
#nav li ul {
position: absolute;
width: 15em;
left: -500em;
}
#nav li:hover ul {
left: auto;
}
#nav li:hover ul, #nav li.sfhover ul {
left: auto;
}
.item{
color:#0E4063;
text-decoration:none;
}
.tabquad{
color:white;
margin:auto;
position:relative;
border:1px solid #000;
border-color:rgb(82,115,154);
width:200px;
height:30px;
text-align:center;
padding-top:10px;
top:25px;
background-color:rgb(0,56,130);
}
.tabquad:hover{
background-color:rgb(49,87,132);
cursor: hand;
}
My problem is: Item 1 list and Item 2 list are not in the center of my page, they are left-oriented.
I've tried with the center parameter and with the float, without success.
Any suggestion?
You can find a fiddle here: Link
you have to set the width of the #nav to the right value (corresponding to the width of the content). change your example to the following value and the two menus will be centered on the page.
#nav {
width: 30em;
margin: 0 auto;
}
updated jsFiddle: http://jsfiddle.net/F9Wrb/17/
and just to increase cross-browser compatibility, add the following CSS to the parent node of your #nav:
body {
text-align: center;
}
You might want to read about the basics of CSS page centering and page layouts.
Normally, you'd have a "container" <div> that contains the entire page and defines the layout and boundaries of the page. You can call it anything (not necessarily container)
Now, you need to decide what layout fits you best, most commonly used layout sizes can vary anywhere between 960px to 1200px.
general styling for your container would be,
.container{
width:960px; //<-- this can be anything, based on your layout.
margin:0 auto;
}
This ensures your page is centered almost always. You would want to use some kind of a CSS Reset to ensure you reset browser defaults
Here is your solution. I have just changed these two styles. FYI, unless you are using html5, you cannot put div inside <a> tag; it's invalid.
#nav, #nav ul {
padding: 0;
margin: 0 auto;
list-style: none;
text-align:center
}
#nav li {
width: 15em;
display:inline-block;
}
Three possible solutions:
Using absolute positioning and a negative left margin:
#nav {
position: absolute;
left: 50%;
margin: 0 0 0 -15em;
}
Demo 1
Using auto margins with ul width specified:
#nav {
width: 30em;
margin: 0 auto;
}
Demo 2
If you want to use the auto-margin solution and don't want to specify the ul width, declare the LIs as display: inline-block (instead of floating them). This works because the LIs will now be in the page flow instead of out of it, as they are when floated. Answer courtesy of Kashif Raza.
#nav {
margin: 0 auto;
}
#nav > li {
display: inline-block;
width: 15em;
}
Demo 3
#nav a {
display: block;
width: 15em;
margin:auto;
}
#nav>li {
width:50% !important;
}
#nav li:hover ul {
position:relative;
left: auto;
}
Make these changes in your CSS and lists will be in the center of your page.
Just put a width on the surrounding div, then change that to margin:0 auto, which will center it in the page.
.center{
width:400px;
margin:0 auto;
}
And the html, rather than float="center"
<div class="center">
http://jsfiddle.net/F9Wrb/12/
As mentioned, there is no such thing as float="center", or for that matter, float:center;! For advanced positioning of elements, I'd recommend using a CSS framework such as zurb foundation or Gridpak.com
Put this where you want to center:
div align="center"
Here's some HTML.
<div id = "header">
<div id = "menu">
<ul>
<li>Home</li>
<li>Browse</li>
<li>Account</li>
<li>About</li>
</ul>
</div>
And, here's some CSS
#menu
{
width: 80%;
height: 30px;
}
#menu li a
{
display: block;
background-color: #999999;
color: #FFFFFF;
font-weight: bold;
line-height: 30px;
text-align: center;
}
The above HTML and CSS give 4 rectangles. I'd like to have those rectangles to be evenly stretched horizontaly inside the the menu div.
Each time I try, I either get free space after About rectangle (i.e. the last rectangle) or get the About box flow to the next line.
Thanks for helping
You need to add:
#menu li a
{
float:left;
width:25%;
}
#menu li
{
display:inline;
}
Demo: http://jsfiddle.net/BPyUB/