0

I'm trying to do layout like the image but i failed to do .

enter image description here

My code:

<div>
<div style="float: left; width: 70%">I'm on the left</div>
<div style="float: left; width: 30%"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
</div>

<div>
<div style="float: left; width: 30%"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
<div style="float: left; width: 70%">I'm on the second on the left</div>
</div>

<div>
<div style="float: left; width: 70%">I'm on the left</div>
<div style="float: left; width: 30%"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
</div>

Any ways that i could achieve the layout i need?

hatched
  • 795
  • 2
  • 9
  • 34

4 Answers4

1

add width: 100%; float:left; to parent divs like below

<div style="width: 100%; float:left; margin-bottom: 30px;">
<div style="float: left; width: 70%; background: green">I'm on the left</div>
<div style="float: left; width: 30%; background: red"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
</div>

<div style="width: 100%; float:left; margin-bottom: 2px">
<div style="float: left; width: 30%; background: green"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
<div style="float: left; width: 70%; background: red">I'm on the second on the left</div>
</div>

<div style="width: 100%; float:left;">
<div style="float: left; width: 70%; background: green">I'm on the left</div>
<div style="float: left; width: 30%; background: red"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
</div>
Pons Purushothaman
  • 2,225
  • 1
  • 14
  • 23
  • Thanks for the code, I'm trying to
    in between sections but it doesn;t work, any ways i could achieve ?
    – hatched Apr 09 '19 at 10:26
  • it works , its just an extra , because i want to have more space between each section , but
    between the
    s doesn't seems working
    – hatched Apr 09 '19 at 10:43
  • is not the best way. Use margin for the parent element so you can adjust the spacing pixel by pixel. I have updated the code with margin. – Pons Purushothaman Apr 09 '19 at 10:49
1

When you float items, they lose their height. When you add a minimum height to the parent divs, the problems will be fixed.

<div style="min-height: 150px;">
  <div style="float: left; width: 70%">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div style="float: left; width: 30%">
  <img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%">
  </div>
</div>

<div style="min-height: 150px;">
<div style="float: left; width: 30%"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
<div style="float: left; width: 70%">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>

<div style="min-height: 150px;">
<div style="float: left; width: 70%">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div style="float: left; width: 30%"><img src="https://i.ibb.co/C7SNdjk/cimb-foundation-image.png" width="100%"></div>
</div>
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
0

Many ways to achieve this

Can add direction:rtl to second parent div

Can change order of second div so div with image is after div with text.

Can float img right in second div

can use display:flex and flex-direction:row-reverse

TeT Psy
  • 341
  • 3
  • 7
0

You could go with flexbox and make use of flex-direction: row-reverse;. The flex property on the child elements (image and text) defines the ratio, in this case, the text with flex: 3 is three times bigger than the image.

Flexbox Browser support: https://caniuse.com/#search=flexbox

.section {
  display: flex;
  padding-bottom: 1rem;
}

.section--reversed {
  flex-direction: row-reverse;
}

.section__text {
  flex: 3;
  border: 1px dashed tomato;
}

.section__image {
  flex: 1;
  border: 1px dashed tomato;
}
<div class="section">
  <div class="section__text">Here is some text</div>
  <div class="section__image">here is an image</div>
</div>

<div class="section section--reversed">
  <div class="section__text">Here is some text</div>
  <div class="section__image">here is an image</div>
</div>

padding and border are only for demo purposes

wiesson
  • 6,544
  • 5
  • 40
  • 68