How to center align row content within a container div using bootstrap? - html

I am doing an assignment for a Web Development course using css, html and bootstrap. I need to make a website and I am having some issues with the layout, I am using bootstrap cards (with images) within container and row div but the 3 cards in the row aren't aligned at the center of the col, there is some space left on the right side of the col as you can see in this screenshot.
This is my code:
<div class="container">
<div class="row ">
<div class="col-4">
<div class="card text-center " style="width: 18rem;">
<img src="imgs\products\cameras\1.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Canon EOS Rebel T7</h5>
<p class="card-text">Price : Rs. 37000.00</p>
Add to cart
</div>
</div>
</div>
<div class="col-4">
<div class="card text-center" style="width: 18rem;">
<img src="imgs\products\cameras\2.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Fujifilm X-T3</h5>
<p class="card-text">Price : Rs. 34000.00</p>
Add to cart
</div>
</div>
</div>
<div class="col-4">
<div class="card text-center" style="width: 18rem;">
<img src="imgs\products\cameras\3.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Nikon D5600</h5>
<p class="card-text">Price : Rs. 43000.00</p>
Add to cart
</div>
</div>
</div>
</div>
</div>
I would like to know if there is any way to center align the card in the col div using css or bootstrap?
I would also like to know how to make all the card the same size (width and height) even if the images inside them are different sizes, as you can see in screenshot the last 2 cards are of different sizes because of the different image sizes.

To your first question:
Just use col-4 d-flex justify-content-center on your col definition.
By doing this, the card should be aligned in the middle of the col, see here.
Blue = container, purple = column, green = card.
Hope this works for you!

Related

adjusting span value inside a col in bootstrap 4

I am currently using bootstrap 4 for the cards. I would like to add a numbering beside my card. which is show in the image below.
I tried adding padding-top on the span text the same goes with the assigned col of the span text but the card beside it is adjusting as well. Can anyone help me with this one?
I want the numbering to be at the horizontal range of the card either at the top or at the middle, as you can see it's above the card.
<div class="container">
<div class="col-sm-2">
<span style="display:inline-block; padding-top: 15px;" >1</span>
</div>
<div class="col-sm-12">
<div class="card h-100" style="width: 15rem;">
<div>
<a class="fancybox" href="admin/files/Images/flutterfest.png">
<img class="card-img-top" src="admin/files/Images/flutterfest.png">
</a>
</div>
<div class="card-body">
<h5 class="card-title text-center">Library Management System</h5>
<p class="card-text">Date added: <span>May 23, 2021</span></p>
</div>
</div>
</div>
</div>
give column classes as col-sm-2 and col-sm-10. there are 12 columns max you can create in bootstrap. So you have given col-sm-12 to second div thats why it is going to next line
<div class="col-sm-2">
<span style="display:inline-block; padding-top: 15px;" >1</span>
</div>
<div class="col-sm-10">

i want a card loop in html bootstrap but it didnot work properly the cards vertically align after each other

I want to create cards in a loop runtime but the card show vertically align eachother i gave the col-md-4 but it didnot work
enter code here
<div class="row-fluid border mt-3">
<div *ngFor="let data of allWidgets">
<div class="col-md-4 border mt-3">
<div class="card">
<div class="img1">
<img src="https://images.havenly.com/unsafe/550x334/filters:quality(50)/https://s3.amazonaws.com/static.havenly.com/assets/2a35d539-e889-405e-b495-815aa33f65ea" alt="">
</div>
<div class="img2">
<img src="https://images.havenly.com/unsafe/180x180/filters:quality(50)/https://s3.amazonaws.com/havenlydesignerphotos/13078-profile-eb69eheadshot02.jpg" alt="">
</div>
<div class="mian-text mt-0">
<p>{{data.name}}</p>
<p><small>{{data.description}} </small> </p>
View Profile <span style="color: lightgray;">|</span> Select Cherise
</div>
</div>
</div>
</div>
you must use the ngFor not on the first div but on the second like this :
<div class="col-md-4 border mt-3" *ngFor="let data of allWidgets">
this is the solution of your problem.
if you want to create a good angular card you must see:https://mdbootstrap.com/docs/angular/
is the most used site in angular.
have a nice day :)

how to make paragraph center [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I am trying to make paragraph center
I am using card
I am trying margin-left and margin-right and padding-left and padding-right but not work properly
I am trying to make paragraph one line below the service
<div class="card" style="width: 70rem;margin-top:120px;margin-left:120px;">
<div class="card-img-overlay d-flex flex-column">
<div class="row">
<img class="card-img-top" src="~/Graphics/homeiconservicepage.svg" style="width:25px;height:25px;" />
#ViewBag.text
</div>
<div class="justify-content-center d-flex">
<span> <text font-size:50px;"> #ViewBag.text </text></span>
</div>
<div class="justify-content-center d-flex" >
<p>
We enable organizations from startups to large enterprises
to make their work environment more efficient.
</p>
</div>
</div>
<img class="card-img-top" src="https://images.unsplash.com/photo-1552519507-da3b142c6e3d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" style="width:1200px;height:400px;" alt="Card image">
</div>
Current Output
I am on phone so I cannot use the snippet (Codepen: https://codepen.io/Maxigui/pen/NWrxwRg)
First thing would be to set flex-column in your card-img-overlay.
On last p I just set: class="mx-auto"
And if you want to set center, instead of align-items-center repoace that by justify-content-center (I did not do that)
I suggest you the below code:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div class="card" style="width: 70rem;margin-top:120px;margin-left:120px;">
<div class="card-img-overlay d-flex flex-column">
<div class="row">
<img class="card-img-top" src="~/Graphics/homeiconservicepage.svg" style="width:25px;height:25px;" />
#ViewBag.text
</div>
<div class="justify-content-center d-flex flex-column m-auto">
<span class="mx-auto"> <text style="font-size:50px;"> #ViewBag.text </text></span>
<p>
We enable organizations from startups to large enterprises
to make their work environment more efficient.
</p>
</div>
</div>
<img class="card-img-top" src="https://images.unsplash.com/photo-1552519507-da3b142c6e3d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" style="width:1200px;height:400px;" alt="Card image">
</div>
Use this:
<p text style="color:black;margin-top:180px;padding-right:150px;padding-left:150px;text-align: center">
We enable organizations from startups to large enterprises
to make their work environment more efficient.
</p>
Change the value of padding-right and padding-left according to your preference.
Set style="text-align:center". It works for me.
<p style="text-align:center"> We enable organizations from startups to large enterprises to make their work environment more efficient.</p>
#niskuremdir, How about this does this work?
<span style="color:black;margin-top:180px;text-align:center">
<p>
We enable organizations from startups to large enterprises to make their work environment more efficient.
</p>
</span>
Try doing this with and without the <p> </p> and also try with both <div> </div> and <span> </span>
If that works, then you can go ahead and try to add the left/right padding:)
You've to give width and height to parent container of text.
<div class="card" style="width: 70rem;margin-top:120px;margin-left:120px;">
<div class="card-img-overlay d-flex flex-column">
<div class="row">
<img class="card-img-top" src="~/Graphics/homeiconservicepage.svg" style="width:25px;height:25px;" />
#ViewBag.text
</div>
<div class="d-flex flex-column justify-content-center align-items-center w-100 h-100">
<div>
<span> <text font-size:50px;"> #ViewBag.text </text></span>
</div>
<div>
<p>
We enable organizations from startups to large enterprises
to make their work environment more efficient.
</p>
</div>
</div>
</div>
<img class="card-img-top" src="https://images.unsplash.com/photo-1552519507-da3b142c6e3d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" style="width:1200px;height:400px;" alt="Card image">
</div>

bootstrap sm resize not brakeing

So this is my HTML Code. (bootstrap css is in head included)
According to Bootstrap it should take for each col div on small screens 12 which is basically the full screen.
also it should hide the img with the .d-sm-none .d-md-block
but when iam doing the resize in chrome tools its not going well.
google coulndt help me so far. also tried removing flex class , dosent changed a thing
<body cz-shortcut-listen="true">
<div class="container-fluid">
<div class="row d-flex flex-wrap">
<div class="col-sm-12 col-md-6 col-lg-3 ">
<div class="card" style="width: 18rem;">
<img class="card-img-top d-sm-none d-md-block" src="img/1.jpg" alt="Tower">
<div class="card-body">
<h5 class="card-title">Tower</h5>
<p class="card-text">City: dubay Zip-Code 4993
<br>
</p><p>Created: 1.2.2015, 05:40:00
</p>
</div>
</div>
</div>
<div class="col-sm-12 col-md-6 col-lg-3 ">
<div class="card" style="width: 18rem;">
<img class="card-img-top d-sm-none d-md-block" src="img/1.jpg" alt="Palme">
<div class="card-body">
<h5 class="card-title">Palme</h5>
<p class="card-text">City: Dubai Zip-Code 333
<br>
</p><p>Created: 1.2.2011, 05:40:00
</p>
</div>
</div>
</div>
<div class="col-sm-12 col-md-6 col-lg-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="img/2.jpg" alt="Mcdonald">
<div class="card-body">
<h5 class="card-title">Mcdonald fastfood</h5>
<p class="card-text">City: melbourn Zip-Code 4544
<br>
</p><p>
<label>Phone, Web</label>
<br>
+66 4895643312
<br>
www.getfat.com
</p><p>Created: 2.3.2020, 04:30:00
</p>
</div></div>
</div>
<div class="col-sm-12 col-md-6 col-lg-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="img/2.jpg" alt="Burgerkin">
<div class="card-body">
<h5 class="card-title">Burgerkin fastfood</h5>
<p class="card-text">City: Switzerlan Zip-Code 4334
<br>
</p><p>
<label>Phone, Web</label>
<br>
+66 443543512
<br>
www.getfatter.com
</p><p>Created: 2.3.2011, 04:30:00
</p>
</div></div>
</div>
<div class="col-sm-12 col-md-6 col-lg-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="img/3.jpg" alt="Eating Burger">
<div class="card-body">
<h5 class="card-title">Eating Burger</h5>
<p class="card-text">City: burgertown Zip-Code 4234
<br>
<label> Event Infos </label>
</p><ul>
<li>14. June. 2100</li>
<li>3 o clock</li>
<li>56</li>
<li>www.event.com</li>
</ul>
<p>Created: 1.2.2019, 03:25:00
</p>
</div></div>
</div>
<div class="col-sm-12 col-md-6 col-lg-3">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="img/3.jpg" alt="Sky diving">
<div class="card-body">
<h5 class="card-title">Sky diving</h5>
<p class="card-text">City: skytown Zip-Code 4666
<br>
<label> Event Infos </label>
</p><ul>
<li>14. June. 2000</li>
<li>5 o clock</li>
<li>560</li>
<li>www.eve222nt.com</li>
</ul>
<p>Created: 1.2.2011, 03:25:00
</p>
</div></div>
</div>
</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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
The d-sm-none is functioning correctly. You have it on the img tag and therefore in the breakpoints referenced in bootstraps documentation here you can see the sm breakpoint is actually for landscape phones and as such will only hide the img from 576px to 768px. If you were trying to target this on a regular vertical orientation on a phone than you can replace where you have d-sm-none d-md-block and replace it with .d-none .d-sm-block like so:
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-3 ">
<div class="card">
<img class="card-img-top d-none d-sm-block" src="img/1.jpg" alt="Palme">
<div class="card-body">
<h5 class="card-title">Palme</h5>
<p class="card-text">
City: Dubai Zip-Code 333
<br>
</p>
<p>HIDE ME</p>
<p>
Created: 1.2.2011, 05:40:00
</p>
</div>
</div>
</div>
</div>
You can see this here in dev tools when i set it to 576px and you see the img is visible with the PALME section saying HIDE ME which i added to mark the card you have the img you want to hide on.
And then here when we go to 575px you can see it disappear like its supposed to:
As far as your column div problem I am not sure I understand what you are asking but I do see what is wrong with your code. In bootstrap you must have the class container which you do and then inside goes a row (on each row there are 12 columns). Once a row is filled up you must create another row to put your content into which again can hold up to 12 columns.
How you have the code currently is 1 row inside of a container and then your cards which all have col 12 on it. Below is a demo of what I assume you were trying to do which is to have the cards span the whole screen from a small screen up till when you want them to take up half the screen which I see from you putting col-md-6.
I did the demo by removing the flex classes and also by removing the width of the card that you had set as 18rem. It now spans the whole screen as I also added in col-xs-12 to be full screen on vertical orientation phones since the lowest you had on your cards was col-sm-12 which as I said above only is the width of landscape orientation for phones.
Demo:https://jsfiddle.net/atcvmqLx/
Explanation: Youll see on there that even though the images arent attached that the cards themselves expand to be full screen until it hits the col-md-6 breakpoint # 768px.
Let me know if you have any questions!

How to get two bootstrap cards side by side to act responsive using a media query?

Media Query for bootstrap 4, unresponsive. I have two cards side by side in a section. When I get down to 576px I want to be able to display the cards side by side.
I've tried setting the parent containers to float: none and float: left. Also tried incorporating display: block on the Query.
<section id="recipeList">
<div class="recipeName"></div>
<div class="row">
<div class="col-sm-6">
<div class="card image-container">
<img class="card-img-top" src="./assets/images/Dish.svg"
alt="places restaurants png">
<div class="card-body ingredients" id="foodImage">
<p class="card-text"></p>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="card ingredients-card">
<img class="card-img-top"
src="./assets/images/Contents.svg" alt="places restaurants png">
<div class="card-body overflow-auto">
<h2 class="card-title"></h2>
<p class="card-text overflow-auto" id="facts"></p>
</div>
</div>
</div>
</div>
</section>
I would love for the two bootstrap cards to be responsive rather than just squeezing downwards