Boostrap 5, How can I add multiple elements in a column - html

I have this Bootstrap 5 code that shows some images like in this link in a grid, the images are affected by a few CSS code.
#projects img {
width: 100%;
height: 100%;
}
<section id="projects">
<div class="container text-center">
<h4 class="fw-bold mb-3 border-bottom border-3">Mis Proyectos</h4>
<div class="row g-4">
<div class="col-md-6">
<img
src="./images/cardcomponent.png"
alt="card component"
class="rounded-3"
>
</div>
<div class="col-md-6">
<img
src="./images/yardsale.png"
alt="yard sale"
class="rounded-3"
>
</div>
<div class="col-md-6">
<img
src="./images/qrcard.png"
alt="web developer"
class="qr card"
>
</div>
<div class="col-md-6">
<img
src="./images/cardcomponent.png"
alt="web developer"
class="rounded-3"
>
</div>
</div>
</div>
</section>
I want to add a title <h6></h6> to each image.
<div class="col-md-6">
<h6>Title</h6>
<img
src="./images/cardcomponent.png"
alt="card component"
class="rounded-3"
>
</div>
But for some reason, the title isn't included or recognized in the column, showing something like this, while this is similar to what i want to do.

#Emiliano Acevedo, Thanks to draw attention to the bug of bootstrap, I am sure the team must get those alignment issue fixed for row column.
Here's quick fix for the issue. <div class="row g-4 align-items-end"> You can fix the issue with adding item alignment in the row element.
I hope this helps.
CodePen Example

Div with text-center class makes all the text align at the center inside of it. But since you just want your <h4> tag align at the center only, just remove the text-center class from that div and add it to the <h4> tag. Once you do that, your <h6> tag inside of your column will work as you intended. Like:
#projects img {
width: 100%;
height: 100%;
}
<section id="projects">
<div class="container">
<h4 class="text-center fw-bold mb-3 border-bottom border-3">Mis Proyectos</h4>
<div class="row g-4">
<div class="col-md-6">
<h6>Title 1</h6>
<img src="./images/cardcomponent.png" alt="card component" class="rounded-3">
</div>
<div class="col-md-6">
<h6>Title 2</h6>
<img src="./images/yardsale.png" alt="yard sale" class="rounded-3">
</div>
<div class="col-md-6">
<h6>Title 3</h6>
<img src="./images/qrcard.png" alt="web developer" class="qr card">
</div>
<div class="col-md-6">
<h6>Title 4</h6>
<img src="./images/cardcomponent.png" alt="web developer" class="rounded-3">
</div>
</div>
</div>
</section>

Related

Bootstrap 5: div different align for desktop and mobile

I have a bootstrap 5 website template where I want to display divs in these order:
DESKTOP:
1st line of divs: image, text
2nd line of divs: text, image
MOBILE:
1st line of divs: image, text
2nd line of divs: image, text
Simply said, there should be different order/alignment of every second line of divs for mobile devices. Could you please help me how to achieve it? I've tried to use several bootstrap classes, or even hardcode float left and right. Nothing helped. Here's my code:
<!-- Img1 -->
<div class="row" data-aos="fade-up" data-aos-delay="100">
<div class="col-md-6">
<img src="assets/img/portfolio/img1.jpg" class="rounded mx-auto d-block" alt="">
</div>
<div class="col-md-6 text-center align-items-center mob_padding_T20" style="color: #FF0000;">
Copy1
</div>
</div>
<!-- Img2 -->
<div class="row" data-aos="fade-up" data-aos-delay="100" style="padding: 20px 0 0;">
<div class="col-md-6" style="float:right;">
<img src="assets/img/portfolio/img2.jpg" class="rounded mx-auto d-block" alt="">
</div>
<div class="col-md-6 text-center align-items-center mob_padding_T20" style="float:left; color: #FF0000;">
Copy2
</div>
</div>
Thank you.
SUGGESTION: Avoid using inline CSS (style:: float:left or float:right) because managing responsive devices is quite challenging.
Because it affects SEO, please remove the second row if you don't require it and use the direct col-sm/col-md/col class instead.
Use col-6 in bootstrap 5 if you want to display text and an image on a single line.
Review it here:  run code snippe .
In case you have any other queries, kindly let me know.
Please review it codepen URL. I have provided you both examples in codepen.
Please let me know if you have any further question.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="text-center">
<div class="row" data-aos="fade-up" data-aos-delay="100">
<div class="col-lg-6 p-3 order-sm-1 order-md-1 bg-primary">
<img src="https://raw.githubusercontent.com/MSRAJAWAT298/msrajawat298/main/images/background-images/msrajawat298_bg-min.png" width="75px" cla ss="rounded mx-auto d-block" alt="">
</div>
<div class="col-lg-6 p-3 text-center align-items-center mob_padding_T20 order-sm-2 order-md-2 float-end bg-info" style="color:red">Copy1</div>
<div class="col-lg-6 p-3 order-sm-4 order-md-4 bg-danger">
<img src="https://raw.githubusercontent.com/MSRAJAWAT298/msrajawat298/main/images/background-images/msrajawat298_bg-min.png" width="75px" class="rounded mx-auto d-block " alt="">
</div>
<div class="col-lg-6 p-3 text-center align-items-center mob_padding_T20 order-sm-3 order-md-3 bg-dark" style="color:red">Copy2</div>
</div>
</body>
</html>
Here is the solution:
<div className="row" data-aos="fade-up" data-aos-delay="100">
<div className="col-6">
<img src="https://picsum.photos/200" className="rounded mx-auto d-block" alt="" />
</div>
<div className="col-6 text-center align-items-center mob_padding_T20" >
Copy1
</div>
</div>
<div className="row" data-aos="fade-up" data-aos-delay="100" style={{ padding: "20px 0 0" }}>
<div className="col-6 text-center align-items-center mob_padding_T20" style={{ float: "left", color: "#FF0000" }}>
Copy2
</div>
<div className="col-6" style={{ float: "right" }}>
<img src="https://picsum.photos/200" className="rounded mx-auto d-block" alt="" />
</div>
</div>
Your Problem:
The problem was that when you specified the column sized for your divs you used col-md-6 this will only be applied to medium-sized screens and larger. Here is a link explaining bootstrap grid
Please try this one
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row data-aos="fade-up" data-aos-delay="100"">
<!-- Start Img 1 -->
<div class="col-sm-6">
<img src="assets/img/portfolio/img1.jpg" class="rounded mx-auto d-block" alt=""> IMG 1
</div>
<div class="col-sm-6">
Text 1
</div>
<!-- End Img 1 -->
<!-- Start Img 2 -->
<div class="col-sm-6">
<img src="assets/img/portfolio/img1.jpg" class="rounded mx-auto d-block" alt=""> IMG 2
</div>
<div class="col-sm-6">
Text 2
</div>
<!-- End Img 2 -->
</div>
</div>
<div class="container mt-5">
<div class="row data-aos="fade-up" data-aos-delay="100"">
<!-- Start Img 1 -->
<div class="col-6">
<img src="assets/img/portfolio/img1.jpg" class="rounded mx-auto d-block" alt=""> IMG 1
</div>
<div class="col-6">
Text 1
</div>
<!-- End Img 1 -->
<!-- Start Img 2 -->
<div class="col-6">
<img src="assets/img/portfolio/img1.jpg" class="rounded mx-auto d-block" alt=""> IMG 2
</div>
<div class="col-6">
Text 2
</div>
<!-- End Img 2 -->
</div>
</div>
SUGGESTION: Please don't use inline CSS (style:: float:left or float:right) because it's very difficult to manage responsive devices.
If you don't need the second row then please remove it and use the direct col-sm/col-md/col class because it affects in SEO.
If you display text + img in single line then you can use col-6 in bootstrap 5.
Please review it codepen URL. I have provided you both examples in codepen.
Please let me know if you have any further question.

Layering images with Bootstrap

I'm attempting to create the following (mockup) with Bootstrap
https://i.stack.imgur.com/vkgBu.jpg
The idea is that each of those squares is an image and I'll have Javascript control when they appear/disappear when the background image is clicked. What I'm having trouble with is getting the little squares to stack on top like I have it. I've done research here, tried a bunch of things on my own, but get close but no cigar.
This is the closest I've come (testing with 1 row first).
https://i.stack.imgur.com/mqTc1.jpg
The squares line up next to eachother pretty well however they aren't showing within the same grid column/row correctly.
Relevant HTML:
<div class="container-fluid">
<div class="row-centered">
<div class="row row-centered">
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img" onclick="StockCount();" />
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
</div>
</div>
<div class="row row-centered">
<div class="col- col-centered">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-">
<img src="resources/images/grey.jpg" class="character-img">
</div>
</div>
</div>
CSS:
.character-img{
position: relative;
z-index: 10;
}
.stock-img{
position: relative;
z-index: 20;
}
EDIT:
Updated version yields slightly better results? Included adding the blue boxes to the second row.
HTML
<div class="container-fluid">
<div class="row-centered">
<div class="row row-centered">
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img" onclick="StockCount();" />
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
</div>
</div>
<div class="row-centered">
<div class="row row-centered">
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img" onclick="StockCount();" />
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
<img src="resources/images/blue.jpg" class="stock-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
<div class="col-2">
<img src="resources/images/grey.jpg" class="character-img">
</div>
</div>
</div>
</div>
CSS
.character-img{
position: relative;
z-index: 10;
}
.stock-img{
position: relative;
z-index: 20;
}
Result: https://imgur.com/vd3IZsP
Based on the images you've shown us to depict your desired outcome, I think Bootstrap is a smart choice. Much of what you want to do in your layout can be achieved using native classes. But it's also pretty apparent that you lack a fundamental understanding of the Bootstrap Framework, and I would strongly encourage you to revisit their documentation.
A full example of the structure you're trying to achieve can be viewed at: https://www.bootply.com/SwuSKmlKWe
But let's look at the HTML of a single 'box' item:
<div class="col-6 col-md-4 col-lg-3 mb-4">
<div class="h-100 bg-secondary p-4">
<img src="//placehold.it/200x200" class="rounded-circle img-fluid d-block mx-auto" />
<div class="row mt-4">
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
<div class="col-3 text-center mb-2"><img src="//placehold.it/50x50" class="img-fluid" /></div>
</div>
</div>
</div>
The first <div> element is a column wrapper that supplies information on the breakpoints that will affect the display. From left to right it is written with the smallest breakpoint (col-*) to the largest. If we were to translate this to something readable it might read:
At the smallest breakpoint use 6/12 columns. At the medium breakpoint
begin using 4/12 columns, and at the large breakpoint use 3/12
columns.
Note: col-sm-* exists but was not used here. Because a 'small' breakpoint was not used Bootstrap will default to the next smallest (in this case col-6
Inside the column wrapper we have another wrapper with several classes:
.h-100 helps us create equal-height items regardless of whether the
content dictates a smaller height.
.bg-secondary is a utility class that provides the dark background color
.p-4 is a utility class that provides padding on both the X and Y margins (4 indicates the 'level' of padding and can be as low as 0 and as high as 5)
At this point we start seeing the actual content. Your primary image has several classes that affect its shape and its position:
.rounded-circle is an image thumbnail class that forces a circular border radius.
.img-fluid is a responsive class that helps the image size up and down proportional to the element in which it resides.
.d-block transforms the img into a block-level element
.mx-auto sets the X margins to 'auto' which, in combination with d-block, centers the image.
From here we reach the 8 button/images referenced in your original post. The Bootstrap Grid is the perfect solution for this sort of organization, but since we're already working with content inside a Grid Element we need to create a new .row.
Inside the new .row we have a single breakpoint column (.col-3) with a couple of utility classes: text-center centers the content and mb-2 sets the bottom margin in a similar way that the p-* utility class alters padding.

HTML CSS Fit image in fixed size

I know this topic has been featured a couple times but none of the answers given to those questions fixed my problem. That's why I'm asking it here. I have a site where I have a standard Bootstrap card with an Image above it, But It looks like this
How can I make the images so they automatically fit in the middle and don't look like they're stretched out?
This is my HTML code (There is no CSS attached, Its standard bootstrap)
<div class="row mt-4">
<div class="col-md-4">
<div class="card mb-4">
<div class="card-image">
<img class="card-img-top" src="images/example.png">
</div>
<div class="card-header">
<h6 class="m-0">Some text</h6>
</div>
<div class="card-body" style="height: 130px">
<p class="m-0">Some text</p>
</div>
<div class="card-footer">
Footer
</div>
</div>
</div>
</div>
The best thing to do is to use the image as a background in conjunction with background cover. This will allow the photo to grow and resize without looking wonky.
.card-header {
height:200px;
background-image:url('http://www.fillmurray.com/g/500/200');
background-size:cover;
background-position:center center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row mt-4">
<div class="col-md-4">
<div class="card mb-4">
<div class="card-image">
<img class="card-img-top sr-only" src="images/example.png">
</div>
<div class="card-header">
<h6 class="m-0">Some text</h6>
</div>
<div class="card-body" style="height: 130px">
<p class="m-0">Some text</p>
</div>
<div class="card-footer">
Footer
</div>
</div>
</div>
</div>
add this to your 'img' styling:
img { height: 200px;width:100%;object-fit: cover; }

Bootstrap 4 responsive image with set height

If I have a card inside a column, that needs to have an image with a set height, how do I make it not stretched out, as it currently is in my code example? And without having to have a set width on the image because I want it to be replaceable.
What am I missing and/or doing wrong? Thanks!
.card-block {
padding: 20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<div class="col-md-6">
<div class="card">
<img class="card-img-top" src="https://placeimg.com/640/480/animals" alt="Card image cap" height="200">
<div class="card-block text-center">
<p class="card-text">Text goes here</p>
<p class="card-subtitle">More text goes here</p>
</div>
</div>
</div>
Your best solution is to instead of placing the image directly onto the page you can create an empty div and then set the background image to be https://placeimg.com/640/480/animals then set its size to cover and then it will cover the height and width of the div you have specified
so for example...
css
card-image-container{
background: url('https://placeimg.com/640/480/animals') 50% no-repeat;
background-size: cover;
}
Making an image responsive in Bootstrap 4 is very easy:
You just add the img-fluid class to the image.
HOWEVER... responsiveness comes at a cost and that is: You cannot set a minimum height for a responsive image (because that in itself would make the image non-responsive).
Luckily, there are a few workarounds for managing the size of your card image while still keeping it responsive. You can do that by manipulating the horizontal padding (use the px-* class and swap the * for a number between 0 and 5) as well as increasing or decreasing the column width.
Take a look at the 3 examples here:
<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="container">
<div class="row mt-3">
<div class="col-md-6">
<div class="card">
<img class="card-img-top img-fluid" src="https://placeimg.com/640/480/animals" alt="Card image cap">
<div class="card-block text-center">
<p class="card-text">Text goes here</p>
<p class="card-subtitle">More text goes here</p>
</div>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6 px-5">
<div class="card">
<img class="card-img-top img-fluid" src="https://placeimg.com/640/480/animals" alt="Card image cap">
<div class="card-block text-center">
<p class="card-text">Column with px-5 class</p>
<p class="card-subtitle">to make the image smaller</p>
</div>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-5 px-5">
<div class="card">
<img class="card-img-top img-fluid" src="https://placeimg.com/640/480/animals" alt="Card image cap">
<div class="card-block text-center">
<p class="card-text">Smaller column AND px-5 class</p>
<p class="card-subtitle">to make the image even smaller!</p>
</div>
</div>
</div>
</div>
</div>
Usually, when using Bootstrap you should use div with the class of container or container-fluid and after that div with the class of row. This worked for me
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="card">
<img class="card-img-top" src="https://placeimg.com/640/480/animals"
alt="Card image cap" height="200">
<div class="card-body text-center">
<p class="card-text">Text goes here</p>
<p class="card-subtitle">More text goes here</p>
</div>
</div>
</div>
</div>
</div>
Cards know to be pain in the.. But I hope this helps.

Align image center and retain aspect ratio

I have a set of images that i want to create a set of buttons with titles from. My attempt so far is below....
Issue: the images seem to be skewed to reduce their width but not their height or indeed not stretch to their max size possible.
How can i keep the aspect ratio and center the image for 3 images each in their own column and of different sizes.
<div class="row row-bottomspace">
<div class="col-md-8 col-md-offset-2">
<!--carouselstart-->
<div class="feature">
<div class="col-md-4 text-center">
<div class="row text-center">
<img src="img/home/contactus.png" alt="residential image not available" class="img-responsive">
<h3 class="sitetext">Contact us</h3>
</div>
</div>
<div class="col-md-4">
<div>
<img src="img/home/gallery.png" alt="residential image not available" class="img-responsive">
<h3 class="sitetext">Our Gallery</h3>
</div>
</div>
<div class="col-md-4">
<div>
<img src="img/home/facebook.png" alt="residential image not available" class="img-responsive">
<h3 class="sitetext">Visit our Facebook page</h3>
</div>
</div>
</div>
</div>
</div>
CSS
.row-topspace {
padding-top: 2%;
}
.row-bottomspace {
padding-bottom: 2%;
.sitetext {
color: white;
font-family: 'Cooper Black';
font-size: 17px;
}
got a adequate solution, not perfect but simple. I set the parent div of the image to class="thumbnail" which by result centers the image" without distorting it. If anybody has a css only method then il set their answer to correct....
<div class="row row-bottomspace">
<div class="col-md-8 col-md-offset-2">
<!--carouselstart-->
<div class="feature">
<div class="col-md-4 text-center sitetext">
<div class="thumbnail siteImageButton">
<img src="img/home/contactus.png" alt="residential image not available" class="img-responsive">
<h3>Contact us</h3>
</div>
</div>
<div class="col-md-4">
<div class="thumbnail siteImageButton">
<img src="img/home/gallery.png" alt="residential image not available" class="img-responsive">
<h3>Our Gallery</h3>
</div>
</div>
<div class="col-md-4">
<div class="thumbnail siteImageButton">
<img src="img/home/facebook.png" alt="residential image not available" class="img-responsive">
<h3>Visit our Facebook page</h3>
</div>
</div>
</div>
</div>
</div>