I have these two div
classes next to each other. The one on the left is text and the one on the right is a grid
. Now, I implemented code using the media queries between 1050px
and 1280px
to change the rows and columns of the grid when the screen width is lowered. However, below 1049px
, there isn’t enough space anymore, and so I want the divs
to be stacked on top of each other, the text on the top and the div
on the bottom, and I want to make both of the divs
be at the center of the screen, no matter what the screen width is below 1049px
.
.item {
text-align: center;
background-color: #fff;
padding: 15px;
width: 160px;
height: 100px;
border-radius: 5px;
cursor: pointer;
box-shadow: 0 1px 1px black;
}
.item p {
margin-top: 20px;
}
.index-third-grid {
display: grid;
grid-template-rows: 100px;
grid-template-columns: repeat(3, 100px);
row-gap: 2em;
column-gap: 5em;
color: #704a1b;
font-size: 20px;
margin: 100px;
margin-right: 170px;
margin-top: -10px;
float: right;
}
.index-third-text {
margin-top: 70px;
}
.index-third-text p {
font-size: 25px;
color: #704a1b;
margin-left: 100px;
position: absolute;
}
.index-third-text h4 {
font-size: 35px;
color: #614124;
margin-left: 100px;
margin-top: 40px;
position: absolute;
}
.index-third-text h5 {
font-size: 15px;
color: #614124;
margin-left: 130px;
margin-top: 120px;
position: absolute;
}
.index-third-text hr {
font-size: 35px;
border: 0.5px solid #614124;
width: 0px;
height: 120px;
margin-left: 100px;
margin-top: 110px;
position: absolute;
}
@media screen and (min-width: 1050px) and (max-width: 1280px) {
.index-third-grid {
grid-template-rows: 100px;
grid-template-columns: repeat(2, 100px);
margin-top: -100px;
}
.index-third-text {
margin-top: 150px;
}
body {
overflow-x: hidden;
}
}
@media screen and (max-width: 1049px) {
/* Part I'm Confused About */
}
<div class="index-third">
<div class="index-third-text">
<p id="heading">How We Can Help You</p>
<h4 id="subheading">Our Services</h4>
<hr>
<h5 id="body">The services we offer are to ensure seamless consultation <br> processes. These features intend to provide necessary <br> and effective remote medical support to those in need. <br> <br> Click on the buttons to learn more!</h5>
</div>
<div class="index-third-grid">
<div class="item item-1">
<i class="fa-solid fa-magnifying-glass"></i>
<p>Search</p>
</div>
<div class="item item-2">
<i class="fa-solid fa-check-to-slot"></i>
<p>Consult</p>
</div>
<div class="item item-3">
<i class="fa-solid fa-comment"></i>
<p>Chat</p>
</div>
<div class="item item-4">
<i class="fa-solid fa-list"></i>
<p>Diagnose</p>
</div>
<div class="item item-5">
<i class="fa-solid fa-bars-progress"></i>
<p>Manage</p>
</div>
<div class="item item-6">
<i class="fa-solid fa-star"></i>
<p>Review</p>
</div>
</div>
</div>
2
Answers
This isn’t perfect, but it should get you started. The issue is that you’re using
position: absolute;
on everything in the left column. By doing that you take it out of the flow and it has 0 height, so thing things overlap.Instead put the whole section in an outer grid and as the correct size, change it from one column to 2.
UPDATED: This is a more bulletproof set of styles and it will handle all of the scaling. Plus I fixed a few of the markup issues around headings.
Take a look into semantic markup. It will help you understand how to write better and more accessible HTML.
Also, look into grid auto-fit and auto-fill. That would give you a better and even more fluid version of the grid you have on the right.
My approach to the problem presented is below, with explanatory comments in the code to try and explain how that code works:
JS Fiddle demo.
References:
border-inline-start
.border-radius
.box-shadow
.box-sizing
.display
.font-size
.gap
.grid-auto-rows
.grid-template-columns
.inline-size
.line-height
.margin
.margin-block-start
.margin-inline
.padding
.padding-block
.padding-block-end
.padding-block-start
.padding-inline
.padding-inline-start
.place-content
.place-items
.