Solution 1 :

According to your codes and result image, I guess you don’t import the bootstrap or jquery successfully. So the bootstarp class is not work well for your html codes.

I suggest you should firstly include the bootstarp CSS and JavaScript in your razor page and add justify-content-center to the row, then you could find all your control will become center.

Reference:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

Html codes:

<div class="container align-content-center" style="border:solid">
    <div class="row justify-content-center">
        <div class="col-md-6 col-md-offset-3 text-center">
            <div class="form-group">
                <label asp-for="Customer.name">Name:</label>
                <input asp-for="Customer.name" class="form-control text-center" type="text" />
            </div>
            <div class="form-group">
                <label asp-for="Customer.notes">Notes:</label>
                <input asp-for="Customer.notes" class="form-control text-center" />
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-dark text-center" />
            </div>
        </div>
    </div>
</div>

Result:

enter image description here

If you want the lable and the input should in same row, you should modify the html codes to let one form control inside one row like below:

<div class="container" style="border:solid">
    <div class="form-horizontal col-md-offset-3 justify-content-center">
        <div class="form-group ">
            <label asp-for="Customer.name" class=" col-md-2 control-label">Name:</label>
            <div class="col-md-4">
                <input asp-for="Customer.name" class="form-control text-center" type="text" />
            </div>
        </div>
        <div class="form-group ">

            <label asp-for="Customer.notes" class=" col-md-2 control-label">notes:</label>
            <div class="col-md-4">
                <input asp-for="Customer.notes" class="form-control text-center" type="text" />
            </div>
        </div>
        <div class="form-group ">
            <div class="col-md-6 text-center">
                <input type="submit" value="Create" class="btn btn-dark text-center" />
                </div>
            </div>
        </div>
</div>

Result:

enter image description here

Problem :

I can’t seem to figure out how to center some input boxes inside my ASP.NET Core web application. I have been trying out CSS solutions and using different HTML solutions like class = “text-center”, but I haven’t gotten any results yet. Here is the code that I have at the moment trying to center these boxes on the page:

@page
@model CustomerPageTest.Pages.Customer.AddCustomerModel

@{
    ViewData["Title"] = "AddCustomer";
}

@{ 
    Layout = null; //Added
}

<div class="container align-content-center" style="border:solid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3 text-center">
            <div class="form-group">
                <label asp-for="Customer.name">Name:</label>
                <input asp-for="Customer.name" class="form-control text-center" type="text" />
            </div>
            <div class="form-group">
                <label asp-for="Customer.notes">Notes:</label>
                <input asp-for="Customer.notes" class="form-control text-center" />
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-dark text-center" />
            </div>
        </div>
    </div>
</div>

This is the result I get when I run the application:
enter image description here

Sorry for the size of it, I’m not sure how to enlarge that. Either way, everything stays on the left. Is there some type of bootstrap file I should be importing to fix this?

Comments

Comment posted by CKneeland

Great answer! Thanks for the help. I didn’t have the bootstrap imported.

By