tags:

views:

139

answers:

4
+3  Q: 

2 <div>s in <div>

the best way center 2 divs inner div. Like this

  1 2 3 4 5 6 7 8 9 10 11 12 
1 ---------------------------
2 -       main div          -
3 -    ------      ------   -
4 -    -div1-      -div2-   -
5 -    ------      ------   -
6 -                         -
7 ---------------------------

Width and Height of main div is fixed.

thanks

A: 

float one to the left and two to the right

  <div style="width:100px;">
      <div style="float:right;width:50px;">I m div 1</div>
      <div style="float:left;width:50px;">I m div 2</div>
  </div>
Yassir
what about vertical align?
loviji
the vertical alignment can be done with the aid of JavaScript, margins/paddings or CSS3.
Mickel
is this actually centering the two inner divs? div 1 will hard right, and div 2 hard left
Richard
Hi, Richard, as you say, it is not centering, divs will be hard left\right.
loviji
@loviji : would you use javascript ?
Yassir
A: 

a simple solution might be to use a table:

<table>
<tr>
<td>Div 1 content here</td><td>Div 2 content here</td>
</tr>
</table>

Centering the content in each of the tds will give you what I think you're after.

Edit: OK this is getting voted down because I'm suggesting using a table for "presentation". At the end of the day, this works. It will give the desired result. The first answer, voted up, using css, is wrong. I cannot see that using a table instead of a div will ever have any impact on maintainability or future design changes, or that it makes any difference whatsoever.

Edit 2: To complete my answer, here is an example:

<html>
<head>
</head>
<body>

<style>
  div { width: 100px; height: 30px; border: 1px solid red; margin: 0 auto 0 auto; text-   align: center; }
  table { border: 1px solid blue; width: 400px; }
  td { padding: 20px 0 20px; }
</style>

<table>
<tr>
  <td> <div class="one">div 1</div> </td>
  <td> <div class="one">div 2</div> </td>
</tr>
</table>

</body>
</html>

This will give you the layout requested. I have added some padding to the divs in order to demonstrate one way of vertically aligning your content.

Richard
thanks, I know how to center table in a div horizontally align="center", but vertically with css and html is possible?
loviji
using tables for layout is so out of date.
cherouvim
-1 for suggesting tables
Charlie Somerville
@cherouvim: sorry - that is nonsense. there is nothing wrong with using tables, if that is the simplest, cleanest solution. If you think that wasting time faffing about with css is a better alternative to 5 seconds implementing a table, you have too much time on your hands.
Richard
@Richard: It's not a clean solution since it couples structure with presentation
cherouvim
@loviji: 1) don't listen to these people who are saying "don't use tables". Since the intro of CSS it has, for some, become very fashionable to dismiss using tables for _any_ kind of layout and that it should all be done in css. Last time I looked <table> was perfectly valid HTML4 and if it offers the simplest solution, use it!
Richard
@cherouvim: and how is two nested divs in a parent div any less structure than the table suggestion?
Richard
@loviji: 2) to center a block element using css, you can do: margin: 0 auto 0 auto;
Richard
@Everyone: this just about sums up Stack Overflow: The WRONG answer has been voted up twice, and an answer which gives a totally acceptable solution is being voted down!!
Richard
+1 for suggesting tables. CSS purists **would** be right **if** it was possible to achieve the desired layout without resorting to every kind of ugly trickery... people speak of accessibility and semantics and then you should fix your layuout with JavaScript?! Pure CSS divs just don't have enough tools and you can just get away with horrible hacks. Yes it **should** be the right way... if it only worked as it should.
kemp
@Richard this only hardly about validity. <table> should be used to represent tabular data and not for layouting. It's about semantics, not syntax.
Horst Gutmann
We don't know what the divs contain. Perhaps they represent tabular data.
DR
+1, I agree with Richard, there's nothing inherently wrong with using tables.
Dave Paroulek
@Richard: I agree with everything you said, but in this case the OP asked about divs. You are providing an alternative, but not an actual answer.
DR
@DR judging from the example of the OP I can only imagine a layouting-problem, but if tabular data is really what is represented here, then, OP, by all means use a table. That's what it's there for after all :-)
Horst Gutmann
@kemp: thanks, you have expressed the issue perfectly
Richard
Jeez. The problem with this answer is not that it uses tables (come on guys, seriously, get over it; a simple table isn't the end of the world; the real problem with yesterday's complex nested table layouts was poor accessibility and maintainability), but that it doesn't include the additional styling that would be necessary to actually centre the elements.
bobince
thanks peoples for attention and activity.each div include only simple text.
loviji
@bobince, loviji: I have added some example html, css to my answer
Richard
This is easily done with css, why add tables into the mix?
Davey
A: 

use the css by this position:absolute; or position: relative; and use left: px; top: px;

moustafa
+1  A: 

How about this? The only fixed thing you need to set are the margins of the inner div's, but this shouldn't harm as you didn't tell about any restrictions in the margins in the comments on this topic. As to the styles, only the borders are purely presentational, the remaining are the minimum required styles.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 1892901</title>
        <style>
            #main {
                position: relative;
                width: 500px;
                height: 300px;
                border: 1px solid black;
            }
            .inner {
                position: absolute;
                top: 0;
                bottom: 0;
                margin: 20px;
                border: 1px solid black;
            }
            .inner.left {
                left: 0;
                right: 50%;
            }
            .inner.right {
                right: 0;
                left: 50%;
            }
        </style>
    </head>
    <body>
        <div id="main">
            <div class="inner left">inner left</div>
            <div class="inner right">inner right</div>
        </div>
    </body>
</html>

I should add, the strict doctype is very important, otherwise this ain't going to work in IE due to the box model bug.

BalusC
really, works, thanks
loviji
You're welcome.
BalusC