Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centered form with material design lite

I am looking for an easy to get a login form centered on the screen using Google's Material Design Lite library.

I've been through a number of iterations and this is the best I've come up with:

<div class="mdl-cell--12-col mdl-grid">
    <div class="mdl-cell mdl-cell--4-col mdl-cell--2-col-tablet">&nbsp;</div>

    <div class="mdl-grid mdl-cell--4-col">

        <div class="mdl-textfield mdl-js-textfield mdl-cell-12-col">
            <input class="mdl-textfield__input" type="text" id="username" />
            <label class="mdl-textfield__label" for="username">Username</label>
        </div>

        <div class="mdl-textfield mdl-js-textfield mdl-cell-12-col">
            <input class="mdl-textfield__input" type="password" id="password" />
            <label class="mdl-textfield__label" for="password">Password</label>
        </div>
    </div>

    <div class="mdl-cell mdl-cell--4-col mdl-cell--2-col-tablet">&nbsp;</div>
</div>

Is there a better way of achieving a centered form on all screen sizes?

The divs with &nbsp; feel really yucky!

like image 226
Clarkie Avatar asked Jul 16 '15 20:07

Clarkie


5 Answers

How about using "mdl-layout-spacer": See codepen

<div class="mdl-grid">
    <div class="mdl-layout-spacer"></div>
    <div class="mdl-cell mdl-cell--4-col">This div is centered</div>
    <div class="mdl-layout-spacer"></div>
</div>

Or if you prefer a css solution: Add an extra class to the grid containing the column to be centered. See codepen

<div class="mdl-grid center-items">
    <div class="mdl-cell mdl-cell--4-col">This div is centered</div>
</div>

.mdl-grid.center-items {
  justify-content: center;
}

Both options play nice when resizing.

like image 182
passerby Avatar answered Oct 02 '22 23:10

passerby


You can use 'mdl-typography--text-center' :

<div class="mdl-grid">
  <div class="mdl-cell mdl-cell--4-col mdl-typography--text-center">
    My center div
   </div>
</div>
like image 21
Maxxx Avatar answered Oct 03 '22 00:10

Maxxx


Why not using mdl-cell--N-offset class? As pointed on the Components -> Layout -> Grid -> Configuration options in https://getmdl.io/components/index.html#layout-section/grid:

mdl-cell--N-offset

Adds N columns of whitespace before the cell

<div class="mdl-grid">
  <div class="mdl-cell mdl-cell--4-col mdl-cell--4-offset">
    <p>Centered content!</p>
  </div>
</div>

This should work.

like image 26
Pedro Adame Vergara Avatar answered Oct 03 '22 00:10

Pedro Adame Vergara


Just add these CSS rules:

.mdl-layout {
  align-items: center;
  justify-content: center;
}

and then put your content in this container:

<div class="mdl-layout">
  <!-- Your Content -->
</div>
like image 33
Paweł Hajduk Avatar answered Oct 03 '22 00:10

Paweł Hajduk


I'm not familiar with Google's Material Design Lite library, but why not just make a "container" element wrapping the columns? Example:

CSS:

.customContainer {
    max-width: 960px;
    margin: 0 auto;
}

HTML:

<div class="customContainer">
    ...
</div>

Then from there you can put the full width column as desired.

like image 34
Josh Salazar Avatar answered Oct 03 '22 01:10

Josh Salazar