What is the best way to align text in the center of a bootstrap container?

I am in the early stages of learning to code with bootstrap and have encountered a frustrating issue. Every element I add after including the jumbotron component seems to automatically align itself to the left and remain stuck there. I have tried using .text-center, as well as custom CSS styling with properties like "text-align: center;" and "margin:0 auto;".

I am new here, so I hope this is the correct way to display my code. Here's what I've written:

<head>

    <title>MyApp</title>

    <link rel="stylesheet" 
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
    integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
    crossorigin="anonymous">

    <style type="text/css">
        .navbar-brand       {
                            margin-right:10px;
                            position:relative;
                            top:2px;
                            }
        .jumbotron          {
                            margin-top:50px;
                            background-image: URL(BG.jpg);
                            background-size: 100% 100%;
                            text-align:center;
                            color: white;
                            }
        .input-group        {
                            width:350px;
                            }
        #formAlign          {
                            text-align:center;
                            width:350px;
                            margin:0 auto;
                            margin-top:30px;
                            }
        #appsum             {
                            text-align:center;
                            }

    </style>

</head>

<body>

    <nav class="navbar navbar-expand-lg fixed-top navbar-light bg-light">

        <a class="navbar-brand" href="#">
            <img src="logo.png" width="100" height="30" alt="">
        </a>

                <ul class="navbar-nav mr-auto">

                    <li class="nav-item active">
                        <a class="nav-link" href="#">Home</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="#">About</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="#">Download The App</a>
                    </li>

                </ul>

                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="email" placeholder="Email" aria-label="Search">
                    <input class="form-control mr-sm-2" type="password" placeholder="Password" aria-label="Search">
                    <button class="btn btn-info my-2 my-sm-0" type="submit">Login</button>
                </form>
    </nav>

    <div class="jumbotron">
        <h1 class="display-4">My Awesome App</h1>
        <p class="lead">This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
        <hr class="my-4">
        <p>It uses utility classes for typography and spacing to space content out within the larger container.</p>

            <div id="formAlign">
                <div class="form-horizontal">

                    <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>

                    <div class="input-group mb-0 mr-sm-2">
                        <div class="input-group-prepend">
                            <div class="input-group-text">@</div>
                        </div>

                        <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">

                        <a class="btn btn-primary btn-md ml-2" href="#" role="button">Sign Up</a>
                    </div>

                </div>
            </div>
    </div>

    <div class="container" id="appsum">

        <div class="row">
            <h1>Why this is so awesome.</h1>        
        </div>
        <div class="row">
            <p class="lead">Summary, once again, of your app's awesomeness</p>          
        </div>

    </div>

        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" 
            integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
            crossorigin="anonymous">
        </script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
            integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
            crossorigin="anonymous">
        </script>

        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" 
            integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
            crossorigin="anonymous">
        </script>

</body>

Answer №1

To ensure proper alignment in the Bootstrap grid system, it is important that each .row div contains only .col or .col-* divs as direct children. It seems like the nested divs within your rows are not following this structure.

Consider implementing the following:

<div class="container" id="appsum">

    <div class="row">
        <div class="col">
            <h1>Discover the greatness.</h1>
        </div>        
    </div>
    <div class="row">
        <div class="col">
            <p class="lead">A concise summary highlighting the awesomeness of your app.</p>  
        </div>        
    </div>

</div>

You also have the option to set specific column widths or breakpoints by utilizing col-* classes (e.g., specifying a number between 1 and 12 or a breakpoint size).

For further information on the Bootstrap 4 grid layout, refer to the documentation available at: https://getbootstrap.com/docs/4.0/layout/grid/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Jquery to change an id with the .attr method may not always produce the desired results

I have a div element that functions as a button when clicked, with the ID of knight. Every time I click on any of the 3 buttons (including knight), it changes the ID of knight to elf (yes, I'm creating a game). I have a hover effect for knight usi ...

Hide the burger menu when a link is clicked

Is there a way to make my Bootstrap burger menu automatically close when the user clicks on a menu link, rather than having to click on the burger itself? I've tried using some JavaScript code but it ends up disabling the burger menu entirely. Can any ...

How to Use CSS to Copy a Style Path and Conceal an Element within an iFrame

My goal is to adjust a division by including a width element in its CSS properties. I have attempted to use Chrome and Firebug to copy the CSS path, but it seems that the result is incorrect. After inserting it into my style.css file, the division remains ...

Trigger a new tab opening following an ajax response with Javascript

Having trouble opening in a new tab after receiving an ajax response with JavaScript, I attempted using both _newtab and _blank However, neither of them seem to be working. I wonder, Is there a solution available to find the answer? ...

To what extent can the Vuetify data tables be customized?

https://i.sstatic.net/x4qhA.png I am currently working on replicating the layout shown in the image above. The table is already functional in my Vue project. The following code snippet represents the Vuetify datatable template in use: <v-card> ...

Ways to retrieve lost methods in django-endless-pagination?

When new containers are added with ajax, the methods initialized for them stop working. How can I ensure that django-endless-pagination adds some JQuery to its generated containers? For instance: $(".fact").each(function() { $(this).css('border- ...

Tips for hiding navigation items on mobile screens

I've been learning how to create a hamburger menu for mobile devices. Within a navigation structure, I have three components: Logo, nav-items, and hamburger menu. Utilizing flexbox, I arranged them side by side and initially hid the hamburger menu on ...

Transform the check box into a button with a click feature

I've customized a checkbox to resemble a button, but I'm encountering an issue where the checkbox is only clickable when you click on the text. Is there a way to make the entire button clickable to activate the checkbox? .nft-item-category-lis ...

AngularJS HTTP POST request encountering issue with success function not functioning as expected

I am currently working on implementing a basic HTTP post request to insert data into my database. The following pages are involved: register.php contains the registration form. maincons.js houses the application controllers. sqlregister.php include ...

Change the text color of the Vuetify button to customize its appearance

Is there a way to change the default active color for button text in a toolbar using CSS? v-btn(:to="item.path" active-class="v-btn--active toolbar-btn-active") {{item.meta.title}} I attempted to override it with this custom class: .toolbar-btn-active { ...

Prepare yourself for the possibility of receiving a caution while within a try-catch block

After implementing the MongoClient.connect method and encountering the warning 'await' has no effect on the type of this expression, it became clear that including the .catch line immediately following (which is currently commented out) mitigated ...

What could be causing this code to malfunction on jsfiddle?

Why doesn't this code from W3schools work on jsfiddle? I tried to implement it here: https://jsfiddle.net/u6qdfw5f/ <!DOCTYPE html> <html> <title>W3.CSS</title> <meta name="viewport" content="width=device-width, initial ...

Hide the div only if a specific element is found on the page

I am currently exploring different methods to show or hide a left-hand navigation menu on my Volusion store. Is it possible to use JavaScript or jQuery to alter the CSS display property of a div based on the presence of another element on the page? The i ...

"Unraveling Loopback: The Ultimate Guide to Accessing Every Role Assigned to a

Currently, I am in the process of developing a Loopback application where I have customized a user model based on the pre-built User model. { "name": "user", "base": "User", "idInjection": true, "properties": { "test": { "type": "string" ...

Leveraging an Array of Objects in JavaScript

Need help with my JavaScript code! I want to adjust the date in a Date object by adding specific days and then save it. Here is what I have so far: let orderIdDateCorrectionDict = [ { "orderId": "2020053100", "dayCorrection": -146 }, { "orderId" ...

Prevent the reloading of the page by utilizing Ajax technology when submitting a form in Laravel

I'm facing a challenge with processing a form submit using ajax instead of Laravel to prevent page reloads. Unfortunately, it's not working as expected and I'm struggling to figure out the issue. Despite researching numerous examples online, ...

Tips for ensuring proper function of bullets in glidejs

I am currently working on implementing glidejs as a slider for a website, but I am facing issues with the bullet navigation. The example on glidejs' website shows the bullets at the bottom of the slider (you can view it here: ). On my site, the bullet ...

Debugging TypeScript on a Linux environment

Approximately one year ago, there was a discussion regarding this. I am curious to know the current situation in terms of coding and debugging TypeScript on Linux. The Atom TypeScript plugin appears promising, but I have not come across any information ab ...

Creating code in AngularJS

I have the following template structure: <h1 class="text-center" ng-bind-html="row.text"></h1> When the content of my row.text is a string like this: Hi your name is {{ name }} It will display as shown below: Hi your name is {{ name }} ...

Looking for regex to extract dynamic category items in node.js

Working on node.js with regex, I have accomplished the following tasks: Category 1.2 Category 1.3 and 1.4 Category 1.3 to 1.4 CATEGORY 1.3 The current regex is ((cat|Cat|CAT)(?:s\.|s|S|egory|EGORY|\.)?)(&#xA0;|\s)?((\w+)?([. ...