I need 2 div with one is floated left so when we resize the window into a small window the second div will move downward.
body, html { width: 100%; height: 100%; } .container { overflow: hidden; } .container div { width: 500px; height: 500px; border: 1px solid black; }
<!DOCTYPE html> <html> <head> <title>Home</title> </head> <body> <div class="container"> <div style="float: left"> aaa </div> <div> bbb </div> </div> </body> </html>
this code will make the second div overlap with the first div, if I add display:flex in the container it won't overlap anymore but the div size is resizing with the windows size and the second div won't go downward.
What is wrong? I need my div to be exactly 500px. Thanks :)
From what I understand, you want to make the second div go down after resizing the browser. So you can use media queries for that:
body,
html {
width: 100%;
height: 100%;
}
.container {
overflow: hidden;
}
.container div:first-child {
float: left;
width: 500px;
height: 500px;
border: 1px solid red;
}
.container div:last-child {
width: 500px;
height: 500px;
border: 1px solid black;
}
@media (max-width: 500px) {
.container div:last-child {
clear: both;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<div>
aaa
</div>
<div>
bbb
</div>
</div>
</body>
</html>
I separated the style of the two divs, and removed the float:left
from the inline style. The <meta>
is also important for the media query to work. I used clear:both
to clear the float of the first div from the second, thus not affecting the second div.
I didn't put this in a snippet because the media does not seem to work there, but is working in my computer
You have to set float
in second div also. Or in media query you have to set the display: block
in both div. check updated snippet below..
body, html { width: 100%; height: 100%; } .container { overflow: hidden; } .container div { width: 500px; height: 500px; border: 1px solid black; }
<!DOCTYPE html> <html> <head> <title>Home</title> </head> <body> <div class="container"> <div style="float: left"> aaa </div> <div style="float: right"> bbb </div> </div> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.