This question already has answers here:
Make flex items take content width, not width of parent container
(3 answers)
Closed 1 year ago.
How do I avoid items in flex column stretching to full width? I want it to be original size as created.
.myForm {
display: flex;
flex-direction: column;
}
<form class="myForm">
<input type="text" />
<input type="text" />
<button>Test</button>
</form>
In CSS the default value for the flex property align-items is stretch which as you would guess stretches the children of the flex parent to full width. You can change it something else to fix that. For instance
.myForm {
display: flex;
flex-direction: column;
align-items: flex-start;
}
<form class="myForm">
<input type="text" />
<input type="text" />
<button>Test</button>
</form>
Read more about this property here https://developer.mozilla.org/en-US/docs/Web/CSS/align-items
Use max-width: max-content;, or width: max-content; on child element.
.myForm {
display: flex;
flex-direction: column;
}
/* below will be select any first level children under .myForm */
.myForm > * {
max-width: max-content;
}
<form class="myForm">
<input type="text" />
<input type="text" />
<button>Test</button>
</form>
You mentioned display:flex; so your elements size is also flexed to column size.
You can provide css property for your first level child individually like
.myForm > * {
width: fit-content;
}
.myForm {
display: flex;
flex-direction: column;
}
/* below will be select any first level children under .myForm */
.myForm > * {
width: fit-content;
}
<form class="myForm">
<input type="text" />
<input type="text" />
<button>Test</button>
</form>
Related
This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed last month.
I have this rather simple setup, and would just like to move the outer div (neumorphic-input) to the bottom of my page. How can I do this using the current CSS I have.
I am currently using the CSS to align the two elements in the dive side by side and horizontally centred on my page.
HTML:
<body class="body">
<div class="neumorphic-input">
<textarea class="text-input" id="text-input" placeholder="Enter your message"></textarea>
<button class="send-button">Send</button>
</div>
</body>
CSS:
.neumorphic-input {
display: flex;
flex-direction: row;
justify-content: center;
}
You can use min-height: 100vh; and some flexbox to position it at the bottom of the screen:
.neumorphic-input {
display: flex;
flex-direction: row;
justify-content: center;
}
body {
display: flex;
flex-direction: column;
justify-content: flex-end;
min-height: 100vh;
}
<body class="body">
<div class="neumorphic-input">
<textarea class="text-input" id="text-input" placeholder="Enter your message"></textarea>
<button class="send-button">Send</button>
</div>
</body>
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I don't understand Why justify-content:center not working?
I think it's a inputs width issue i already tried give width to input fields but still not working.
Here is my code:
.container {
width: 100vw;
}
.container form {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
.container form input {
width: 80%;
}
<div class="container">
<form action="" method="post" novalidate>
<div>
<label for="my_username">Username:</label>
<input type="text" name="username" class="inputs" autofocus required id="my_username">
</div>
<div>
<label for="my_email">Email:</label>
<input type="email" name="email" maxlength="254" id="my_email">
</div>
<div>
<label for="my_password1">Password1:</label>
<input type="password" name="password1" class="inputs" required id="my_password1">
</div>
<div>
<label for="my_password2">Confirm Password:</label>
<input type="password" name="password2" class="inputs" required id="my_password2">
</div>
</form>
</div>
justify-content defines the alignment along the main axis (in your case column. So you are centering the content within its row.
It also works only for the direct decendents of the flex container (in this case the div elements that are direct children of the form. If you change justify-content to align-items (which works for the cross-axis) then this should center the div elements horizontally, and then you can add the css to align the label and input elements within those divs.
There's a really good guide available for the properties of flex-box and what they affect here.
.container {
width: 100vw;
}
.container form {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
.container form input {
width: 80%;
}
It can be solved if you use align-items instead of justify-content. It's because of justify-content works in main axis and align-items in cross-axis, perpendicular to the main axis.
.container form {
display: flex;
flex-direction: column;
align-items: center;
}
This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 3 years ago.
I'm trying to use flexbox to achieve a column layout with an element "floated" to the right.
Something like this:
Thing is, I cannot alter the HTML since it's an embedded form so trying to achieve this via pure CSS.
So far, I have this:
.form {
display: flex;
flex-direction: column;
text-align: center;
}
.form-container {
display: flex;
flex-direction: column;
text-align: center;
justify-content: space-between;
}
.form-form {
width: 100%;
}
form {
display: flex;
justify-content: center;
}
fieldset {
border: 0;
}
textarea {
resize: none;
}
.form-columns-2 {
display: flex;
}
.form-columns-2 input:first-child {
margin-bottom: .5em
}
.form-columns-1 textarea {
height: 100%
}
.submit-wrapper{
flex-direction:column;
}
<div class="form">
<div class="form-container">
<div class="form-form">
<form>
<fieldset class="form-columns-2">
<input type="text" placeholder="test">
<input type="text" placeholder="test">
</fieldset>
<fieldset class="form-columns-2">
<input type="text" placeholder="test">
<input type="text" placeholder="test">
</fieldset>
<fieldset class="form-columns-1">
<textarea placeholder="test">Text</textarea>
</fieldset>
<div class="submit-wrapper">
<div class="actions">
<input type="submit" value="Submit">
</div>
</div>
</form>
</div>
</div>
</div>
Edit:
I also need the submit button to sit under the textarea, but since the .submit-wrapper is a direct child of form, the only way I can see addressing this is by adding flex-direction: column; to form. However, this turns the whole layout into a column, which I do not want.
You are apllying display: flex to the wrong elements and to more elements than you need to.
Check this: https://codepen.io/anon/pen/dwPoEP
* {
box-sizing: border-box;
}
fieldset{
border:0;
}
textarea{
resize:none;
}
form { // you want the form to be a flex box, not everything!
display: flex;
justify-content: center;
}
.form-columns-2 {
display: flex; //you can use other options here, this works but you can use display: block on the inputs for example
}
// this 2 are just to make it look like the image
.form-columns-2 input:first-child {
margin-bottom: .5em
}
.form-columns-1 textarea {
height: 100%
}
This question already has answers here:
input / button elements not shrinking in a flex container
(7 answers)
Why don't flex items shrink past content size?
(5 answers)
Closed 5 years ago.
I'm trying to create a form with first row a single text field 100% width and 2nd row 3 fields equidistant. It works fine on Chrome. However it's overflowing on FireFox.
.form {
display: flex;
flex-direction: column;
width: 300px;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.form input {
width: 100%;
padding: 20px;
}
.form .number {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.form .expiry {
display: flex;
width: 100%;
justify-content: space-between;
}
<div class="form">
<div class="number">
<input data-stripe="number" placeholder="Credit Card Number" class="" type="text">
</div>
<div class="expiry">
<input data-stripe="exp-month" placeholder="MM" class="" type="text">
<input data-stripe="exp-year" placeholder="YY" class="" type="text">
<input data-stripe="cvc" placeholder="CVC" class="" type="text">
</div>
</div>
https://jsfiddle.net/xhr031yr/2/
You will need to set:
.form .expiry input {
min-width: 0;
}
jsFiddle
The reason is flex items have min-width:auto set by default, and for a replaced element such as <input>, it has intrinsic dimensions. The flex item won't shrink when the total width of the input boxes exceeded.
You can also give the size attribute a smaller value (default is 20 for text input) to make it smaller:
<input type="text" size="3">
References
Automatic Minimum Size of Flex Items
Replaced elements
HTML <input> attribute size
I can't tell you why that's happening, my guess is the way the browser handles the input element.
You can achieve the layout by...
Add the box-sizing rule.
Wrapping the inputs in a container
fiddle
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.form {
display: flex;
flex-direction: column;
width: 300px;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.form input {
width: 100%;
padding: 20px;
}
.form .number {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.form .expiry {
display: flex;
width: 100%;
justify-content: space-between;
}
<div class="form">
<div class="number">
<input data-stripe="number" placeholder="Credit Card Number" class="" type="text">
</div>
<div class="expiry">
<div class="input">
<input data-stripe="exp-month" placeholder="MM" class="" type="text">
</div>
<div class="input">
<input data-stripe="exp-year" placeholder="YY" class="" type="text">
</div>
<div class="input">
<input data-stripe="cvc" placeholder="CVC" class="" type="text">
</div>
</div>
</div>
<!---->
This question already has an answer here:
Why are not all flexbox elements behaving like flexbox divs?
(1 answer)
Closed 8 years ago.
I want to use the flex display property on my web form in order to have two text fields side-by side and automatically growing to fill the space. I chose to use flex over a table layout because I want them to move to their own line when the window shrinks.
I believe I am misunderstanding how to use flexbox.
Below is my work so far (simplified for posting here). I am using a fieldset as a flex parent, and each form element which should "grow" side-by-side is encased in a div (set to display: inline so they can be on the same line). I also have a legend for the fieldset, set to display:block and width:100% so that it will be on its own line.
jsFiddle link
fieldset {
display: flex;
flex-direction: row;
justify-content: space-between;
border: none;
}
label {
display: none;
}
legend {
display: block;
width: 100%;
}
fieldset > div {
display: inline;
flex: 1 1 auto;
}
input {
width: 100%;
}
<form>
<fieldset>
<legend>These are Text Fields</legend>
<div>
<input type="text" id="text1">
<label for="text1">Text Field</label>
</div>
<div>
<input type="text" id="text2">
<label for="text2">More Text</label>
</div>
</fieldset>
</form>
As you can see, the divs are each on their own line (despite the display: inline and flexbox stuff). If you add a border to the div elements, you'll see that they are 100% width right now; but even if you manually define a width (like 30%), they remain on their own lines.
How can I use flex to display the divs side-by-side and allow them to scale to fill the parent's width?
EDIT: This bug has been fixed in Firefox and Chrome. The example as originally posted in the question now works as intended.
https://bugzilla.mozilla.org/show_bug.cgi?id=1230207
https://bugs.chromium.org/p/chromium/issues/detail?id=375693
Two things here:
When you do display: flex on an element, every single direct child is affected. So in your example (if it were working) the legend will also be displayed on the same line.
For whatever reason, fieldset doesn't like to play nicely with flexbox. since you don't want the legend to be on the same line anyway, I would suggest specifically wrapping the elements you want to be on the same line.
<form>
<fieldset>
<legend>These are Text Fields</legend>
<div class="flex-wrapper">
<div>
<input type="text" id="text1">
<label for="text1">Text Field</label>
</div>
<div>
<input type="text" id="text2">
<label for="text2">More Text</label>
</div>
</div>
</fieldset>
</form>
fieldset {
border: none;
}
.flex-wrapper {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
}
.flex-wrapper > div {
flex: 1 1 auto;
}
label {
display: none;
}
legend {
display: block;
width: 100%;
}
input {
width: 100%;
}