How to create a form with two vertically divided sections - html

I am currently trying to create a basic form using HTML & CSS. In it there are 4 fields: amount of men, amount of women, amount of kids and amount of beer per adult.
I want the first three inputs to be on one side, and the fourth to have its own space on the right. I have not been able to achieve this effect. The beer field should be centered horizontally to the right, and beneath it, the input box.
Maybe I should be using a table for this but alas, I am very new to CSS and am honestly a bit clueless.
body {
display: flex;
align-items: center;
justify-content: center;
}
#app {
border: 1px dotted black;
padding: 1.5em;
width: 30em;
}
#left-div {
display: inline-block;
padding: 0.5em;
border: 1px dotted black;
}
#left-div input {
width: 2em;
margin: 0.5em;
}
#left-div label {
margin: 0.5em 0em 0.5em 0em;
display: inline-block;
}
/* Chopp */
#div-chopp {
display: inline-flex;
border: 1px dotted black;
padding: 0.5em;
}
#div-chopp label {
display: block;
}
#div-chopp input {
width: 2em;
}
<div id="app">
<form action="POST">
<div id="left-div">
<label for="men">Men</label>
<input type="number" value="0" name="men">
<br>
<label for="women">Women</label>
<input type="number" value="0" name="women">
<br>
<label for="kids">Kids</label>
<input type="number" value="0" name="kids">
</div>
<div id="div-chopp">
<label for="beer">Litros de Beer por Adulto</label>
<input type="number" value="0" name="beer">
</div>
</form>
</div>

Here's a start. You could use more custom classes instead of the descendant selectors. Avoid using IDs, though, so your CSS is reusable.
body {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper {
display: flex;
border: 1px dotted black;
padding: 1.5em;
width: 30em;
}
.wrapper>div {
display: flex;
flex-direction: column;
padding: 0.5em;
border: 1px dotted black;
}
.wrapper>div>div {
display: flex;
justify-content: space-between;
}
.wrapper>div:last-child>div {
justify-content: center;
}
.wrapper input {
width: 2em;
margin-left: .5em;
}
<div id="app">
<form action="POST">
<div class="wrapper">
<div>
<div>
<label for="men">Men</label>
<input type="number" value="0" name="men">
</div>
<div>
<label for="women">Women</label>
<input type="number" value="0" name="women">
</div>
<div>
<label for="kids">Kids</label>
<input type="number" value="0" name="kids">
</div>
</div>
<div>
<div>
<label for="beer">Litros de Beer por Adulto</label>
</div>
<div>
<input type="number" value="0" name="beer">
</div>
</div>
</div>
</form>
</div>

Related

How to make my radio, checkbox and text area inputs responsive?

Below code was done in Codepen. Here is link: https://codepen.io/andyzam/pen/mdVvrLB
When I resize the window, the items in the table resize properly. But the radio buttons, checkboxes, and text area inputs stay fixed. How do I get those to move?
Do I need to add something to the #radio, #checkboxes, and #textarea IDs? I tried using # radio label {} and using different display properties but they still remained stationary.
body{
background: lightblue;
font-family: monospace;
margin: 0px;
}
p {
text-align: center;
font-size: 14px;
}
#title {
text-align: center;
}
#description {
margin-left: auto;
margin-right: auto;
width: 500px;
font-size: 20px;
text-align: justify;
padding: 10px;
}
input {
width: 250px;
}
label {
font-size: 14px;
margin-right: 0px;
}
table {
margin-left: auto;
margin-right: 35em;
}
td {
text-align: right;
width: 250px;
padding: 10px;
}
input:invalid {
border: 2px dashed red;
}
input:invalid:required {
background-image: linear-gradient(to right, pink, white);
}
input:valid {
border: 2px solid black;
}
select {
width: 260px;
}
select:invalid:required {
background-image: linear-gradient(to right, pink, white);
}
select:valid {
border: 2px solid black;
}
#radio {
display: block;
margin-left: 53em;
padding: 10px;
}
input[type="radio"] {
width: 20px;
}
#checkboxes {
display: block;
margin-left: 53em;
padding: 10px;
}
input[type="checkbox"] {
width: 20px;
}
#text-field {
display: block;
margin-left: 45em;
padding: 10px;
}
#submit-button {
text-align: center;
padding: 20px;
}
<html>
<body>
<title>TikTok User Survey</title>
<main>
<h1 id="title">TikTok User Survey</h1>
<p id="description">We are gathering information from various TikTok users to gauge their satisfaction with the social media platform.</p>
<p> All fields are required.</p>
<form id="survey-form">
<table>
<tr>
<td><label for="name" id="name-label">Your name:</label></td>
<td><input type="text" id="name" name="name-label" placeholder="Your Name" required></td>
</tr>
<tr>
<td> <label for="email" id="email-label">Enter your email:</label></td>
<td><input type="email" id="email" name="email" placeholder="Email" required></td>
</tr>
<tr>
<td> <label for="number" id="number-label">How many followers:</label></td>
<td><input type="number" id="number" name="number" placeholder="No. of Followers (0-100k)" required min="0" max="100000"></td>
</tr>
<tr>
<td>
<label for="age">How old are you?:</label></td>
<td>
<select id="dropdown" name="age" required>
<option disabled selected value> -- select an option -- </option>
<option value="under13">Under 13</option>
<option value="13-20">13-20</option>
<option value="20-30">20-30</option>
<option value="over30">Over 30</option>
</select>
</td>
</tr>
</table>
<p>My gender:</p>
<div id='radio'>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</div>
<p>Favorite types of videos:</p>
<div id="checkboxes">
<input type="checkbox" id="dancing" name="dancing" value="dancing">
<label for="dancing">Dancing</label><br>
<input type="checkbox" id="cooking" name="cooking" value="cooking">
<label for="cooking">Cooking</label><br>
<input type="checkbox" id="pranks" name="pranks" value="pranks">
<label for="pranks">Pranks</label><br>
</div>
<p>Any additional comments about what you like:</p>
<div id="text-field">
<textarea name="message" rows="10" cols="30">I also like....</textarea>
</div>
<div id="submit-button">
<button type="submit" id="submit">Submit</button>
</div>
</form>
</main>
</body>
</html>
Your problem is that you set a fixed margin-left on your radio buttons.
#radio {
display: block;
margin-left: 53em;
padding: 10px;
}
If you want to center your items perhaps you should do like you do with the description and use auto:
#description {
margin-left: auto;
margin-right: auto;
width: 500px;
font-size: 20px;
text-align: justify;
padding: 10px;
}
However an even better way might be to give a flexbox value to the containing element and then you can center elements from there if that is how you wish to position them.
.container {
display: flex;
justify-content: center;
align-items: center;
}
Flexbox isnt defined by horizontal or vertical centering since you can change the direction of centering. But in a default case justify-content: center; will center horizontally and align-items: center; will center vertically.
Outside of this i would also sugest you style using classes instead of ids. Classes can be repeated in more than one place and that is something you will appreciate when styling something like a checkbox that is likely to exist at several places.

I want my form layout to look exactly the same as the example( picture attached)

I want my form layout to look like the picture below:
This is what I currently have:
label {
display: inline-block;
border: 1px solid red;
}
input {
display: inline-block;
width: 28%;
margin-top: 1.5em;
margin-left: 2em;
border: 1px solid greenyellow;
}
<label for="First_Name"> First Name: </label>
<input type="text" id="First_Name">
<label for="Last_Name"> Last Name: </label>
<input type="text" id="Last_Name">
This is my output:
There are many ways to achieve this. Here is one possibility:
wrap each pair of label and input in a div (or similar)
use flexbox to get them to stack vertically
Everything else seems to be just a matter of inspecting the reference regarding the used colors and sizes, which you can do by using your Browser's Inspector (or looking at the source code). Alternatively, bring the provided image into an image editing program and check the colors there.
Here is a rough approximation based on the approach outlined above:
body {
background-color: #f6f7f5;
}
fieldset {
display: flex; /* use flexbox for content */
justify-content: center; /* center items horizontally */
border: none; /* remove default fieldset border */
}
.input {
flex-grow: 1; /* take up free space within parent */
flex-basis: 50%; /* width (before growing/shrinking) */
display: flex; /* use flexbox for content */
flex-direction: column; /* arrange content vertically */
margin: 1rem;
}
label {
padding: 0.5rem 0;
font-family: sans-serif;
font-size: 11pt;
color: #5b5b60;
}
input {
padding: 0.5rem;
color: #9d9d9d;
background: #fff;
border: 1px solid #ccc;
border-radius: 2px;
box-shadow:inset 0 1px 1px #eee;
}
.required label::after {
content: "*";
}
.required input {
border: 1px solid #a94442;
}
<fieldset>
<div class="input required">
<label for="First_Name">First Name:</label>
<input type="text" id="First_Name">
</div>
<div class="input">
<label for="Last_Name">Last Name:</label>
<input type="text" id="Last_Name">
<div>
</fieldset>
Try this code
label {
display: block;
}
input {
display: block;
width: 100%;
border: 1px solid greenyellow;
padding: 4px
}
.overflow {
width: 100%;
overflow: hidden
}
.pull-left {
width: calc(50% - 20px);
float: left;
margin: 10px
}
<div class="overflow">
<div class="pull-left">
<label for="First_Name"> First Name: </label>
<input type="text" id="First_Name">
</div>
<div class="pull-left">
<label for="Last_Name"> Last Name: </label>
<input type="text" id="Last_Name">
</div>
</div>

3 <label> tags share one same rule but display differently

First, let's see my screenshot. It expresses my problem very detail:
Demonstration
Here is a small part from my code:
.contain {
align-items: center;
display: flex;
justify-content: space-around;
min-width: 450px;
padding: 0 50pt 10pt 50pt
}
.contain.layout-col {
flex-direction: column
}
.item.field {
border-bottom: 2px solid #E0E0E0;
display: flex;
justify-content: center;
margin-bottom: 6pt;
max-width: 650px;
min-width: 300px
}
.item.field > label {
display: block;
font-weight: bold;
padding: 10pt 5pt 6pt 5pt;
width: 50px
}
.item.field > input {
padding: 10pt 16pt 6pt 16pt;
transition: all 0.21s ease-in-out;
width: 100%
}
.item.field > input:focus {
border-bottom: 2px solid #F06292;
font-size: 1em;
margin-bottom: -2px
}
<div class='contain layout-col'>
<div class='item field'>
<label for='name' id='name-label'>Name</label>
<input placeholder='What's your name?' id='name' type='text' name='name' required/>
</div>
<div class='item field'>
<label for='email' id='email-label'>Email</label>
<input placeholder='Do you have email?' id='email' type='email' name='email' required/>
</div>
<div class='item field'>
<label for='number' id='number-label'>Age</label>
<input placeholder='How old are you?' id='number' type='number' min='7' max='69' name='age' required/>
</div>
</div>
As you can see, 3 tags use one rule .item.field > label. And it all defines width as 50px and label tag has been set to block element. At this time, it's expected to be 50px for width and the 3 of tags should be equal to each other. That is the idea, but only the 2 first tags follow the rule. The last one #number-label doesn't do so. It loses a few pixels!
I really have no idea how come it could be like that. I hope you guy could enlighten me. (I have other solution is using float property, but this bug is bugging me off so I very much want to understand about it.)
My code was write on CodePen.io and run test on both Chrome-69 and Firefox-62. The results are the same.
width does not work quite the same way when applied to flex items, especially when you have told the input to be 100% wide.
For a fixed width use flex: 0 0 50px instead.
.contain {
align-items: center;
display: flex;
justify-content: space-around;
min-width: 450px;
padding: 0 50pt 10pt 50pt
}
.contain.layout-col {
flex-direction: column
}
.item.field {
border-bottom: 2px solid #E0E0E0;
display: flex;
justify-content: center;
margin-bottom: 6pt;
max-width: 650px;
min-width: 300px
}
.item.field > label {
display: block;
font-weight: bold;
padding: 10pt 5pt 6pt 5pt;
flex: 0 0 50px
}
.item.field > input {
padding: 10pt 16pt 6pt 16pt;
transition: all 0.21s ease-in-out;
/*width: 100% */
/*try*/
flex:1;
}
.item.field > input:focus {
border-bottom: 2px solid #F06292;
font-size: 1em;
margin-bottom: -2px
}
<div class='contain layout-col'>
<div class='item field'>
<label for='name' id='name-label'>Name</label>
<input placeholder='What's your name?' id='name' type='text' name='name' required/>
</div>
<div class='item field'>
<label for='email' id='email-label'>Email</label>
<input placeholder='Do you have email?' id='email' type='email' name='email' required/>
</div>
<div class='item field'>
<label for='number' id='number-label'>Age</label>
<input placeholder='How old are you?' id='number' type='number' min='7' max='69' name='age' required/>
</div>
</div>

Float right without creating new line

HTML:
<div>Due date:<input type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required disabled="disabled"></input></div>
<div>Created by:<input type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required disabled="disabled"></input></div>
CSS:
.open-tickets-view input {
border: 0px solid #474a52;
border-radius: 0px;
max-width: 200px;
RESULT:
If I try to float right with inline-block display:
CSS:
.open-tickets-view input {
border: 0px solid #474a52;
border-radius: 0px;
max-width: 200px;
display: inline-block;
float: right;
RESULT:
I have tried several different combinations among display: flex and use justify-content: space-between but the text always breaks a new line.
With input elements it's a good thing to use <label>. Cause it's a label for the input. Default browser behavior with labels is that if you click on the label the mouse will focus the input. Read more about it here
Using float:
div.myDiv {
width: 300px;
border: solid 2px red;
}
div.myDiv:after {
/* clear floats is a good thing */
content: '';
display: block;
clear: both;
}
div.myDiv input {
float: right;
border:solid 2px green;
}
div.myDiv label {
border:solid 2px green;
}
<div class="myDiv">
<label for="uname">Choose a username: </label>
<input type="text" id="uname" name="name">
</div>
using positioning:
div.myDiv {
position: relative;
width: 300px;
border: solid 2px red;
}
div.myDiv input {
border:solid 2px green;
position: absolute;
right: 0;
}
div.myDiv label {
border:solid 2px green;
}
<div class="myDiv">
<label for="uname">Choose a username: </label>
<input type="text" id="uname" name="name">
</div>
You can use margin-left:auto on a child element of display:flex with flex-direction:row like below.
.container, .container > div {
display: flex;
flex-direction: row;
white-space:nowrap;
}
.container .right {
margin-left: auto;
}
<div class='container'>
<div>
<span>Due date:</span>
<input type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required disabled="disabled" />
</div>
<div class='right'>
<span>Created by:</span><input type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required disabled="disabled" />
</div>
</div>
Or maybe:
.container,
.container>div {
display: flex;
}
.container {
flex-direction: column;
}
.container>div {
flex-direction: row;
padding:1em;
}
.container .right {
margin-left: auto;
}
<div class='container'>
<div>
<span>Due date:</span>
<input class='right' type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required disabled="disabled" />
</div>
<div>
<span>Created by:</span><input class='right' type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required disabled="disabled" />
</div>
</div>

Align 2 items side-by-side in a flex column

I am very new to flex. But I'm loving it so far. The question I have is, how do I align two items side-by-side when the parent has flex-direction: column;?
I have pasted my HTML and Sass code here along with screenshots.
I have a form like so:
// manage.html
<div class="form2" hidden>
<div class="message" hidden></div>
<form id="updateUserForm" action="https://someaction.com" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton"></button>
</form>
</div>
That is styled like so:
// style.scss
.form2 {
#updateUserForm {
display: flex;
flex-direction: column;
}
label {
text-align: center;
}
input {
border-style: solid;
border-color: $black;
color: $black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
}
input:focus::-webkit-input-placeholder {
color: transparent;
}
input:focus:-moz-placeholder {
color: transparent;
}
input:focus::-moz-placeholder {
color: transparent;
}
input:focus:-ms-input-placeholder {
color: transparent;
}
a,
button {
border: none;
color: $white;
background-color: $blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
display: inline-block;
align-self: flex-start;
cursor: pointer;
}
}
Here is the output:
I would like to align 'Cancel' link and 'Reactivate' button side by side. I tried the following:
// style.scss
a {
align-self: flex-start;
}
button {
align-self: flex-end;
}
This only slides the elements to left and right respectively without being on the same line. Here is the output I wish to get:
Any suggestions on how to reach my expected output?
You can just use flex-wrap: wrap with row direction and set flex: 0 0 100% on inputs.
form {
display: flex;
flex-wrap: wrap;
}
input {
flex: 0 0 100%;
}
<div class="form2">
<div class="message"></div>
<form id="updateUserForm">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton">Lorem</button>
</form>
</div>
Just place them in a container and change direction.
#updateUserForm {
display: flex;
flex-direction: column;
}
label {
text-align: center;
}
input {
border-style: solid;
border-color: black;
color: black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
}
input:focus::-webkit-input-placeholder {
color: transparent;
}
input:focus:-moz-placeholder {
color: transparent;
}
input:focus::-moz-placeholder {
color: transparent;
}
input:focus:-ms-input-placeholder {
color: transparent;
}
/*Just place them in a container and change direction*/
#button_container{
display:flex;
}
a,
button {
border: none;
color: white;
background-color: blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
display: inline-block;
align-self: flex-start;
cursor: pointer;
text-transform: uppercase;
}
<div class="form2">
<div class="message" hidden></div>
<form id="updateUserForm" action="https://someaction.com" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
<!--Just place them in a container and change direction -->
<div id="button_container">
Cancel
<button type="submit " class="updateButton">Reactivate</button>
<div>
</form>
</div>
With the existing markup, using flex-direction: column, you can't make those two "buttons" align side-by-side.
One way is to change to row wrap and make all the input + label 100% width, which can be done with either width: 100% (used below) or flex-basis: 100%.
With that the items being 100% wide will stack vertical and the rest horizontal (unless their total width doesn't exceed 100%)
Stack snippet
form {
display: flex;
flex-wrap: wrap;
}
label {
text-align: center;
width: 100%;
}
input {
border-style: solid;
border-color: black;
color: black;
font-size: 16px;
padding: 10px 14px;
text-align: left;
margin: 4px 2px;
width: 100%;
}
a,
button {
border: none;
color: white;
background-color: blue;
padding: 12px 16px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
<div class="form2">
<div class="message"></div>
<form id="updateUserForm" action="https://api.ghjobssubscribe.com/manage/update" method="POST">
<label class="userInfo"></label>
<input type="text" name="userFirstName" class="userFirstName" placeholder="First Name">
<input type="text" name="userLastName" class="userLastName" placeholder="Last Name">
<input type="text" name="subTag" class="subTag" placeholder="Title, benefits, companies, expertise">
<input type="text" name="subLocation" class="subLocation" placeholder="City, state, zipe code or country">
Cancel
<button type="submit " class="updateButton">Submit</button>
</form>
</div>