Why isn't the card positioned in the center of the screen?

Why is the card not centered on the screen as it is supposed to be?

I am utilizing Bootstrap 4.5

<div class="container">
 <div class="row">
   <div class"col"></div>
    <div class="col-8">
     <div class="card">
      <div class="card-body">
       This is some text within a card body.
      </div>
     </div>      
    </div>
   <div class"col"></div>
 </div>
</div>

Answer №1

For improved positioning, consider incorporating the following CSS code into your stylesheet:

  .card {
            margin: 0 auto; 
            float: none; 
    }

Alternatively, experiment with the mx-auto class from Bootstrap 4 for a similar effect.

Answer №2

The previous col class added before col-8 does not contain any content, so it does not affect the spacing of your col-8 column. To ensure that this column is centered within your container (which has 12 slots to fill with bootstrap content), you can use an offset class like this:

<div class="container">
 <div class="row">
   
    <div class="col-8 offset-2">
     <div class="card">
      <div class="card-body">
       This is some text within a card body.
      </div>
     </div>      
    </div>
   
 </div>
</div>

You can achieve this using only CSS styles, eliminating the need for the extra bootstrap layout code. Utilize flex utility classes to center the card content within the container, adjusting the justification based on your preference.

For more information, refer to the documentation here - https://getbootstrap.com/docs/4.0/layout/overview/

Answer №3

Not certain if centering is necessary by default. However, you can accomplish Horizontal Alignment by using the css class justify-content-center, which can be added to the row:

<div class="container">
  <div class="row justify-content-center">
    <!-- No need for empty col before and after in this case :) -->
    <div class="col-8">
     <div class="card">
      <div class="card-body">
       This is some text within a card body.
      </div>
     </div>      
    </div>

  </div;
</div>

Additionally, make sure you address the small mistake here:

<div class"col"></div>
. It could be just a typo, and in the actual code it might be like
<div class="col"></div>
:)

Answer №4

It appears that you have overlooked adding the Bootstrap 4 CDN, which stands for (Content Delivery Network)

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

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

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

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

To include this CDN in your project HTML page, it should be placed within the <head></head> section. Once added, you can utilize it with two different options


First Option

<div class="container">
  <div class="row">

    <div class="col-12 center">
     <div class="card">
      <div class="card-body">
       This is some text within a card body.
      </div>
     </div>      
    </div>

  </div>
</div>

Don't forget to include the following CSS in your css file

.center{
  display: flex;
  justify-content: center;
  align-items: center;
}

Second Option

Bootstrap 4 already provides a class called justify-content-center

<div class="container">
  <div class="row justify-content-center">
    <div class="col-8">
     <div class="card">
      <div class="card-body">
       This is some text within a card body.
      </div>
     </div>      
    </div>

  </div>
</div>

For a preview, you can visit this link

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

Understanding the float property in CSS

Check out this CSS example: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Jenware | Personalized Gifts</title> <style type="text ...

How can you conceal an HTML element when the user is using an iOS device?

I need the code to determine if a user is using an iOS device and, if not, hide the HTML input type "Play" button. So, I'm uncertain whether my iOS detection is correct, the hiding of the "Play" button in the code, or both: <!DOCTYPE html> < ...

Is it possible to dynamically assign trigger and target divs to bPopup?

On our ColdFusion page, we have a dynamic list of paired divs created using a loop over a query. For example, each pair consists of internshipHandleX and internshipHiddenX: <div id="internshipHidden#internship.currentrow#" class="hidden pop-up"> We ...

What steps can I take to fix the Error with webpack's style hot loader JavaScript?

Just starting out with native script and encountered an issue when running this code: <template> <view class="container"> <text class="text-color-primary">My Vue Native Apps</text> </view> </template> &l ...

Transform one div to another using a CSS animation slide

I am experiencing an issue with two divs (page1 + page2) inside a container (also a div). The container has overflow:hidden property, but when the animation starts, the overflow configuration is being ignored. Additionally, the page that should be displaye ...

Django-Based Navigation Bar Design for Online Shopping Site

I'm currently working on an eCommerce website using Django for the backend. I'm looking to design a dynamic navbar that will display menu options based on different categories of products. The goal is for this navbar to automatically update whene ...

Placing a list item at the beginning of an unordered list in EJS with MongoDB and Node.js using Express

What I've done: I already have knowledge on how to add an LI to UL, but it always goes to the bottom. What I'm trying to achieve: Add an LI to the top so that when my div.todos-wrapper (which has y-oveflow: hidden) hides overflow, the todos you a ...

What is the best way to disable the button hover effect after it has been clicked?

Many posts I've come across have similar issues to mine, but the suggested solutions are not working for me. I am struggling to remove the hover property of my button when it is clicked before triggering the removal event I have set up. Edit: Just t ...

When using margin-right and a width of 100% in the WordPress backend, a scrollbar may be triggered

I have been working on developing a configuration page for my WordPress plugin. In order to display the content, I included an <ul> element inside a <div> container and added it to the configuration page. However, I encountered an issue where a ...

Troubleshooting Date Problems in Angular

When using the HTML5 date picker, I have encountered an issue where after choosing a date, the ng-model displays an older date instead of the current one selected. <input type="date" ng-model="dateModel" /> For example, when selecting the current d ...

Trouble accessing files in the assets folder of Angular 2

I am encountering a 404 error when attempting to access a local file within my application. I am unable to display a PDF that is located in a sub-folder (pdf) within the assets folder. I am using CLI. <embed width="100%" height="100%" src="./assets/pdf ...

What steps can I take to make my animation work in the opposite direction as well?

I'm currently working with an angular slider that is set to TRUE/OPEN by default. The issue I am facing is that while I am able to slide it using angular animations in one direction, I am unable to see the transition when sliding it back. Any assistan ...

Images displaying correctly in Next.js development environment, but failing to load on Github Pages production site

I am currently using Next.js alongside Tailwind CSS and I'm encountering an issue while attempting to set a background image for a specific div. Oddly enough, the image appears to load correctly in development mode, but fails to display when deployed ...

Modifying the TITLE dynamically post header insertion: A guide

Is there a way to dynamically change the title of an HTML page based on the content included below the header section using PHP? For example: <html> <header><title>I would like to change</title></header> <!--CONTENT--> ...

Horizontal Alignment of DIV Tags

I'm currently working on aligning some div tags to serve as contact links on my website. My goal is for them to be positioned on the right-hand side and aligned in a single line. Here's how they look at the moment: https://i.sstatic.net/rQ1uG.p ...

Creating a Form in a Popup with Bootstrap

I have implemented Bootstrap on my website. Check it out at: hubb.tekkkz.com I am facing an issue where, when clicking on the login/register button on the right, the modal pops up. However, the user input elements within the modal are not taking up the ful ...

Exploring the world of XML parsing with iText

I am curious to learn whether it is feasible to modify the font, color, and size while converting HTML to PDF using xmlWorker.parser. Thus far, I have successfully parsed the input as is, but now I am seeking ways to customize the font, size, color, and ...

Top method for utilizing overlays

Is there a method to randomly select hex codes for specific colors? I want to replicate the design in this image through coding. Image Here is the code I have so far: HTML <div id="group"> <div class="sub red panel"> </div><!--su ...

Creating a fixed footer within a div that is absolutely positioned using CSS

I am looking to implement a sticky footer within an absolutely positioned div element. My initial approach was to position the footer div absolutely as well - within an additional relatively positioned "page" div (which would contain the main content of t ...

What causes getBoundingClientRect() in Javascript to occasionally produce decimal values?

Currently, I am experimenting with an HTML5 canvas element. A major concern of mine is setting up a mousemove event to monitor the movement of the mouse over the canvas for drawing and other purposes. Unfortunately, I have not been able to locate a definit ...