c# - ASP.NET MVC : Align checkbox and label on same line

标签 c# html css asp.net-mvc razor

标签(“酸稳定氨基酸”)和复选框在我的 ASP.NET Core MVC 应用程序中位于不同的行。 (样本 ID 在这里无关紧要。)我希望他们在同一条线上。当前未对齐的输出:

misaligned checkbox

模型中的字段:

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

Razor View (删除大多数其他不相关的元素):

<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> 

我尝试了类似问题中推荐的一些方法。在标签之前使用内联 block 显示:

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

                </style>

或者修改 site.css 文件中的标签或复选框类型:

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

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

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

我试过更改为“form-horizo​​ntal”。

没有改变复选框和标签的对齐方式。任何输入将不胜感激。 float 复选框可能是一种解决方案,但我不确定该怎么做。相关问题:here , here , here .

编辑:呈现的 HTML-- rendered html

最佳答案

你可以像这样应用css

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

关于c# - ASP.NET MVC : Align checkbox and label on same line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55915175/

相关文章:

php - jQuery e.preventDefault() 不工作

javascript - 使用 CSS 打印页面的不同部分

javascript - 条件 CSS 不在 React Native 中呈现

c# - 实现一个MySQL命令进度条

c# - 如何在 C# 中初始化默认值?

c# - H264 -> 使用 .NET 的容器

html - 正则表达式根据 src 值有条件地选择所有图像 html 标签

C# System.Security.Cryptography - 为什么指定 key 和 IV 两次?

html - 具有 flex 布局的网站内容包装器

html - 图像悬停不起作用 - 代码有什么问题?