How do I keep two divs on the same line?

105,416

Solution 1

You can make two divs inline this way:

display:inline;
float:left;

Solution 2

For me, this worked much better, as it didn't eliminate spacing between floated items:

display:inline-block;

In case that helps anyone else.

Solution 3

Simple do display: inline-block on both div's but be sure and set min-width and max-width both. Example below:

div {
  max-width: 200px;
  min-width:200px;
  background:grey;
  display:inline-block;
  vertical-align: top;
}
<div>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
</div>

<div>
  <p>test 2</p>
  <p>test 2</p>
  <p>test 2</p>
</div>

Solution 4

If you want to make more than one div in a single continuation, then just add the below line of code to your css file, with each div, div class etc.

display: inline-block;

Solution 5

Add this to the container div

.container_class {
    display: flex;
    justify-content: center;
}
Share:
105,416
Riet
Author by

Riet

I do primarily scientific computing in research, but dabble in web design as well.

Updated on May 31, 2021

Comments

  • Riet
    Riet about 3 years

    I've been working on a dropdown menu similar to suckerfish. I've got the dropdown side working now, but I have some images I'm trying to put on either side of the links. Right now I'm using a div the size of the image, then setting the background-image property to the image I need (so that it can change using the pseudo :hover class).

    Here is the CSS That applies:

    ul#menu3 li {
        color: #000000;
        float: left;
        /*I've done a little playing around here, doesn't seem to do anything*/
        position: relative;
        /*margin-bottom:-1px;*/
    
        line-height: 31px;
        width: 10em;
        padding: none;
        font-weight: bold;
        display: block;
        vertical-align: middle;
        background-image: url(../../images/dropdown/button-tile.gif);
    }
    .imgDivRt {
        width: 20px;
        height: 31px;
        display: inline;
        float: right;
        vertical-align: middle;
        background-image: url(../../images/dropdown/button-right.gif);
    }
    .imgDivLt {
        width: 20px;
        height: 31px;
        display: inline;
        float: left;
        vertical-align: middle;
        background-image: url(../../images/dropdown/button-left.gif);
    }    
    

    I was using selectors to save a little on having different classes, but internet explorer doesn't seem to support them :(

    Here is my HTML that applies:

    <li><div class="imgDivLt"></div>Option 1<div class="imgDivRt"></div>
    <ul>
        <li><a href="#"><div class="imgDivLt"></div>Sub 1<div class="imgDivRt"></div></a>
            <ul>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sandwich<div class="imgDivRt"></div></a></li>
            </ul>
        </li>
        <li><a href="#"><div class="imgDivLt"></div>Sub 2<div class="imgDivRt"></div></a> 
        <ul>
                <li><a href="#"><div class="imgDivLt"></div>Sub 1<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sub 2<div class="imgDivRt"></div></a> </li>
                <li><a href="#"><div class="imgDivLt"></div>Sub 3<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sub 4<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sub 5<div class="imgDivRt"></div></a></li>
                <li><a href="#"><div class="imgDivLt"></div>Sub 6<div class="imgDivRt"></div></a></li>
            </ul>
        </li>
        <li><a href="#"><div class="imgDivLt"></div>Sub 3<div class="imgDivRt"></div></a></li>
        <li><a href="#"><div class="imgDivLt"></div>Sub 4<div class="imgDivRt"></div></a></li>
        <li><a href="#"><div class="imgDivLt"></div>Sub 5<div class="imgDivRt"></div></a></li>
        <li><a href="#"><div class="imgDivLt"></div>Sub 6<div class="imgDivRt"></div></a></li>
    </ul>
    </li>
    <li><div class="imgDivLt"></div>Option 2<div class="imgDivRt"></div>
    

    I'm not sure if there's a glitch in my code, or if I'm on the wrong track. It works in firefox, safari, and chrome, but not IE or opera, so I'm not sure if they're making up for stupidity or what. Ideally, the second image floats greedily to the right in the containing block. In the problem browsers, it sits the next line down (at the far right at least)