How will I make perfect circle image using card in bootstrap 5? - html

In my project, I used bootstrap v.5 cards. I wanted to make the image a perfect circle and I used bootstrap class="rounded-circle" but the output image appears oval in shape. here is my code,
<img src={elonMusk} class="card-img-top rounded-circle" alt={elonMusk} />
How do I fix this problem ?

You can use class ratio ratio-1x1 which is pre-defined classes in Bootstrap-5 and if your image is not in Square format then you need to write single line of css code like object-fit: cover.
.img-cover{
object-fit: cover;
object-position: center;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container py-4">
<div class="row g-4 row-cols-1 row-cols-sm-2 row-cols-md-3">
<div class="col">
<div class="card">
<div class="ratio ratio-1x1 rounded-circle overflow-hidden">
<img src="https://i.stack.imgur.com/fcbpv.jpg?s=256&g=1" class="card-img-top img-cover" alt="Raeesh">
</div>
<div class="card-body">
<h5 class="card-title">Raeesh Alam</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
View More
</div>
</div>
</div>
</div>
</div>

as far as I know, rounded-circle class is just border-radius:50%. So if your image dimensions are not equal (like 50 x 50). you won't get perfect circle.
You can give your image some width and height to fix it's dimensions.
like :
img:{
width:50px;
height:50px
}
or
<img id="myImage" src=".." >
#myImage{
width:50px;
height:50px
}

Create your css code and remove bootstrap rounded class.
<img src="elonMusk.png" class="card-img-top" alt="elonMusk">
and in css file
img {
border-radius: 50%;
}
Use the border-radius property to add rounded corners to an image. 50% will make the image circular:

In .css:-
img {
border-radius: 50%;
}
For more follow this link

put equal height and width...
<img src={elonMusk} class="card-img-top rounded-circle" alt={elonMusk}
style={{height:"200px", width:"200px"}} />

Just use card component from Bootstrap and update little code for Circle image.
Bootstrap Card Component Documentation: https://getbootstrap.com/docs/5.1/components/card/
Already updated below code snippet, i hope it'll help you out. Thank You
.card .card-img-top {
height: 140px;
width: 140px;
}
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="card" style="width: 18rem;">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/640px-Image_created_with_a_mobile_phone.png" class="rounded-circle card-img-top mt-3 mx-auto img-thumbnail">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>

Related

Bootstrap centering div image and text

I need to make like a card, I've made it with a div, with an image and a text, and I don't know how to center them vertically. This is how my card looks:
and this is how it should look:
This is the code of the div:
<div class="panel mx-auto col align-items-center">
<img class="atajo-content" :src="shortcut.img" height="83" />
<p class="pt-1 card-name text-wrap">{{ shortcut.name }}</p>
</div>
And I just couldn't center the icon with the font. I've tested it with almost all combinations of bootstrap and I cannot understand what happens.
Can you please check the below code? Hope it will work for you. If you want your content to be vertically center, then you need to give specific height to the parent .main-content. Here we have used flex utility classes to center the content as per your requirement.
Please refer to this link: https://jsfiddle.net/yudizsolutions/n39zby6g/
.main-content {
height: 100vh;
text-align: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="main-content d-flex align-items-center justify-content-center">
<div class="panel">
<img class="atajo-content" src="https://via.placeholder.com/100x50" height="83" />
<p class="pt-1 card-name text-wrap">{{ shortcut.name }}</p>
</div>
</div>

Place bootstrap cards in horizantal order

I have a simple page with a navbar + one main-body div + one footer.
What I am trying to achieve is the navbar and footer remain in that position no matter what, while only the main white space show these bootsrap cards, but in horizontal position max-4 per row. What I managed so far is:
<div="wrap>
<navbar boostrap>
</navbar>
<form>
<div class="card">
</div>
</form>
</div>
<footer>
</footer>
And applying this css for the wrap div and the footer:
WRAP:
.wrap {
min-height: 100%;
margin-bottom: -12vh;
padding-bottom: 12vh;
}
Footer:
.footer {
text-align: center;
height: 12vh;
width: 100%;
background-color: #393e46;
}
What I did after is adding a class to my form that encapsualtes the bootsrap card and in css doing this:
.form-events {
display: flex;
flex-wrap: wrap;
}
Is there a better practise or my solution works??
You can try to do as follows if you are using Bootstrap.
Keep the navbar and footer as it is. Only thing you will need to work on is the main whitespace part.
First create a <div></div> which has a class of container as such <div class="container"></div>
Then you can put another <div></div< element with a class of row, so that your cards will be placed in same row horizontally <div class="row"></div>
Next you can use the col classes that Bootstrap offers, to create a div element for each of your cards <div class="col"></div>. It is basically used for defining how many elements you will need in a single row and it makes your content responsive.
Be sure to check the documentation here if you need more details: Bootstrap Grid System
Finally you can put your cards. Just remember to wrap a single card inside each <div class="col"></div>
Below you can find the code example of how I did it. Run the snippet in full page to see the cards horizontally; otherwise, it will show then stacked on top of each other because of screen size
From my experience, I would suggest using container class for a div to put your elements in, especially card elements.
JSFiddle Demo Here
EDIT : If you have a problem where your cards stack on top of each other while resized, wrap all of them up in "card deck" class inside a <div> element
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
</head>
<body>
<div class="container mt-5">
<div class="row ">
<div class="card-deck">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Card link
Another link
</div>
</div>
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Card link
Another link
</div>
</div>
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Card link
Another link
</div>
</div>
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Card link
Another link
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You could add the position:fixed line to your ".footer" CSS class like so.
.footer {
position:fixed;
bottom:0;
left:0;
text-align: center;
height: 12vh;
width: 100%;
background-color: #393e46;
}
By doing this it will stay positioned at the bottom of the page even if a user scrolls down the page.

Can Bootstrap 4 Cards Have Tiled Background Images

I am trying to get a card to have a tiled background image. To get a single image background I use:
<div class="card img-fluid">
<img class="card-img-top" src="images/backcard-brushedmetal.jpg">
<div class="card-img-overlay">
<div class="card-body text-justify">
<h3 class="articleh3">xxxDescriptionxxx</h3>
<p>
<img src="images/xxxImagexxx.jpg" alt="xxxDescriptionxxx" height="100" width="80" style="margin-right: 10px;float: left" />
xxxSomeTextHerexxx
</p>
</div>
</div>
</div>
My problem is that I use cards of different sizes and right now my image size is governing the size of my card; I guess I want it the other way round (the card to govern the size of the displayed background). So I wondered if it was possible to tile a background image so it just fills the background and then it shouldn't matter what size the card or image is.
For all I know, this may not even be possible and if not, that's fine, I'll look for another way to do something like :)
Anyway, thanks for any advice :)
James
If the only purpose of using img element is as a background - use background-image CSS property which sets background image on an element.
Edit per comments:
Obviously the float part cause some "problems" on the given markup, BUT there is something more out there... Can't figure myself why this not working - so i remove BS4 (added 2 examples):
.card.img-fluid {
background: url(http://www.rocksquad.co.uk/images/backcard-brushedmetal.jpg) no-repeat;
background-position: center;
background-size: cover;
}
<div class="card img-fluid">
<div class="card-img-overlay">
<div class="card-body text-justify">
<h3 class="articleh3">xxxDescriptionxxx</h3>
<p>
<img src="http://www.rocksquad.co.uk/images/backcard-brushedmetal.jpg" alt="xxxDescriptionxxx" height="100" width="80" style="margin-right: 10px;" />
xxxSomeTextHerexxx<br>xxxSomeTextHerexxx
</p>
</div>
</div>
</div>
<hr>
<div class="card img-fluid">
<div class="card-img-overlay">
<div class="card-body text-justify">
<h3 class="articleh3">xxxDescriptionxxx</h3>
<p>
<img src="http://www.rocksquad.co.uk/images/backcard-brushedmetal.jpg" alt="xxxDescriptionxxx" height="100" width="80" style="margin-right: 10px;" />
xxxSomeTextHerexxx<br>xxxSomeTextHerexxx<br> xxxSomeTextHerexxx<br>xxxSomeTextHerexxx<br> xxxSomeTextHerexxx<br>xxxSomeTextHerexxx<br> xxxSomeTextHerexxx<br>
</p>
</div>
</div>
</div>
This question need some "bootstrap 4 expert" to answer on - clearly this is not me (:
Old (not working) answer:
In your case, since you want the image would cover the element, you can do something like that:
<style>
.card.img-fluid {
background: url(images/backcard-brushedmetal.jpg) no-repeat;
background-position: center;
background-size: cover;
}
</style>
<div class="card img-fluid">
<div class="card-img-overlay">
<div class="card-body text-justify">
<h3 class="articleh3">xxxDescriptionxxx</h3>
<p>
<img src="images/xxxImagexxx.jpg" alt="xxxDescriptionxxx" height="100" width="80" style="margin-right: 10px;float: left" />
xxxSomeTextHerexxx
</p>
</div>
</div>
</div>

bootstrap - make cards with images having same height

As you can see, i do have 3 bootstrap cards having 3 images with different heights. I want to make the images having same height compared to others (like the bigger one "takes the lead" and all the other one in the row adapting to this bigger one).
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="/images/produits/1" alt=""><hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
I already put the h-100 class in the card div but it only adapts the card's height, and I want the images to be adapted (in order to not have the blank after the text), how to do that pls?
Bootstrap class card-body has property flex: 1, it means, that this element also grows for available space.
You can add some special class, for example to row, or higher, where you use card, to modify that parameter. Something like that.
.special .card-body {
flex-grow: 0;
}
Also wrap link image with div which has class="flex-fill", it makes grow the link images.
Look into the snippet, I;ve added special class to row, and set different height to link images to illustrate the idea.
UPDATE. Made link images center
Play with flexboxes. For example: add to div which wraps <a> class d-flex it will make this div with display: flex. And then you can add to this div classes justify-content-center and align-items-center, but I prefer in your case to add class m-auto to <a>, it gives margin: auto, which make element centralized with flex parent. Look into snippet.
.special .card-body {
flex-grow: 0;
}
.img_mock1 {
padding: 50px;
background-color: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="row special">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="flex-fill d-flex">
<span class="img_mock1"></span>
</div>
<hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<div class="flex-fill d-flex">
<a href="/led/catalogue/produit/1" style="height:300px;" class="m-auto">
<img src="" alt="">
</a>
</div>
<hr>
<div class="card-body">
<h6 class="card-title">
slt la mif
</h6>
<small class="text-muted">Puissance : 10 à 100W</small>
</div>
</div>
</div>
</div>
Use the following CSS
.card-img-top {
width: 100%;
height: 15vw;
object-fit: cover;
}
The object-fit: cover; enables zoom instead of image stretching.
Give fix height suppose 150px to the wrapper of image(anchor tag). Then for the image tag use max-width: 100%, height: auto, display: block
give the image div a fixed height, use the height preference of the largest image to put a fix height on the dive of the image, this will allow the all to show the same way.

how to force images to show in portrait?

I have a web page that contain images in bootstrap card but some of them in landscape and some in portrait so how I force all images to show in portrait preferably using only css or bootstrap 4 or html5.
here is my code:
<div class="card mb-4"style="min-width: 18rem; max-width: 18rem;">
<img src="images/1.jpeg" alt="image" class="card-img-top img-responsive">
</div>
This is going to require some form of cropping using a div element. It is hard to tell exactly what you are after without seeing the website, but here is one potential solution using height:0; and a percentage padding-bottom to create a div with a fixed aspect ratio and then using a background image instead of an image.
If you wanted to still use an image you would either need to use Javascript or the new object-fit CSS property, but object-fit isn't supported by IE.
.portrait {
height:0;
padding-bottom:150%;
background-repeat: no-repeat;
background-size:cover;
background-position: center center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="card mb-4"style="min-width: 18rem; max-width: 18rem;">
<div class="portrait" style="background-image:url(https://via.placeholder.com/200x300);"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>
<div class="card mb-4"style="min-width: 18rem; max-width: 18rem;">
<div class="portrait" style="background-image:url(https://via.placeholder.com/300x200);"></div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>