How to approach this type of layout

i have tried to make the button and the overflow people as the separate component and make them absolute, and for the other part i made it as background image....
No description
72 Replies
StefanH
StefanH4w ago
I don't know what the question is What the heck am i even looking at
kamekameha
kamekamehaOP4w ago
yeah, i mean
kamekameha
kamekamehaOP4w ago
No description
kamekameha
kamekamehaOP4w ago
i want to arrange the position of the orange box outside from the white container @StefanH @Lamer of Sweden what is the list of possible approach and what is the best approach ?
StefanH
StefanH4w ago
i'd use position absolute
kamekameha
kamekamehaOP4w ago
ok yeah i have seen kevin youtube video also, it's also possible with transform approach and position relative approach...
StefanH
StefanH4w ago
i mean transform and absolute positioning go hand in hand if you want the box to take up space in the flow you could also use a grid here. but i'd only do that if required
kamekameha
kamekamehaOP4w ago
ok
curiousmissfox
Personally would use grid, it’s excellent way to achieve stacking and overlapping content.
kamekameha
kamekamehaOP3w ago
hmmm interesting, usually i was using flex, never really implemented grid approach... but to make the content overlapping we still need control the insect(top,left,...) right ?
StefanH
StefanH3w ago
insects are not needed. not sure what good adding bugs to your code does but anyway you'd define 3 rows like this and then assign the elements to sit in those rows
No description
StefanH
StefanH3w ago
and if you put the red square after the white square in the dom it will naturally be drawn on top of it. no absolute positioning needed But again i'd only do this if you explicitly want the red square to be part of the dom. That choice is up to you
curiousmissfox
to position it perfectly though youd also need columns and another row
No description
curiousmissfox
e.g.
No description
kamekameha
kamekamehaOP3w ago
kind of confuse sir, is there a simple code example that i can see and run on my local ? @StefanH @curiousmissfox is that grid or flex also ?
StefanH
StefanH3w ago
I think @curiousmissfox had a full example? I only drew something in snipping tool
kamekameha
kamekamehaOP3w ago
for example this is the starter template <div class="parent"> <div class="bowl"> </div> </div> usually i made parent relative then bowl relative
StefanH
StefanH3w ago
I think this is a good grid exercise though. Kevin made lots of grid videos, he aught to teach something very similar here
kamekameha
kamekamehaOP3w ago
then push to the top kevin did also teach this technique made element relative and push
StefanH
StefanH3w ago
Yeah that's the absolute positioning solution which works fine. We just offered an alternative solution you can use if you want the red square to take up space in the flow. Because right now it's just going to overlap whatever is above which, which might be what you want. Depends on what you want really. These are just two different ways of doing it. Neither is better. It just depends on what you need
kamekameha
kamekamehaOP3w ago
ok, i'm wondering about this
kamekameha
kamekamehaOP3w ago
No description
kamekameha
kamekamehaOP3w ago
if i resize the design to small, how to make the bear also small ?
StefanH
StefanH3w ago
How do you want to constrain this? Can the white rectangle grow? How it grows is important so you know how the bear should grow with it If the aspect ratio is constrained you can make the bear's width a percentage of the parent's width and then it'll always stay as big it's just width: 20% on the bear
kamekameha
kamekamehaOP3w ago
so let say that's on desktop so max always like that, let's say the max-width is 2000px so can't grow anymore and let say mobile start < 992px
StefanH
StefanH3w ago
if you want it to stop growing you can then set a max-width in addition to width which is set to an absolute pixel value (should really use rem)
kamekameha
kamekamehaOP3w ago
the problem is if it's resize down
StefanH
StefanH3w ago
width: 20%;
max-width: 30rem;
width: 20%;
max-width: 30rem;
"this element's width is 20% of that of the parent but cannot be bigger than 30rem"
kamekameha
kamekamehaOP3w ago
wait grid or flex parent doesn't matter ?
StefanH
StefanH3w ago
if you specify a percentage value for width or height, that is based on the parent's size please correct me if i'm wrong. percentage is honestly a measure i try to avoid
kamekameha
kamekamehaOP3w ago
i'm not sure also
StefanH
StefanH3w ago
I mean it is based on the parent but there's always weird edge cases. In general it's based on the parent though
kamekameha
kamekamehaOP3w ago
but
kamekameha
kamekamehaOP3w ago
No description
kamekameha
kamekamehaOP3w ago
so this is working
kamekameha
kamekamehaOP3w ago
No description
kamekameha
kamekamehaOP3w ago
width 100% but if need to make the head of the bear out of the box .container { display: flex; justify-content: center; max-width: 2000px; gap: 10px; } .parent{ flex: 0 1 500px; background-color: red; } img { width: 100%; } nvm, but my question is using flex is the right way on here ? and i'm wondering when i should add width to an element ? when should use % or px or other type ?
StefanH
StefanH3w ago
percentage is used if you want an element's size to be proportional with the parent's size. width: 100% is very common for example to make an element be as big as the parent (block level elements like divs are already take up 100% width so no need to declare it manually)
StefanH
StefanH3w ago
px is a pixel. The issue is that because different screens have different pixel density and sizes, 1:1 pixel scaling is almost never useful. Turn on "Desktop view" in your mobile browser and you'll see what it'd look like if 1px = 1px. Because of this we usually prefer using the rem unit which is equivalent to 16px by default. "The lengths are perceptual: 16px looks roughly the same on a phone, laptop, or TV screen at typical viewing distance" - mdn
MDN Web Docs
CSS values and units - Learn web development | MDN
CSS rules contain declarations, which in turn are composed of properties and values. Each property used in CSS has a value type that describes what kind of values it is allowed to have. In this lesson, we will take a look at some of the most frequently used value types, what they are, and how they work.
StefanH
StefanH3w ago
There's also unity like these which i never used. I guess they're useful in print?
No description
kamekameha
kamekamehaOP3w ago
ok
StefanH
StefanH3w ago
the mdn page i linked is really good btw
kamekameha
kamekamehaOP3w ago
ok, anyway sir i'm wondering
kamekameha
kamekamehaOP3w ago
No description
kamekameha
kamekamehaOP3w ago
if you have layout like this let say red is button, purple is icon same like the other kinds of image if me i'm goind to make bear and button separate component and make the other as background but what's the most optimize way ?
StefanH
StefanH3w ago
I'd make the purple icon also a normal element in the flow I guess you can do a lot with background images too but prefer to use real elements in cases where the size of an object can change Background images have to either stretch or crop to accommodate different container sizes. They can't intelligently rearrange themselves
kamekameha
kamekamehaOP3w ago
so you will export all the component separately ?
StefanH
StefanH3w ago
Also never use a background image for text because you can't select / copy it and a screen reader can't read it
kamekameha
kamekamehaOP3w ago
yes that's why button i made separate
StefanH
StefanH3w ago
Honestly i find it hard to understand this example Why is there a pentagram
kamekameha
kamekamehaOP3w ago
that's just kind like sticker
StefanH
StefanH3w ago
I'd make that its own element yeah
kamekameha
kamekamehaOP3w ago
but then what is the div structure looks like ?
StefanH
StefanH3w ago
I'd use background images only for things that are actually backdrops. Where it doesn't matter if something gets cropped off, like a landscape background or some kind of pattern
kamekameha
kamekamehaOP3w ago
and if it's resize it's still okay and perfect ?
StefanH
StefanH3w ago
If you make it work then yeah Programming is problem solving. There's no one way to do everything. You have to look at your design and then figure out how to use the tools available to you to make it work You keep changing the design, so the way i'd build it also changes (this is actually really annoying at work lol)
StefanH
StefanH3w ago
You could make a grid like this
No description
StefanH
StefanH3w ago
And then the flowers are either absolute positioned or a background image. Really depends on what they actually are
kamekameha
kamekamehaOP3w ago
ok, i never implemented grid before i need to watch some kevin video about grid
StefanH
StefanH3w ago
Kevin made tons of videos about it yeah
kamekameha
kamekamehaOP3w ago
so do you mean the grid will lock the absolute/relative element ?
StefanH
StefanH3w ago
not sure what you mean with lock
kamekameha
kamekamehaOP3w ago
i mean limit movement so for example red button can't go to the star area
StefanH
StefanH3w ago
The grid lets you position all the different elements nicely and when you resize the card, the elements will follow your grid's row and column sizes
kamekameha
kamekamehaOP3w ago
owh ok, but why need that ?
No description
StefanH
StefanH3w ago
You can configure the columns and rows like this
No description
StefanH
StefanH3w ago
1fr means "take up as much space as you can" auto means "be as big as the child wants to be but no bigger" those grid lines are gonna be there but the bear simply spans over multiple rows of course you won't actually see them. this is just debug visualization
kamekameha
kamekamehaOP3w ago
owh ok so it's posibble to delete that so it become like this
No description
kamekameha
kamekamehaOP3w ago
ok kevin suggest at outline isn't usually
StefanH
StefanH3w ago
you don't "delete" it. you just tell the element to take up multiple rows
kamekameha
kamekamehaOP3w ago
ok
StefanH
StefanH3w ago
here you can see how the image is set up to take up multiple rows
No description

Did you find this page helpful?