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"
Related
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/`
I want to make an image in the left and the text on the right.
This is a jsfiddle
http://jsfiddle.net/VnnZ2/
I put the image in a div with class imageClass and I put the text in a div with class information and I gave the imageClass left float and the information right float.
I also tried to make this:
display: inline-block;
for both classes but still the result as you see in the jsfiddle.
I tried the same css with label and input and it works but I don't know why it is not working with div and div.
Plus, I already give the imageClass a width 200 and the informaton class a width 300 and the ul width 500px so I tried all what I could
Update 1
Sorry I gave you the wrong jsfiddle by mistake, this is the correct one http://jsfiddle.net/VnnZ2/9/
Check this updated code
HTML:
div class="allRestaurants">
<ul>
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">
nameres </div>
</li>
</ul>
<ul>
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">
Zuma 2 </div>
</li>
</ul>
</div>
CSS:
.allRestaurants{
background-color: #376b66;
width:100%;
float:left;
}
.allRestaurants ul{
margin: 10px;
padding: 0;
width: 600px;
background-color: #ffffff;
margin-left: 10px;
width:90%;
overflow:auto;
}
.allRestaurants ul li{
margin-bottom: 10px;
list-style: none;
box-sizing: border-box;
border-radius: 10px;
-webkit-border-radius: 10px;
}
.allRestaurants ul li .imageClass{
float: left;
}
.allRestaurants ul li .imageClass img{
width: 200px;
height: 200px;
}
.allRestaurants ul li .information{
width: 250px;
}
.allRestaurants ul li{
float:left;
clear:both;
}
Updated JS FIDDLE CODE
You need to clear your floats to stop the parent element collapsing. There are a number of ways. This is probably the simplest to understand:
WORKING DEMO
<li>
<div class="imageClass">
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</div>
<div class="information">nameres</div>
<div class="clearfix"></div> /* Add an empty element with class .clearfix */
</li>
In your CSS:
.clearfix {
clear: both;
}
Or this method:
WORKING DEMO
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
Where the clearfix class would be applied to the parent element(li)
Add overflow: auto; to .allRestaurants ul li, that way the li will know that there are elements inside it that float.
Also, check this updated Fiddle.
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%;
}
not sure why, but I can't get the icons centered on the page without using padding-left:130px;
Which isn't ideal of course because the icons don't center properly when you re-size the browser window. Maybe I need more coffee, but I could use some stacker help this morning!
http://towerdive.net/
HTML
<div id="center2">
<div id="social_icons">
<p>
Thanks for your interest in our blog!
You can also find us here, as well as subscribe to our newsletter:
</p>
<ul>
<li id="facebook">
<img src="img/icon_facebook.png" alt="Facebook"/>
</li>
<li id="twitter">
<img src="img/icon_twitter.png" alt="Twitter"/>
</li>
<li id="newsletter">
<img src="img/icon_newsletter.png" alt="Newsletter"/>
</li>
</ul>
</div>
</div>
CSS
#center2 {
width: 100%;
}
#social_icons {
margin: 0 auto;
width: 400px;
height: 250px;
text-align: center;
}
#social_icons p {
color: #e3cda4;
}
#social_icons ul {
margin: 0 auto;
list-style: none;
text-align: center;
}
#social_icons ul li {
float: left;
padding-right: 10px;
}
Let me know if you guys need the full HTML or CSS!
You can use display:inline-block for this. Write Like this:
#social_icons ul li {
display: inline-block;
*display:inline;/* For IE7*/
*zoom:1;/* For IE7*/
vertical-align:top;
padding-right: 10px;
}
You currently use floats to display your navigational list horizontally. You can't align the list-items in the middle of the unordered lists because of the floats.
Another technique is instead of float: left; using display: inline-block;. The list-items will be displayed horizontally but also all extra white-space will taken into account. You'll get extra margins between the items (like 4px). Now you can use text-align: center; on the UL to center the list-items.
There are several (easy) workouts described in this article by Chris Coyier:
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
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>