- This topic is empty.
-
AuthorPosts
-
March 18, 2013 at 6:27 am #43468notfilcMember
I’m trying to create a grid of divs (without using a table!). What I don’t want to happen is any doubling up of borders, it should all be 1px.
I’ve done the following which works great when the grid is full:
The basis of this is the following css:
.box {
width: 33%;
float: left;
box-sizing: border-box;
display:inline-block;
border-left:1px solid black;
border-top:1px solid black;
}.outer {
width: 100%;
height: auto;
line-height: 0;
border-right:1px solid black;
border-bottom:1px solid black;
}
But when items are missing (as with the example above) there are some missing borders (bottom of div 6, right of div 8) as would be expected with the solution I have used.Does anyone have a better way of doing this? I don’t really want to be adding blank divs but would accept a jQuery solution.
The width may not always be 33% – it may be 25% or even 10% sometimes so a css table solution here probably won’t work either.
March 18, 2013 at 7:11 am #128162Paulie_DMemberI think javascript will be your answer but if you KNOW how may divs will be across you can do things with pseudo classes..
March 18, 2013 at 7:12 am #128163SenffParticipantNot sure if this would cover all possible situations, but you might want to switch it around and give the outer box a top/left border and each individual div a bottom/right border: http://jsfiddle.net/senff/vrLhY/41/
If you also need a border at the bottom right (where there’s a DIV “missing”) then you can give the outer box also a bottom right border and then work with some negative margins.
March 18, 2013 at 8:12 am #128616notfilcMemberThat looks perfect Senff – thanks a bunch. I won’t mark this as solved yet as I would be interested to see any other methods but that looks awesome.
March 18, 2013 at 8:44 am #128610wolfcry911ParticipantHere’s a pen similar with a different technique:
http://codepen.io/wolfcry911/pen/zAyFw
edit// for what it’s worth, it can be adapted to full width container and percentage children
March 18, 2013 at 9:17 am #128613notfilcMemberThanks – looks like a nice solution too but Senff’s is probably a bit more old browser friendly.
March 18, 2013 at 9:24 am #128614wolfcry911ParticipantObviously you’re free to choose any method you like, but tell me how border-box box sizing and inline-block are more friendly to older browsers than negative margins?
March 18, 2013 at 9:59 am #128624MerriParticipantQuickie: http://codepen.io/Merri/pen/KgBDE
March 18, 2013 at 10:28 am #128628SenffParticipant@wolfcry : negative margins have more (older) browser support than border-box I think? My original solution doesn’t involve either, though! :)
March 18, 2013 at 11:52 am #128639MerriParticipantThis feels so wrong, but it works: http://codepen.io/Merri/pen/ptwcg
Browser limitations…
– IE8: no inner borders. I take it IE8 places them incorrectly somewhere it shouldn’t place them.
– IE7: no inner borders, percentages for widths are no good (unless you round them down yourself).
– IE6: same as IE7.I guess it is likely to expect mobile browsers to get this wrong.
**Edit!**
Improved further: now row height kind of inherits to each cell in the row.
March 18, 2013 at 7:37 pm #128688notfilcMemberThis is awesome guys – I haven’t had a chance to fully look through all of these yet but certainly keep them coming – maybe a good article for the blog I’m launching soon. I’ll drop a better reply in here tomorrow.
-
AuthorPosts
- The forum ‘CSS’ is closed to new topics and replies.