Can row and container classes be used together on a single element?

While watching a tutorial today where someone was creating a website using Bootstrap 4, I noticed that they applied both 'row' and 'container' as classes for the same div. This goes against Bootstrap documentation, which states that they should be nested within each other. Surprisingly, it seemed to work fine in the video. Curious, I tried replicating it in my own test document, but found that the 'container' class failed to centralize the div. The code from the tutorial did not yield the same results for me. Can anyone shed light on why this discrepancy exists? Here's the non-working code snippet I used:

<div class="container row">
    <div class="col-md-6 d-flex justify-content-center align-items-center" style="background-color: steelblue;height:50vh;">
       <h1 class="d-inline-block" style="outline: 1px solid red;">Some text</h1>
    </div>
    <div class="col-md-6" style="background-color: pink;height:50vh;"></div>
</div>

Answer №1

When utilizing Bootstrap 4, the .container class is designed to automatically center the div with styles like

margin-left: auto; margin-right: auto;
. However, if you then apply the .row class within the .container, the margins are overridden by the row's styling which includes negative margins like
margin-left: -15px; margin-right: -15px;
. This can cause the div to no longer be centered as documented in the Bootstrap documentation. To address this issue, the tutorial suggests using the mx-auto class, which essentially translates to
margin-left: auto !important; margin-right: auto !important;
, ensuring that the div remains centralized.

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

IE11 experiencing issues with font loading

I need help troubleshooting why the fonts are not loading properly in the body section of my articles, specifically when viewed in Internet Explorer. Take a look at an example article here: I am fetching from this CSS file: , and I have included the nece ...

Unable to alter the dimensions of the `symbol` element from an external SVG using CSS, although the other `symbol` within the same document is responsive to styling changes

Could someone help me find the bug in my code related to resizing SVG symbols with CSS? I am able to resize one <symbol> from an external SVG file, but not another <symbol> from the same file. In my CSS, I am trying to change the width and hei ...

Animating scrollTop using JQuery

There are two buttons with hidden parts related to each. When a button is clicked, its associated part will be displayed and the document will scroll to the top. Issue: The displayed part does not move to the top as intended. Your assistance in resolving ...

Can the value of a CSS class be obtained even if it is not being used?

I have an application that pulls data from a database and displays it with associated metadata that dictates the display style. Let's simplify it and say the only metadata is the size of the square it should appear in. For example: dataArray : { ...

Ensuring proper input with JavaScript form validation

Currently, I am working on implementing a basic form validation feature, but it is not functioning as intended. The desired behavior is for the field border to change color to green or red based on its validity, while displaying text indicating whether t ...

Beside the element, a dropdown navigation menu emerges

Trying to work on a dropdown menu here. It appears to be functioning, but there's a small issue - the dropdown shows up on the right side of the element instead of directly below it. I have a feeling it has to do with either position or padding, but I ...

Breaking down a string and then retrieving elements from an array

Just diving into the world of Javascript and jQuery, so I have a simple query. I've got a date that I need to break down into a string and then display it as an array. var date = "12/10/2010"; var dateArray = date.split(/); $('#printbox') ...

Personalize the Hover color on your flexdashboard with a touch of custom CSS

I am currently working on building a dashboard with Flexdashboard and I want to customize the hover and active colors of the Flexdashboard pages named Capture and Results with specific colors that are unique to the client. I prefer not to use the default F ...

Tips for displaying specific HTML elements in AngularJS using ng-repeat and ng-if

I am working with some bootstrap HTML code that includes an ng-repeat function. <div class="row"> <div class="col-lg-4" ng-repeat="p in persons"> <img class="img-circle" src="images/{{ p.image }}" width="140" height="140"> < ...

The width of a CSS border determines its height, not its width

When I try to use border-width: 100px; to set the horizontal width to 100px, it ends up setting the height to 100px instead. Any help would be greatly appreciated. .border-top-green { border-top: 1px solid #4C9962; border-width: 100px; padding-t ...

The presence of element a within the element ul is not permitted in this particular scenario. Additional errors from this section will not be displayed

Having trouble with this code snippet: <nav role="navigation"> <div id="menuToggle"> <input type="checkbox"/> <span></span> <span></span> <span></span> ...

In regards to navigational bars that are oriented horizontally

Trying to create a simple navigation bar with horizontal links but facing issues. Can't seem to make it work despite following examples online: <!-- Navigator --> <div id="nav"> <ul> <li><a href="#"& ...

Fast search using two dropdown menus

Looking to implement a search functionality with two drop down boxes using ajax. Here's the code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(do ...

Implementing a dynamic navigation bar that expands and collapses on mouse hover in a React application

I have a side navigation bar on my webpage that I obtained from a React Bootstrap website, so the class names are already pre-loaded. Instead of using a toggle hamburger icon to expand or collapse the navigation, I want to achieve this functionality based ...

What is the method for displaying map tooltips by default rather than on mouseover?

Currently, I have a script set up to display a map with two markers. Whenever I hover over one of the markers, a popup tooltip appears with location information. My question is, how can I make the information appear by default without needing to hover ov ...

Is there a way to make a Bootstrap grid with four columns span across two rows in the second column?

How can I create a Bootstrap grid with four columns where the second column spans two rows? Hello, I am looking for help to achieve a portfolio grid layout with 4 columns (refer image here) and have the second column span 2 rows. I plan to include images ...

The process of binding two variables in the same select element

Here is the code snippet that I am working with: <div class="form-group "> <label for="field_type"><b>Type</b></label> <div class="input-icon right"> <select required class="form-control" ...

Problem with CSS Classes Styling

Having an issue with CSS in Windows Notepad: <!DOCTYPE html> <html> <head> <title>UniqueScience</title> <style> #title { display: block; ...

Can I split icons instead of combining them when stacking in Font Awesome?

Is it possible to stack icons by dividing their size instead of multiplying them? I am looking to have my first icon be the same size as the font I am using, and the icon on top of it to be half the font size. Can this be achieved with Font Awesome or do I ...

Expanding the content in Bootstrap Navbar

I am working with a bootstrap navbar and need to customize it by adding a SignOut button and a Welcome message to the menu bar. I also want to adjust the positioning of the logo and company/city name within the navbar. Here is the current setup of my navba ...