Forum Moderators: not2easy

Message Too Old, No Replies

Laying out 3 div's in a row and not a column

         

theshib

8:51 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



Hi!

I have 3 text blurbs that i would like to position side be side.

Here's some example code:

<div style="">
<div>3 blah blah blah </div>
<div>2 blah blah blah </div>
<div>1 blah blah blah </div>
</div>

I want this to appear as

3 blah blah blah 2 blah blah blah 1 blah blah blah

instead of

3 blah blah blah
2 blah blah blah
1 blah blah blah

Am I headed in the right direction? What's the solution?

Thanks for any help!

swa66

12:12 am on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"
display: inline
" or alternatively "
display:inline-block
" would do what you need (note the standard says inline is default but browsers give div a block property).
[w3.org...]

Another approach (much used) is to float the inner divs.

I'm a bit hesitant to say you're in the right direction. Use <div>s sparingly, you can style any element. Use those that make the most sense should there be no stylesheet (e.g. screen readers "see" it without the stylesheet ...

penders

3:42 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(note the standard says inline is default but browsers give div a block property)

I guess the browsers internal stylesheet will define block-level elements as

display:block
. If not then it defaults to
display:inline
?

theshib

5:09 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



Thanks for the replies but it doesn't appear to be working. I forgot to mention there's breaks in each div

<div>

<div>1 blah blah <br> blah blah blah </div>
<div>2 blah blah <br> blah blah blah </div>
<div>3 blah blah <br> blah blah blah </div>

</div>

I would like each chunk/div to be next to each other. This entire dive is inside a <center> tag too =/

topr8

5:59 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you need to give each div a width, either in px or % etc.

float:left;width:150px;

swa66

2:41 am on Apr 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The line breaks would force you towards "
display:inline-block
, but that one has *bad* support form many browsers (IE6, IE7, FF2 all mess it up AFAIK)"

So floating it will be a good solution, but that one makes centring it very hard.

To get it centered using CSS you'll need to set some width on it I'm afraid.

The standard way to center an element is to set auto margins left and right. To center text inside an element use "

text-align:center
"

Anyway the following works nicely in a browser that suports display:inline block (e.g. safari):


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" \
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Title</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
#test {
text-align:center;
}
#test div {
display:inline-block;
text-align:left;
}
</style>
</head>
<body>
<div id="test">
<div>3 blah<br /> blah blah</div>
<div>2 blah<br /> blah blah</div>
<div>1 blah<br /> blah blah</div>
</div>
</body>
</body>
</html>

I didn't bother testing it in IE, will probably break miserably.

Alternatively display:table-* could be used, but that too is not without it's lack of support

Dave75

4:31 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



This entire dive is inside a <center> tag too =/

That div needs the following properties:

div{width:auto;margin:0 auto;}

The top and bottom value for margin is arbitrary, the key is your side margins.

Xapti

7:12 pm on Apr 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dave75, as far as I know, that doesn't work unless you assign the divs widths (and for the centered div, width:auto is a default, so you don't need to include it). Now using ems, you could get a pretty accurate value, but still it's not perfect, and if the text is dynamic, then it wouldn't work at all, without using javascript.

The center tag is deprecated, like dave said, if you want to center something (block elements), margin:0 auto is the best way to go, the only thing is you need set widths.

What you may want to do is ask yourself if it needs to be centered at all though. If not, then there would be no problem. If just adding a fixed left margin is good enough, then just do that. If it really needs to be centered, then use ems for the widths, and size a few percent extra to account for different fonts (which will slightly offset centering but one would not notice)

swa66

11:27 pm on Apr 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A div in the flow (not a floated one e.g.) with auto margins is actually rendered as having the full available width, and hence auto margins could center the block, but due to the width it's still positioned right where it would be left aligned. (there's no space to move the block about). Add borders/background on your blocks to see what I mean.

The alternate to using auto margins on a block is to use text-align on the parents of inline elements, that way you do not need to know (or guess) the width. Add to it IE6's unwillingness to center some block every so often ...

I hardly dare mention it, but display: table-* ... could be an alternative, but IE is even less cooperative there.

swa66

11:27 pm on Apr 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A div in the flow (not a floated one e.g.) with auto margins is actually rendered as having the full available width, and hence auto margins could center the block, but due to the width it's still positioned right where it would be left aligned. (there's no space to move the block about). Add borders/background on your blocks to see what I mean.

The alternate to using auto margins on a block is to use text-align on the parents of inline elements, that way you do not need to know (or guess) the width. Add to it IE6's unwillingness to center some block every so often ...

I hardly dare mention it, but display: table-* ... could be an alternative, but IE is even less cooperative there.

theshib

2:14 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Hey guys,

Thanks for all the input. I will get back to tinkering with this using some of your suggestions!