Placing two divs at same horizontal level

Go To StackoverFlow.com

6

I need to put two div elements at the same horizontal level. Doing the way I have done makes the second one get displayed under the first. I want to place them in a way that they cross over each other while transition.

Edit 1- Here when the button is pressed to swap their classes, divs move up and down.

#aaidi,#aaidi3 {
-webkit-transition: margin-left 1s ease-in-out;
-moz-transition: margin-left 1s ease-in-out;
-o-transition: margin-left 1s ease-in-out;
transition: margin-left 1s ease-in-out;
}
.left {
margin-left: 45%;
border: 1px solid green ;
width: 20px;
height: 4px;
background: #FF0000;
} // similar for right with margin-left:55%
......
        <tr>
           <td colspan=3>
              <div id="aaidi" class="left">
              </div>
              <div id="aaidi3" class="right">
              </div>
           </td>
        </tr> // after this there is a button pressing whom changes the class of both divs. 
2012-04-05 16:40
by Sumit Sinha
Dear see my answer and let me know if i am lagging some where - w3uiguru 2012-04-05 16:53
Thanks Dinesh, but its not working when I put the button there. See comments in your answer - Sumit Sinha 2012-04-05 17:03
Here when the button is pressed to swap their classes, divs move up and down - Sumit Sinha 2012-04-05 17:22


12

You can achieve this using the float property:

<style type="text/css">
    div.container {
        margin: 15px;   
    }
    div.left, div.right {
        float: left;
        padding: 10px;    
    }
    div.left {
        background-color:orange;    
    }
    div.right {
        background-color: yellow;    
    }
</style>

<div class="container">
    <div class="left">Left</div>
    <div class="right">Right</div>
</div>

See this jsFiddle for a demonstration. Here's the output:

enter image description here

2012-04-05 16:42
by James Johnson
Please see why isn't this working link . - Sumit Sinha 2012-04-05 17:01
It's dropping to the next line because the combined margin equal 100%, which forces the element to drop to the next line. See this updated fiddle: http://jsfiddle.net/ht6M9/2 - James Johnson 2012-04-05 17:06
Here when the button is pressed to swap their classes, divs move up and down - Sumit Sinha 2012-04-05 17:20
Try adding the property (display:inline-block) to the styl - Uttam 2014-02-06 13:16


2

Use a span, or you can set the style to display:inline-block

2012-04-05 16:41
by Justin Pihony


2

I believe you would like to float the divs

.float-left{
  float:left;
}

 <div id="aaidi" class="float-left left"></div>
 <div id="aaidi3" class="float-left right"></div>

If the right div is still being viewed as a 'block' element then it will take up the entire row. Both elements need to be floated, or specific widths need to be set.

2012-04-05 16:43
by Kevin Bowersox


1

see fiddle for code and demo

fiddle: http://jsfiddle.net/ht6M9/

demo: http://jsfiddle.net/ht6M9/embedded/result/

2012-04-05 16:53
by w3uiguru
but see now it is not working link . - Sumit Sinha 2012-04-05 17:00


0

Just float all divs left if you require them to be side by side.

Depending on contents of div, you may need to assign a width to each class too.

2012-04-05 16:46
by zigojacko
Ads