Bootstrap 4 link on card image with overlay - css

I am struggling to get the link on the image itself to work. So far I could only manage to get the link on the text overlay at the bottom over the image, but not on the rest of the image. Every time I try something it breaks the layout of the card and the overlay does not work anymore. Any idea?
.bg-semitransparent { background-color: rgba(60,60,60,0.6); }
<div class="card text-white">
<img src="...image" alt="Angel #1" class="img-fluid" style="" />
<div class="card-img-overlay h-100 d-flex flex-column justify-content-end">
<div class="card-text border-0 bg-semitransparent text-center">
Anchor Text
</div>
</div>
</div>

If you want, you can try like this: thats a simple trick but solve your issue
<div class="card text-white">
<img src="https://dummyimage.com/350x450/" alt="Angel #1" class="img-fluid" style="" />
<a href="...link">
<div class="card-img-overlay h-100 d-flex flex-column justify-content-end">
<div class="card-text border-0 bg-semitransparent text-center">
Anchor Text
</div>
</div>
</a>
</div>

Solution 1
By changing HTML code structure
.bg-semitransparent { background-color: rgba(60,60,60,0.6); }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<a href="...link"><div class="card text-white">
<img src="https://dummyimage.com/450x450/" alt="Angel #1" class="img-fluid" style="" />
<div class="card-img-overlay h-100 d-flex flex-column justify-content-end">
<div class="card-text border-0 bg-semitransparent text-center">
Anchor Text
</div>
</div>
</div>
</a>
Solution: 2
With JQuery
$(".card").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
.bg-semitransparent { background-color: rgba(60,60,60,0.6); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card text-white">
<img src="https://dummyimage.com/450x450/" alt="Angel #1" class="img-fluid" style="" />
<div class="card-img-overlay h-100 d-flex flex-column justify-content-end">
<div class="card-text border-0 bg-semitransparent text-center">
Anchor Text
</div>
</div>
</div>

Related

How to make a button consistent on all size of display?

Image attached is the button from display of two different sizes (24inch and 13inch), button on 24inch display appears normal whereas add to cart button on 13inch display is broken.13inch display button 24 inch display button
<div class="row my-5" id="3">
<span id="lblArtworkId" style="display: none;">3</span>
<div class="col-2">
<div class="d-flex justify-content-center">
<a href="./">
<img src="upload/artworks/Artwork3.jpg" alt="" class="card-img-top" style="max-height: 8rem;">
</a>
</div>
</div>
<div class="col-6">
<div class="h-100 d-flex flex-column justify-content-between">
<span>
<a href="./" class="text-decoration-none text-muted fs-4">
Construction in Red, Blue & Yellow Drawing
</a>
</span>
<hr>
<p class="m-0">Artist: Karin White</p>
</div>
</div>
<div class="col-2">
<div class="h-100 d-flex align-items-center justify-content-center">
<span class="fw-bold">$ 1120.00</span>
</div>
</div>
<div class="col-2">
<div class="h-100 d-flex align-items-center justify-content-between">
<button id="btnRemove" class="btn btn-outline-danger">Remove</button>
<button id="btnCart" class="btn btn-primary">Add to Cart</button>
</div>
</div>
</div>
Welcome to stackoverflow.
You can use cols with a breakpoint prefix to adjust the layout. (e.g. col-xxl-2 col-3). Read Bootstrap Breakpoints and Grid options for more information. Also, since Bootstrap is sensitive to pixels, it is much better to consider pixels instead of inches.
Example:
<div class="container">
<div class="row my-5" id="3">
<span id="lblArtworkId" style="display: none;">3</span>
<div class="col-xxl-2 col-3">
<div class="d-flex justify-content-center">
<a href="./">
<img src="upload/artworks/Artwork3.jpg" alt="" class="card-img-top" style="max-height: 8rem;">
</a>
</div>
</div>
<div class="col-xxl-6 col-9">
<div class="h-100 d-flex flex-column justify-content-between">
<span>
<a href="./" class="text-decoration-none text-muted fs-4">
Construction in Red, Blue & Yellow Drawing
</a>
</span>
<hr>
<p class="m-0">Artist: Karin White</p>
</div>
</div>
<div class="col-xxl-2 col-4">
<div class="h-100 d-flex align-items-center justify-content-center">
<span class="fw-bold">$ 1120.00</span>
</div>
</div>
<div class="col-xxl-2 col-8">
<div class="h-100 d-flex align-items-center justify-content-between">
<button id="btnRemove" class="btn btn-outline-danger w-50 mx-1">Remove</button>
<button id="btnCart" class="btn btn-primary w-50 mx-1">Add to Cart</button>
</div>
</div>
</div>
</div>
Edit
Don't forget to include the required meta tags:
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

How can I increase the width and spacing of bootstrap cards

I've made bootstrap cards that appear on two different pages. One on the landing page which I want to increase the spacing and one on the books page which I want to increase the width of the cards. Here's the code that I've written and used in both pages. The only thing that I've changed is on the 6th line from '...col-lg-3' to '...col-lg-2'. If anyone can assist me in adding the spacing and increasing the with of the card, help me if you can. d-flex justify-content-evenly doesn't work when i use col-lg-3.
<section id="about" class="ts-block" >
<div id="cards_landscape_wrap-2">
<div class="container " style="width:70%">
<div class="row d-flex justify-content-around">
#foreach($products as $product)
<div class="col-xs-12 col-sm-6 col-md-3 col-lg-3">
<a href="{{route("products.show",$product->slug)}}">
<div class="card-flyer ">
<div class="text-box">
<div class="image-box">
<img src="{{asset($product->image)}}" alt="{{$product->title}}" class="img-fluid rounded mx-auto d-block" width="100%"/>
</div>
<div class="card-footer">
<i class="text-muted"><strike>Ksh.{{$product->old_price}}</strike></i>
<span class="text-primary font-weight-bold">Ksh.{{$product->price}}</span><br>
<span class="mybuttonoverlap btn btn-primary">View</span>
</div>
</div>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
</section>
Try including d-flex justify-content-around to a direct parent containing the cards instead of
<div class="row d-flex justify-content-around">. And add margin to the card.
<div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 d-flex justify-content-around">
<a href="{{route("products.show",$product->slug)}}">
<div class="card-flyer m-2">
<div class="text-box">
<div class="image-box">
<img src="{{asset($product->image)}}" alt="{{$product->title}}" class="img-fluid rounded mx-auto d-block" width="100%"/>
</div>
<div class="card-footer">
<i class="text-muted"><strike>Ksh.{{$product->old_price}}</strike></i>
<span class="text-primary font-weight-bold">Ksh.{{$product->price}}</span><br>
<span class="mybuttonoverlap btn btn-primary">View</span>
</div>
</div>
</div>
</a>
</div

Bootstrap button is not working, Tried cursor pointer and (click), still didn't work

I'm new to Angular and Bootstrap, but I found a code from a website but when I put it in my project, the button did not work.
Any help would be appreciated.
<div class="container">
<div class="row">
<div class="col-12 col-sm-8 col-md-6 col-lg-4" *ngFor="let P of products">
<div class="card" *ngIf="P.Category == selectedCategory">
<img class="card-img" src="{{P.Img}}" alt="Vans">
<div class="card-img-overlay d-flex justify-content-end">
</div>
<div class="card-body">
<h4 class="card-title">{{P.Name}}</h4>
<h6 class="card-subtitle mb-2 text-muted">Serial No. {{P.SerialNumber}}</h6>
<p class="card-text">{{P.Description}}</p>
<div class="buy d-flex justify-content-between align-items-center">
<div class="price text-success"><h5 class="mt-4">${{P.Price}}</h5></div>
<button type="button" (click)="addToCart(P)" class="btn btn-danger mt-3 curs"> Add to Cart</button>
</div>
</div>
</div>
</div>
</div>
</div>
and this is the css code
body {
padding: 2rem 0rem;
}
.like {
font-size: 1.5rem;
}
Thanks in advance
did you insert the bootstrap CDN link for CSS? if not then paste this under your css file inside the head section
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">

CSS class align-self-end not working

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-md-3">
<div class="card bg-dark text-white">
<img class="card-img" src="http://via.placeholder.com/300x340" alt="Card image">
<div class="card-img-overlay">
<h5 class="card-title">Title</h5>
<h3 class="card-text font-weight-bold"><span class="mr-auto">Some other title here</span></h3>
<div class="align-self-end">Text I want at bottom</div>
</div>
</div>
</div>
I am unable to get the text in the last div by applying this class align-self-end to the bottom of the image. I understand that flex has been applied to the .card-img-overlay div with the direction of column then why am I unable to get the div to the bottom of the image?
The card-img-overlay div isn't display:flex.
You can do this by adding the d-flex flex-column classes to it, and use mt-auto to push the text to the bottom.
https://www.codeply.com/go/SRdCTDDoRr
<div class="card bg-dark text-white">
<img class="card-img" src="http://via.placeholder.com/300x340" alt="Card image">
<div class="card-img-overlay d-flex flex-column">
<h5 class="card-title">Title</h5>
<h3 class="card-text font-weight-bold"><span class="mr-auto">Some other title here</span></h3>
<div class="mt-auto">Text I want at bottom</div>
</div>
</div>

Vertical alignment in bootstrap 4

I'm trying to have a center text over image. I've used mx-auto to Horizontal centering my overlay text and use align-middle for a Vertical alignment. But the vertical alignment didn't work. Does somebody know why ?
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card ">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex">
<div class="mx-auto">
<h4 class="card-title align-middle">Animal Farm</h4>
<h6 class="text align-middle">George Orwell</h6>
<p class="card-text align-middle">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>
</div>
</div>
Just use my-auto for vertical center...
<div class="card">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex">
<div class="my-auto mx-auto text-center">
<h4 class="card-title">Animal Farm</h4>
<h6 class="text">George Orwell</h6>
<p class="card-text">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>
</div>
</div>
http://codeply.com/go/ZQM4ANFcXC
align-middle will work on display: table or display: inline elements.
Also see this similar question
You don't need .mx-auto. You can just use .justify-content-center, .align-items-center and .flex-column on the parent. https://v4-alpha.getbootstrap.com/utilities/flexbox/
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="card ">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex justify-content-center align-items-center flex-column">
<h4 class="card-title align-middle">Animal Farm</h4>
<h6 class="text align-middle">George Orwell</h6>
<p class="card-text align-middle">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>
</div>
Try it with this code, here:
<div class="card ">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex">
<div class="mx-auto" style="margin-top: auto;margin-bottom: auto;">
<h4 class="card-title">Animal Farm</h4>
<h6 class="text">George Orwell</h6>
<p class="card-text">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>
</div>
</div>
Basically just added style="margin-top: auto;margin-bottom: auto;" to the div with mx-auto class.

Resources