在MVC中水平居中这个Bootstrap表单的正确方法是什么?

时间:2017-07-28 01:48:21

标签: html css asp.net-mvc twitter-bootstrap twitter-bootstrap-3

当我想将div放在另一个div中时,我通常会重新阅读this post。它通常有效。但现在它没有,而且距离我还有一百万年的时间才能成为CSS的专家。

以下是我的表单构建方式:

<h2 class="no-margin-top">Revisiones Administrativas</h2>

  @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.TipoVisitante, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="TipoVisitante" id="TipoVisitante" class="form-control">
                        @foreach (var item in Model.TiposVisitante)
                        {

                            <option value="@item.Descripcion">
                                @item.Descripcion
                            </option>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.TipoVisitante, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.MotivoVisita, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="MotivoVisita" id="MotivoVisita" class="form-control">
                        @foreach (var item in Model.MotivosVisita)
                        {

                            <option value="@item.MotivoVisita">
                                @item.MotivoVisita
                            </option>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.TipoVisitante, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Hallazgo, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="Hallazgo" id="Hallazgo" class="form-control">
                        @foreach (var item in Model.Hallazgos)
                        {

                            <option value="@item.Hallazgo">
                                @item.Hallazgo
                            </option>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.TipoVisitante, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.FechaVisita, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.FechaVisita, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FechaVisita, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Visitante, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Visitante, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Visitante, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.DireccionFisica, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <textarea class="form-control width-100" id="DireccionFisica" name="DireccionFisica"></textarea>
                    @Html.ValidationMessageFor(model => model.DireccionFisica, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.UsoNoAutorizadoDesde, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.UsoNoAutorizadoDesde, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UsoNoAutorizadoDesde, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.UsoNoAutorizadoHasta, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.UsoNoAutorizadoHasta, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UsoNoAutorizadoHasta, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.DirectorAuxiliar, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.DirectorAuxiliar, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.DirectorAuxiliar, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.FechaRevisionCargos, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.FechaRevisionCargos, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FechaRevisionCargos, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Cliente, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Cliente, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Cliente, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.RepresentanteRevision, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.RepresentanteRevision, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.RepresentanteRevision, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

注意:我的布局页面没有任何其他div。我删除了它们。这段代码在HTML body标签之间单独呈现。

有人可以帮忙吗?

修改

根据一位成员的要求,这里是呈现HTML的较短片段

body {
    padding-top: 50px;
    padding-bottom: 20px;
}

/* Set padding to keep content from hitting the edges */
.body-content {
    padding-left: 15px;
    padding-right: 15px;
}

/* Override the default bootstrap behavior where horizontal description lists 
   will truncate terms that are too long to fit in the left column 
*/
.dl-horizontal dt {
    white-space: normal;
}

/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
    max-width: 280px;
}

.index-parent-div{
    position: absolute;
    top: 50%;
    margin-top: -200px; /* half of #content height*/
    left: 0;
    width: 100%;
}

.index-inner-div {
    width: 50%;
    margin-left: auto;
    margin-right: auto;
    height: 395px;
}

.width-100{
    width: 100%;
}

.no-margin-top{
    margin-top: 0px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<body>
        <h2 class="no-margin-top">Revisiones Administrativas</h2>

<form action="/" method="post"><input name="__RequestVerificationToken" type="hidden" value="uNfiMxTWxX4Cz4baOODnVnwAJRNdAtr9KxcPyrJgOneJyn-xgfjybke088Ya3r39SMxLnEApagu_BKZv2wbnVc3ifIPrDrTZeGrv_EBojas1" />            <div class="form-horizontal">
                <hr />
                
                <div class="form-group">
                    <label class="control-label col-md-2" for="TipoVisitante">Tipo de Visitante: </label>
                    <div class="col-md-10">
                        <select name="TipoVisitante" id="TipoVisitante" class="form-control">
                                <option value="Descripci&#243;n 1">
                                    Descripci&#243;n 1
                                </option>
                                <option value="Descripci&#243;n 2">
                                    Descripci&#243;n 2
                                </option>
                        </select>
                        <span class="field-validation-valid text-danger" data-valmsg-for="TipoVisitante" data-valmsg-replace="true"></span>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Create" class="btn btn-default" />
                    </div>
                </div>
            </div>
</form>

    <script src="/Scripts/jquery-1.10.2.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
    <script src="/Scripts/respond.js"></script>
    <script src="/Scripts/revision.js"></script>


</body>

1 个答案:

答案 0 :(得分:1)

将您想要居中的内容包裹在.text-center内(您也可以将其应用于.container)。此外,在这种情况下,您需要:

.text-center select {
  margin: 0 auto;
}

这就是全部。示例:

body {
  padding-top: 50px;
  padding-bottom: 20px;
}


/* Set padding to keep content from hitting the edges */

.body-content {
  padding-left: 15px;
  padding-right: 15px;
}


/* Override the default bootstrap behavior where horizontal description lists 
   will truncate terms that are too long to fit in the left column 
*/

.dl-horizontal dt {
  white-space: normal;
}


/* Set width on the form input elements since they're 100% wide by default */

input,
select,
textarea {
  max-width: 280px;
}

.index-parent-div {
  position: absolute;
  top: 50%;
  margin-top: -200px;
  /* half of #content height*/
  left: 0;
  width: 100%;
}

.index-inner-div {
  width: 50%;
  margin-left: auto;
  margin-right: auto;
  height: 395px;
}

.width-100 {
  width: 100%;
}

.no-margin-top {
  margin-top: 0px;
}

.text-center select {
  margin: 0 auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<div class="container text-center">
<h2 class="no-margin-top">Revisiones Administrativas</h2>
<form action="/" method="post"><input name="__RequestVerificationToken" type="hidden" value="uNfiMxTWxX4Cz4baOODnVnwAJRNdAtr9KxcPyrJgOneJyn-xgfjybke088Ya3r39SMxLnEApagu_BKZv2wbnVc3ifIPrDrTZeGrv_EBojas1" />
  <div class="form-horizontal">
    <hr />

    <div class="form-group">
      <label class="control-label col-md-2" for="TipoVisitante">Tipo de Visitante: </label>
      <div class="col-md-10">
        <select name="TipoVisitante" id="TipoVisitante" class="form-control">
        <option value="Descripci&#243;n 1">
                                    Descripci&#243;n 1
                                </option>
                                <option value="Descripci&#243;n 2">
                                    Descripci&#243;n 2
                                </option>
                        </select>
        <span class="field-validation-valid text-danger" data-valmsg-for="TipoVisitante" data-valmsg-replace="true"></span>
      </div>
    </div>

    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
      </div>
    </div>
  </div>
</form>
</div>

或者,如果您只想将<form>元素居中,那么在Bootstrap中执行此操作的最简单方法是使用其网格系统:

<div class="container">
  <div class="row">
    <div class="col-lg-6 col-lg-offset-3 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12 col-offset-xs-0">
       <form>form here...</form>
    </div>
  </div>
</div>

示例:

body {
  padding-top: 50px;
  padding-bottom: 20px;
}

.dl-horizontal dt {
  white-space: normal;
}

input,
select,
textarea {
  max-width: 280px;
}

.width-100 {
  width: 100%;
}

.no-margin-top {
  margin-top: 0px;
}

.text-center select {
  margin: 0 auto;
}
@media (min-width: 768px) {
form .form-horizontal .control-label {
    text-align: center;
    background-color: #f5f5f5;
    line-height: 32px;
    height: 34px;
    padding: 0 15px;
    border-radius: 4px;
    border: 1px solid #eee;
    width: 100%;
  }
}
input[type="submit"] {
  width: 280px;
  max-width: 280px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<div class="container">
  <div class="row text-center">
    <div class="col-lg-6 col-lg-offset-3 col-md-8 col-md-offset-2 col-sm-offset-1 col-offset-xs-0 col-xs-12 col-sm-10">
      <h2 class="no-margin-top">Revisiones Administrativas</h2>
      <form action="/" method="post"><input name="__RequestVerificationToken" type="hidden" value="uNfiMxTWxX4Cz4baOODnVnwAJRNdAtr9KxcPyrJgOneJyn-xgfjybke088Ya3r39SMxLnEApagu_BKZv2wbnVc3ifIPrDrTZeGrv_EBojas1" />
        <div class="form-horizontal">
          <hr />

          <div class="form-group">
            <div class="col-sm-6 text-center">
              <label class="control-label" for="TipoVisitante">Tipo de Visitante: </label>
            </div>
            <div class="col-sm-6">
              <select name="TipoVisitante" id="TipoVisitante" class="form-control">
        <option value="Descripci&#243;n 1">
                                    Descripci&#243;n 1
                                </option>
                                <option value="Descripci&#243;n 2">
                                    Descripci&#243;n 2
                                </option>
                        </select>
              <span class="field-validation-valid text-danger" data-valmsg-for="TipoVisitante" data-valmsg-replace="true"></span>
            </div>
          </div>

          <div class="form-group ">
            <div class="col-xs-12 text-center">
              <input type="submit" value="Create" class="btn btn-default" />
            </div>

          </div>
        </div>
      </form>
    </div>
  </div>
</div>