14
votes

I'm hoping there's a way to do this without JavaScript. I have two elements displayed with inline-block. They are both 200 pixels in width and height, so they both appear on the same line unless the browser is sized very small (or with mobile browsers). I want there to be a 50px space between the two elements, so on the second element I added "margin-left: 50px", which works fine. When the browser is resized to a size where both elements cannot fit on the same line, the second element wraps to the next line, which is what I want it to do. The problem is that the second element still has the 50px left margin, so the elements don't appear centered. I could add JavaScript to detect when the container height changes (i.e. the element wrapped to the next line) and remove the left margin, but is there a way to accomplish this without JavaScript?

Here's my code, simplified:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <div id="wrapper" style="text-align: center;">
            <div id="elem1" style="display: inline-block; background-color: #f00; width: 200px; height: 200px;"></div>
            <div id="elem2" style="display: inline-block; background-color: #00f; width: 200px; height: 200px; margin-left: 50px;"></div>
        </div>
    </body>
</html>

Fiddle: http://jsfiddle.net/YRshx/

7
I think this will work. It's a pretty cool technique. Let me give it a try... - Gavin
Actually, I should have realized from the demos that this won't work in my case because after wrapping, the elements are left aligned, not centered. Thanks though, I can use this technique in the future for sure. - Gavin

7 Answers

14
votes

Based on bastianonm's solution, try this:

    <div id="wrapper" style="text-align: center; margin:0 -25px;">
        <div id="elem1" style="display: inline-block; background-color: #f00; width: 200px; height: 200px; margin:0 25px;"></div>
        <div id="elem2" style="display: inline-block; background-color: #00f; width: 200px; height: 200px; margin:0 25px;"></div>
    </div>

http://jsfiddle.net/YRshx/6/

6
votes

Here;s a different approach to the problem. It exploits the fact that spaces are discarded if they are at the start or end of a line. So it uses a space to separate the blocks.

Fidlle: http://jsfiddle.net/xKVG3/

<div id="wrapper">
  <div><div id="elem1"></div></div>
  <div><div id="elem2"></div></div>
</div>

#wrapper { text-align:center; }

#wrapper > div > div { 
    display: inline-block; 
    width: 200px; 
    height: 200px; 
    vertical-align:top;
}

#elem1 {
    background-color: #f00;
}
    
#elem2 {
    background-color: #00f;
}

#wrapper > div {
    display:inline;
}

#wrapper > div:after {
    content: ' ';
    font-size:12.5em;
    line-height:0px;
}

#wrapper { text-align:center; }

#wrapper > div > div { 
    display: inline-block; 
    width: 200px; 
    height: 200px; 
    vertical-align:top;
}

#elem1 {
    background-color: #f00;
}
    
#elem2 {
    background-color: #00f;
}

#wrapper > div {
    display:inline;
}

#wrapper > div:after {
    content: ' ';
    font-size:12.5em;
    line-height:0px;
}

#wrapper  {
  border:2px solid black;
  animation: 4s linear 0s infinite alternate wide;
}

@keyframes wide { 
  from { width: 490px; } 
  to { width: 430px; }  
}
<div id="wrapper">
  <div><div id="elem1"></div></div>
  <div><div id="elem2"></div></div>
</div>
2
votes

You could do something similar to:

@media screen and (max-width: 453px){
    #elem2 { margin-left:0 !important; }
}

http://jsfiddle.net/YRshx/3/

1
votes
    <div id="wrapper" style="text-align: center;">
        <div id="elem1" style="float:left; display: inline-block; background-color: #f00; width: 200px; height: 200px; margin:0 25px;"></div>
        <div id="elem2" style="float:left; display: inline-block; background-color: #00f; width: 200px; height: 200px; margin:0 25px;"></div>
    </div>
0
votes

Working jsFiddle Demo

Try to add margin to both left and right, and to your both elements:

<div id="wrapper" style="text-align: center;">
    <div id="elem1" style="margin: 0 25px; display: inline-block; background-color: #f00; width: 200px; height: 200px;"></div>
    <div id="elem2" style="margin: 0 25px; display: inline-block; background-color: #00f; width: 200px; height: 200px;"></div>
</div>

However based on your real layout, this trick maybe won't work, or need more things.

0
votes

Just keep the inline container in a inline div and float them...

Code:-

<div id="wrapper" style="text-align: center;">
    <div id="outer" style="display: inline-block;">
    <div id="elem1" style="float:left; background-color: #f00; width: 200px; height: 200px;"></div>
    <div id="elem2" style="float:left; background-color: #00f; width: 200px; height: 200px; margin-left: 50px;"></div>
    </div>
</div>

Demo:- http://jsfiddle.net/YRshx/2/

Thanks...

0
votes

If you have more elements inside one container and wrap them evenly use below code it will work perfect.

I am using UL, LI and minimal css

https://jsfiddle.net/mkpasala/ayw8szcn/

<h1>Wrap children evenly</h1>
<ul class="skillscont-list">
  <li>TestSkill</li>
  <li>TestSkill1</li>
  <li>TestSkill2</li>
  <li>Test - Skill</li>
  <li>Chat-Skill</li>
  <li>testing disposition</li>
  <li>Narender</li>
  <li>Inya</li>
  <li>Chat_Inya</li>
  <li>Agent1</li>
  <li>agenttwo</li>
  <li>mahender</li>
  <li>naresh</li>
  <li>venkat-skill</li>
  <li>English</li>
  <li>French</li>
  <li>testpoc</li>
  <li>mahender1</li>
  <li>devskill</li>
  <li>praveen</li>
</ul>


.skillscont-list{
    margin: 0px;
    padding: 0px;
    overflow: overlay;
    list-style-type:none;
}
.skillscont-list li{
    border:1px solid black;
    float: left;
    widht: auto;
    padding: 5px 10px;
    margin: 5px;
    color:white;
    font-weight:bold;
    background-color:gray;
}