I want to use label as used "Static Control" label on that page. However, although I use the same Bootstrap version, I cannot aligned my form controls like that (I want the label is right float, data fields is left float -side by side - and there is a ":" sign between them. Howwver, the result is as shown on the image below. Could you please have a look at my code and inform what is wrong?
The code on that page:
<div class="form-group">
<label class="col-md-3 control-label">Static Control</label>
<div class="col-md-9">
<p class="form-control-static"> email#example.com </p>
</div>
</div>
The code I use on my Razor page:
<div class="form-group">
#Html.LabelFor(m => m.Name, new { #class = "col-md-3 control-label" }):
<div class="col-md-9">
#Html.DisplayFor(m => m.Name, new { #class = "form-control-static" })
</div>
</div>
<br />
<div class="form-group">
#Html.LabelFor(m => m.Surname, new { #class = "col-md-3 control-label" }):
<div class="col-md-9">
#Html.DisplayFor(m => m.Surname, new { #class = "form-control-static" })
</div>
</div>
You have two elements which equal 100% width plus the colon, so it pushes the value down a line. You'll need to just code the label instead of using an HtmlHelper to add the colon. Change it to this:
<div class="form-group">
<label class="col-md-3 control-label">#Model.Name:</label>
<div class="col-md-9">
#Html.DisplayFor(m => m.Name, new { #class = "form-control-static" })
</div>
</div>
Here is the final solution fixed the problem. Thanks a lot for all of your helps...
<style>
.col-sm-3 .control-label {
float: right !important;
margin-top: 0 !important;
}
/*Add colon ( : ) to the end of labels */
.col-sm-3 > label:after {
content: " :";
}
</style>
<div class="form-group">
<div class="col-sm-3">
#Html.LabelFor(m => m.Name, new { #class = "control-label" })
</div>
<div class="col-sm-9">
#Html.DisplayFor(m => m.Name)
</div>
</div>
<br />
<div class="form-group">
<div class="col-sm-3">
#Html.LabelFor(m => m.Surname, new { #class = "control-label" })
</div>
<div class="col-sm-9">
#Html.DisplayFor(m => m.Surname)
</div>
</div>
<br />
Related
How can I set the login view to center of screen? No matter what I try it gets messy. I'm new to Bootstrap and coding.
I've tried align items center and justify but they don't seem to work.
<h2 style="padding-top: 50px">Welcome to Portal.</h2>
<hr/>
<h4 style="padding-top: 20px">Please log in to continue.</h4>
<div class="row justify-content-center" style="padding-top: 30px">
<div class="col-6">
<section id="loginForm">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
#Html.CheckBoxFor(m => m.RememberMe)
#Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</div>
<p>
#Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>
}
</section>
</div>
</div>
Desired output:
My output:
Try adding d-flex to your row so that the justify content center works.
Wrap your login div with a container with max-width. That will solve your problem.
This will make sure the container is position centered.
I have removed the justify-content-center and extra col-6 that you had put for the login section and wrapped it with a container with max-width.
<h2 class="mt-3">Welcome to Portal.</h2>
<hr/>
<div class="container mt-5" style="max-width:400px">
<h4 class="mt-2">Please log in to continue.</h4>
<div class="row">
<section id="loginForm">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
#Html.CheckBoxFor(m => m.RememberMe)
#Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</div>
<p>
#Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>
}
</section>
</div>
</div>
To center the content from top and bottom add align-items-center to row , and And pay attention plz , you must give height to row
and for left and right use width : max-content , margin : 0 auto
see this :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>asdasd</title>
<link rel="stylesheet" href="css/general/bootstrap.css">
<style>
.row{
height: 100vh;
}
.main_field{
width: max-content;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row align-items-center">
<div class="main_field">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
I am designing an edit form using MVC 5 Razor. I want the input controls to fill the whole blank width as shown in the picture but it is not working. see image here.
Page here
I have put controls inside table dimensions. One dimension is for the label and the other dimension is for input editor (HTML Helper).
I have tried to override the form-control width with the following css class but no success:
.form-control {
width: 100%!important;
}
and even made another css class
.newinputwidth {
width: 400px!important;
}
but it does not change the width of input.
The full MVC code is here.
#model test2.Models.CaseReport
<style>
.form-control {
width: 100%!important;
}
.newinputwidth {
width: 400px!important;
}
</style>
<h2>Edit</h2>
<div class="container">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="row">
<div class="col-lg-12 col-md-12 col-xs-12">
<table id="dt" class="table table-condensed table-responsive table-bordered" width="100%" cellspacing="0">
<tr>
<td>Main Id</td>
<td>
#Html.EditorFor(model => model.CaseReportId, new { htmlAttributes = new { #class = "newinputwidth" } })
</td>
</tr>
<tr>
<td>Location</td>
<td>
#Html.EditorFor(model => model.Location, new { htmlAttributes = new { #class = "form-control" } })
</td>
</tr>
<tr>
<td>Disease </td>
<td>
#Html.EditorFor(model => model.ReportDate, new { htmlAttributes = new { #class = "form-control" } })
</td>
</tr>
</table>
</div>
<div class="row col-lg-12 col-md-12 col-xs-12 text-center">
<div>
<div>
#Html.Action("_DetailsIndex", new { id = Model.CaseReportId })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
<div>
#Html.ActionLink("Back to List", "Index")
</div>
</div>
}
</div>
In bootstrap, you put blank controls in a container class and apply custom css on it. That will take care of the width of the blank controls. Now, sometimes even when you set container class's width, it does not mean it is responsive or it really makes controls width so short! To achieve responsive desired behavior readable to users set css for each control and apply # style="100% !important; min-width:300px;".
Here is an example:
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{ <div class="container" style="width:40%; margin:6%">
<h4 style="color:darkorange">Register</h4>
<div class="row text-center">
<h5 style="color:tomato">#ViewBag.Message</h5>
</div>
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control", #placeholder = "First Name", #style = "width:100% !important; min-width:180px;" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
}
Since you're using Bootstrap, I would suggest abandoning the table completely for this type of layout.
You should be able to achieve this with the grid system using a horizontal form:
http://getbootstrap.com/css/#forms-horizontal
<div class="row form-horizontal">
<div class="col-md-12">
<div class="form-group">
#Html.LabelFor(model => model.CaseReportId, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-9">
#Html.EditorFor(model => model.CaseReportId, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-9">
#Html.EditorFor(model => model.Location, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ReportDate, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-9">
#Html.EditorFor(model => model.ReportDate, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
</div>
</div>
If you really want to use a table, I noticed you have a table-responsive class on the table element. This is meant to be used on a parent (wrapper) div.
http://getbootstrap.com/css/#tables-responsive
<div class="table-responsive">
<table class="table">
...
</table>
</div>
I really don't know what I'm doing wrong. I want my textboxes with same size of my buttons below. I already tried change a lot in the cshtml but without success.
Below my cshtml file:
#model SomeModel.Models.LoginViewModel
<div class="row">
<div class="col-md-8 col-sm-12 col-xs-12">
<section id="loginForm">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(m => m.UserName, new { #class = "col-md-2 control-label" })
<div class="col-md-12">
#Html.TextBoxFor(m => m.UserName, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.UserName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-12">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-xs-12">
<button type="submit" class="btn btn-success btn-block">Entrar</button>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-sm-12 col-xs-12 visible-sm visible-xs">
<button type="submit" class="btn btn-primary btn-block">Ainda Não Tenho Conta</button>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-sm-12 col-xs-12 visible-sm visible-xs">
<button type="submit" class="btn btn-info btn-block">Esqueci Meu Usuário Ou Senha</button>
</div>
</div>
<p>
#Html.ActionLink("Register", "Register") if you don't have a local account.
</p>
}
</section>
</div>
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
What am I doing wrong?
The default MVC5 template has a css rule in Site.css:
input, select, textarea { max-width: 280px; }
I usually remove this rule.
It causes problems with Bootstrap v3 input group where the input box does not extend to its container's width.
In my project I have a form with three inputs, and for all I want to use the input-group and input-group-addon offered by Bootstrap. If I use the following code,
<div class="input-group form-group">
<span style="text-align:left;" class="input-group-addon">... vs Frank</span>
#Html.TextBoxFor(model => model.vsFrank, new { #class = "form-control" })
</div>
<div class="input-group form-group">
<span style="text-align:left;" class="input-group-addon"> ... vs any other guy</span>
#Html.TextBoxFor(model => model.vsAnyOther, new { #class = "form-control" })
</div>
<div class="input-group form-group ">
<span style="text-align:left;" class="input-group-addon">... vs any other monster</span>
#Html.TextBoxFor(model => model.vsAnyMonster, new { #class = "form-control" })
</div>
the result looks like this,
Now, I'd like to have the labels of the last two rows lined up, but the one of the first row shorter. If I add some width to them, as follows,
<div class="input-group form-group">
<span style="width:15ch; text-align:left;" class="input-group-addon">... vs Frank</span>
#Html.TextBoxFor(model => model.vsFrank, new { #class = "form-control" })
</div>
<div class="input-group form-group">
<span style="width:25ch; text-align:left;" class="input-group-addon"> ... vs any other guy</span>
#Html.TextBoxFor(model => model.vsAnyOther, new { #class = "form-control" })
</div>
<div class="input-group form-group ">
<span style="width:25ch; text-align:left;" class="input-group-addon">... vs any other monster</span>
#Html.TextBoxFor(model => model.vsAnyMonster, new { #class = "form-control" })
</div>
the result looks like this:
Now the whole first control is shortened! How can I prevent that without giving all the controls explicit width?
Add a min-width for the input-group.
CSS
.input-group {
min-width: 100%;
}
HTML
<div class="row">
<div class="col-md-3">
<div class="input-group form-group">
<span style="width:15ch; text-align:left;" class="input-group-addon">... vs Frank</span>
<input class="form-control">
</div>
<div class="input-group form-group">
<span style="width:25ch; text-align:left;" class="input-group-addon">... vs any other guy</span>
<input class="form-control">
</div>
<div class="input-group form-group">
<span style="width:25ch; text-align:left;" class="input-group-addon">... vs any other monster</span>
<input class="form-control">
</div>
</div>
</div>
Bootply
Thanks to James Donnelly, this has been solved. My example has been updated to the working solution.
My reputation isn't high enough so I can't post an image, so I am going to try and describe it as best as possible. I have 7 textboxes and a textarea on a form, and when I load the page, it currently loads each textbox on a separate line.
However, I want the textboxes to load like this:
1 2 3
4 5 6 7
---8--- (span across)
Should I put each row inside a div? I tried floating each textbox to the left, but that does not work correctly. I have Twitter bootstrap setup in my project if that makes things easier.
Here is my working code:
<div class="well">
<h4>Enter Movie in System:</h4>
#using (Html.BeginForm()) {
<div>
<div class="input-prepend">
<span class="add-on"><i class="icon-film"></i></span>
#Html.TextBoxFor(m => m.Title, new { placeholder = "Title" })
#Html.ValidationMessageFor(m => m.Title)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-facetime-video"></i></span>
#Html.TextBoxFor(m => m.Director, new { placeholder = "Director" })
#Html.ValidationMessageFor(m => m.Director)
</div>
#* Make dropdown *#
<div class="input-prepend">
<span class="add-on"><i class="icon-tags"></i></span>
#Html.TextBoxFor(m => m.parentGenre, new { placeholder = "Genre" })
#Html.ValidationMessageFor(m => m.parentGenre)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-star-empty"></i></span>
#Html.TextBoxFor(m => m.Stars, new { placeholder = "Actor" })
#Html.ValidationMessageFor(m => m.Stars)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-time"></i></span>
#Html.TextBoxFor(m => m.Duration, new { placeholder = "Duration (mins)" })
#Html.ValidationMessageFor(m => m.Duration)
</div>
#* Make dropdown *#
<div class="input-prepend">
<span class="add-on"><i class="icon-warning-sign"></i></span>
#Html.TextBoxFor(m => m.Rating, new { placeholder = "Rating" })
#Html.ValidationMessageFor(m => m.Rating)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-calendar"></i></span>
#Html.TextBoxFor(m => m.releaseDate, new { placeholder = "Release Date" })
#Html.ValidationMessageFor(m => m.releaseDate)
</div>
<div class="control-group">
<div class="input-prepend">
<span class="add-on"><i class="icon-comment"></i></span>
#Html.TextAreaFor(m => m.Description, new { placeholder = "Description", #class="input-xxlarge" })
#Html.ValidationMessageFor(m => m.Description)
</div>
</div>
<p>
<button class="btn" type="submit" value="Register">Register</button>
</p>
#Html.ValidationSummary()
</div>
}
.content-group is by default set to display:table; which is what's forcing each of your inputs down onto a new line. You'd simply need to put each of your .input-prepend containers into just one container:
<div>
<div class="input-prepend">
<span class="add-on"><i class="icon-tags"></i></span>
#Html.TextBoxFor(m => m.parentGenre, new { placeholder = "Genre" })
#Html.ValidationMessageFor(m => m.parentGenre)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-tags"></i></span>
#Html.TextBoxFor(m => m.parentGenre, new { placeholder = "Genre" })
#Html.ValidationMessageFor(m => m.parentGenre)
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-tags"></i></span>
#Html.TextBoxFor(m => m.parentGenre, new { placeholder = "Genre" })
#Html.ValidationMessageFor(m => m.parentGenre)
</div>
</div>
Is this what u mean?
<div>
<input id="tbx1" />
<input id="tbx2" />
<input id="tbx3" /><br />
<input id="tbx4" />
<input id="tbx5" />
<input id="tbx6" />
<input id="tbx7" />
<span id="span1" style="padding-left:200px">
<input id="tbx8" />
</span>
</div>
you have to post the css style you have tried. but I am guessing that giving the form a float left, width and height should let you be able to float the input fields, make sure none of the first six inputs is greater than 30% of the form width. for the last one width 100%