Ok, so am I missing something but I can't seem to line up a simple ul list of list items so that they stretch the entire width of their parent div. Here is an example of my problem here http://jsfiddle.net/37E55/17/.
What I'm trying to do is get grey boxes to line up in a row so that the first box's left hand edge is inline with left hand edge of the #wrapper div and the last box's right hand edge is inline with the #wrapper div's right hand edge.
I have tried successfully to line the boxes up by giving them an absolute positioning but is there a way to use a combination of margin and padding that I'm missing?
#wrapper {
width: 400px;
height: 300px;
background-color:#F0F0F0;
margin: 10px auto;
}
.box {
width: 92px;
height:92px;
background-color:#333;
margin:0px 10px 10px 0px;
float:left;
}
<div id="wrapper">
<ul>
<li class="box"></li>
<li class="box"></li>
<li class="box"></li>
<li class="box"></li>
</ul>
</div>
I knew there was a way to do it with inline-block instead of floating (if you do not have to support overly old browser).
Here's a fiddle demo!
The li do not have margin applied, they are evenly disposed in the area and cling to borders. I followed this guide.
ul {
font-size: 0 !important; /* remove physical spaces between items */
text-align: justify;
text-justify: distribute-all-lines; /* distribute items in IE */
list-style-type: none;
margin: 0;
padding: 0;
}
/* fully justify all items in browsers other than IE */
ul:after {
content: "";
display: inline-block;
width: 100%;
}
ul li {
text-align: left; /* customize to suit */
vertical-align: top; /* can customize to suit */
display: inline-block;
width: 31.3%; /* optional, only need for text content to wrap */
margin-bottom: 1em; /* optional, customize to suit */
}
use :last-child to select the last box and apply margin-right: 0 to it. Make sure the remaining margins will fill the space properly.
.box {
width: 92px;
height:92px;
background-color:#333;
margin:0px 10px 10px 0px;
float:left;
}
.box:last-child {
margin-right: 0;
}
If you have to stick with a width of 92px you won't get them to align properly. The remaining space that the margins need to fill is 32px, which doesn't divide evenly by 3.
The first thing you need to do is remove the last element's right margin.
.box:last-child { margin-right:0; }
Beyond that, sometimes you don't have the ability to fit elements with, for example, exact even margins based on their space and the size of the container. Sometimes you can apply different margins on (for example) every-other element to keep the layout looking "even" but to handle the lack of space, something like:
.box:nth-of-type(2n) { margin-right:14px } /* add extra pixels to right margin of even elements*/
In your case though, only one of the boxes needs extra margins, say, the first. Here's how I did it (with color contrast increased just to make it easier to see).
.box {
width: 90px;
height:90px;
background-color:blue;
margin:0px 13px 10px 0px;
float:left;
}
.box:last-child {
background:green;
margin-right:0;
}
.box:first-child {
background:yellow;
margin-right:14px;
}
Cheers
Your boxes with the margins were too large. Note that padding is in additional to the specified height and width. See it work on http://jsfiddle.net/37E55/32
Related
I need to design a menu, in which the menu is always centered, with variable number of items, browser resolutions, and the items would be aligned to the left (menu centered in the page, but items aligned to the left).
(As you can see it is not centered at all).
This is my code:
<html>
<head>
<style type="text/css">
.extPanel{
background-color:#555;
padding: 0px 20% 0px 20%;
display: table;
}
.split{
clear: both;
}
.menuElement{
float:left;
background-color:#aaa;
margin: 0px 20px 20px 0px;
width: 200px;
height: 200px;
text-align: center;
}
</style>
</head>
<body>
<div class="extPanel">
<div class="menuElement">item1</div>
<div class="menuElement">item2</div>
<div class="menuElement">item3</div>
<div class="menuElement">item4</div>
<div class="split"></div>
External Panel. 20% left and right padding.
</div>
</body>
</html>
As you can see, the external div has 20% padding in order to center the items. Items float to the left. Items are not centered at all because a remaining space exists in which item4 doesn't have enough space, so it floats to the next line.
And if the menu would have only one menu item, this item float to left so is more obvious that the menu is not centered. If I try to use some style to center all the items (text-align or something like this), item4 would appear centered below item2, and I need that the items are align to left.
I need:
Menu to be centered in the page, with any number of items
Items centered
Cross-browser compatibility (to IE8 at least, and mobile explorers)
No JavaScript
Try using display: inline-block;.
You can add the following code:
.extPanel {
text-align: center;
}
.menuElement {
display: inline-block;
*display: inline; //ie
zoom: 1; //ie
//remove float: left;
}
instead padding, you could simply use margin and inline-block for childs.
http://codepen.io/anon/pen/quByg
.extPanel {
margin:0 20%;
text-align:justify;
}
.menuElement {
display:inline-block;
width:200px;
height:200px;
margin:0 20px 20px 0;
border:solid;
}
You want to set text-align: center; on the surrounding body. Then .extPanel needs a margin: 0 auto;
http://jsfiddle.net/markdelorey/ttZgZ/
I found the solution by combining all your answers.
You can found it here:
http://codepen.io/anon/pen/odhrp
I used diffent IE hack here:
http://codepen.io/anon/pen/flEsm
As you can see, works with one element, two, three... When items menu don't fit in extPanel max width, items goes to next line, align to left, and menu is still centered.
.extPanel {
background-color:#555;
text-align:center;
display: table;
margin: auto;
max-width: 80%;
text-align: justify;
}
.menuElement {
display:inline-block;
width:200px;
height:200px;
margin:20px 20px 0;
border:solid;
text-align: center;
}
And text-justify: distribute solve the problem with text-align: justify in IE.
I have an issue with the sliding doors technique here. The heading right after the description is floating left due to the sliding doors technique, but all I want is that is stands alone in the center, above the products.
Can you help me understanding how to do it?
Here is the CSS I used for the heading:
h3.offer-title, h3#comments, h3#reply-title {
background:url(images/offer-title-bg.png) no-repeat right bottom;
color:#434343;
display:block;
float:left;
font-size: 14px;
margin-right: 6px;
padding-right:6px;
text-decoration:none;
height: 43px;
line-height: 0;
position: relative; }
h3.offer-title span, h3#comments span, h3#reply-title span {
background:url(images/offer-title-bg.png) no-repeat;
display:block;
padding-left: 20px;
height: 43px;
line-height: 43px;
padding-right: 16px;
}
Thank you.
It's floating because you set float: left in your first CSS code block. To get rid of that behaviour you need to get rid of the float.
Once the float is gone, if you want the header's background to nicely fit the text like it did before, the element needs to have display: inline-block.
But with display: inline-block and no set width on the header (you could add a width, but then it might break if you want to change the text or font size), it's not centered. To get it centered, you need a wrapper element around it which has text-align: center.
So:
Add this block:
h3.offer-title {
display: inline-block; /* this makes the bg fit the text */
float: none; /* this overrides float:left */
}
Wrap the offer-title element in another div.
Style the wrapper with
.offer-title-wrapper {
text-align: center;
}
I want to create a robust css style that works whith almost all browser (included IE7, firefox 3)
that show me two columns and one footer divided by dotted border.
I was trying to implement the following code,
but I have one problem:
when I apply border-right-style:dotted; to left class
A and B are not at the same horizontal level.
please halp me to fix the css style.
Click here for the current example.
HTML
<div class="container">
<div clas="left">A</div>
<div class="right">B</div>
<div class="footer">C</div>
</div>
CSS
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 50%;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 50%;
}
.footer {
background: none repeat scroll 0 0 #eef;
clear: both;
border-top-style:dotted;
}
The problem that you're experiencing is that the border of the element is not contained within the defined width of that element; so the element is 50% of its parents width, but with an additional width added by the border.
If you reduce the width of the elements to, for example, 48%, then it seems to work as you'd like:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 48%;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 48%;
}
JS Fiddle demo.
Edited with update,
You could, for Firefox and Chromium (FF5.x and Chromium 12.x on Ubuntu 11.04) use:
div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box; /* Left this in, but it doesn't seem to work... */
}
JS Fiddle demo.
Which incorporates the border width into the width of the element; with this approach you could retain the width: 50%; on the elements and borders would sort themselves out. Unfortunately it doesn't work on Opera or, presumably, IE.
Fixed
http://jsfiddle.net/euYTQ/19/
What you've got to remember is that a border counts + of the % assigned.
So say you have a box thats 100px's wide (100%), and you put one side with a 1px border (1%), thats actually 101%. So in your case, it was breaking to the next line of space, hence giving you your error.
In my fix i simply set the right container to 49%. Which would be great for fluid solutions, or if you have a fixed layout, set it to a fixed value.
Remember, padding is the same too... it will count + of the assigned size or percent.
Hope this helps!
The reason A and B are on different levels is because they don't fit into one width. You have them each declared with width: 50% but one of the also has a border. Border width is added to the width of the div - thus the two divs plus the border don't fit into horizontal spacing.
For example, try putting width: 49% on each of them - and you'll see the difference. This is not ideal, as you don't always know the width of the viewport. If you can work with exact pixel widths, it would be easier. Try this CSS for a change:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 374px;
border-right:dotted 2px black;
}
.right {
background:#eee;
float: right;
width: 374px;
}
.footer {
background: none repeat scroll 0 0 #eef;
clear: both;
border-top-style:dotted;
}
This is because 50% + 50% + 1px(the border) is higher than 100%.
If your .container isn't going to change width's you could give them both a fixed pixel value.
However if your .container is going to change width's you could try adding another element that contains the border alone like so:
.border {
height:100%;
width:0;
border-left:3px dotted #000;
position:absolute;
left:50%;
top:0;
}
Don't forget to give .container a position:relative;.
#Antojs; padding & border add width to the element if the element in percentage it's create problem. So; can give width to one like this:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 50%;
border-right-style:dotted;
}
.right {
background:#eee;
overflow:hidden;
}
Now in .right if you give border & padding it's not effect anything & you can also use css3 box-sizing: border-box; but it's not supported by IE7
check this fiddle http://jsfiddle.net/euYTQ/30/
The problem is that the border adds width to the div with .left. As the container div appears to be of fixed width, you could simply give the .left and .right elements fixed width values too (or reduce their percentage widths), and make .left slightly narrower:
.left{
background:#ddd;
float: left;
width: 372px;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 375px;
}
Here's an updated fiddle. I would also suggest reading up on the box model to get an idea of how borders, padding etc. add on to width.
http://jsfiddle.net/euYTQ/18/
50% and 50% = 100% so no space for the border.
Put your div right in the div left
<div class="left">section left
<div class="right">section right</div>
</div>
and change a little the css
.left{
background:#ddd;
float: left;
width: 50%;
}
.right {
background:#eee;
float: right;
border-left-style:dotted;
}
Example : http://jsfiddle.net/euYTQ/28/
I'm trying to make a part of my webpage that fit the width of the browser, for this I'm using width: 100%, the problem is that it shows scrollbars and I can't use overflow-x: hidden; because it will make some of the content hidden, so how I can fix this?
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin-right: 10px;
margin-left: 10px;
padding: 0;
-webkit-user-select: text;
}
Because you're using position: absolute, instead of using:
width: 100%; margin-right: 10px; margin-left: 10px
you should use:
left: 10px; right: 10px
That will make your element take the full width available, with 10px space on the left and right.
You have to remove the margins on the #news item
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin-right: 10px; /*REMOVE THIS*/
margin-left: 10px; /*REMOVE THIS*/
padding: 0;
-webkit-user-select: text;
}
If this doesn't work, you might have margin and padding set on the element itself. Your div - if that is what you are using - might have styles applied to it, either in your stylesheet or base browser styles. To remove those, set the margins specifically to 0 and add !important as well.
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin: 0 !important;
padding: 0 !important;
-webkit-user-select: text;
}
It seems that you have set the width to 100%, but there are also margins that force the width to expand beyond that.
Try googling for "css flexible ( two/three-collumn) layouts".
Here's an example,
<div id="cont">
<div id="menu"></div>
<div id="main"></div>
<div class="clear"></div>
</div>
and the css
#menu{
float:left;
height:100%;
width:200px;
}
#main{
padding-left:200px;
}
.clear{clear:both;}
The #menu div, will be aligned to the left and have a static width of 200px.
The #main div, will "begin" below #main, but because of it's 200px padding (can also be margin) its content and child elements will start - where #menu ends.
We must not set #main to a percent width, (for example 100%) because the 200 pixels of left padding will be added to that, and break the layout by adding scrollbars to the X axis.
I had a similar issue with a absolute positioned element, and I wanted to use width 100%. This is the approach I used and it solved my problem:
box-sizing=border-box
Otherwise I always had a little content and padding pushing past the scroll bar.
The answer is that you have margins set that will make the div wider than the 100%; hence the scrollbars.
If you can rid yourself of margins do it! However, often you'll want the margins. In this case, wrap the whole thing in a container div and set margins to 0 with width at 100%.
I am building a 3 column web page (header & menu bar above a left nav, main area and right sidebar with a full width footer on the bottom). I have background colors for each column but they cut off at the end of the text instead of filling the whole column. Is there a way to fill the whole column that does not rely on using a table (td)?
Here is my CSS for the general layout:
#container
{
float: left;
width: 100%; /* IE doubles the margins on floats, this takes care of the problem */
display: inline; /* this is where Ryan Brill (author of the ALA's article) and I go in "opposite directions" */
margin-left: -200px;
}
#left
{
float: left;
width: 180px; /* IE doubles the margins on floats, this takes care of the problem */
display: inline;
margin-left: 200px;
padding: 0px 0px 0px 5px;
background: url('/App_Themes/Default/images/RightColumnBackground.png') repeat left top;
}
#main
{
/* the width from #left (180px) + the negative margin from #container (200px) */
margin-left: 380px;
padding: 0px 0px 0px 5px;
}
#sidebar
{
/* this is to keep the content of #sidebar to the right of #main even if the content of "main is shorter */
padding-left: 100%; /* this is to "bring back" the #sidebar that has been moved out of the viewport because of the padding value */
margin-left: -220px;
}
I know I can set a height in the style but that pins the height to a certain number of pixels. Is there a height: fill; type option?
This is a very common problem. A good approach is to use faux columns.
Not in any CSS that'd be currently widely supported across browsers, but there are ways of approximating it.
What you need is the Perfect Liquid Layout in Percentage widths, em widths, or pixel widths.