I have some articles in my code which has a display of grid. I have set the padding of the body to padding: 0 1em; and the min-width of the following elements and classes to 450px;
can someone please tell me why the padding on the right disappears when I go lower than the breakpoint 480px???
Here is a screenshot of my page below breakpoint 480px:
Here is a snippet of my css:
body{
padding: 0 1rem;
}
/* Media Queries */
.article-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(450px, auto));
column-gap: 30px;
row-gap: 50px;
}
/* Utilities */
nav,
.article-container,
article,
.jour-hero,
.about-hero{
margin: 0 auto;
}
footer,
nav,
.jour-hero,
.about-hero,
.hero{
min-width: 450px;
}
nav,
.jour-hero,
.about-hero,
.hero,
.article-container{
max-width: 1600px;
}
2
Answers
Rahul was close with his comment, but it’s actually the margin that you set. The
column-gap
property won’t have any effect if there’s only one column, but you have a margin of1rem
on each side.1rem
is almost always 16px, so that will add up to 482px. If you don’t want to go about creating an entirely different layout, I’d change that width frommin-width: 450px
tomin-width: min(450px, calc(100vw-2rem))
.It is because you are using
min-width's
in your CSS.If your display only has a width of e.g. 300px, your elements (marked with
min-width
) will be bigger than your display.Exactly the problem you are encountering.
Solution:
Get rid of all static
px
measure units in your CSS, which are used for your page layout, and use responsive measure units like%
or even better theflex layout
.As addition and/or alternative you can implement
media queries
, which allow you to assign CSS only to devices with awidth
lower than 500px for example.From dev to dev:
Some will hate me for that, some will love me for that:
It is a good practice to use this CSS and eliminate many Problems with it:
This CSS code snippet sets the box-sizing property of all elements to border-box. This means that
padding
andborder
are included in the element’s total width and height, rather than being added to the outside. It’s a useful way to ensure consistent sizing in layouts.