Centralize input within a div using materialize

Asked

Viewed 1,783 times

1

I have a form with a input inside, I’d like to leave it centered. I can do it on custom css. that I have. But, as I am using Materoalize, I would like to know if it has how to do it through it.

<form class="col s12">
  <div class="row">
    <div class="input-field col s12">
      <input placeholder="Digite seu nome" id="name" type="text">
      <label for="name" class="active">Nome</label>
    </div>
    <div class="input-field col s12">
      <input placeholder="Digite seu telefone" id="phone" type="tel">
      <label for="phone" class="active">Telefone</label>
    </div>
    <div class="input-field col s12">
      <input placeholder="Digite seu e-mail" id="email" type="email">
      <label for="email" class="active">E-mail</label>
    </div>

  </div>
</form>

  • You want to align the form as a whole or the texts within the form?

2 answers

3

Yes Materialize has a special class to align texts to the center, the class calls center-align here is the documentation https://materializecss.com/helpers.html

Despite this only with him you will not be able to line up everything. We inputs I used the center-align and worked well, already for the label I had to customize some classes.

Already to align the whole form in the middle of the screen the grid has to be aligned as @Leandro said. 3 - 6 - 3 or 5 - 2 - 5 if you want narrower, or in and equal parts as I did col s4 offset-s4

See how the result turned out.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    
.input-field label:not(.label-icon).active {
    -webkit-transform-origin: center;
    transform-origin: center;
}
.input-field.col label {
    left: 0;
}
.input-field label {
    text-align: center;
    width: 100%;
}
 
</style>
</head>
<body>
    
<form class="col s12">
    <div class="row">
        <div class="input-field col s4 offset-s4">
        <input class="center-align" placeholder="Digite seu nome" id="name" type="text">
        <label for="name" class="active">Nome</label>
        </div>
        <div class="input-field col s4 offset-s4">
        <input class="center-align" placeholder="Digite seu telefone" id="phone" type="tel">
        <label for="phone" class="active">Telefone</label>
        </div>
        <div class="input-field col s4 offset-s4">
        <input class="center-align" placeholder="Digite seu e-mail" id="email" type="email">
        <label for="email" class="active">E-mail</label>
        </div>
    </div>
</form>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
</body>
</html>

1


Try putting this in your html: form class="col S6 offset-S3"

Note: Sent by cell phone

  • It worked, I just didn’t understand the calculation of grids. In my case it became input-field col S9 offset-S1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.