1

I used Visual Studio Code and opened with live server.
I noticed 'position: relative' of box_1's '#red' is not working
How do you make it work?


edit image link of expecting result
also tried with unique ids
ex) red_1, orange_1, yellow_1
if possible can you tell me the reason it's not working
Thinking about conflict between ralative and float but I'm not sure


HTML
result
expecting_result

.box_1{  
      width: 100px;
      height: 100px;
      float: left; 
    }
    
.box_2{
      width: 100px;
      height: 100px;
    }
   
.box_3{ 
      width: 100px;
      height: 100px;
    }

#red{
     background-color: red;
     top: 100px;
     left: 100px;
     position: relative;        
   }

#orange{
     background-color: orange;
     top: 100px;
     left: 100px;
     position: relative;
   }

#yellow{
     background-color: yellow;
     top: 100px;
     left: 100px;
     position: relative;
   }
html
<div class="box_1" id="red"></div>
<div class="box_1" id="orange"></div>
<div class="box_1" id="yellow"></div>

<div class="box_2" id="red">
    <div class="box_2" id="orange">
        <div class="box_2" id="yellow">
        </div>
    </div>
</div>

<div class="box_3" id="red"></div>
<div class="box_3" id="orange"></div>
<div class="box_3" id="yellow"></div>
na08
  • 13
  • 3

3 Answers3

0

just add another <div class="box_1" id="red"></div> to the top:

.box_1{  
      width: 100px;
      height: 100px;
      float: left; 
    }
    
.box_2{
      width: 100px;
      height: 100px;
    }
   
.box_3{ 
      width: 100px;
      height: 100px;
    }

#red{
     background-color: red;
     top: 100px;
     left: 100px;
     position: relative;        
   }

#orange{
     background-color: orange;
     top: 100px;
     left: 100px;
     position: relative;
   }

#yellow{
     background-color: yellow;
     top: 100px;
     left: 100px;
     position: relative;
   }
<div class="box_1" id="red"></div>
<div class="box_1" id="red"></div>
<div class="box_1" id="orange"></div>
<div class="box_1" id="yellow"></div>

<div class="box_2" id="red">
    <div class="box_2" id="orange">
        <div class="box_2" id="yellow">
        </div>
    </div>
</div>

<div class="box_3" id="red"></div>
<div class="box_3" id="orange"></div>
<div class="box_3" id="yellow"></div>

or better rewrite it to a valid HTML:

.box_1{
      width: 100px;
      height: 100px;
      float: left; 
    }
    
.box_2{
      width: 100px;
      height: 100px;
    }
   
.box_3{ 
      width: 100px;
      height: 100px;
    }

.red{
     background-color: red;
   }

.orange{
     background-color: orange;
   }

.yellow{
     background-color: yellow;
   }

.box_2,
.box_2 *
{
     top: 100px;
     left: 100px;
     position: relative;
}
<div class="box_1 red"></div>
<div class="box_1 red"></div>
<div class="box_1 orange"></div>
<div class="box_1 yellow"></div>
    <div class="box_2 orange">
        <div class="box_2 yellow">
        </div>
    </div>
<div class="box_3 red"></div>
<div class="box_3 orange"></div>
<div class="box_3 yellow"></div>
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • sorry to bother not putting the expecting result. I edited the link of image.. thank you – na08 Aug 11 '21 at 05:33
0

To Achieve your desired result, you have to given the margin for the first red box, so that next relative red box will place in that and will get the desired result.

.box_1#red {
  margin-left:100px;
}

.box_1{  
      width: 100px;
      height: 100px;
      float:left;
    }
 .box_1#red {
      margin-left:100px;
 }
    
.box_2{
      width: 100px;
      height: 100px;
    }
    
.box_3{ 
      width: 100px;
      height: 100px;
      
    }

#red{
     background-color: red;
     top: 100px;
     left: 100px;
     position: relative;        
   }

#orange{
     background-color: orange;
     top: 100px;
     left: 100px;
     position: relative;
   }

#yellow{
     background-color: yellow;
     top: 100px;
     left: 100px;
     position: relative;
   }
<div class="box_1" id="red"></div>
<div class="box_1" id="orange"></div>
<div class="box_1" id="yellow"></div>

<div class="box_2" id="red">
    <div class="box_2" id="orange">
        <div class="box_2" id="yellow"></div>
    </div>
</div>
      

<div class="box_3" id="red"></div>
<div class="box_3" id="orange"></div>
<div class="box_3" id="yellow"></div>
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54
0

This video will explain your issue why float doesn't take a block space.
https://youtu.be/UAWLXYUGqpc

And the reason is the floated item is taken out from the flow.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/In_Flow_and_Out_of_Flow#taking_an_item_out_of_flow

Try this example to see how float is working.

<style>
.float_div{  
      width: 100px;
      height: 100px;
      background-color: blue;
      float: right;
    }
    
.box{
    width: 200px;
    height: 200px;
    background-color: red;
    opacity: 30%;
}
   
</style>
<html>
    <div class="float_div"></div>
    <div class="box"></div>
</html>

Just fyi, I'd not recommend to use float to layout divs. https://stackoverflow.com/a/15177860

AutumnSky
  • 394
  • 2
  • 14