How to center form elements with materialize classes - css

I have a form like this:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<div class="container">
<div class="row">
<form class="col s12" onSubmit={formSubmit}>
<h2>Login</h2>
<div class="row">
<div class="input-field col s10">
<label for="username">Username</label>
<input name="username" type="text" />
</div>
</div>
<div class="row">
<div class="input-field col s10">
<input name="password" type="password" />
<label for="password">Password</label>
</div>
</div>
<div class="row">
<div class="input-field col s10">
<input type="submit" value="submit" class="btn waves-effect waves-light" />
</div>
</div>
</form>
<p>Don't have an account? <strong>Register</strong></p>
</div>
</div>
I want to horizontally center the form input fields within the .container div and left align the h3 and submit button with those input fields but can't figure out how to do it using Materialize helpers and built-in grid classes. I've tried .center-align but as per the docs its only for text. I'm missing something in how the grid system works. Thanks in advance!

While using Materialize you can use the grid utility of push/pull to center your col.
Explanation:
If you have col s8, then the remaining columns for this row is 4 so to center it there should be col s2 before col s8.
Now, push-s2 will acts as an empty col s2 before col s8 so it will be centered.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<div class="container">
<div class="row">
<form class="col s8 push-s2" onSubmit={formSubmit}>
<h2>Login</h2>
<div class="row">
<div class="input-field col s12">
<label for="username">Username</label>
<input name="username" type="text" />
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="password" type="password" />
<label for="password">Password</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input type="submit" value="submit" class="btn waves-effect waves-light" />
</div>
</div>
<div class="row">
<div class="col s12">
<p>Don't have an account? <strong>Register</strong></p>
</div>
</div>
</form>
</div>
</div>
Disadvantages of this way
You can't do this way with an odd col number (ie: col s7) because then the remaining columns for this row is 5 and there is no col s2.5 neither push-s2.5.
My Opinion
Remove <div class='row'> from above form and remove class='col s12' from inside the form, instead you can use max-width with margin: 0 auto to center the form then keep everything else as is, see below:
.main-form {
max-width: 700px;
margin: 0 auto;
}
#media (max-width: 768px) {
.main-form {
max-width: 100%;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<div class="container">
<form class="main-form" onSubmit={formSubmit}>
<h2>Login</h2>
<div class="row">
<div class="input-field col s12">
<label for="username">Username</label>
<input name="username" type="text" />
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="password" type="password" />
<label for="password">Password</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input type="submit" value="submit" class="btn waves-effect waves-light" />
</div>
</div>
<div class="row">
<div class="col s12">
<p>Don't have an account? <strong>Register</strong></p>
</div>
</div>
</form>
</div>

Related

two vertical inputs share a common button

I have a form, in that n number of form fields available. only For 2 inputs, I want to share a common button without affecting remaining layout, but it's not aligned properly as common.
Here is what I tried. First two input fields should have common button
<div class="container">
<div class="row">
<div class="col">
<div class="form-group pt-4">
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
<div class="col">
<button type="submit" class="btn btn-secondary mt-4"><i class="fas fa-ellipsis-v"></i></button>
</div>
</div>
I assume that you want to center the button for two input elements. Is that so you can try display:table for the .row this might have fix your issue.
.row {
display: table;
width: 100%;
}
.row .col{
display: table-cell;
}
.col1 {
width: 40%;
}
.col2 {
position: relative;
vertical-align: top;
}
.col2 button {
position: absolute;
top: 30px;
left: 0;
}
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css" >
</head>
<body>
<div class="container">
<div class="row">
<div class="col col1">
<div class="form-group pt-4">
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
<div class="col col2">
<button type="submit" class="btn btn-secondary mt-4"><i class="fas fa-ellipsis-v"></i></button>
</div>
</div>
</div>
</body>
</html>
However if you want to increase the size for the button you can always add padding or set height for the button.
Codepen link
Pure bootstrap 4 impementation wll be the one below.
You have to group the two inputs into one single .col and the button to an another .col with flex-grow-0 flex-shrink-1 mb-3 d-flex to shrink the container to the minimum of button width and to have sufficient margin at bottom.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col">
<div class="row">
<div class="col">
<div class="form-group pt-4">
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
<div class="col flex-grow-0 flex-shrink-1 mb-3 d-flex">
<button type="submit" class="btn btn-secondary mt-4"><i class="fas fa-ellipsis-v"></i></button>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
</div>
</div>
col-auto in bootrap 4.0.0 does not have padding. And there may be some other bugs too. You should better use the latest version of bootstrap 4. it is version 4.6.0
Use two .row. The first .row for the first two inputs and the button and the second .row for the rest of the inputs.
Use .col-auto and .d-flex for the column that contains the button to make it take as much space at it need and full height
Use .mb-3 for the button since the .form-group has that much margin-bottom.
You may use .btn-outline-secondary instead of .btn-secondary
<div class="col-auto d-flex ">
<button type="submit" class="btn btn-secondary mt-4 mb-3"><i class="fas fa-ellipsis-v"></i></button>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col">
<div class="form-group pt-4">
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
<div class="col-auto d-flex">
<button type="submit" class="btn btn-outline-secondary mt-4 mb-3"><i class="fas fa-ellipsis-v"></i></button>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
</div>
🎁
Doing so you avoid multiple nesting of .row and .col.
here is another approach, also rethinking the structure with only BS class for the styling
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col ">
<div class="form-group pt-4">
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<div class="form-group">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>
<button type="submit" class="btn btn-secondary mt-4 mb-3"><i class="fas fa-ellipsis-v"></i></button>
</div>
<div class="form-group row pl-3">
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
</div>
</div>

How to center text inputs using Materialize CSS?

I am trying to center an input text using Materialize CSS but the "center" and "center-align" classes seem to have no effect. I cannot figure out what am I missing here
<div class="section">
<div class="row center-align">
<div class="input-field col s3 center-align">
<input id="email" type="email" class="validate">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s3 center-align">
<input id="password" type="password" class="validate">
<label for="password">Password</label>
</div>
</div>
</div>
.center-align only applies text-align: center; which has no effect on floating elements.
Use their grid classes: col s4 offset-s4:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<div class="section">
<div class="row">
<div class="input-field col offset-s4 s4">
<input id="email" type="email" class="validate">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col offset-s4 s4">
<input id="password" type="password" class="validate">
<label for="password">Password</label>
</div>
</div>
</div>
To make it responsive, you can play with different offsets for different widths:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<div class="section">
<div class="row">
<div class="input-field col offset-l5 l2 offset-m4 m4 offset-s3 s6">
<input id="email" type="email" class="validate">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col offset-l5 l2 offset-m4 m4 offset-s3 s6">
<input id="password" type="password" class="validate">
<label for="password">Password</label>
</div>
</div>
</div>

Bootstrap Align inputs and label for different columns size

I have a big form and the problem with alignment. I cut some fields, but the demo will show the problem.
I have two columns in the begining of the form, some rows use half of the width, some rows use full witdh. And, the problem with the last one. As you can see textareas is not aligned with the others inputs:
How can I fix it?
DEMO
PS. I use Bootstrap 4, but, I think this question is related to Bootsrap 3 too.
.full-width {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://npmcdn.com/tether#1.2.4/dist/js/tether.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<form novalidate>
<div class="row">
<div class="col-md-8">
<div class="form-group row">
<label for="field1" class="col-md-2 col-form-label">Text Area</label>
<div class="col-md-10">
<textarea id="field1" class="form-control"
cols="30" rows="4"></textarea>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group row">
<div class="col-md-12">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox"> Checkbox 1
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox"> Checkbox 2
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox"> Checkbox 3
</label>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group row">
<label for="field2" class="col-md-2 col-form-label">Input</label>
<div class="col-md-10">
<input id="field2" class="form-control"/>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group row">
<label class="col-md-1 col-form-label">Options</label>
<div class="col-md-11">
<div class="row">
<div class="col-md-6">
<span>Label 1</span>
<input type="radio" name="group1" value="Value 1"> Value 1
<input type="radio" name="group1" value="Value 1"> Value 2
</div>
<div class="col-md-6">
<span>Label 2</span>
<input type="radio" name="group2" value="Value 1"> Value 1
<input type="radio" name="group2" value="Value 2"> Value 2
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-11">
<textarea class="full-width"></textarea>
</div>
<div class="col-md-1">
<span">Icon</span>
</div>
</div>
<div class="row">
<div class="col-md-11">
<textarea class="full-width"></textarea>
</div>
<div class="col-md-1">
<span">Icon</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
</form>
The problem is the first 2 rows are nested in col-md-8, and the misaligned "options" row is nested in col-md-12.
Also, clean up the various malformed HTML in the code.
http://www.codeply.com/go/sCYzgdFXxa

How to set bootstrap form with multi controls aligned at same row (and responsive)

I'd like to draw a table using bootstrap css and looks like this:
After reading many tutorials on internet, I didn't find any examples directly support multi controls aligned as the pictrue above (either using .form-horizontal or .form-inline).
Can anyone show me an example please ?
You can also use grid system without adding any customized styles:
<form>
<div class="row">
<div class="col-md-1 col-sm-1 form-group">
<label>Name</label>
</div>
<div class="col-md-5 col-sm-11 form-group">
<input type="text" class="form-control" />
</div>
<div class="col-md-1 col-sm-1 form-group">
<label>Age</label>
</div>
<div class="col-md-5 col-sm-11 form-group">
<input type="text" class="form-control" />
</div>
</div>
<div class="row">
<div class="col-md-1 col-sm-1 form-group">
<label>Sex</label>
</div>
<div class="col-md-5 col-sm-11 form-group">
<input type="text" class="form-control" />
</div>
<div class="col-md-1 col-sm-1 form-group">
<label>Job</label>
</div>
<div class="col-md-5 col-sm-11 form-group">
<input type="text" class="form-control" />
</div>
</div>
</form>
Results:
large
small
Smallest
#ineztia: try this source code and let me know.
.form-row > .control-label {
margin-top: 5px;
}
.form-row > .input-text {
margin-bottom: 5px;
}
#media screen (min-width: 768px) {
.form-row > .control-label {
margin-top: 0;
}
.form-row > .input-text {
margin-bottom: 0;
}
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-sm-12">
<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- Text input-->
<div class="form-group">
<div class="form-row">
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-3 control-label">Name</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-9 input-text">
<input type="text" placeholder="Name..." class="form-control input-md">
</div>
</div>
<div class="form-row">
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-3 control-label">Age</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-9 input-text">
<input type="text" placeholder="Age..." class="form-control input-md">
</div>
</div>
</div>
<!--/form-group-->
<!-- Text input-->
<div class="form-group">
<div class="form-row">
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-3 control-label">Sex</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-9 input-text">
<input type="text" placeholder="Sex..." class="form-control input-md">
</div>
</div>
<div class="form-row">
<label class="col-lg-2 col-md-2 col-sm-2 col-xs-3 control-label">Job</label>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-9 input-text">
<input type="text" placeholder="Job..." class="form-control input-md">
</div>
</div>
</div>
<!--/form-group-->
<!-- Button (Double) -->
<div class="form-group text-right">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button name="btn-ok" class="btn btn-cancel btn-success">OK</button>
<button name="btn-cancel" class="btn btn-cancel btn-danger">Cancel</button>
</div>
</div>
<!--/form-group-->
</fieldset>
<!--/fieldset-->
</form>
<!--/form-->
</div>
</div>
</div>

How do I center everything inside 2 bootstrap columns? (col-md-6 and col-md-6)?

Basically I have something like this
<div class="container">
<div class="row">
<div class="col-md-6>
<form class="form-group-sm">
<input />
<button type="submit"></button>
</form>
</div>
</div>
<div class="col-md-6>
<form class="form-group-sm">
<input />
<button type="submit"></button>
</form>
</div>>
</div>
2 columns side by side, I'm trying to figure out a way to center the forms, input, and button inside each column but cant seem too figure out how.
<form method="POST" action="./createProduct" class="form-group-sm" >
<div class="form-group">
<label for="t" class="col-md-8 control-label">Product Type</label>
<div class="col-md-8">
<input name="productType" id="t"/>
</div>
</div>
<div class="form-group">
<label for="sn" class="col-md-8 control-label">Material Cost PerSqFoot</label>
<div class="col-md-8">
<input name="matCostSqf" id="sn"/>
</div>
</div>
<div class="form-group">
<label for="snum" class="col-md-8 control-label">Labor Cost PerSqFoot</label>
<div class="col-md-8">
<input name="laborCostSqf" id="snum" />
</div>
</div>
<br />
<div class="col-md-8">
<br />
<button class=" btn btn-danger btn-lg center-block" type="submit">Submit</button>
</div>
</form>
Twitter Bootstrap has a default class for that called text-center.
Just add the text-center class to your col- divs like this:
<div class="col-md-6 text-center">
-- your content --
</div>
P.S. You forgot to properly close the column divs. Do make sure you close them always
Your markup contains some errors like an extra closing div and a missing double cotes for the class attribute : class="col-md-6>
here is the solution you can use the bootstrap class "text-center"
<div class="container">
<div class="row">
<div class="col-md-6 text-center">
<form class="form-group-sm">
<input />
<button type="submit">Submit</button>
</form>
</div>
<div class="col-md-6 text-center">
<form class="form-group-sm">
<input />
<button type="submit">Submit</button>
</form>
</div>
</div>
here is a pen

Resources