gpt4 book ai didi

html - 在 MVC 中水平居中此 Bootstrap 表单的正确方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:25 25 4
gpt4 key购买 nike

当我想将一个 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 正文标记之间单独呈现。

有人能帮忙吗?

编辑

应一位成员的要求,这是呈现的 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>

最佳答案

将您想要居中的内容包裹在 .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>

关于html - 在 MVC 中水平居中此 Bootstrap 表单的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45363600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com