If you have a number of block items in a list, I was wondering if there was a way to get
0 margin on the left,
0 margin on the right, and
margins between the items in the middle
Right now on the items in the list I only know how to do this margin:0px 5px 5px 0px;
This way there is no margin on the left, so it matches to the left of my container, but there is always 5px space on the far right column... however I need margin-right 5px to get space in between my items.
Any suggestions?
div.container{
width:calc(100% - 100px);
margin: 0 auto;
height:100%;
display:block;
background:black;
}
div div{
height:200px;
width:160px;
display:inline-block;
background:grey;
margin:0px 5px 5px 0px;
}
Ref: http://jsfiddle.net/Ly2M3/1/
Again I would like my items to be able to but up against the far left and right side of the container but also have margins to separate the items.
Yes, add text-align: justify to the div.container element.
I'm making another answer because it works well at centering the divs and it also matches the divs on the last line!
It requires a little knowing of the content you are using but some media queries can help you remove some extra space if you need to support multiple resolutions.
You need two things:
* The text justification (as Josh Rutherford answered) to align all lines except the last, and second
* A few invisible divs to fill the rest of the line so the browser can match the divs on the last line with the rest of the lines
The result is this: http://jsfiddle.net/wBe4F/1/, resize it so you can see the last line always matches the other lines no matter if there are 1, 2, 3, 4 or 5 divs, they always match.
So, the idea is to add extra divs with height 0px to fill one more line so the browser will also justify it and all visible divs will be aligned.
You only need to think how many ".extra" divs you put at the end. I would put enough .extra divs to cover a complete line with the widest screen size you want to support, then you could add media queries to hide unneded .extra divs if they are making too much extra space at the bottom of your container div (the background color is there to show that). Resize it small enough and you'll see all that space below the original divs created by all those unneeded .extra divs. You could give them an id for each .extra div and "display: none;" some of them if the resolution is less than some values to keep that controlled.
To make the extra divs the same width as the original divs just add them to the original divs declaration:
.data, .extra {
display: inline-block;
width: 120px;
height: 40px;
background: #666;
}
and then tweak them a little to make them invisible
.extra {
height: 0px;
}
Related
I have two divs. One that is floated left and one floated right. The one of the left has a width set to 18% and a min-width of 217px. I want to have the div on the right take up the remaining space, while also being able to resize to fit the window.
The problem I am having is that I can set the right div's width to 82% and to float right, which works until I make the window side too small, in which case the min-width of the left div kicks in and it stops shrinking. The right div doesn't have enough space to fit, so it is pushed down.
Here's some sample code.
HTML
<div id="div1">
stuff inside of it
</div>
<div id="div2">
stuff inside of it
</div>
CSS
#div1
{
float: left;
width: 18%;
height: 100vh;
min-width: 130px;
box-shadow: 0px .3em .2em #000;
z-index: 2;
}
#div2
{
width: 82%;
float: right;
z-index: 1;
}
So this is where I'm stuck, how should I approach fixing div2? I've tried using a table instead of divs, but a border appeared around the cells that I couldn't change and it removed my box-shadow, so I would prefer a solution without it.
Your thinking of using tables is somewhat on the right track, as table elements do actually have many properties that make them capable of such a thing, but as people are pointing out in the comments, it's no longer a valid approach to use table elements for the purposes of layout for non-tabular data.
This is why CSS implemented a set of style rules built to reflect those unique properties. You can set a container around two elements with the style display: table;, and then give it's children the style display: table-cell;
Setting the width for the right side div to 100% will ensure it always fills as much space as is available to it.
But, since table cells can't break to a new row when the content exceeds the width of the table, it will automatically adjust to fit. So when another div (the left one) has a specific min-width, the div on the right is given less space in order to keep the cells contained.
Here's an example using your code:
http://jsfiddle.net/Q5rjL/
CSS table display properties give you all the benefits of these unique elements, but without the semantic issues. They are great for complex layouts where other style display types fall short.
You can also contain floats with overflow:hidden:
#div2{
overflow:hidden;
z-index: 1;
}
The DIV will fill up the remaining space (http://jsfiddle.net/MAjwt/)
I need DIV blocks which have some data. Due to different content, blocks have variable height. I need to align div blocks by two. Because of the different height of blocks there is "free space" remaining. How to align these blocks so it would be aligned properly?
The data comes from JSON reques so making table seems : 1. more tricky to make grid using JS (2 elements in a row) 2. Obsolete (?) . What would you recommend?
CSS:
.book-details{
float:left;
display:block;
border-style:solid;
border-width:1px;
padding: 2px 2px 2px 2px;
border-color:gray;
width: 450px;
display: table;
}
PIC:
IE7 and below doesn't get display:table, for IE8, you have to use the html5 doctype, ie 9 will work.
Better to avoid it and just float. Remember your box model. Your width is actually 456px so what's the parents' width?
And, you should clear the float after two if you're just doing 2x across.
You could just set a height that will be tall enough for the longest div. What you're pulling in seems to be easy enough to estimate.
I have Div named Container whose width is 100%; so its actual width in pixels can be changing from one user to another. Inside that Container, I have 10 boxes, each with the following values:
width:8%;
margin-left:1%;
margin-right:1%;
border:1px solid #000000;
float:left;
With those values, it should render like this, right?
Actually it's not. The boxes have 8% width in space and 2% width out space (margin)and because of the border line (1px), every box have 10% width + 2px
When you play with the resolution, you will see that from time to time the boxes are extending a second line and it appears like this:
How can i fix this layout?
I thought about placing another box inside those boxes which would set the border for the outer box, but it's not quite working for me.
I have created a jsfiddle for this. Adding a new div for each inner boxes and setting following properties will solve the problem. (The key was to move the border from the out boxes to the inner box.)
width: 100%;
height:100%;
border: 1px solid #000000;
I have a fluid width layout. Left and right column have a fixed width and my center column changes its width in between max and min width specified as the browser width changes or screen resolution changes. It looks like image below:
As you can see, there are some small containers in middle column, they hold up several products etc.
The problem that I am experiencing is that when the width changes and middle column cannot accommodate 3 containers, 1 will fall below, as they are floated and then it looks like something below:
Now this space that comes in the right of containers looks ugly. What I want to do is to keep them centered if one falls below when width decreases then two should appear in the center of the middle column and when another one falls then 1 container left should also appear in the middle like below:
Can I do this with css only? or I need to introduce some scripting language for doing it dynamically?
This is the css for container, that I am using
.prod-container{
float: left;
width: 180px;
height: 290px;
margin: 2px 6px;
border: 1px solid black;
}
Example on http://www.myappontest.heliohost.org/index1.html
Replacing float: left; with display: inline-block; in .prod-container's style and adding text-align: center; to #center-content-container's style will achieve the desired behavior.
Working version of your page: little link. Here's the modified CSS file, too: another little link.
Hope that helped you in any manner!
I would put the center floating blocks inside a div with a max-width 100% (in the scope of the center column). Center that block with "margin-left: auto margin-right: auto". This will keep the floating blocks moving to the next line if there's not enough room, and the centering div will size with it's contents, allowing it to center within the center column.
I have a fixed width container <div> that displays one or more widget <div>s. I want it to look like this:
<- grey blocks are widgets, red border is the container
Simplified, my structure in HTML looks like this:
<div id="container">
<div id="widget1">1</div>
<div id="widget2">2</div>
<div id="widget3">3</div>
<div id="widget4">4</div>
<div id="widget5">5</div>
<div id="widget6">6</div>
<div id="widget7">7</div>
</div>
Considerations
Widgets will have a fixed height e.g. 100px
Widgets will have a fixed width e.g. 100px but they may also be a multiple of that width (plus any margins crossed - see widget 1)
Widgets should be spaced nicely with a margin (or similar) e.g. 10px
I don't know how many widgets there will be (the user can assign as many or few as they like to the container).
The container is a fixed width but doesn't have any "visual" styling (the red border is there for demonstration)
Solution has to work in modern browsers (and MSIE7) and would ideally be pure CSS.
Because of consideration 4. I can't assign additional markup e.g. row div, classes (.first-child, .last-child) and because of 2. :nth-child wouldn't work AFAIK.
Things I've tried
margin-left on widgets with :first-child setting margin-left: 0 won't display a new row properly.
margin-right on widgets with :last-child setting margin-right: 0 the first row forces the container div wider and last-child isn't supported until MSIE9.
equal left and right margins (e.g. margin: 0 5px 10px) forces the container wide again.
overflow - works great in my head! Not so much with either margins or padding.
Is there a way to do this in CSS?
http://jsfiddle.net/agtb/VHXGT/
I believe you are thinking too complicated :-)
If I understand you correctly you don't need any special handling of the separate widgets. Just give the widgets an all around margin of half the spacing, and the container the same margin but negative.
#container {
width: 440px;
margin: -5px;
}
#container div {
background-color: gray;
height: 100px;
width: 100px;
float: left;
margin: 5px;
}
See http://jsfiddle.net/SGdG3/1/
set container width 400 and the first div width 200 float left, rest width 100 float left