tags:

views:

78

answers:

6
+1  Q: 

Div's in one line

Here is my code:

<style type="text/css">
    div.page {
      text-align:center;
      border: 1px solid rgb(0,0,0);
      width:20px;
      height:20px;              
    }

    span.page {
      text-align:center;
      border: 1px solid rgb(0,0,0);
      width:20px;
      height:20px;              
    }
 </style>


<div class="page">1</div>
<div class="page">2</div>
<div class="page">3</div>

<span class="page">1</span>
<span class="page">2</span>
<span class="page">3</span>

Div's look fine but they places vertically. Is there any way to place them horizontally in one line?

Span's place in the one line, but the span can not have the width as any inline element. If there is no way to use DIV's and SPAN's for my task I will use a table, but I am looking for the no-table solution.

+1  A: 

Use

display:inline-block

in the div's style

Samuel
just one thing: inline-block isn't supported in IE6 and didn't play good in IE7.
xandy
In this case DIV will not have a width :)
demas
@demas: with inline-block they will. For ie7 workaround you can look here http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/
Jan
I use firefox and they look like span.
demas
+1  A: 

Use this

div.page {
    text-align:center;
    border: 1px solid rgb(0,0,0);
    width:20px;
    height:20px;              
    float: left;
}
Lorenzo
+1  A: 

use display:inline; and your div's will be in one line.

other solution : float:left;

Samuel
'display:inline' will not have a witdh; float:left is a good solution. thanks.
demas
+1  A: 

You can try out with the combination of ul/li with list-style ( css property ) as none.

some thing like

<ul> <li> <div ....</li> <li><div...></li></ul>

or

you can try within table / tds inside divs.

kadalamittai
Why div in li? If you put ul and li just because you want it to style, that's totally out of the point.
xandy
A: 

Lorenzo's answer is correct, but I would add something to the markup:

<div class='pageHolder'>
    <div class='page'>1</div>
    <div class='page'>2</div>
    <div class='page'>3</div>
    <div class='pageHolder-footer'></div>
</div>

in CSS, add:

div.pageHolder-footer{
    clear: left;
    height: 0;
}

So that the rest of your stuff will flow correctly.

==Alternative method (From Jan, and SitePoint) ==

No need to have the div.pageHolder-footer (but keep pageHolder). And then:

div.pageHolder { overflow: auto; } /* Jans' method */
/* or */
div.pageHolder { overflow: hidden; } /* From SitePoint */

They both may have drawbacks, but it depends on what you need.

xandy
or .pageHolder{ overflow: auto; }
Jan
What is purpose of pageHolder-footer?
demas
@Jan, you missed the point. Coz float left may make the div.pageHolder became zero height, adding the footer and clear the left is to make the pageHolder can actually "wrap" the content; and also the remaining stuff (like p or span) won't stay next to those div.page, overflow:auto doesn't help to solve this.
xandy
@xandy: yes it does, it's one of the best kept CSS secrets. Try it and you'll never use useless empty div elements to clear floats.
Jan
@Jan, if it works, it would be interesting (and clean). thx, I definitely will look at it.
xandy
It's in fact the same method, any value for overflow will do
Jan
+3  A: 

xandy is correct, but this is better:

<div class='pageHolder'>
    <div class='page'>1</div>
    <div class='page'>2</div>
    <div class='page'>3</div>
</div>

with CSS:

.page {
  text-align:center;
  border: 1px solid rgb(0,0,0);
  width:20px;
  height:20px;              
  float: left;
}

.pageHolder{
  overflow: auto;
  width: 100%;
}

Elements to clear floats is markup. It's like using <br> but for floats. Mixing markup and content is considered bad practice in semantic web.

Read this article for more information.

Jan
+1 This is the better solution.
Jeremy