0

This is very simple and newbie question. I just started to learn basic HTML, and when trying to place 3 divs together in different order, I got stucked. I need 3 DIV elements: The first one should go to the left and it is DIV for thumb. The second one should go to the right side, having padding-left option set at 5px, then the third div should be placed below the second div, and the height of both divs should be matching the first div thumb size (50x50px).

Here is my code, that I was trying to create, but still no success.

    <div style="width: 500px;">
        <div style="float: left;">
            <img src="http://www.avatarsdb.com/avatars/fire_01.gif" width="50px" height="50px">
        </div>
        <div style="padding-left:10px; width:100px;">top</div>
        <div style="float: left; padding-left:10px; width:100px;">bottom</div>
    </div>

I know how to solve it with Table, but with DIV I was not able to solve it myself being super new.

Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
Carlos Florian
  • 171
  • 1
  • 9

5 Answers5

0

If I truly understand you, you need absolute positioning for 3'rd div.

<div style="width: 500px;position:relative;">
 <div style="float: left;"><img src="http://www.avatarsdb.com/avatars/fire_01.gif" width="50px" height="50px"></div>
  

 <div style="padding-left:10px; width:100px;float:right;height:25px;">top</div>
 
 <div style="padding-left:10px; width:100px;position:absolute;top:25px;right:0;">bottom</div>
 </div>
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
0

This should solve your issue:

    <div style="width: 500px;">
       <div style="float: left;">
         <img src="http://www.avatarsdb.com/avatars/fire_01.gif" width="50px" height="50px">
       </div>
       <div style="float:right;">
         <div style="padding-left:10px; width:100px; height: 25px;">top</div>
         <div style="padding-left:10px; width:100px; height: 25px;">bottom</div>
       </div>
    </div>

The easiest way is to group your contact. One DIV for the left content and one for the right. Every div get it's position. So no need to position element inside these divs. In this way, you have more freedom to design your elements independently within the groups.

And what about using style classes? This make your code more readable and faster.

.master {
  width: 500px;
}

.thumb {
  float: left;
}

.thumb img {
  width: 50px;
  height: 50px;
}

.description {
  float: right;
}

.description > div {
  padding-left:  10px;
  width: 100px;
  height: 25px;
}
<div class="master">
   <div class="thumb">
     <img src="http://www.avatarsdb.com/avatars/fire_01.gif">
   </div>
   <div class="description">
     <div>top</div>
     <div>bottom</div>
   </div>
</div>
Jan Franta
  • 1,691
  • 2
  • 16
  • 25
0

I'm slightly confused by your question but hopefully this helps you...

I would wrap the thumb div in its own wrapper and then wrap the second 2 div's together in another wrapper and float the wrappers to the left. As follows:

* {
  box-sizing: border-box;
}
.wrapper > .left, .wrapper > .right {
  float: left;
}
.left {
  width: 50px;
  height: 50px;
}
.right .top, .right .bottom {
  height: 25px;
  width: 100px;
  padding-left: 5px;
}
.right .top {
  background-color: yellow;
}
.right .bottom {
  background-color: red;
}
<div class="wrapper">
  <div class="left">
    <img src="http://www.avatarsdb.com/avatars/fire_01.gif" width="50px" height="50px">
  </div>
  <div class="right">
    <div class="top">
    </div>
    <div class="bottom">
    </div>
  </div>
</div>
David Wilkinson
  • 5,060
  • 1
  • 18
  • 32
0

Like this,

<div style="width: 500px;display:flex;">
    <div style="float: left;flex:1;">
        <img src="http://www.avatarsdb.com/avatars/fire_01.gif" width="50px" height="50px">
    </div>
    <div style="float: left;flex:1;">
        <div style="padding-left:10px; width:100px;">top</div>
        <div style="padding-left:10px; width:100px;">bottom</div>
    </div>
</div>

Note: Do not use inline styles.

clu3Less
  • 1,812
  • 3
  • 17
  • 20
0

You could use flexbox for this task. All divs match the height of the avatar and you could easily reorder them so you can have a responsive layout

Example codepen


Markup

<div class="wrap">
    <div class="wrap__avatar">
       <img src="http://placehold.it/50x50" />
    </div>
    <div class="wrap__column">
       Column (100px left padding)
    </div>
    <div class="wrap__main">
        Main (5px left padding)
    </div>
</div>  

CSS (compiled from SCSS)

.wrap {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: stretch;
}

.wrap__avatar {
  order: 1;
}

.wrap__main {
  padding-left: 5px;
  flex: 1;
  order: 2;
}

.wrap__column {
  width: 200px;
  padding-left: 100px;
  order: 3;
}

.wrap > div {
  box-sizing: border-box;
}
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177