Centering the checkbox in a line [duplicate] - html

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 3 years ago.
I would like to center the checkbox in a line with text next to it.
I've tried already style="text-align: center;" in <div> but it doesn't work.
input {
font-size: 16px;
}
label {
font-size: 11px;
vertical-align: middle;
}
form {
margin-top: 30px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
input[type="checkbox"] {
width: 15px;
height: 15px;
-webkit-appearance: none;
background: white;
outline: none;
border: none;
border-radius: 50%;
transition: .5s;
box-shadow: inset 0 0 5px rgba(0, 0, 0, .2)
}
input:checked[type="checkbox"] {
background-color: transparent;
}
<form class="form-box__form form">
<input type="e-mail" name="e-mail" id="e-mail" placeholder="e-mail" />
<input type="password" name="password" id="password" placeholder="password" />
<button>Create account</button>
<div style="text-align: center;">
<input type="checkbox" name="consent" id="consent" />
<label for="consent">I agree to whatever you want me to agree</label>
</div>
</form>
At the moment it looks like this in the attached picture. So can someone please guide me on how to center the checkbox in a line?
here

Please add below line only your css class, not need to change in html you written:
vertical-align: middle;
position: relative;
bottom: 1px;
input {
font-size: 16px;
}
label {
font-size: 11px;
vertical-align: middle;
}
form {
margin-top: 30px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
input[type="checkbox"] {
width: 15px;
height: 15px;
-webkit-appearance: none;
background: white;
outline: none;
border: none;
border-radius: 50%;
transition: .5s;
box-shadow: inset 0 0 5px rgba(0, 0, 0, .2);
vertical-align: middle;
position: relative;
bottom: 1px;
}
input:checked[type="checkbox"] {
background-color: transparent;
}
<form class="form-box__form form">
<input type="e-mail" name="e-mail" id="e-mail" placeholder="e-mail"/>
<input type="password" name="password" id="password" placeholder="password"/>
<button>Create account</button>
<div style="text-align: center;">
<input type="checkbox" name="consent" id="consent"/>
<label for="consent">I agree to whatever you want me to agree</label>
</div>
</form>

Just put the checkbox in the tag and use
display: flex;
align-items: center;
on the label
input {
font-size: 16px;
}
label {
font-size: 11px;
vertical-align: middle;
}
form {
margin-top: 30px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
input[type="checkbox"] {
width: 15px;
height: 15px;
-webkit-appearance: none;
background: white;
outline: none;
border: none;
border-radius: 50%;
transition: .5s;
box-shadow: inset 0 0 5px rgba(0, 0, 0, .2)
}
input:checked[type="checkbox"] {
background-color: transparent;
}
label {
display: flex;
align-items: center;
}
<form class="form-box__form form">
<input type="e-mail" name="e-mail" id="e-mail" placeholder="e-mail" />
<input type="password" name="password" id="password" placeholder="password" />
<button>Create account</button>
<div>
<label for="consent"><input type="checkbox" name="consent" id="consent" />I agree to whatever you want me to agree</label>
</div>
</form>

Use align-items property.
div { display: flex; align-items: center; } //immediate parent of checkbox and label

For parent div of input add the below style property
.div{
Display:inline-flex;
}
Or you can add the below code
<label for="input" ><input type="checkbox" name="input" /></label>

Try position:relative and bottom:**px as per your need
input {
font-size: 16px;
}
label {
font-size: 11px;
vertical-align: middle;
}
form {
margin-top: 30px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
input[type="checkbox"] {
width:15px;
height: 15px;
-webkit-appearance: none;
background: white;
outline: none;
border: none;
border-radius: 50%;
transition: .5s;
box-shadow: inset 0 0 5px rgba(0, 0, 0, .2);
margin:0;
bottom:-3.5px;
position:relative
}
input:checked[type="checkbox"] {
background-color: transparent;
}
<form class="form-box__form form">
<input type="e-mail" name="e-mail" id="e-mail" placeholder="e-mail"/>
<input type="password" name="password" id="password" placeholder="password"/>
<button>Create account</button>
<div style="text-align: center;display:inline;">
<input type="checkbox" name="consent" id="consent" >
<label for="consent" > I agree to whatever you want me to agree</label>
</div>
</form>

Related

Form field alignment issues using flexbox [duplicate]

This question already has an answer here:
Input field wrapping using flexbox
(1 answer)
Closed 1 year ago.
PROBLEM:
I'm having some alignment issues with this form.
CODE SNIPPET:
.form-container {
display: flex;
margin: 0 auto;
align-items: center;
justify-content: center;
}
.verification-form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.verification-form .form-group {
display: flex;
flex-direction: row;
margin-bottom: 35px;
width: 100%;
}
.verification-form .form-group:nth-child(3) {
margin-bottom: 5px;
}
.form-group label {
text-align: right;
font-size: 11px;
font-weight: bold;
color: #484343;
margin-left: 10px;
max-width: 150px;
}
.form-group input {
padding: 0px 0px;
margin-left: 20px;
flex: 3;
border: 2px solid #d2d2d2;
border-width: 0 0 2px 0;
transition: border-color 0.3s;
font-size: 18px;
}
#required::after {
content: "*";
color: #f06d41;
font-size: 28px;
}
.form-group input:hover {
border-color: #aa2c2f;
outline: 0;
}
.form-group input:focus {
border-color: #aa2c2f;
outline: 0;
}
input[type="checkbox"] {
width: 28px !important;
height: 28px !important;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance: none;
appearance: none;
outline: none;
box-shadow: none;
text-align: center;
background: #ffffff;
flex: none;
padding: 0px 0px;
margin-left: 20px;
border: 2px solid #d2d2d2;
border-width: 2px;
transition: border-color 0.3s;
font-size: 18px;
cursor: pointer;
}
input[type="checkbox"]:checked:after {
content: "✔";
color: #aa2c2f;
<div class="form-container">
<div class="verification-form">
<div class="form-group">
<label>FIRST</label>
<input id="firstName" type="" name="firstName" value="" required />
<label>LAST</label>
<input id="lastName" type="" name="lastName" value="" required />
</div>
<div class="form-group" id="required">
<label>EMAIL</label>
<input id="email" type="email" name="email" value="" required />
</div>
<div class="form-group">
<label>PHONE</label>
<input id="phone" type="" name="phone" value="" required />
<label class="sms-label">SMS<br />OPT IN</label>
<input id="sms" type="checkbox" name="sms" value="" required />
</div>
<div class="form-group" id="required">
<label>STREET<br />ADDRESS</label>
<input id="address" type="text" name="address" value="" required />
</div>
<div class="legend">
<p>
<sub>*</sub>Required field
</p>
</div>
</div>
</div>
As you can see, the form looks wonky.
I feel like this is the culprit:
.form-group input {
padding: 0px 0px;
margin-left: 20px;
flex: 3;
border: 2px solid #d2d2d2;
border-width: 0 0 2px 0;
transition: border-color 0.3s;
font-size: 18px;
}
I would like the form to align properly where all fields in a single column are the same size and aligned. I also want the rows with two form fields to be even with rows that have only one field.
I've tried excluding flex:3 with a static width, however, this is not the solution. Would appreciate any help in making this look better.
EDIT: Also open to a solution using CSS grid.
I did some changes in your code. You should use flex for the paten div for the form and if you give witdh:100% to the input fileds it will fit the container of its own. So here the html and css for your form.
Html:
<div class="form-container">
<div class="verification-form">
<div class="form-group" >
<label class="required">FIRST</label>
<input id="firstName" type="" name="firstName" value="" required />
<label class="required">LAST</label>
<input id="lastName" type="" name="lastName" value="" required />
</div>
<div class="form-group" id="required">
<label class="required">EMAIL</label>
<input id="email" type="email" name="email" value="" required />
</div>
<div class="form-group">
<label class="required">PHONE</label>
<input id="phone" type="" name="phone" value="" required />
</div>
<div class="form-group row">
<label class="sms-label required">SMS OPTION</label>
<input id="sms" type="checkbox" name="sms" value="" required />
</div>
<div class="form-group" id="required">
<label class="required" >STREET ADDRESS</label>
<input id="address" type="text" name="address" value="" required />
</div>
<div class="legend">
<p>
<sub class="required"></sub>Required field
</p>
</div>
</div>
</div>
CSS:
.verification-form .form-group:nth-child(3) {
margin-bottom: 5px;
}
.form-group {
display:flex;
flex-direction:column;
text-align:start;
justify-content:flex-start;
max-width:320px;
padding:10px;
}
.form-group.row {
flex-direction:row;
align-items:center;
margin-bottom:10px;
}
.form-group label {
text-align: start;
font-size: 11px;
font-weight: bold;
color: #484343;
margin-left: 20px;
margin-bottom:10px;
max-width: 200px;
}
.form-group input {
width:100%;
padding: 0px 0px;
margin-left: 20px;
flex: 3;
border: 2px solid #d2d2d2;
border-width: 0 0 2px 0;
transition: border-color 0.3s;
font-size: 18px;
margin-bottom:10px;
}
.required::after {
content: "*";
color: #f06d41;
font-size: 16px;
}
.form-group input:hover {
border-color: #aa2c2f;
outline: 0;
}
.form-group input:focus {
border-color: #aa2c2f;
outline: 0;
}
.legend {
margin-left:20px;
}
input[type="checkbox"] {
width: 28px !important;
height: 28px !important;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance: none;
appearance: none;
outline: none;
box-shadow: none;
text-align: center;
background: #ffffff;
flex: none;
padding: 0px 0px;
margin-left: 20px;
border: 2px solid #d2d2d2;
border-width: 2px;
transition: border-color 0.3s;
font-size: 18px;
cursor: pointer;
}
input[type="checkbox"]:checked:after {
content: "✔";
color: #aa2c2f;}

How to make some elements visible in place of others on click of a button in a website?

I am trying to make a login and signup form which just takes username and password as input by default and when the user presses sign in button new set of inputs like first name, last name, date of birth etc. have to be taken..I tried to write a code with the intention of achieving the same but it doesn't work..
#form{
width: 300px;
margin: 40px auto;
display: flex;
flex-direction: column;
justify-content: center;
border: 3px solid rgb(6, 61, 30);
padding: 20px;
border-radius: 30px;
background-color: #e4e1ff;
}
.butts{
display: flex;
justify-content: center;
position: relative;
}
#form .butt{
width: 150px;
height: 50px;
font-size: 25px;
display: inline-block;
background-color: #e4e1ff;
border: none;
}
hr{
height: 4px;
width: 150px;
margin: 0;
background: tomato;
border: none;
transition: 0.2s ease-in-out;
position: absolute;
left: 0px;
bottom: 0px;
}
#butt1:focus ~ hr{
left: 0;
}
#butt2:focus ~ hr{
left: 50%;
}
#login_form{
margin-top: 20px;
}
#login_form input{
width: 300px;
height: 50px;
font-size: 20px;
margin-bottom: 10px;
border-radius: 20px;
}
#signup_form{
display: none;
}
#butt2:focus ~ #signup_form{
display: block;
}
#butt2:focus ~ #login_form{
display: none;
}
<section id="form">
<div class="butts">
<button class="butt" id="butt1">Login</button>
<button class="butt" id="butt2">Sign Up</button>
<hr>
</div>
<div id="login_form">
<input type="text" placeholder="username">
<input type="password" placeholder="password">
</div>
<div id="signup_form">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type='date' placeholder="Birthdate">
<input type='text' placeholder="username">
<input type="password" placeholder="password">
<input type="password" placeholder="confirm password">
<button>Connect Github</button>
<button>Create Account</button>
</div>
</section>
function formSwitch(){
var lf=document.getElementById('login_form');
var sf=document.getElementById('signup_form');
if(event.target.id == "butt1"){
lf.style.display='block';
sf.style.display='none';
}else{
lf.style.display='none';
sf.style.display='block';
}
}
#form{
width: 300px;
margin: 40px auto;
display: flex;
flex-direction: column;
justify-content: center;
border: 3px solid rgb(6, 61, 30);
padding: 20px;
border-radius: 30px;
background-color: #e4e1ff;
}
.butts{
display: flex;
justify-content: center;
position: relative;
}
#form .butt{
width: 150px;
height: 50px;
font-size: 25px;
display: inline-block;
background-color: #e4e1ff;
border: none;
}
hr{
height: 4px;
width: 150px;
margin: 0;
background: tomato;
border: none;
transition: 0.2s ease-in-out;
position: absolute;
left: 0px;
bottom: 0px;
}
#butt1:focus ~ hr{
left: 0;
}
#butt2:focus ~ hr{
left: 50%;
}
#login_form{
margin-top: 20px;
}
#login_form input{
width: 300px;
height: 50px;
font-size: 20px;
margin-bottom: 10px;
border-radius: 20px;
}
#signup_form{
display: none;
}
#butt2:focus ~ #signup_form{
display: block;
}
#butt2:focus ~ #login_form{
display: none;
}
<section id="form">
<div class="butts">
<button class="butt" id="butt1" onClick="formSwitch()">Login</button>
<button class="butt" id="butt2" onClick="formSwitch()">Sign Up</button>
<hr/>
</div>
<div id="login_form">
<input type="text" placeholder="username">
<input type="password" placeholder="password">
</div>
<div id="signup_form">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type='date' placeholder="Birthdate">
<input type='text' placeholder="username">
<input type="password" placeholder="password">
<input type="password" placeholder="confirm password">
<button>Connect Github</button>
<button>Create Account</button>
</div>
</section>

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>

inline field are merged when adjusting label

I am trying to create a responsive form using flex where the first name and last name will be in same column in large devices and be in separate column in small devices. I designed the form and it was working as expected(not the responsive part though)until I adjust the label in the input box. I used the position: absolute to adjust the label, so it break the design of first name and last name field. They got merge now.
Here is the demo
http://jsbin.com/pecijupicu/3/edit?html,css
Meanwhile, here is the source code
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3 {
color: rgb(67, 77, 90);
}
form {
min-width: 400px;
width: 600px;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
.form-input {
flex: 1;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0, 213, 229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"],
textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0, 213, 229);
}
textarea {
width: 100%;
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>
I have made some changes to the html structure and added some extra styles to suit your use case. My solution supports responsiveness as well.
link to working example: JSbin
Here is the code:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html, body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3{
color: rgb(67, 77, 90);
}
form {
width: 100%;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
form .combined{
display: flex;
flex-direction: row;
}
.form-input {
flex: 1;
}
form .combined .field{
flex:1;
justify-content: space-between;
/* border: 1px solid red; */
}
form .combined .field .form-input{
margin-right: 20px;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0,213,229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"], textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus, textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0,213,229);
}
textarea {
width: 100%;
}
#media screen and (max-width: 520px) {
form .combined{
display: flex;
flex-direction: column;
}
form .combined .field{
flex:1;
justify-content: space-between;
margin: 25px 0;
}
form .combined .field label{
position: absolute;
top: -11px;
}
form .combined .field .form-input{
margin-right: 0;
}
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="combined">
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>
Add left: 50% to the lastname label - it makes sense as the absolutely positioned labels share the full-width 50-50 due flex: 1 given to them. See demo below:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
background: #f2f3f78a;
-webkit-font-smoothing: antialiased;
}
aside.form-container {
display: flex;
flex-direction: column;
flex: 1;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px 0 #f0efef;
margin: 0 40px;
border-radius: 6px;
}
.form-container h3 {
color: rgb(67, 77, 90);
}
form {
min-width: 400px;
width: 600px;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 auto;
}
form .field {
margin: 15px 0;
/* flex-direction: column; */
display: flex;
position: relative;
}
.form-input {
flex: 1;
}
label {
position: absolute;
top: 4px;
}
form .field label {
margin: 10px 0;
color: rgb(67, 77, 90);
}
form .field label:nth-of-type(2) {
left: 50%; /* ADDED */
}
button.next {
padding: 10px 0;
width: 100px;
background: rgb(0, 213, 229);
border: none;
display: flex;
justify-content: center;
}
button.next:after {
content: "Next";
color: #fff;
}
input[type="text"],
textarea {
padding: 8px;
border: transparent;
border-bottom: solid 1px #999;
}
input[type="text"]:focus,
textarea:focus {
outline: none;
border-bottom: solid 1px rgb(0, 213, 229);
}
textarea {
width: 100%;
}
<aside class="form-container">
<h3>Personal</h3>
<form>
<div class="field">
<label>First Name</label>
<input type="text" name="name" class="form-input" />
<label>Last Name</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Country</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>City</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="field">
<label>Description</label>
<textarea></textarea>
</div>
<!-- show button to the bottom right corner -->
<button class="next"></button>
</form>
</aside>

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>