vertical alignment for an element in the middle

Can anyone help me out to center vertically in the middle as i always fail to achieve this.(min-height is given in the requirement) https://codepen.io/kvandana451/pen/pomWzgK
58 Replies
Jochem
Jochem7mo ago
The easiest way to center something both vertically and horizontally is using flex:
.container {
display: flex;
justify-content: center;
align-items: center;
...
}
.container {
display: flex;
justify-content: center;
align-items: center;
...
}
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
Vandana
VandanaOP7mo ago
Tried doesnt works in my example
Mannix
Mannix7mo ago
seems to be centered in the linked pen
Jochem
Jochem7mo ago
I tried it in your example, and it works just fine. And like Mannix said, it's working in the linked codepen now
Vandana
VandanaOP7mo ago
In vscode it isnt
No description
Vandana
VandanaOP7mo ago
since 3hrs trying this thing feels so dumb
Mannix
Mannix7mo ago
i don't see the min-height on the screenshot
Jochem
Jochem7mo ago
I think you mean min-height? Vertical alignment only works when there's somewhere for the thing to align to. When you don't set a fixed or minimum height, the containing element (flex parent) will just be the exact same height as the children. Those will then try to align themselves vertically inside the parent, but because the parent is the exact same height as they are, there's nowhere for them to go
Mannix
Mannix7mo ago
yeah min-height sorry 😄
Jochem
Jochem7mo ago
When you get stuck with this stuff, use the inspection tools, or even just put a border / outline on everything so you can see what's going on
Vandana
VandanaOP7mo ago
yeah its working now! i dont know why sometimes it doesnt comes for certain designs.i dont know this trick of handling it. will keep this in mind Thanks in the above example
.container{
width:min(80%,728px);}
.container{
width:min(80%,728px);}
this is the shorthand for writing width and max-width property. min value will be 80% only then .80x100 =80px is this thinking approach right?
Jochem
Jochem7mo ago
no, it will set the width to whatever is lower, 80% of the containing block, or 728px. It won't shrink any smaller than that minimum though. Max-width will stop the element growing past the set max width, but doesn't otherwise set or influence width what approach?
Vandana
VandanaOP7mo ago
here if we write the shorthand as width:min(80%,768px);min value is 80px write ?because its parent is 100% that is that banner background.80x100so 80px Thats what lower will be 80% in our example!!
Jochem
Jochem7mo ago
something with width: min(80%, 728px); in an 100px wide parent will indeed be 80px wide. Something with max-width: 80%; will be up to 80px wide. There is a difference
Vandana
VandanaOP7mo ago
In our codepen example
width: min(80%,728px);
width: min(80%,728px);
has a 100% parent i.e that red background color div element. So min is 80px not 728px!! Something like consider,,
width: max(80%,728px);
width: max(80%,728px);
here max is 728px!!!This is what you are saying?
Jochem
Jochem7mo ago
no, I'm saying width: min() and max-width: aren't the same thing what you're saying is accurate though, those are the values that would be used in that particular example
Vandana
VandanaOP7mo ago
then why are we writing shorthands
Jochem
Jochem7mo ago
width isn't shorthand for max-width
Vandana
VandanaOP7mo ago
Yes..
width:80%;
max-width:768px;
width:80%;
max-width:768px;
instead of this we write
width: min(80%,768px)
width: min(80%,768px)
what is it called then instead of shorthand?
Jochem
Jochem7mo ago
shorthand is a specific term in CSS, for a property that is used to set multiple values simultaneously. border is a shorthand for border-color border-width and a bunch of others. What you're writing is just a different, shorter way of achieving a similar thing
Vandana
VandanaOP7mo ago
So this is right ?
Jochem
Jochem7mo ago
yes, in that example that is how that works
Vandana
VandanaOP7mo ago
ok thanks a ton for your patience in responding .Thank you
Jochem
Jochem7mo ago
glad to help!
Vandana
VandanaOP7mo ago
Keep up this great work !! Thanks once again!
Mannix
Mannix7mo ago
the correct term is function min() is a function 🙂
Vandana
VandanaOP7mo ago
Okay. how can we save all these post for future reference because the explaination is awesome and after 3 days of inactivity we lose the content
Jochem
Jochem7mo ago
you don't. They'll be hidden from the active posts list in the sidebar, but they'll still be in #Looking up solutions vs figuring it out yourself while learning/creating front-end projects we don't remove posts ever, unless they're against the rules
Jochem
Jochem7mo ago
vertical alignment for an element in the middle - Kevin Powell - Co...
Can anyone help me out to center vertically in the middle as i always fail to achieve this.(min-height is given in the requirement) https://codepen.io/kvandana451/pen/pomWzgK
Jochem
Jochem7mo ago
though it'll take a few hours for the most recent messages to show up there
Vandana
VandanaOP7mo ago
Ok i got backup of all my posts in Answer Flow. So shall i assume it will be life long and it wont get vanished??
Jochem
Jochem7mo ago
That's never a safe assumption but it should be around for a bit.
Vandana
VandanaOP7mo ago
Ok then how will i keep a backup of this post apart from discord for my personal reference for long run.
Jochem
Jochem7mo ago
save it to your hard drive? store it in google drive?
Vandana
VandanaOP7mo ago
Ok then ..
Jochem
Jochem7mo ago
like I said, this post isn't going anywhere any time soon, and answers overflow seems to be doing fine too, but nothing on the internet is permanent. If you want continued access to something, you have to have multiple copies you yourself control
Vandana
VandanaOP7mo ago
you youself control meaning?
Jochem
Jochem7mo ago
that you control the files. They are on your hardware, with you the sole person with access to them
Vandana
VandanaOP7mo ago
oh! I tried to export the chat to google drive but couldnt instead we need a third party tool for it.Any sugestions?
Jochem
Jochem7mo ago
Then go to the answers overflow site and click file, save as
Vandana
VandanaOP7mo ago
no such option.
Vandana
VandanaOP7mo ago
No description
Jochem
Jochem7mo ago
click the three dots top right, go to save and share, then click save page as or just hit ctrl+s
Vandana
VandanaOP7mo ago
it gets saved as a file folder which is completely unreadable
Jochem
Jochem7mo ago
it should be a file and a folder, and you should be able to just doubleclick the file and open it but alternatively, you can print it to a PDF or otherwise just manually take some notes and save it in a word doc or something
Vandana
VandanaOP7mo ago
Yea after opening that folder everything is in some machine readable format. No such options. Its too lengthy because you guys have explained in a very simplified manner which i appreciate
Jochem
Jochem7mo ago
you open the file, not the folder
Jochem
Jochem7mo ago
ctrl+p, select Microsoft Print to PDF on the right
No description
Jochem
Jochem7mo ago
in the answers overflow thing
Vandana
VandanaOP7mo ago
Yea I achieved it thanks thanks thanks. @Jochem Could you please look into this https://discord.com/channels/436251713830125568/1250478936236752988
Jochem
Jochem7mo ago
@Vandana please don't @ people to look at your posts or ask in other locations in general, new posts will get the attention they get
Vandana
VandanaOP7mo ago
Ok sorry ill delete this I said it because its been too long for waiting for the solution that is why.
Jochem
Jochem7mo ago
You can bump the post the question is in by saying something like "I'm still looking for an answer" or something similarly polite.
Vandana
VandanaOP7mo ago
okay ill try this .What if still the question is being left unanswered .
Jochem
Jochem7mo ago
Not much to be done then. Maybe try refining the question or sharing a clearer example Not all questions get answered
Vandana
VandanaOP7mo ago
What if those questions are of high priority then? We may not get to know who is on a holiday right?Since all have their own pace
Jochem
Jochem7mo ago
Find someone to pay to answer your question, everyone here volunteers their time 🤷 But don't try to find someone to pay through this server or by dm'ing people from this server
Vandana
VandanaOP7mo ago
👍 I understand.
Want results from more Discord servers?
Add your server