简体   繁体   English

ASP.NET MVC:在同一行上对齐复选框和标签

[英]ASP.NET MVC: Align checkbox and label on same line

The label ("Acid-stable amino acids") and checkbox are on different lines in my ASP.NET Core MVC application. 标签(“酸稳定的氨基酸”)和复选框在我的ASP.NET Core MVC应用程序中的不同行上。 (Sample ID is irrelevant here.) I would like them to be on the same line. (此处与样品ID无关。)我希望它们位于同一行。 Current misaligned output: 电流未对准输出:

复选框未对齐

The field from the model: 来自模型的字段:

[Display(Name = "Acid-stable amino acids")]
        public bool AcidStables { get; set; }

The Razor view (removing most other irrelevant elements): 剃刀视图(删除了大多数其他不相关的元素):

<div class="row">
    <div class="col-md-6">
        <form asp-action="Create">

            <div class="form-group">
                <label asp-for="ClientSampleID" class="control-label"></label>
                <input asp-for="ClientSampleID" class="form-control" />
                <span asp-validation-for="ClientSampleID" class="text-danger"></span>
            </div>

            <div class="checkbox">
                <label asp-for="AcidStables" class="control-label"></label>
                <input asp-for="AcidStables" class="form-control"/>
                <span asp-validation-for="AcidStables" class="text-danger"></span>
            </div>
    </div>
</div> 

I've tried a few things recommended in similar questions. 我已经尝试了类似问题中推荐的一些方法。 Using inline-block display right before the label: 在标签前使用内联块显示:

<div class="checkbox">
                <style type="text/css">
                    label {
                        display: inline-block;
                    }

                </style>

Or modifying the label or checkbox types in the site.css file: 或修改site.css文件中的标签或复选框类型:

.label {
    display: inline;
    font-size: 1.2em;
    font-weight: 600;
    padding: 5px;
}

input[type=checkbox] {
    float: left;
    margin-right: 0.4em;
}

or 要么

​input[type=checkbox] + label {
    display: inline-block;
    margin-left: 0.5em;
    margin-right: 2em;
    line-height: 1em;
}

I've tried changing to "form-horizontal". 我尝试过更改为“水平形式”。

Nothing changes the alignment of checkbox and label. 没有任何改变复选框和标签的对齐方式。 Any input would be appreciated. 任何输入将不胜感激。 Floating the checkbox may be a solution but I'm not sure how to do that. 浮动复选框可能是一种解决方案,但是我不确定如何做到这一点。 Relevant SO questions: here , here , here . 相关的问题: 这里这里这里

edit: rendered HTML-- 编辑:呈现的HTML-- 渲染的HTML

you can apply css like 你可以应用css一样

.checkbox .control-label{
   float:left;
   width:40%;
}
.checkbox input[type="checkbox"]{
   float:left;
   width:60%;
}
.checkbox:after{
   content:'';
   display:block;
   clear:both;
}

From Bootstrap v4.0 and higher you can use: 在Bootstrap v4.0及更高版本中,您可以使用:

<div class="form-group form-check">
    <label asp-for="AcidStables" class="form-check-label"></label>
    <input asp-for="AcidStables" class="form-check-input"/>
    <span asp-validation-for="AcidStables" class="text-danger"></span>
</div>

This is a cleaner solution if you're already using Bootstrap, because you don't need to write additional CSS. 如果您已经在使用Bootstrap,则这是一种更清洁的解决方案,因为您无需编写其他CSS。

Note: The 'form-group' class is optional, but it gives nicer spacing if used in a form. 注意:“ form-group”类是可选的,但如果在表单中使用,则可以提供更好的间距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM