开发者

how to align 4 DIV's right of each other within another DIV

i want to position 4 divs next to each other, at a certain position within the parent div. So the div structure is this (i'll call the top most div the maindiv):

<div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</d开发者_高级运维iv>
</div>

now i want to set div with text '1' at the left in the maindiv

now i want to set div with text '2' at 50 px from the left side in the maindiv

now i want to set div with text '3' at 150 px from the left side in the maindiv

now i want to set div with text '4' at 200 px from the left side in the maindiv

I think :-) i have tried ALL possible css constructs, but it didn't work, sadly.

Anyone an idea?


Give position: relative to the main div

position: absolute to the children and position them using left: xyz; top: xyz .


Hope this would help you

                <div> 
                    <div style="float: left;">1</div> 
                    <div style="float: left; margin-left: 50px;">2</div> 
                    <div style="float: left; margin-left: 100px;">3</div> 
                    <div style="float: left;margin-left: 50px;">4</div> 
                </div>


try this:

 div div
      {
          float:left;
          margin-left:50px;
      }

EDIT:as in: Fix for the parent:

div
{
    float:left;
    margin-left:-50px;
}
div div
{
    float:left;
    margin-left:50px;
}


flexbox

Flexbox is now the recommended way, simply because it's the most simple and easiest solution

.parent{
  display:inline-flex;
}
.child{
  width:50px;
  height:50px;
  border:1px solid black;
}
<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜