I am using Bootstrap 4+ to create a card with collapsable card-body, triggered by a link in the card-head.
<div class="card text-left mb-3 mt-3">
<div class="card-head p-3">
<h4>How do we get started?</h4>
<a class="float-right" data-toggle="collapse" href="#collapse1" aria-expanded="false" aria-controls="collapse1">
More
</a>
</div>
<div class="collapse" id="collapse1">
<div class="card card-body">
<p>Content</p>
</div>
</div>
</div>
The tag showing "more" is not lining up vertically inline with the heading element next to it.
It looks like this in chrome:
How can I get the heading and "More" link (vertically) inline with each other? thereby reducing the wasted vertical height.
Just add d-flex justify-content-between classes with your card-head class will resolve your issue. I hope it'll help you out. Thanks
Bootstrap 4 Flexbox - Documentation
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="card text-left mb-3 mt-3">
<div class="card-head p-3 d-flex justify-content-between">
<h4>How do we get started?</h4>
<a class="float-right" data-toggle="collapse" href="#collapse1" aria-expanded="false" aria-controls="collapse1">
More
</a>
</div>
<div class="collapse" id="collapse1">
<div class="card card-body">
<p>Content</p>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<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>
You can use d-flex align-items-center which is available in Bootstrap 4+ in your card-head and also you don't need to use float-right instead you can use ml-auto. Something like this
<div class="card text-left mb-3 mt-3">
<div class="d-flex align-items-center card-head p-3">
<h4>How do we get started?</h4>
<a class="ml-auto" data-toggle="collapse" href="#collapse1" aria-expanded="false" aria-controls="collapse1">
More
</a>
</div>
<div class="collapse" id="collapse1">
<div class="card card-body">
<p>Content</p>
</div>
</div>
</div>
Related
I'm following this video tutorial https://www.youtube.com/watch?v=36jRXMsIFuA
I am using bootstrap 5.0.0 beta 1
My code is following:
<header>
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-12 col-12 bg-light">
<div class="btn-group">
<button class="btn border dropdown-toggle my-md-4 my-2" data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">AUD</button>
<div class="dropdown-menu">
USD
</div>
</div>
</div>
<div class="col-md-4 col-12 text-center bg-dark"></div>
<h2 class="my-md-3 site-title">Online Store</h2>
<div class="col-md-4 col-12 text-right bg-danger">
<p class="my-md-4 header-links">
Sign In
Create an Account
</p>
</div>
</div>
</div>
</header>
And it looks like this, and when I click the button there is no dropdown. Layout is messed up as well.
my website
Compared to tutorial:
tutorial picture
The tutorial's using Bootstrap 4.3.1, the 4th version rather than the newest 5.0. If you switch to v4, your dropdown would works. Since a major update of a framework (increase of the first version number) is always not compatible to below versions.
About the layout, there's a mistake in your code. You need to put your <h2> tag within the tag with a background color and a style, the above <div> tag.
So, change the lines below:
<div class="col-md-4 col-12 text-center bg-dark"></div>
<h2 class="my-md-3 site-title">Online Store</h2>
To:
<div class="col-md-4 col-12 text-center bg-dark">
<h2 class="my-md-3 site-title">Online Store</h2>
</div>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<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>
<header>
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-12 col-12 bg-light">
<div class="btn-group">
<button class="btn border dropdown-toggle my-md-4 my-2" data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">AUD</button>
<div class="dropdown-menu">
USD
</div>
</div>
</div>
<div class="col-md-4 col-12 text-center bg-dark">
<h2 class="my-md-3 site-title">Online Store</h2>
</div>
<div class="col-md-4 col-12 text-right bg-danger">
<p class="my-md-4 header-links">
Sign In
Create an Account
</p>
</div>
</div>
</div>
</header>
I was trying to align a part of the text to right and rest to left end of bootstraplist group, but everything always align to left,
I was trying to make text 'Drago' to left end, and the emoji to the left end of each row
<div class="container-fluid">
<div class="row d-flex justify-content-center">
<div class="col-lg-6 col-md-6 col-xs-12 col-sm-12">
<div class="list-group shadow">
{% for newsletter in newsletter_list %}
<a href="{{newsletter.slug}}" class="list-group-item list-group-item-action">
<span class="float-left">🐯</span>
<span class="float-right">Drago</span>
</a>
{% endfor %}
</div>
</div>
</div>
You can simply use d-flex and justify-content-between on your a elements. This will pull the text the to right and will be responsive in the modern browsers as well.
I will not recemend using float-right or float-left - Using flex classes is way much better for responsiveness.
Demo:
<!-- 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>
<div class="container-fluid">
<div class="row d-flex justify-content-center">
<div class="col-lg-6 col-md-6 col-xs-12 col-sm-12">
<div class="list-group shadow">
<a href="{{newsletter.slug}}" class="d-flex justify-content-between list-group-item list-group-item-action">
<span class="">🐯</span>
<span class="">Drago</span>
</a>
</div>
<div class="list-group shadow">
<a href="{{newsletter.slug}}" class="d-flex justify-content-between list-group-item list-group-item-action">
<span class="">🐯</span>.
<span class="">Drago</span>
</a>
</div>
</div>
</div>
Try using class="pull-left" instead of class="float-left"
<div class="container-fluid">
<div class="row d-flex justify-content-center">
<div class="col-lg-6 col-md-6 col-xs-12 col-sm-12">
<div class="list-group shadow">
{% for newsletter in newsletter_list %}
<a href="{{newsletter.slug}}" class="list-group-item list-group-item-action">
<span class="pull-left">🐯</span>
<span class="pull-right">Drago</span>
</a>
{% endfor %}
</div>
</div>
</div>
I am trying to put a button in a collapse (part of an accordion) that will go to a new page when clicked. But right now when I try to click the button, it instead just closes the collapse.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<div id="accordion">
<div class="card">
<div class="card-header p-1">
<a class="card-link stretched-link text-body" data-toggle="collapse" href="#collapseOne">
<div class="row no-gutters">
<div class="col-8 pl-1"><strong>Title </strong></div>
<div class="col-4 text-right"><span class="badge badge-danger p-1">High Priority</span></div>
</div>
<div class="row no-gutters">
<div class="col text-center">
<!-- Lock this to prevent word wrap from too mane names -->
<span class="badge badge-pill badge-primary mr-1">Name</span><span class="badge badge-pill badge-primary mr-1">Name</span><span class="badge badge-pill badge-warning mr-1">Name</span>
</div>
</div>
</a>
</div>
<div id="collapseOne" class="collapse" data-parent="#accordion">
<div class="card-body p-1">
<div class="row no-gutters">
<div class="col-9">
<div class="row m-0 p-0 ">
<div class="col-sm">
<div class="row">
<div class="col text-center">
<small><strong>Start:</small></strong><br>30 May 08:00
</div>
<div class="col text-center">
<small><strong>Late/Expire:</small></strong><br>30 Jun 16:00
</div>
</div>
</div>
<div class="col-sm">
<div class="row">
<div class="col text-center">
<small><strong>Category</small></strong><br>Catering
</div>
<div class="col text-center">
<small><strong>Reward:</small></strong><br>300
</div>
</div>
</div>
</div>
</div>
<!-- Here is the button I can't seem to click -->
<div class="col-3 p-1 align-self-center"><strong>View Chore</strong></div>
</div>
</div>
</div>
</div>
Oddly enough, when I put the snippet and run it on stack overflow, it works fine. But when I run it in my browser, clicking anywhere in the card-body hides the collapse element.
Thanks for the help
Seth
Never mind, I found it. The "stretched-link" in the card header is making the whole card, including the body a stretched-link. For some reason the stack overflow snippet doesn't do that but the Chrome browser does.
When in mobile view, cards are not centring as expected.
I have tried using d-flex and justify-content-center as suggested in a previous answer to no avail.
I've noticed that adding mx-auto to each of the card classes sort of works and keeps them nicely centered, however, the gap between the cards is also removed using this method.
Please see my site here.
Looks fine in desktop view, but once in mobile view all the cards are pushed to the right, I can see that the cards are going over the margin-right defined in my main tag.
Here's the HTML:
<!DOCTYPE html>
<html lang="en">
<body>
<main>
<!--Cards-->
<div class="card-deck mx-auto">
<!--Games Card-->
<div class="card text-center text-white bg-dark mb-3 d-flex" style="width: 18rem;">
<a href="#">
<img src="../img/games.png" class="card-img-top" alt="Games">
<div class="card-body">
<p class="card-text"><strong>Games</strong></p>
</div>
</a>
</div>
<!-- Consoles Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="##">
<img src="../img/consoles.png" href="#" class="card-img-top" alt="Consoles">
<div class="card-body">
<p class="card-text"><strong>Consoles</strong></p>
</div>
</a>
</div>
<!-- Getting Started Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="###">
<img src="../img/gettingStarted.png" href="#" class="card-img-top" alt="Getting Started">
<div class="card-body">
<p class="card-text"><strong>Getting Started</strong></p>
</div>
</a>
</div>
</div>
</main>
<!--JavaScript-->
<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>
</body>
</html>
I'm using Bootstrap 4, please help!
Please dont fixed the width for your card. use style="width: auto;" instead of style="width: 18rem;" this will solve your problem
<!DOCTYPE html>
<html lang="en">
<body>
<main>
<!--Cards-->
<div class="card-deck mx-auto">
<!--Games Card-->
<div class="card text-center text-white bg-dark mb-3 d-flex" style="width: auto;">
<a href="#">
<img src="../img/games.png" class="card-img-top" alt="Games">
<div class="card-body">
<p class="card-text"><strong>Games</strong></p>
</div>
</a>
</div>
<!-- Consoles Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: auto;">
<a href="##">
<img src="../img/consoles.png" href="#" class="card-img-top" alt="Consoles">
<div class="card-body">
<p class="card-text"><strong>Consoles</strong></p>
</div>
</a>
</div>
<!-- Getting Started Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: auto;">
<a href="###">
<img src="../img/gettingStarted.png" href="#" class="card-img-top" alt="Getting Started">
<div class="card-body">
<p class="card-text"><strong>Getting Started</strong></p>
</div>
</a>
</div>
</div>
</main>
<!--JavaScript-->
<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>
</body>
</html>
if you put the align-items-center class in card-deck parent div you can get center card.
<!DOCTYPE html>
<html lang="en">
<body>
<main>
<!--Cards-->
<div class="card-deck mx-auto align-items-center">
<!--Games Card-->
<div class="card text-center text-white bg-dark mb-3 d-flex" style="width: 18rem;">
<a href="#">
<img src="../img/games.png" class="card-img-top" alt="Games">
<div class="card-body">
<p class="card-text"><strong>Games</strong></p>
</div>
</a>
</div>
<!-- Consoles Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="##">
<img src="../img/consoles.png" href="#" class="card-img-top" alt="Consoles">
<div class="card-body">
<p class="card-text"><strong>Consoles</strong></p>
</div>
</a>
</div>
<!-- Getting Started Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="###">
<img src="../img/gettingStarted.png" href="#" class="card-img-top" alt="Getting Started">
<div class="card-body">
<p class="card-text"><strong>Getting Started</strong></p>
</div>
</a>
</div>
</div>
</main>
<!--JavaScript-->
<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>
</body>
</html>
thank you
A really simple way is to add margin:0 auto; to the card-deck div!
<!DOCTYPE html>
<html lang="en">
<body>
<main>
<!--Cards-->
<div class="card-deck" style="margin:0 auto">
<!--Games Card-->
<div class="card text-center text-white bg-dark mb-3 d-flex" style="width: 18rem;">
<a href="#">
<img src="../img/games.png" class="card-img-top" alt="Games">
<div class="card-body">
<p class="card-text"><strong>Games</strong></p>
</div>
</a>
</div>
<!-- Consoles Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="##">
<img src="../img/consoles.png" href="#" class="card-img-top" alt="Consoles">
<div class="card-body">
<p class="card-text"><strong>Consoles</strong></p>
</div>
</a>
</div>
<!-- Getting Started Card-->
<div class="card text-center text-white bg-dark mb-3" style="width: 18rem;">
<a href="###">
<img src="../img/gettingStarted.png" href="#" class="card-img-top" alt="Getting Started">
<div class="card-body">
<p class="card-text"><strong>Getting Started</strong></p>
</div>
</a>
</div>
</div>
</main>
<!--JavaScript-->
<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>
</body>
</html>
I have comments section on my template, I use w-75 float-right but other column got pull to my comment side.
here my template code.
<div class="col-md-12 mb-3">
....
<!-- comments with no parrent -->
<div class="card mb-3">
<div class="card-body">
....
</div>
</div>
<!-- comments with parrent -->
<div class="card mb-3 w-75 float-right">
....
</div>
<br>
</div>
<!-- comments Form -->
<div class="col-md-12 mb-5">
....
</div>
I wan't like this
Instead of
<div class="card mb-3 w-75 float-right">
Set the left margin to auto, this will push this div element all the way to the right and leave the area to its left clear.
Like this:
<div class='card mb-3 w-75 ml-auto">
You'll want to add .row classes to force a new line, and ensure that each row has exactly 12 columns. To adhere to this, you'll want to remove your w-75 class, and change the parent to col-md-7 in conjunction with offset-md-5 (this totals 12). This allows your card to be offset on desktop screens (with no elements forcing their way in on the left) and stack correctly on mobile screens.
This can be seen in the following (click Run code snippet and then Full page to see the offset):
body {
margin: 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="row">
<div class="col-md-12 mb-3">
....
</div>
</div>
<div class="row">
<div class="col-md-12 mb-3">
<!-- comments with no parent -->
<div class="card mb-3">
<div class="card-body">
....
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7 offset-md-5">
<!-- comments with parent -->
<div class="card mb-3">
<div class="card-body">
....
</div>
</div>
<br>
</div>
</div>
<div class="row">
<!-- comments Form -->
<div class="col-md-12 mb-5">
....
</div>
</div>