Why the card is not at the center of the screen? [duplicate] - html

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 2 years ago.
The card is supposed to be at the center of the screen but its not, why is it so ??
I'm using 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>

Try adding below css in your css file
.card {
margin: 0 auto;
float: none;
}
or try mx-auto bootstrap 4 css class

The col class you added before the col-8 has no content so won't actively control the spacing of your col-8 column. One way of ensuring this column will sit in the centre of your container (which effectively has 12 available slots to be filled with bootstrap content) is to 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 could use css styles alone, but that would mean the bootstrap layout that you had applied would be extra, unnecessary code. You can then use flex utility classes to centre the card content within that container, depending on how you want that to be justified.
Have look at the docs here - https://getbootstrap.com/docs/4.0/layout/overview/

Not sure it should be centered, by default. But you can achieve Horizontal Alignment with css class justify-content-center which you can add to row:
<div class="container">
<div class="row justify-content-center">
<!-- You don't need 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>
Also, pay attention that you have a small error here: <div class"col"></div>, maybe this is just a typo and in the real code it is like <div class="col"></div> :)

I think you have not added bootstrap 4 CDN i.e (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>
you have to add this CDN in your project HTML page under <head></head> tag after adding you can use with two option
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>
add this CSS in your css file
.center{
display: flex;
justify-content: center;
align-items: center;
}
Second Option
Bootstrap 4 already gives a class 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>
You can check preview Here

Related

making a grid system in bootstrap 5

I want to make something like this image in bootstrap 5 .
image
also I want to add image inside every three of them . How can I Do this with the bootstrap grid system?
This should be the raw Bootstrap flex box based layout for your design:
<div class="container">
<div class="row">
<div class="col-6">
<div class="row flex-column">
<div>
<img>
</div>
<div>
<img>
</div>
</div>
</div>
<div class="col-6">
<img>
</div>
</div>
</div>

Boostrap grid aligment [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 1 year ago.
I have three elements and I want to center two in the middle of the page and the last item completely to the left.
I still don't understand the col system.
Thanks in advance.
html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="container w-100">
<div class="row">
<div class="col d-flex justify-content-start">
item 1 // align this element to the left
</div>
<div class="col">
item 2 // center this element in the middle of the page
</div>
<div class="col">
item 3 // center this element in the middle of the page
</div>
</div>
</div>
To accomplish what you are looking for we need to create a row that contains 3 columns with the same width and leave the third column empty. Then we use Bootstrap d-flex class to position the elements inside each column exactly where we want.
In this case I'm using justify-content-start to position the flex element at the start (left) of the container.
.m-1{border:1px solid black}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="row w-100">
<div class="col">
<div class="d-flex justify-content-start">
<div class="m-1 p-1"> item one</div>
</div>
</div>
<div class="col">
<div class="d-flex justify-content-center">
<div class="flex-fill m-1 p-1">item two</div>
<div class="flex-fill m-1 p-1">item three</div>
</div>
</div>
<div class="col">
</div>
</div>
You can read more about this here and here

What's the best way to vertically align content within Bootstrap 4 grid columns? [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 3 years ago.
I have the following elements with Bootsrap 4(.3.1) classes:
<footer class="container-fluid">
<div class="row">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>
The height of <footer> is 100px and I'd like the content fo the cols to sit veritcally in the center of the element.
I've tried adding d-flex align-items-center to the elements (trying container, row and cols in turn) but didn't get any results.
Will I have to scrap the grid classes all together and re-do this with flex classes? Or does anyone know of a way Bootstrap can acheive this out-of-the-box using it's own classes?
In addition to adding d-flex align-items-center to the footer and flex-grow-1 to the row class - see demo below:
footer {
height: 100px;
border: 1px solid;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<footer class="container-fluid d-flex align-items-center">
<div class="row flex-grow-1">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>
You should add the height to the row. and use align-items-center to vericaly align the flex-items i.e, col
.footer {
height: 100px;
background: #dedede;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<footer class=" container-fluid">
<div class="footer row align-items-center justify-content-center">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>

Vertically aligning text inside a bootstrap column

I have minimal CSS and HTML knowledge and would like to achieve the following: I am using a WordPress theme that uses Bootstrap, and I want to create a row with two columns. Left column has an image, right column has a headline that should be vertically centered. I am using a column shortcode
<div class="row">
<div class="col-6">
IMAGE
</div>
<div class="col-6">
HEADLINE
</div>
</div>
Can anyone help me how to center my headline vertically?
Thank you for your help.
If you are using Bootstrap 4. You can use the below snippet
<div class="row">
<div class="col-6">
<img src="" alt="">
</div>
<div class="col-6 d-flex align-items-center">
Your heading
</div>
</div>
If you are using Bootstrap 3.x.x. You can use the below snippet
<div class="row">
<div class="col-sm-6">
<img src="" alt="">
</div>
<div class="col-sm-6 flex-vcenter">
Your heading
</div>
</div>
CSS (only if Bootstrap ver is 3)
.flex-vcenter {
display: flex
align-items: center
}

Bootstrap - Center content vertically and horizontally inside container-fluid [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 3 years ago.
Just like the title says, in Bootstrap 4, I can't figure out how to center content inside a column that's inside .container-fluid.
Here's my code, can someone please let me know what I did wrong?
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-md-4 my-auto mx-auto">
<div class="mx-auto">
<h1 class="mb-5">This is a title
</h1>
<p class="mb-5">Lorem ipsum</p>
Click me
</div>
</div>
<div class="col-md-8">
<p>Something else that's not important but needs to be here </p>
</div>
</div>
Why doesn't the .mx-auto work and what can I use instead of it?
The col-* are flex items, so you have to also make them flex container in order to center their content like you want (vertically or/and horizontally) using margin:auto with elements (my-auto/mx-auto) or align-items-*/jusity-content-* with the container:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-md-4 d-flex flex-column align-items-center justify-content-center">
<h1 class="mb-5">This is a title
</h1>
<p class="mb-5">Lorem ipsum</p>
Click me
</div>
<div class="col-md-8 d-flex flex-column ">
<p class="my-auto mx-auto">Something else that's not important but needs to be here </p>
</div>
</div>