I'm trying to put 3 divs into 1 div. Based on my code, I'm trying to make Div 3 a container for Div 4-6. However, even though Div 5 and Div 6 have the same code, they overlap.
<!DOCTYPE html> <html> <head> <style> #div1 { background-color: blue; width: 60%; height: 400px; text-align: center; margin: auto; } #div2 { background-color: red; width: 90%; height: 300px; text-align: center; margin: auto; } #div3 { background-color: gray; width: 95%; height: 200px; text-align: center; margin: auto; } #div4 { background-color: yellow; width: 20%; height: 100%; text-align: center; float:left; } #div5 { background-color: green; width: 40%; height: 100%; text-align: center; float:left; } #div6 { background-color: purple; width: 40%; height: 100%; text-align: center; float:left; } </style> </head> <body> <div id="div1">test 1 <div id="div2">test 2 <div id="div3">test 3 <div id="div4">test 4</div> <div id="div5">test 5</div> <div id="div6">test 6</div> </div> </div> </div> </body> </html>
Test 3 and test 6 are shown, but I don’t want to see test 3
My professor checked the code for me and said the problem was with my browser. I really don't know what to do.
I tried using flex, but all the content ran outside the div. I tried changing the float position of div 6 but nothing changed. I tried everything I've ever learned and nothing worked.
P粉9820098742023-09-10 00:49:59
If you're using flex like this, it's pretty easy to fix except having to consider DIV 3's text. Using another wrapper with different flex direction solves this problem.
<!DOCTYPE html> <html> <head> <style> #div1 { background-color: blue; width: 60%; height: 400px; text-align: center; margin: auto; } #div2 { background-color: red; width: 90%; height: 300px; text-align: center; margin: auto; } #div3 { background-color: gray; width: 95%; height: 200px; text-align: center; margin: auto; display:flex; } #div4 { background-color: yellow; width: 20%; height: 100%; text-align: center; } #div5 { background-color: green; width: 40%; height: 100%; text-align: center; } #div6 { background-color: purple; width: 40%; height:100%; text-align: center; } </style> </head> <body> <div id="div1">测试 1 <div id="div2">测试 2 <div id="div3"> <div id="div4">测试 4</div> <div id="div5">测试 5</div> <div id="div6">测试 6</div> </div> </div> </div> </body> </html>
P粉5510842952023-09-10 00:37:36
DIV 4, 5 and 6 have float: left
attributes and float around the text "test 3". If you delete this text, DIV 3 will no longer be visible:
#div1 { background-color: blue; width: 60%; height: 400px; text-align: center; margin: auto; } #div2 { background-color: red; width: 90%; height: 300px; text-align: center; margin: auto; } #div3 { background-color: gray; width: 95%; height: 200px; text-align: center; margin: auto; } #div4 { background-color: yellow; width: 20%; height: 100%; text-align: center; float:left; } #div5 { background-color: green; width: 40%; height: 100%; text-align: center; float:left; } #div6 { background-color: purple; width: 40%; height: 100%; text-align: center; float:left; }
<div id="div1">test 1 <div id="div2">test 2 <div id="div3"> <div id="div4">test 4</div> <div id="div5">test 5</div> <div id="div6">test 6</div> </div> </div> </div>