I am trying to code a simple average monthly weather table, with 12 divs inside a larger div, and each ‘monthly div’ showing an emoji, and HI/LO temperatures. Mockup image below:
My issue is handling responsiveness when on mobile/tablet. I would like it to split onto 2 (or even 3) rows if needed based on screen width, looking something like this:
However, I’m not too sure how to do it. Currently the gap between the edge of the small div and the larger outer div will increase until it can fit another small div in. Instead, I don’t want the distance to change – I’d rather it move onto another row.
Here’s what I’ve got so far:
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.weather-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin: 0 auto;
padding: 10px;
border-radius: 15px;
border: 2px solid #000;
/* Curved black border */
max-width: 100%;
background-color: #fff;
/* Ensure white background inside the border */
}
.weather-box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 10px 20px;
width: 80px;
background-color: #fff;
position: relative;
}
.weather-icon {
font-size: 36px;
}
.temperature {
display: flex;
justify-content: space-between;
width: 100%;
margin-top: 5px;
font-size: 18px;
}
.temperature span {
width: 45%;
text-align: center;
}
.month-label {
font-size: 14px;
font-weight: bold;
margin-bottom: 5px;
}
.separator {
position: absolute;
top: 0;
right: 0;
width: 2px;
height: 100%;
background-color: #000;
}
/* Remove separator from the last box */
.weather-box:last-child .separator {
display: none;
}
<h2>New York City Monthly Weather Forecast</h2>
<div class="weather-container">
<div class="weather-box">
<div class="month-label">January</div>
<div class="weather-icon">☁️</div>
<!-- Cloudy -->
<div class="temperature">
<span>HI 39°</span>
<span>LO 26°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">February</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 42°</span>
<span>LO 29°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">March</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 50°</span>
<span>LO 35°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">April</div>
<div class="weather-icon">🌧️</div>
<!-- Rainy -->
<div class="temperature">
<span>HI 61°</span>
<span>LO 45°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">May</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 71°</span>
<span>LO 54°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">June</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 79°</span>
<span>LO 64°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">July</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 84°</span>
<span>LO 69°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">August</div>
<div class="weather-icon">☀️</div>
<!-- Sunny -->
<div class="temperature">
<span>HI 83°</span>
<span>LO 68°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">September</div>
<div class="weather-icon">🌧️</div>
<!-- Rainy -->
<div class="temperature">
<span>HI 76°</span>
<span>LO 61°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">October</div>
<div class="weather-icon">☁️</div>
<!-- Cloudy -->
<div class="temperature">
<span>HI 65°</span>
<span>LO 50°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">November</div>
<div class="weather-icon">☁️</div>
<!-- Cloudy -->
<div class="temperature">
<span>HI 55°</span>
<span>LO 41°</span>
</div>
<div class="separator"></div>
</div>
<div class="weather-box">
<div class="month-label">December</div>
<div class="weather-icon">❄️</div>
<!-- Snowy -->
<div class="temperature">
<span>HI 45°</span>
<span>LO 32°</span>
</div>
</div>
</div>
4
Answers
To separate containers dynamically based on the number of columns per row, you’ll need to use JavaScript to wrap every set of
weather-box
elements into separateweather-container
elements based on the current screen width.I’m not sure what you are trying to achieve. If you want the rows to all have the same number of boxes, you have 12 rows that can be split into 2, 3, 4, or 6.
By calculating the padding and margin of the parent and weather boxes, you can use a grid display layout and show them in more rows based on the viewport width.
What I did was set the
display
of theweather-container
togrid
, change the width of theweather-box
toauto
, and then, by calculating the minimum size of each layout (120 * n + 24 + 16
), create multiple grid layouts based on screen size and hide the separator at the end of each row:You can achieve what you expect by reviewing your CSS, you will find the result here: https://jsfiddle.net/s2pwxa60/, you can stretch the content to see the result.
Edit
Not sure the result you want ? you want fixed width on mobile / tablet ? IMHO, it’s a bad idea, responsive was created to have different sizes on different devices, which is useful.
Explanations:
Put the border on the boxes, not the container, and add a box-sizing to have better control on the width:
Be sure to add the radius you want on first and last box:
Then use media queries for medium screen (for example):
The
flex: 1 0 25%
will allow you to have 4 elements on a row (100 / 4 = 25). Then usenth-child(4n)
to add the radius to every first and last element of each row.You can control the number of elements by changing the percent, and changing the nth value. And do not use your current separator, it’s easy to use a CSS border.