How to make varying labels width and input-fields take same horizontal space

Hello guys, sorry to disturb you all; can someone explain how I should proceed for my login labels and input fields to take the same space horizontally please, for e.g in the picture, if the label is longer, our input field will be further to the right... I don't want that.. I want both labels and input fields to take the same width. Here is my codepen: https://codepen.io/Fakeur/pen/WNVaMxx
No description
51 Replies
glutonium
glutoniumā€¢2mo ago
if one lable says "name" and the other says "something very long" u cannot make them of the same size. like they r just 2 sentences with different length the best u can do here is move the input field of the name further towards the right
glutonium
glutoniumā€¢2mo ago
No description
glutonium
glutoniumā€¢2mo ago
or maybe u can try putting label above input field
name
---------------------
---------------------

something long
---------------------
---------------------
name
---------------------
---------------------

something long
---------------------
---------------------
or maybe this. idk
-- name -----------------
-------------------------

-- something long -------
-------------------------
-- name -----------------
-------------------------

-- something long -------
-------------------------
Jochem
Jochemā€¢2mo ago
the easiest quick and dirty way is to wrap the labels and inputs in a form (like you should be doing anyway) and use this:
form {
display: flex;
flex-wrap: wrap;
width: 60ch;
}
form > * {
width: 50%;
}
form {
display: flex;
flex-wrap: wrap;
width: 60ch;
}
form > * {
width: 50%;
}
it's very quick and dirty very best way is to use grid
Zeshan Merchant
Zeshan Merchantā€¢2mo ago
My suggestion would be use grids with 1 parent container, you can use form instead of div for better semantics
<body>
<h1>Learning promises, async and await</h1>
<div class="wrapper">
<label for="name">Name:</label>
<input type="text" id="name" name="name" /> <br/>
<label for="delay">Something very long:</label>
<input type="text" id="delay" name="delay" /><br/>
<button type="submit">Set alarm</button>
</div>
</body>
<body>
<h1>Learning promises, async and await</h1>
<div class="wrapper">
<label for="name">Name:</label>
<input type="text" id="name" name="name" /> <br/>
<label for="delay">Something very long:</label>
<input type="text" id="delay" name="delay" /><br/>
<button type="submit">Set alarm</button>
</div>
</body>
*,
*::after,
*::before {
box-sizing: border-box;
margin: 0;
}

body {
min-height: 100vh;
min-height: 100svh;
}

h1 {
margin-bottom: 1em;
}

input {
margin-bottom: 2em ;
}

.wrapper{
display : grid;
grid-template-columns : repeat(2,1fr);
width: 350px;
}

.wrapper label{
grid-column : 1/2;
}

.wrapper button{
grid-column : 1/3;
justify-self : center;
}
*,
*::after,
*::before {
box-sizing: border-box;
margin: 0;
}

body {
min-height: 100vh;
min-height: 100svh;
}

h1 {
margin-bottom: 1em;
}

input {
margin-bottom: 2em ;
}

.wrapper{
display : grid;
grid-template-columns : repeat(2,1fr);
width: 350px;
}

.wrapper label{
grid-column : 1/2;
}

.wrapper button{
grid-column : 1/3;
justify-self : center;
}
Jochem
Jochemā€¢2mo ago
^ This is the best solution yeah
Zeshan Merchant
Zeshan Merchantā€¢2mo ago
you can adjust the width as you want https://codepen.io/Zeshan-Merchant-the-scripter/pen/NWQOMPo which one?
Jochem
Jochemā€¢2mo ago
yours
V3X4TI0US
V3X4TI0USā€¢2mo ago
Dayummm First time in my life someone appreciated my code šŸ„¹ And first time I actually helped in a post
Jochem
Jochemā€¢2mo ago
are you using two accounts, or did you message on the wrong post?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
i would rather do grid-template-columns:auto 1fr; So the first columns width is based on the width of the label with the most text. And for button i would rather do grid-column:1/-1; so it will always span full width no matter how many columns you have
Zeshan Merchant
Zeshan Merchantā€¢2mo ago
actually the first one is from my work PC, and second one is from my mobile. I saw the post in my mobile. First one I added for any help i might need while I am at work. I hope there are no issues with that yeah this is also cool and better approach
Jochem
Jochemā€¢2mo ago
yeah, you're good. I was just confused
Jeluxe
Jeluxeā€¢2mo ago
Btw what is this code do? body { min-height: 100vh; min-height: 100svh; }
Zeshan Merchant
Zeshan Merchantā€¢2mo ago
idk, it was in his code pen
Jochem
Jochemā€¢2mo ago
it sets the height of the body to 100vh, and if the browser supports it to 100svh. but please make a new question if you want to go into it more, this isn't on topic for Faker's question
Zeshan Merchant
Zeshan Merchantā€¢2mo ago
I always notice this and I really appreciate your dedication to the server and the rules. ā¤ļøšŸ™Œ
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
NO! BAD BAD BAD!!!
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
no margin: 0 on *
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
everything else looks correct
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
I agree on that, but it was not added by Zeshan, it was in Faker's original codepen. I would much rather use a CSS Reset
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
yes, a good css reset i didn't notice the margin on the css, so my apologies for saying this is wrong BAD BAD BAD!!!
Faker
FakerOPā€¢2mo ago
Hmm I always use margin 0 šŸ˜­ why is it bad please
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
no margin: 0 on *
Faker
FakerOPā€¢2mo ago
what is the css reset ?
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
you're removing the ok browser margins to then add them back
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
What i have done is to take CSS Reset from Meyerweb and then i have customized it to my own preference. And added some additional stuff. But it resets some user agent styles
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
a css reset is a set of styles crafted to remove as much bullshittery and inconsistency from browsers, so you start from an equally equal clean slate on all browsers
Faker
FakerOPā€¢2mo ago
ah okay I see, so, no margin of 0 on * , but if I include it on body, it is the same thing ?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
You can use margin:0; on body, thats fine
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
it's different
Faker
FakerOPā€¢2mo ago
oh okay
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
on body, you may want to use it the 8px default margin can cause weird issues
Faker
FakerOPā€¢2mo ago
yep I see, I think normally I put it in the body but don't know why I but it in * šŸ„² Thanks for pointing this out, really appreciate !! And thanks to all for the solution of using grid... I will have a look and come back if I have some doubts
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
I also see a lot of people doing something like
body {
margin:0;
padding:0;
}
body {
margin:0;
padding:0;
}
Like seriously... Come on ! Why would you do that. Body does not even have a default padding in the first place. Why would you set padding to 0 when body doesnt have a padding xD hahaha
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
šŸ˜…
Faker
FakerOPā€¢2mo ago
yeah, I was one of those person šŸ¤£ but I don't do it now
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
just to be double sure because some browsers may do weird stuff
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
They must be using a very strange browser in this case. I have never seen any browser that applies a padding to body. But well, I have not tried every single browser out there. But most of them is just based of chromium so yeah... Most of them are basicly the same
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
i do it because im just paranoid and bad habits
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
haha
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
šŸ’€ but hey, at least i dont put margin: 0 on * šŸ¤£
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2mo ago
Yeah hahaha šŸ˜‚ That's far worse honestly xD
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
some things do have both margins and paddings, like lists probably my bad habit came from there
V3X4TI0US
V3X4TI0USā€¢2mo ago
Not my code. I just edited the wrapper part
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
i know, i didnt respond to your message
V3X4TI0US
V3X4TI0USā€¢2mo ago
Zeshan and me are the same person btw
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢2mo ago
oh, i didnt knew
Want results from more Discord servers?
Add your server