Fixing my layout from desktop to phone

Hey, I'm working on figuring out the basic layout for my portfolio website and started building out a block-out of my design I made in Figma. I'm struggling with using the full viewport on phone. My homepage has 3 cards and on desktop they are placed in 2 even-columns. However, on phone I want the first card to fit the whole viewport and then the other 2 cards will also use up a whole viewport when scrolling down. I've got the basic layout working for desktop and tablet sized screens, just not for phone. Seems like it's not using the full viewport size. Here is my code: https://codepen.io/Hidde2205/pen/ExqwJYP I've also added my designs for reference, any help is appreciated! If my current code that seems to work for desktop and tablet can also be made easier or better, please let me know 😄
Hidde
CodePen
Untitled
...
No description
No description
16 Replies
Hidi_
Hidi_OP•2mo ago
The design on the right is for devices on portrait mode basically (max-width: 768px). The blue card would be the only thing u see when opening the website and the other 2 cards will be the only thing u see when u are scrolled down all the way.
clevermissfox
clevermissfox•2mo ago
You could simplify this down to three cards directly in container and use two columns three rows
grid-template-areas:
‘Profile project’
‘Profile project’
‘Profile info’;

& .profile-card {grid-area: profile;}
& .project-card {grid-area: project;}
& .project-info {grid-area: info;}
grid-template-areas:
‘Profile project’
‘Profile project’
‘Profile info’;

& .profile-card {grid-area: profile;}
& .project-card {grid-area: project;}
& .project-info {grid-area: info;}
Then in your media query just redefine the areas
grid-template-areas:
‘Profile’
‘Project’
‘Info’;
grid-template-areas:
‘Profile’
‘Project’
‘Info’;
Hidi_
Hidi_OP•2mo ago
Let's say another page needs more columns or rows, I just only do that for that page? I used a 12 column and 8 rows layout in Figma, so I thought let me make a grid-template with those columns and rows I've made some adjustments, but I can't seem to center the container div with the grid whilst remaining a minimum margin on the left and right of 4rem. Right now I have centered it by doing margin: 4rem auto; however when making the screen smaller the grid will stick to the sides. Removing auto on margin does work, besides the fact that it's no longer centered. I tried using flex on the body and center everything, but then it all disappears. Here is my new codepen: https://codepen.io/Hidde2205/pen/NWQwgaX
clevermissfox
clevermissfox•2mo ago
For margin on the left and right/inline axis, you can use margin-inline: auto; When using the shorthand margin: 4rem auto, you’re adding 4rem to margin-block which is margin top and bottom , and auto to left and right/inline axis. In the shorthand , when you have two values , the first value is for top and bottom, the second value is for left and right. If you want a minimum of 4rem on each side as well as keeping it centered, why not use the 4rem as padding? Then adjust the width to account for it
—_padding: 4rem;
width: min(100% - var(—_padding) * 2, 1120px + var(—_padding) * 2 );
padding-inline: var(—_padding);
margin-inline: auto;
—_padding: 4rem;
width: min(100% - var(—_padding) * 2, 1120px + var(—_padding) * 2 );
padding-inline: var(—_padding);
margin-inline: auto;
Hidi_
Hidi_OP•2mo ago
That makes a lot more sense, I tried adding padding before but just made the cards bigger but with this calculation it fixes it. I don't really understand why but the minimum margin-inline is the same as the padding. I just dropped down the padding to 2 rem so it does the same spacing, but idk what's the reasoning behind it
clevermissfox
clevermissfox•2mo ago
I’m not sure what your code was that made the cards bigger so can’t answer that but looking at your codepen , you’re still adding the margin to the vertical axis (top and bottom) with your margin shorthand margin: 4rem auto but then you are subtracting it from the min-height; must be what you wanted to do . And don’t see a padding declaration. Just remember inline is horizontal direction (logical properties for margin-left/margin-right or padding-left/padding-right; also known as margin-inline-start/margin-inline-end padding-inline-start/padding-inline-end) and block is vertical direction (eg margin-top/margin-bottom , padding-top/padding-bottom aka margin-block-start/margin-block-end , padding-block-start/padding-block-end) .
Hidi_
Hidi_OP•2mo ago
Yeah must of been that
Hidi_
Hidi_OP•2mo ago
Failing so hard right now and I am being clueless. Honestly don't know if this has saving potential or just need to be re-build with a different approach. Here is the codepen (unfortanetly doesn't have images): https://codepen.io/Hidde2205/pen/qBeVvME
Hidi_
Hidi_OP•2mo ago
Also I know the 12 column and 8 row might be overkill for just this screen, but I think it fits the project since other pages will have different lay-outs so I can just use the same classes for the main-grid I guess what I am trying to manage are cards that grow and shrink in size due to the screen size its being captured on. The content within the cards just need to fit-in, for the card with the profile picture that would probably mean that the picture needs to shrink to make space for the text and button
clevermissfox
clevermissfox•2mo ago
Youre setting a fixed height on the container so it’s overflowing. Give your container outline: 2px solid lime to see the boundaries. You’re making more work for yourself by making 12 cols but at least add an additional selector that’s page specific with grid areas or create names grid lines on your reusable class that follows the pattern you need. Redefining for your smaller screen sizes the way you’re doing it means you need to override each child’s grid cols and grid rows instead of just redefining grid areas or named grid lines on one selector.
@media ….
grid-template-areas: “profile””project””info”
@media ….
grid-template-areas: “profile””project””info”
Vs
@media …
.profile {grid-column: 1;grid-row:1;}
.project{grid-column:1;grid-row:2}
.info{grid-column:1;grid-row:3}
@media …
.profile {grid-column: 1;grid-row:1;}
.project{grid-column:1;grid-row:2}
.info{grid-column:1;grid-row:3}
You’re trying to reuse one class and in the process you’re creating so many more css lines for this page. Just use one page specific selector to define the grid-areas. Or , more complex but still doable , create named grid lines that are reusable but also ask yourself , do you need 12 columns on any page or are you just following bootstrap conventions when that was created before we had better tools like css grid And avoid setting fixed heights. Especially when you’re using grid where you’re defining rows , those will define the height . Or the content will if you’re using. And if you absolutely need to , set a min-height If you must use 12 columns at least make it easy to redefine for different screen sizes. You may end up with your cards being sizes you don’t want them to though esp if you’re setting width and height and then using 12 columns/8rows to fill that space. I too have a habit of over complicating things but given what you’ve described I would do something like this
@media (width > 768px){
.main-grid {
/*reusable styles*/
grid-template-columns: repeat(12,1fr);
grid-template-rows: repeat(8,1fr);
}
/*page specific selector*/
.main-grid.profile-page-grid {
grid-template-areas:
“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”;

& .profile-card {grid-area: profile;
& .project-card {grid-area: project ;
& .info-card {grid-area: info;
}
}
@media (width > 768px){
.main-grid {
/*reusable styles*/
grid-template-columns: repeat(12,1fr);
grid-template-rows: repeat(8,1fr);
}
/*page specific selector*/
.main-grid.profile-page-grid {
grid-template-areas:
“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile project project project project”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”

“profile profile profile profile profile info info info info info”;

& .profile-card {grid-area: profile;
& .project-card {grid-area: project ;
& .info-card {grid-area: info;
}
}
althepal78
althepal78•2mo ago
do a mobile first approach, according to kevin it should have the least resistance or code then possibly container or media queries and some clamps etc.. looks nice though
Hidi_
Hidi_OP•2mo ago
I appreciate the feedback and help once again! I'm looking to implement it, is this going the right way for the page layout?
/* --- PAGE LAYOUT --- */
body {
font-family: var(--ff-primary);
font-weight: var(--fw-regular);
color: var(--clr-neutral-700);
background-color: var(--clr-primary-100);

display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}

.page-wrapper {
--_padding: 1.5rem;

width: min(100vw - var(--_padding) * 2, 70em + var(--_padding) * 2);
height: min(100vh - var(--_padding) * 2, 56em + var(--_padding) * 2);
padding: var(--_padding);
margin-inline: auto;
}

.main-grid {
display: grid;
gap: 2rem;
}

.homepage-grid {
grid-template-areas:
'profile'
'project'
'info';
}

.homepage-grid .profile-card {
grid-area: profile;

background: var(--gradient-primary);
border-radius: 2rem;
}

.homepage-grid .project-card {
grid-area: project;

background-color: var(--clr-neutral-50);
border-radius: 2rem;
}

.homepage-grid .project-info {
grid-area: info;

background-color: var(--clr-neutral-50);
border-radius: 2rem;
}
/* --- PAGE LAYOUT --- */
body {
font-family: var(--ff-primary);
font-weight: var(--fw-regular);
color: var(--clr-neutral-700);
background-color: var(--clr-primary-100);

display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}

.page-wrapper {
--_padding: 1.5rem;

width: min(100vw - var(--_padding) * 2, 70em + var(--_padding) * 2);
height: min(100vh - var(--_padding) * 2, 56em + var(--_padding) * 2);
padding: var(--_padding);
margin-inline: auto;
}

.main-grid {
display: grid;
gap: 2rem;
}

.homepage-grid {
grid-template-areas:
'profile'
'project'
'info';
}

.homepage-grid .profile-card {
grid-area: profile;

background: var(--gradient-primary);
border-radius: 2rem;
}

.homepage-grid .project-card {
grid-area: project;

background-color: var(--clr-neutral-50);
border-radius: 2rem;
}

.homepage-grid .project-info {
grid-area: info;

background-color: var(--clr-neutral-50);
border-radius: 2rem;
}
@media (min-width: 768px) {
.main-grid {
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(8, 1fr);
}

.main-grid .homepage-grid {
grid-template-areas:
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile info info info info info info'
'profile profile profile profile profile profile info info info info info info'
'profile profile profile profile profile profile info info info info info info'
;
}
}
@media (min-width: 768px) {
.main-grid {
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(8, 1fr);
}

.main-grid .homepage-grid {
grid-template-areas:
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile project project project project project project'
'profile profile profile profile profile profile info info info info info info'
'profile profile profile profile profile profile info info info info info info'
'profile profile profile profile profile profile info info info info info info'
;
}
}
The height is still a problem I assume Honestly the grid-template-areas with 12 columns looks ridiculous 😂 I do believe I need 8 rows since .project-card = 5 rows and .project-info = 3 rows so this should be better?
@media (min-width: 768px) {
.homepage-grid {
grid-template-columns: repeat(2, 1fr);
grid-template-areas:
'profile project'
'profile project'
'profile project'
'profile project'
'profile project'
'profile info'
'profile info'
'profile info';
}
}
@media (min-width: 768px) {
.homepage-grid {
grid-template-columns: repeat(2, 1fr);
grid-template-areas:
'profile project'
'profile project'
'profile project'
'profile project'
'profile project'
'profile info'
'profile info'
'profile info';
}
}
Unless I make the columns not 1fr idk what's better?
clevermissfox
clevermissfox•2mo ago
Yes height is a problem. Why are you setting a height at all , the content will determine it. And agree you don’t need twelve columns. And I arbitrarily chose 768px for the media query , you’ll need to decide what the right breakpoint is for your project and when they need to stack
Hidi_
Hidi_OP•2mo ago
The reason I was being so picky on the height is that I dont want scrolling for big screen sizes (anything bigger then width: 768px). I want all the content to be visible for those bigger screen within a single viewport. For mobile devices or a tablet in portrait mode I can't really avoid that and I'm aiming for a simple vertically stacked layout
clevermissfox
clevermissfox•2mo ago
Looks like your content will fit in the viewport without having to set a height, you also don't want it overflowing. You maybe looking to set
body {min-height:100dvh}
body {min-height:100dvh}
Not a height on your wrapper. No fixed heights is about as close as you can get to a golden rule in css
Hidi_
Hidi_OP•2mo ago
Sounds clear, gonna try out some stuff again later today. Thanks!
Want results from more Discord servers?
Add your server