I am trying to get a form's inputs to fill the container width but since I am using two flexboxes, it doesn't seem to allow me to set the width of the input boxes. Is there a way around this? I've tried using flex-shrink but got the same result.
What is the best way to set a form's width to fill its container using flexbox?
.sd-form-group {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: flex-start;
}
.sd-form-wrap {
display: flex;
}
.sd-form-group input[type=text],
.sd-form-group textarea {
width: 100%;
}
<form>
<div class="sd-form-group">
<div class="sd-form-wrap">
<div class="form-name form-first-name">
<label>First Name</label>
<br>
<input type="text">
</div>
<div class="form-name form-last-name">
<label>Last Name</label>
<br>
<input type="text">
</div>
</div>
<div class="form-email">
<label>Email</label>
<br>
<input type="text">
</div>
<div class="form-message">
<label>Message</label>
<br>
<textarea placeholder="Enter your comments, questions and feedback here." rows="4"></textarea>
</div>
<div class="form-checkbox">
<input type="checkbox">
<label>
Communications box
</label>
</div>
<input type="submit" value="Submit">
</div>
</form>
On the container, instead of flex-flow: column nowrap use row wrap.
On the items, force each one to occupy the full width of the row.
.sd-form-group {
display: flex;
/* flex-flow: column nowrap; */
flex-wrap: wrap; /* new */
justify-content: center;
/* align-items: flex-start; */ /* does nothing */
}
.sd-form-group > div {
flex: 0 0 100%; /* new */
}
.sd-form-wrap {
display: flex;
}
.sd-form-group input[type=text],
.sd-form-group textarea {
width: 100%;
}
<form>
<div class="sd-form-group">
<div class="sd-form-wrap">
<div class="form-name form-first-name">
<label>First Name</label>
<br>
<input type="text">
</div>
<div class="form-name form-last-name">
<label>Last Name</label>
<br>
<input type="text">
</div>
</div>
<div class="form-email">
<label>Email</label>
<br>
<input type="text">
</div>
<div class="form-message">
<label>Message</label>
<br>
<textarea placeholder="Enter your comments, questions and feedback here." rows="4"></textarea>
</div>
<div class="form-checkbox">
<input type="checkbox">
<label>
Communications box
</label>
</div>
<input type="submit" value="Submit">
</div>
</form>
Related
Currently what Im trying to achieve is this:
But I tried this using flex attribute but the layout is messed up. Here is my code for the same.
<div className="contact-container" style={{flexDirection:"row",backgroundColor:"#ECECEC"}}>
<div style={{padding:"30px"}}>
<div style={{flexDirection:"column"}}>
<p style={{color:"#0E2043",backgroundColor:"#ECECEC", fontSize:"22px",fontWeight:"700", textAlign:"justify"}}>If you are looking to enjoy exactly these benefits, Passport Legacy is here to assist you with your second citizenship. </p>
<p style={{color:"#0E2043", paddingTop:"30px",backgroundColor:"#ECECEC", textAlign:"justify"}}>If you would like more information about any of the qualifying programs, or our services, please do not hesitate to contact us.</p>
</div>
</div>
<div style={{padding:"30px"}}>
<div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px", width:"100%"}} type="text" placeholder="Name"/>
</div>
<div style={{width:"100%",display:"flex", flexWrap:"wrap", justifyContent:"space-between"}}>
<div style={{padding:"5px"}}>
<input style={{padding:"5px",width:"100%"}} type="text" placeholder="email"/>
</div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px",width:"100%"}} type="text" placeholder="phone"/>
</div>
</div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px", width:"100%"}} type="text" placeholder="Subject"/>
</div>
<div style={{padding:"5px"}}>
<textarea style={{padding:"5px", width:"100%"}} rows="4" placeholder="Message"/>
</div>
</div>
</div>
</div>
Ive added a sandbox too, Please feel free to make changes.
https://codesandbox.io/s/divine-dawn-l27wy?file=/src/App.tsx:71-1766
Here is your code with a little changes, I made it responsive too for width less than 400px.
const App=()=>{
return(
<div className="contact-container" style={{backgroundColor:"#ECECEC"}}>
<div style={{padding:"30px"}}>
<div style={{flexDirection:"column"}}>
<p style={{color:"#0E2043",backgroundColor:"#ECECEC", fontSize:"22px",fontWeight:"700", textAlign:"justify"}}>If you are looking to enjoy exactly these benefits, Passport Legacy is here to assist you with your second citizenship. </p>
<p style={{color:"#0E2043", paddingTop:"30px",backgroundColor:"#ECECEC", textAlign:"justify"}}>If you would like more information about any of the qualifying programs, or our services, please do not hesitate to contact us.</p>
</div>
</div>
<div style={{padding:"30px"}}>
<div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px", width:"100%"}} type="text" placeholder="Name"/>
</div>
<div style={{width:"100%",display:"flex", flexWrap:"wrap", justifyContent:"space-between"}}>
<div style={{padding:"5px"}}>
<input style={{padding:"5px",width:"100%"}} type="text" placeholder="email"/>
</div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px",width:"100%"}} type="text" placeholder="phone"/>
</div>
</div>
<div style={{padding:"5px"}}>
<input style={{padding:"5px", width:"100%"}} type="text" placeholder="Subject"/>
</div>
<div style={{padding:"5px"}}>
<textarea style={{padding:"5px", width:"100%"}} rows="4" placeholder="Message"/>
</div>
</div>
</div>
</div>
)}
ReactDOM.render(
<App />,
document.getElementById("react")
);
.contact-container {
display: flex;
flex-direction: row;
}
.contact-container > div {
width:50%;
}
#media (max-width: 400px) {
.contact-container {
flex-direction: column;
}
.contact-container > div {
width:100%;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="react"></div>
All you have to do is specify the "width" for the "div" on the left.
<div style={{padding:"30px", width:"50%"}}>
First of all it is not a ReactJS issue. It is just you are not configuring the flexbox propertly.
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: grid;
place-items: center;
}
.container {
width: 90%;
display: flex;
max-width: 500px;
flex-direction: column;
border: 1px solid red;
}
.container input,
.container textarea {
width: 100%;
padding: 0.75rem;
border: 1px solid #e5e5e5;
}
.container input {
margin-top: 1rem;
}
.container .flex {
display: flex;
justify-content: space-between;
}
.container .flex input:nth-child(2) {
margin-left: 1rem;
}
.container textarea {
display: block;
margin-top: 1rem;
}
<div class="container">
<form>
<input type="text" placeholder="Name" />
<div class="flex">
<input type="text" placeholder="Email">
<input type="text" placeholder="Phone">
</div>
<input type="text" placeholder="Subject">
<textarea id="" cols="20" rows="10"></textarea>
</form>
</div>
Let me know if this works? 😊
I have a form inside a div inside. The div is inside another div.
<div class="div__signup"> <!-- takes the complete width/height of content section -->
<div class="content_div--white"> <!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
This html is inside a css-grid which has 3 rows - navigation, content and footer.
.css-grid-container{
height:100vh; /*???*/
display: grid;
grid-gap:20px;
grid-template-columns: 1fr; /* 1 columns*/
grid-template-rows: auto 15fr 1fr; /* 3 rows. Auto takes height of navigation, remaining is divided into 2 rows, middle row (content) is 15 times larger than the 3rd row (footer).*/
}
I want the html of the form page to be at the center of the content section of the css-grid. If I use flexbox, I suppose I have to provide specific height for the flexbox. The only height parameter I know is 100vh which equals height of the viewport.
.div__signup{
display:flex;
align-items: center;
justify-content: center;
height: 100vh; /*need something else than 100vh as I dont want alignment w.r.t viewport */
}
But I want the form to be at the center of the content section of the grid, not the center of the viewport. How could I align the form to the center of the content section?
Based on the fact that .div__signup is a child of a grid item, here are two suggestions:
Using absolute position
.css-grid-item {
position: relative;
}
.div__signup {
position: absolute;
left: 0; top: 0;
right: 0; bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
Make the grid item a flex container
.css-grid-item {
display: flex;
align-items: center;
justify-content: center;
}
Do note, sample 2 may, or may not work, based on how the justify-content/align-items is set on the css-grid-item.
Assumed HTML
<div class="css-grid-item">
<div class="div__signup"> <!-- takes the complete width/height of content section -->
<div class="content_div--white"> <!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
</div>
See this I use display fixed, table and table cell
.seline-preloader {
position: fixed;
left: 0px;
top: 0;
width: 100%;
height: 100%;
z-index: 9999;
background: #ffffff;
display: block;
box-sizing: border-box;
}
.table {
display: table;
width: 100%;
height: 100%;
background-color: transparent !important;
}
.table {
width: 100%;
max-width: 100%;
margin-bottom: 20px;
}
.table-cell {
display: table-cell;
width: 100%;
height: 100%;
vertical-align: middle;
}
<section class="seline-preloader">
<div class="table">
<div class="table-cell">
<h2 class="hide">Form</h2>
<!--start of your code-->
<div class="div__signup">
<!-- takes the complete width/height of content section -->
<div class="content_div--white">
<!-- contains the form. Adjust this div to create a border around the form -->
<form class="signup-form" actions="">
<label for="firstname" class="first-name">First Name</label>
<input id="firstname" type="text">
<label for="lastname" class="last-name">Last Name</label>
<input id="lastname" type="text">
<label for="email" class="email"> Email</label>
<input id="email" type="email">
<label for="password" class="password">Password</label>
<input id="password" type="password">
<label for="verify-password" class="verify-password">Verify Password</label>
<input id="verify-password" type="password">
</form>
</div>
</div>
<!--end of your code-->
</div>
</div>
</section>
jsfiddle
Hi there I am creating a form for my webpage. I am trying to implement it using flex so it can adjust to the window size. My problem is that the input box for the lastName overflows. Any pointers how I can fix this so that the last name input box and the label drops to a new line or if its possible to squeeze both in. If you could help that would be greatly appreciated! Here so pics of whats going on and the code will be below that.
form before resizing :
form after resizing :
The code:
#survey {
border: 1px solid black;
padding: 15px;
display: flex;
flex-flow: column nowrap;
}
div#names {
display: flex;
flex-flow: row nowrap;
flex: 1 1 auto;
}
div.formRow {
flex: 1 1 auto;
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.formRow>* {
flex: 1 1 auto;
}
div.horizontalRow {
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.horizontalRow>* {
flex: 1 1 auto;
}
<form id="survey" action="url" method="post">
<label>Name*</label>
<br>
<div id="names">
<div class="formRow">
<input type="text" name="firstname">
<label for="firstname">First Name</label>
</div>
<div class="formRow">
<input type="text" name="lastname">
<label for="lastname">LastName</label>
</div>
</div>
<div class="horizontalRow">
<label for="email">Email Address *</label>
<input type="email" name="email">
</div>
<div class="horizontalRow">
<label for="subject">Subject *</label>
<input type="text" name="subject">
</div>
<div class="horizontalRow">
<label for="message">Message *</label>
<input type="text" name="message">
</div>
<br>
<div id="submitDiv">
<input id="submit" type="submit" value="Submit">
</div>
</form>
You may use media query to change behavior under 400px (or any other value you want) so each input will be in a seperate line instead of having them both in the same line.
It's also better for UX/UI than having two small inputs in one line :
#media all and (max-width:400px) {
div#names {
flex-flow: column nowrap;
}
div.formRow>input {
order:1; /*Put the label on top to behave like other fields*/
}
}
Full code :
#survey {
border: 1px solid black;
padding: 15px;
display: flex;
flex-flow: column nowrap;
}
div#names {
display: flex;
flex-flow: row nowrap;
flex: 1 1 auto;
}
div.formRow {
flex: 1 1 auto;
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.formRow>* {
flex: 1 1 auto;
}
div.horizontalRow {
display: -webkit-flex;
display: flex;
flex-flow: column nowrap;
}
div.horizontalRow>* {
flex: 1 1 auto;
}
#media all and (max-width:400px) {
div#names {
flex-flow: column nowrap;
}
div.formRow>input {
order:1;
}
}
<form id="survey" action="url" method="post">
<label>Name*</label>
<br>
<div id="names">
<div class="formRow">
<input type="text" name="firstname">
<label for="firstname">First Name</label>
</div>
<div class="formRow">
<input type="text" name="lastname">
<label for="lastname">LastName</label>
</div>
</div>
<div class="horizontalRow">
<label for="email">Email Address *</label>
<input type="email" name="email">
</div>
<div class="horizontalRow">
<label for="subject">Subject *</label>
<input type="text" name="subject">
</div>
<div class="horizontalRow">
<label for="message">Message *</label>
<input type="text" name="message">
</div>
<br>
<div id="submitDiv">
<input id="submit" type="submit" value="Submit">
</div>
</form>
Currently I am making a form that has multiple inputs and I am using flex box to make these inputs appear in a column, and text-align center to get the whole form into a centered row. I am attempting to get the text boxes all be in the center of the page and the text move over accordingly.
.mainForm {
text-align: center;
}
.left {
float: left;
}
.inputs {
display: flex;
flex-direction: column;
}
.radio {
display: flex;
justify-content: center;
}
textarea {
overflow-y: scroll;
resize: none;
}
.feedBack {
text-align: center;
}
<div class="information">
<p class="formInputs"> Policy Number:
<input id="polNum" name="polNum" type="text" onkeyup="javascript:displayPolicyNumber()" /></p>
<p class="print">Policy Number: <span class="display" id="display_policyNumber"></span></p>
<p class="formInputs">Control Number:
<input id="membNbr" name="membNbr" type="text" onkeyup="javascript:displayControlNumber()" /></p>
<p class="print">Control Number: <span class="display" id="display_controlNumber"></span></p>
<p class="formInputs">Last Name or Business Name:
<input id="lastName" name="lastName" type="text" onkeyup="javascript:displayLastName()" /></p>
<p class="print">Last Name or Business Name: <span class="display" id="display_lastName"></span></p>
<p class="formInputs">First Name :
<input id="firstName" name="firstName" type="text" onkeyup="javascript:displayFirstName()" /></p>
<p class="print">First Name: <span class="display" id="display_firstName"></span></p>
<p class="formInputs">Comments:
<textarea name="comment" id="comment" cols="30" rows="2" onkeyup="javascript:displayComments()"></textarea></p>
<p class="print">Comments: <span class="display" id="display_comment"></span></p>
</div>
If you want your input/textarea to be absolutely centered, you will have to wrap your text in the <label> element (or any other HTML element, but for usability reason you should always use <label>). That is because we want to position the text independently of the input. An example:
<p class="formInputs">
<label for="polNum">Policy Number:</label>
<input id="polNum" name="polNum" type="text" />
</p>
When that is done, you can use the following trick:
Set a fixed width for your input elements. Let's say we want them to be 200px wide. Instead of setting this on the input elements themselves, we set it on the containing parent, .formInputs, and then set input elements to take up this full width.
Position the <label> element absolutely within the .formInputs parent. Set it to right: 100% so that it will be offset to the left by the full width of the parent.
Use a right padding so that the right edge of the label text is not directly sticking to the left border of your input
See proof-of-concept below:
textarea {
overflow-y: scroll;
resize: none;
}
.formInputs {
position: relative;
width: 200px;
margin: 0 auto;
}
.formInputs label {
position: absolute;
right: 100%;
padding-right: 10px;
white-space: nowrap;
}
.formInputs input,
.formInputs textarea {
width: 100%;
}
<div class="information">
<p class="formInputs">
<label for="polNum">Policy Number:</label>
<input id="polNum" name="polNum" type="text" />
</p>
<p class="formInputs">
<label for="membNbr">Control Number:</label>
<input id="membNbr" name="membNbr" type="text" />
</p>
<p class="formInputs">
<label for="lastName">Last Name or Business Name:</label>
<input id="lastName" name="lastName" type="text" />
</p>
<p class="formInputs">
<label for="firstName">First Name:</label>
<input id="firstName" name="firstName" type="text" />
</p>
<p class="formInputs">
<label for="comment">Comments:</label>
<textarea name="comment" id="comment" cols="30" rows="2"></textarea>
</p>
</div>
Something like this?
.information{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
http://jsfiddle.net/Lcgfqjh9/
You can put the whole thing in a wrapper, apply flex with the below settings to it to center the form, and apply flex and justify-content: space-between to the single lines (i.e. the .formInputs elements) to align the text and input fields at the left and right border:
.wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.formInputs {
display: flex;
justify-content: space-between;
}
<div class="wrapper">
<div class="information">
<p class="formInputs"> Policy Number:
<input id="polNum" name="polNum" type="text" onkeyup="javascript:displayPolicyNumber()" /></p>
<p class="print">Policy Number: <span class="display" id="display_policyNumber"></span></p>
<p class="formInputs">Control Number:
<input id="membNbr" name="membNbr" type="text" onkeyup="javascript:displayControlNumber()" /></p>
<p class="print">Control Number: <span class="display" id="display_controlNumber"></span></p>
<p class="formInputs">Last Name or Business Name:
<input id="lastName" name="lastName" type="text" onkeyup="javascript:displayLastName()" /></p>
<p class="print">Last Name or Business Name: <span class="display" id="display_lastName"></span></p>
<p class="formInputs">First Name :
<input id="firstName" name="firstName" type="text" onkeyup="javascript:displayFirstName()" /></p>
<p class="print">First Name: <span class="display" id="display_firstName"></span></p>
<p class="formInputs">Comments:
<textarea name="comment" id="comment" cols="30" rows="2" onkeyup="javascript:displayComments()"></textarea></p>
<p class="print">Comments: <span class="display" id="display_comment"></span></p>
</div>
</div>
Wrap your form in a div.
Set the div's display to block and text-align to center (this will
center the contained form).
Set the form's display to inline-block (auto-sizes to content), left
and right margins to auto (centers it horizontally), and
text-align to left (or else its children will be center-aligned too).
HTML file
<div class="form">
<form method = "post", action="">
---------
</form>
</div>
in .css
use like this
div.form {
display: inline-block;
text-align: center;
}
This is a Bootstrap-like form:
HTML:
<div class="container">
<div class="row">
<div class="form-group row-group">
<label class="col-6">First name</label>
<input class="col-6" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-6">Last name</label>
<input class="col-6" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-8" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group row-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="col-12 row-group">
<button class="btn">Submit</button>
</div>
</div>
</div>
CSS:
.row {
display: flex
}
.row-group {
display: flex
flex-direction: row !important
align-items: center
label {
flex: 0 0 100px
}
input {
flex: 1
}
}
.form-group {
flex: 1 0 0
flex-direction: column
}
This is the result:
I want the inputs of the first row and third row to reach the end of the container div (to not to stack horizontally with its neigbhor).
I tried this:
.row-group {
input: flex: 1 1 100%
}
But nothing moves.
What's the correct way to do this?
Please check out this Fiddle - is this what you're going for?
https://jsfiddle.net/jspruance/1p739t1h/
Here's the modified CSS:
.row {
xdisplay: flex;
}
.row-group {
display: flex;
flex-direction: row !important;
align-items: center;
}
label {
flex: 0 0 100px;
}
input {
width: 100%;
box-sizing: border-box;
flex: 1;
}
.form-group {
flex: 1 0 0;
flex-direction: column;
}
I fixed a few problems with the CSS:
1) Extra close bracket after input element
2) Missing bracket after .row-group
3) Added 'width: 100%' property and 'box-sizing: border-box'
4) Floated label left.
5) Commented out 'display: flex' on .row ( not needed)
Hope this helps.