【发布时间】:2018-01-03 22:32:48
【问题描述】:
当我想在另一个 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 的 sn-p
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ón 1">
Descripción 1
</option>
<option value="Descripción 2">
Descripció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>
【问题讨论】:
-
我可以在一个条件下提供帮助:您向我展示结果,而不是来源。用minimal reproducible example 中的实际HTML 和CSS 替换您的sn-p,这样它就会重现问题,我会告诉您原因是什么。 (从页面源复制/粘贴标记。同时添加任何 CSS 资源。尝试添加尽可能少的代码,但仍会重现问题)。
-
您好 Andrei,感谢您的快速答复。我认为您希望我发布渲染的 HTML 和 CSS 的 sn-p,以便您实时查看它的外观。这样做的问题是我只能发布渲染的 HTML,但它不会看起来像我的结尾,因为我无法发布 bootstrap.css,因为 30000 个字符的发布限制。即使外观与我最终的外观不匹配,也可以在没有引导 CSS 的情况下发布呈现的 html??
-
您可以将任何外部库链接为资源。确保它们的顺序正确并且它们是从
cdn's(超过https)加载的。即你想谷歌{name of library} + "cdn"。在 Bootstrap 的例子中,你想复制/粘贴 this 到你的 sn-p 顶部,就在 jQuery<script>标记之后。 -
好的,我做到了。请注意,对于外部 bootstrap.css,我只找到了缩小版。那么,我如何解决这个问题以使表单在页面中居中?谢谢!!!
标签: html css asp.net-mvc twitter-bootstrap twitter-bootstrap-3