How can I add spacing between input elements that has flex-direction: row. I'm using components from the angular-material library. Below screenshot with a highlighted inputs that must have spacing between them
HTML
<form class="form-container">
<mat-form-field class="full-width">
<mat-label>First name *</mat-label>
<input type="text" name="firstName" matInput placeholder="First name" value="">
</mat-form-field>
<mat-form-field class="full-width">
<mat-label>Last name *</mat-label>
<input type="text" name="lastName" matInput placeholder="Last name" value="">
</mat-form-field>
<div class="date-of-birth">
<mat-form-field>
<mat-label>Day</mat-label>
<input type="number" name="day" matInput placeholder="Day" value="">
</mat-form-field>
<mat-form-field>
<mat-label>Month</mat-label>
<input type="number" name="month" matInput placeholder="Month" value="">
</mat-form-field>
<mat-form-field>
<mat-label>Year</mat-label>
<input type="number" name="year" matInput placeholder="Year" value="">
</mat-form-field>
</div>
</form>
CSS
.form-container {
min-width: 150px;
max-width: 500px;
width: 100%;
margin: 0 auto;
}
.full-width {
width: 100%;
}
.date-of-birth {
display: flex;
flex-direction: row;
}
Adding gap in .date-of-birth will solve your issue:
<form class="form-container">
<mat-form-field class="full-width">
<mat-label>First name *</mat-label>
<input type="text" name="firstName" matInput placeholder="First name" value="">
</mat-form-field>
<mat-form-field class="full-width">
<mat-label>Last name *</mat-label>
<input type="text" name="lastName" matInput placeholder="Last name" value="">
</mat-form-field>
<div class="date-of-birth">
<mat-form-field>
<mat-label>Day</mat-label>
<input type="number" name="day" matInput placeholder="Day" value="">
</mat-form-field>
<mat-form-field>
<mat-label>Month</mat-label>
<input type="number" name="month" matInput placeholder="Month" value="">
</mat-form-field>
<mat-form-field>
<mat-label>Year</mat-label>
<input type="number" name="year" matInput placeholder="Year" value="">
</mat-form-field>
</div>
</form>
CSS
.form-container {
min-width: 150px;
max-width: 500px;
width: 100%;
margin: 0 auto;
}
.full-width {
width: 100%;
}
.date-of-birth {
display: flex;
flex-direction: row;
gap: 20px;
}
You can use column-gap:
.date-of-birth {
column-gap: 5px;
display: flex;
flex-direction: row;
}
Initially a part of Multi-column Layout, the definition of column-gap has been broadened to include multiple layout methods. Now specified in Box Alignment, it may be used in Multi-column, Flexible Box, and Grid layouts.
https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap
There is already wide support for this, with Safari (both MacOS and iOS) being the unfortunate exception: https://caniuse.com/mdn-css_properties_column-gap_flex_context
If you need support for those exceptions and possibly even Internet Explorer, you can go like this:
.date-of-birth > *:not(:last-child) {
margin-right: 5px;
}
.date-of-birth {
display: flex;
margin: 0 -5px;
}
.date-of-birth mat-form-field {
max-width: 33.33%;
flex: 0 0 33.33%;
padding: 0 5px;
}
I'd suggested this one.
Related
I'm trying to set up my form so that the text input is on one side, and two buttons take up the rest of the side, one on top and the other on the bottom.
I've tried to use br, but this has not done anything. I'm also doing this on Angular 7.0 if it matters.
HTML
<form class="form" (ngSubmit)="onSubmit()">
<input type="text" name="title" [(ngModel)]="title" placeholder="Add Todo">
<input type="submit" value="Submit" class="btn">
<input type="reset" value="Reset" class="btn">
</form>
CSS
.form {
display: flex;
}
.form input[type='text'] {
flex: 10;
padding: 5px;
height: 40px;
}
.form input[type='submit'] {
flex: 2;
height: 20px;
display: block;
}
.form input[type='reset'] {
flex: 2;
height: 20px;
display: block;
}
Currently, all three are side by side, like this. I want the two buttons to be on top of each other.
If you don't want to change your markup, you can use display: grid.
form {
display: grid;
grid-template-areas: 'form topbutton' 'form bottombutton'
}
input[type="text"] {
grid-area: form;
}
input[type="submit"] {
grid-area: topbutton;
}
input[type="reset"] {
grid-area: bottombutton;
}
<form class="form" (ngSubmit)="onSubmit()">
<input type="text" name="title" [(ngModel)]="title" placeholder="Add Todo">
<input type="submit" value="Submit" class="btn">
<input type="reset" value="Reset" class="btn">
</form>
A more solid solution may use flexbox:
div.row {
display: flex;
width: 100%;
/* you may add height if you need it */
/* height: 125px; */
}
div.row > * {
flex: 1 1 80%;
}
div.row > .buttons {
flex: 1 1 20%;
display: flex;
flex-direction: column;
}
div.row > .buttons > * {
flex: 1 1 auto;
}
<form class="form" (ngSubmit)="onSubmit()">
<div class="row">
<input type="text" name="title" [(ngModel)]="title" placeholder="Add Todo">
<div class="buttons">
<input type="submit" value="Submit" class="btn">
<input type="reset" value="Reset" class="btn">
</div>
</div>
</form>
Add display: block to your input elements to have them occupy a new line.
You can also divide the elements into two columns by wrapping the desired elements in <div> classes (in this case of .left and .right), and floating them both to the left.
This can be seen in the following:
.left, .right {
width: 50%;
float: left;
}
.right input {
display: block;
}
<form class="form" (ngSubmit)="onSubmit()">
<div class="left">
<input type="text" name="title" [(ngModel)]="title" placeholder="Add Todo">
</div>
<div class="right">
<input type="submit" value="Submit" class="btn">
<input type="reset" value="Reset" class="btn">
</div>
</form>
I've never used Angular, but I usually use two br afterward with html and it works fine, alternatively you could use css to manually change the location of each element with style and margin (top,left,etc.). ie: like so:
br example:
<form class="form" (ngSubmit)="onSubmit()">
<input type="text" name="title" [(ngModel)]="title" placeholder="Add Todo"><br><br>
<input type="submit" value="Submit" class="btn"><br><br>
<input type="reset" value="Reset" class="btn"><br><br>
</form>
css example:
<form class="form" (ngSubmit)="onSubmit()">
<input style=margin-top:90px type="text" name="title" [(ngModel)]="title" placeholder="Add Todo"><br><br>
<input style=margin-top:100px type="submit" value="Submit" class="btn"><br><br>
<input style=margin-top:110px type="reset" value="Reset" class="btn"><br><br>
</form>
I use angular 6 and angular material, but nothing works. I'd like to put my login card at middle of page
html:
<mat-card class="card" layout-align="center center">
<form (ngSubmit)="onSubmit(UserLogin)">
<div class="container">
<mat-form-field>
<input matInput placeholder="E-mail" [formControl]="email" required>
<mat-error *ngIf="email.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput placeholder="Senha" [type]="hide ? 'password' : 'text'" [formControl]="password">
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' : 'visibility_off'}}</mat-icon>
</mat-form-field>
<div>
<button mat-raised-button color="accent" type="submit">Entrar</button>
</div>
</div>
</form>
</mat-card>
add this in css file of component
:host{
display: flex;
min-height: 100vh;
}
mat-card{
margin: auto;
}
You could use position:absolute with left and top positioning and transform should work
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
position: relative;
height: 100vh;
}
form {
border: 1px solid;
display: inline-block;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
<form>
<div><input type="text" placeholder="First Name" name=""></div>
<div><input type="text" placeholder="Last Name" name=""></div>
<div><input type="text" placeholder="Email" name=""></div>
</form>
I have referenced this article on flexbox referred to me earlier today. https://css-tricks.com/snippets/css/a-guide-to-flexbox/
So far, flex has behaved as documented, except for in the following case where the box wraps its items and/or the items are inputs/selects. Here is an example:
#searchbar {
border: solid 1px LightGray;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
}
.searchbar-control {
display: inline !important;
margin-bottom: 3px;
width: 40%;
}
<div id="searchbar">
<input type="text" class="searchbar-control" value="1">
<input type="text" class="searchbar-control" value="2">
<input type="text" class="searchbar-control" value="3">
<input type="text" class="searchbar-control" value="4">
<input type="text" class="searchbar-control" value="5">
<input type="text" class="searchbar-control" value="6">
<input type="text" class="searchbar-control" value="7">
<input type="text" class="searchbar-control" value="8">
</div>
I expect the box to contain 2 controls per row. 1 & 2 on the first row, 3 & 4 on the second row, etc. So far, so good. However, I am finding that justify-content is ignored no matter what value is used. In addition, flex-wrap (nowrap/wrap) are ignored. It looks like align-items is also ignored.
I haven't had this issue on flex boxes with elements which are not user inputs.
I want to see 2 items per row, and I want there to be 3 equal-width spaces: left/middle/right.
Widths for form elements (especially when mixing <input>s and <select>s) can be a bit fidgety. I've found that using the CSS style box-sizing: border-box can help standardize the widths:
#searchbar {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
border: solid 1px lightgray;
}
.searchbar-control {
box-sizing: border-box;
margin-bottom: 3px;
width: 40%;
}
<div id="searchbar">
<select class="searchbar-control">
<option value="1" selected></option>
<option value="2" selected></option>
</select>
<input type="text" class="searchbar-control" value="2">
<input type="text" class="searchbar-control" value="3">
<select class="searchbar-control">
<option value="3" selected></option>
<option value="4" selected></option>
</select>
<input type="text" class="searchbar-control" value="5">
<input type="text" class="searchbar-control" value="6">
<input type="text" class="searchbar-control" value="7">
<input type="text" class="searchbar-control" value="8">
</div>
How to put form labels inside a input form only on mobile?
example:
my code:
<form>
<div class="form-group">
<label class="form-control-label" for="firstName">Firstname</label>
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="Firstname" required />
</div>
<div class="form-group">
<label class="form-control-label" for="lastName">Lastname</label>
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Lastname" required />
</div>
<div class="form-group">
<label class="form-control-label" for="phone_number">Mobile Number</label>
<input type="text" class="form-control" id="phone_number" name="phone_number" placeholder="A valid 9 or 10 digit phone number" maxlength="10"required pattern="0[689]?[0-9]{8}" />
</div>
</form>
Something like this? I changed the position of input and label in the HTML
.form-group {
position: relative;
min-height: 3.5em;
}
input.form-control {
height: 3em;
position: absolute;
top: 0;
left: 0;
}
label.form-control-label {
position: absolute;
font-size: .8em;
top: 0;
left: 5px;
text-transform: uppercase;
}
<form>
<div class="form-group">
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="Firstname" required />
<label class="form-control-label" for="firstName">Firstname</label>
</div>
<div class="form-group">
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Lastname" required />
<label class="form-control-label" for="lastName">Lastname</label>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone_number" name="phone_number" placeholder="A valid 9 or 10 digit phone number" maxlength="10" required pattern="0[689]?[0-9]{8}" />
<label class="form-control-label" for="phone_number">Mobile Number</label>
</div>
</form>
You can use like the below method. We have to write mobile screen style with in the media queries
body{
font-family:arial;
}
.form-group{
position:relative;
}
.input-element{
padding:30px 5px 5px 5px;
width:100%;
border:1px solid #CCC;
}
#media screen and (max-width: 767px) {
.label-element{
position:absolute;
top:5px;
left:5px;
font-size:12px;
color:#666;
}
}
<div class="form-group"><label class="label-element">First Name</label>
<input type="text" class="input-element"/></div>
I've just added a border to form-group and overwritten some bootstrap code.
For mobile only, try to use CSS3 Media Queries.
You could try this:
.form-group {
border: 1px solid black; /* Change border width and color here */
}
/* the !important declaration is for this snippet, because this snippet include bootstrap after this declarations **/
.form-control {
border: none !important; /* No border for input */
box-shadow: none !important; /* No border for input */
}
.form-control:focus {
box-shadow: none !important; /* Remove the blue shining at focus */
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<form>
<div class="form-group">
<label class="form-control-label" for="firstName">Firstname</label>
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="Firstname" required />
</div>
<div class="form-group">
<label class="form-control-label" for="lastName">Lastname</label>
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Lastname" required />
</div>
<div class="form-group">
<label class="form-control-label" for="phone_number">Mobile Number</label>
<input type="text" class="form-control" id="phone_number" name="phone_number" placeholder="A valid 9 or 10 digit phone number" maxlength="10" required pattern="0[689]?[0-9]{8}" />
</div>
</form>
Inspired by another answer, here is a short way to do it:
.input-group {
position: relative;
}
.input-label {
position: absolute;
top: 5px;
left: 5px;
color: #666;
}
.input-element {
padding: 30px 5px 5px 5px;
}
<div class="input-group">
<label class="input-label">First name</label>
<input type="text" class="input-element" />
</div>
I have the following form. How do I create this look without <table>. Thanks
<table>
<tr><td><label for="firstname">First Name</label></td><td><input type="text" name="firstname" id="firstname" /></td></tr>
<tr><td><label for="lastname">Last Name</label></td><td><input type="text" name="lastname" id="firstname" /></td></tr>
<tr><td><label for="phone">Phone</label></td><td><input type="text" name="phone" id="phone" /></td></tr>
<tr><td><label for="email">Email</label></td><td><input type="text" name="email" id="email" /></td></tr>
<tr><td><label for="address">address</label></td><td><input type="text" name="address" id="address" /></td></tr>
<tr><td><label for="city">City</label></td><td><input type="text" name="city" id="city" /></td></tr>
<tr><td><label for="State">state</label></td><td><input type="text" name="state" id="state" /></td></tr>
</table>
This enough for basic styling:
input {
display: block;
}
label {
width: 100px; /* whatever value you wish */
float: left;
}
You can see how this works at http://dabblet.com/gist/2794359
.label {width:30px;} certainly won't do it. First of all, because when you write .label, that selects elements having a class called label. Secondly, even if you didn't use the dot, the label element is by default an inline element, so setting a width on it is useless if you don't give it a display: block as well (floating it also does the trick).
You can use this styles:
CSS markup:
.divContainer
{
display: table;
}
.divRow
{
display: table-row;
}
.divColumn
{
display: table-cell;
}
HTML markup:
<div class="divContainer">
<div class="divRow">
<div class="divColumn">
<label for="firstname">First Name</label>
</div>
<div class="divColumn">
<label for="lastname">Last Name</label>
</div>
</div>
</div>
Sample HTML:
<div id="container">
<div class="row">
<div>
<label for="firstname">First Name</label>
</div>
<div>
<input type="text" name="firstname" id="firstname"/>
</div>
</div>
<div class="row">
<div>
<label for="lastname">Last Name</label>
</div>
<div>
<input type="text" name="lastname" id="firstname"/>
</div>
</div>
<div/>
CSS:
.row {
width: 100%;
}
.row > div:first-child {
width: 20%;
float: left;
}
.row > div:last-child {
float: left;
width: 80%;
}
jsFiddle: http://jsfiddle.net/Q4g2u/1/
Apart from religious-like issues, there is no reason to format tabular data such as a form without using table markup. But if you must, the technique described in the answer of Luis Sánchez comes closest – but it is just simulating tables in CSS, with more limited browser support.