I’m having problems figuring out how to make two divs not stack on top of each other when I’m on my laptop. it looks normal when I’m on my desktop.
I tried to make the div
s width smaller hopefully making it not big enough to stack when I switch monitors but that didn’t work.
body {
background-color: darkgray;
}
div {
border: solid;
border-color: black;
border-width: 1.5px;
}
.outer {
height: 850px;
width: 225px;
border-radius: 10px;
margin-left: 300px;
margin-top: 75px;
display: inline-block;
}
.mid {
width: 200px;
height: 75px;
margin-left: 12.5px;
margin-top: 10px;
border-radius: 10px;
}
.body-model {
height: 850px;
width: 1000px;
margin-left: 50px;
display: inline-block;
border-radius: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<script href="index.js"></script>
<title>title</title>
</head>
<body>
<div class="outer">
<div class="mid">
</div>
</div>
<div class="body-model">
</div>
</body>
</html>
2
Answers
Im not exactly sure which of the divs you’re talking about, but the general approach is to use relative instead of static units.
Your approach
In the example you provided you mostly use px as a unit, this is static because the size of the pixel is a fixed unit no matter of the device you are using
What you should do instead
Try to use relative units like %, vw (viewport width) or vh (viewport height). These values will scale based on the maximum window size and stay proprotional to that maximum size.
So try switching up your units to relative values
For reference you can look at this
If you want to stop the general overlapping of the content different approaches may be advisable like
If you want the behavior of each container to change based off a certain size of the window you may use mediaqueries
Further instruction on mediaqueries here
In this example I did what I would do in this situation, but in reality there are multiple ways of doing this,
display: grid
being one of them.What I did is change the
wrapper
(could be adiv
, not necessarily thebody
)display
toflex
, by default theflex-direction: column
, making them go side-by-side.And then I proceeded by setting the
.outer
class to be a fixedwidth
, since this looks like a sidebar (if it’s not you can tweak with it), and then on.body-model
I changed thewidth
to100%
. I did remove themargin-*
from eachclass
since if the idea was centering the whole thing,display: flex
ordisplay: grid
should be used instead.PS: If this is a
layout
of sorts, I would recommend changing the.wrapper
height
to100vh
, and changing each childheight
to100%
.