I'm making a Bootstrap 4 card that in its header needs to have an image, title, subtitle, badge and a close button. And the badge needs to be vertically centered on the right, while the image, title and subtitle need to be vertically centered on the left.
Here's an image, it'll explain things better:
And I can't get either title and subtitle, nor badge vertically centered. I must do this using Bootstrap utilities only, so no additional CSS if possible(or very little if there's no other choice). How can I achieve this?
Here's my code:
.close {
font-size: 20px;
text-shadow: none;
color: #000;
opacity: 1;
position: absolute;
right: 0;
top: 0;
}
.card-title {
font-size: 18px;
}
.card-subtitle {
font-size: 11px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<div class="card">
<button type="button" class="close close-chat mr-1" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="card-header border-0 py-3">
<img src="http://placehold.it/45x45" class="rounded-circle float-left mr-3">
<h4 class="card-title mb-0 d-inline align-middle">Card title</h4>
<span class="badge badge-success float-right px-4 py-1 mt-2">Success</span>
<h6 class="card-subtitle text-muted">Card subtitle</h6>
</div>
<div class="card-block">
<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>
Use the flexbox utility classes in Bootstrap 4. Here's a good guide to flexbox if you're not familiar.
The added classes in the code below are d-flex justify-content-between align-items-center, but you'll also need a new wrapper around the title and subtitle with its own classes.
.close {
font-size: 20px;
text-shadow: none;
color: #000;
opacity: 1;
position: absolute;
right: 0;
top: 0;
}
.card-title {
font-size: 18px;
}
.card-subtitle {
font-size: 11px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<div class="card">
<button type="button" class="close close-chat mr-1" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="card-header border-0 py-3 d-flex justify-content-between align-items-center">
<img src="http://placehold.it/45x45" class="rounded-circle float-left mr-3">
<div class="mr-auto">
<h4 class="card-title mb-0 d-inline align-middle">Card title</h4>
<h6 class="card-subtitle text-muted">Card subtitle</h6>
</div>
<span class="badge badge-success px-4 py-1 mt-2">Success</span>
</div>
<div class="card-block">
<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>
Use the BS 4 flexbox utils. You can change the card-header to display:flex using the d-flex class then align its' contents accordingly...
http://codeply.com/go/qj2pblwSWB
<div class="card">
<button type="button" class="close close-chat mr-1" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="card-header bg-info border-0 py-3 d-flex align-items-center">
<img src="http://placehold.it/45x45" class="rounded-circle align-self-start mr-3">
<div>
<h4 class="card-title mb-0">Card title</h4>
<h6 class="card-subtitle text-muted">Card subtitle</h6>
</div>
<span class="badge badge-success ml-auto px-4">Success</span>
</div>
<div class="card-block">
..
</div>
</div>
http://codeply.com/go/qj2pblwSWB
Bootstrap 4 leverages flexbox primarily for it's layout/grid utilities. You can use classes such as align-items-* to vertically position elements within a row. In this case align-items-center can be used to center the items.
You'd use .col-*-auto in combination with .col within a .rowto have Bootstrap auto size elements.
You technically don't need to use a .card if you don't want to as long as you have control over the class representing this "card" like element. The grid system could give you much more control over the layout of your custom "card" like element.
Here is a link to a plunker demonstrating the functionality using the built in Bootstrap 4 classes.
<!-- This extra .row and .col.col-sm-auto allows card to be auto sized -->
<div class="row">
<div class="col col-sm-auto">
<div class="foo">
<div class="row align-items-center">
<div class="col col-sm-auto"><img class="rounded-circle" src="https://placeholdit.imgix.net/~text?txtsize=6&txt=50%C3%9750&w=50&h=50" /></div>
<div class="col col-sm-auto text-center">
<span>TITLE</span>
<br>
<span>SUBTITLE</span>
</div>
<div class="col"></div>
<div class="col col-sm-auto text-right">
<span>BADGE</span>
</div>
<div class="col col-sm-auto">
<span class="close">X</span>
</div>
</div>
</div>
</div>
</div>
Hopefully this helps!
Related
I am facing a problem in which I want the content which is written on the left half of the screen should be written on the image which is on the right side when decreasing the screen size.
for reference, check the image as I want this problem image
to be like this Solution image when I decrease the size of my screen, But my screen looks like this at present. I need the solution as soon as possible.
Note: I used mostly pre-defined classes of bootstrap4 for this.
code is-
<div class="row">
<div class="col p-0 col-6 bg-creame" style="height: 100vh;">
<div class="h-100 d-flex flex-column justify-content-start pt-5">
<div class="m-5 pt-5 pr-5">
<h1 class="mb-5"><b style="font-weight: 900;"><b>Contact</b></b></h1>
<p class="m-0" style="font-weight: 900;font-size: 18px;">Email Address</p>
<p><a style="color: inherit !important;font-size: 23px;" href="mailto:support#regid.ca">support#XXX.com</a>
</p>
<p class="m-0 mt-5" style="font-weight: 900;font-size: 18px;">Mailing Address</p>
<p style="font-size: 23px;">
XXXXXX, 77 XXXX XXX X,
XXXXXX, XX XXX 6XX
</p>
<div class="d-none d-lg-block">
<button class="text-uppercase btn pr-5 pl-5 color-creame bg-orange"
style="border-radius: 50px; letter-spacing: 1px;font-size: 20px;">
Contact Us
</button>
</div>
</div>
</div>
</div>
<div class="col p-0 col-6 bg-orange" style="height: 100vh;">
<div style="position: absolute; bottom: 0; right: 0;">
<div class="h-50 bg-orange d-flex flex-column justify-content-end" style="padding-top: 14rem;">
<div class="text-center p-4">
<i class="fas fa-arrow-up bg-black" style="font-size: 2rem;"></i>
</div>
</div>
</div>
<div class="d-flex flex-column h-100">
<img height="100%" width="100%" src="./assets/img/Rectangle 8 (2).png" class="img img-responsive">
</div>
</div>
</div>
You could use z-index to push the image behind the text and do something like this
#media (max-width: 600px) {
img {
width: 100%;
z-index: -1;
}
div {
z-index: 1;
}
}
I have been trying to make this layout even. How can I have a vertical equidistance between the text and have them contained in the card.
Here is the html code:
<div class="card text-center">
<p class="card-title"><small class="text-muted">Societal</small></p>
<div class="card-body">
<small class="card-text">Large involontary migration</small>
<div class="card-footer">
<small class="text-muted">10.5%</small>
</div>
</div>
</div>
Thanks
There are a few options for achieving this listed in the bootstrap documentation.
Bootstrap Website
I chose a slightly different card variation from bootstrap v4.5.
You can play around with the padding and margins to get it exactly how you'd like.
<div class="container">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title text-danger text-center mb-0 pb-0">Societal</h5>
<p class="card-text text-center mb-0 pb-0"><strong>Large-scale involontary migration</strong></p>
<p class="text-danger text-center">10.5%</p>
</div>
</div>
</div>
.card-body {
border: 20px solid #b19cd9;
}
code output
Code snippet
So I have this card with a div inside it. And the idea is that I want to show a icon to the left and then a text to the right of the icon indicating a username can be up to 32 characters long.
<div>
<h5 class="d-inline"><i class="icofont icofont-social-snapchat"></i></h5>
<p class="d-inline">rwmGhw9El8cBMeyvzQ18fmZP2EbLaQhY</p>
</div>
The issue is that when I start shrinking the page, the text gets places under the icon and it starts going outside of the card.
I'm actually not sure to why this happens or what the best way to fix this is.
How do I properly deal with something like this? Would adding ellipsis be a good idea?
Here is the markup for the card
<div class="col-xl-4 px-2">
<div class="card card-with-border mt-5 ">
<div class="card-body mb-0">
#foreach (var item in user.Tags)
{
<span class="badge badge-primary mt-3 ml-0">#item</span>
}
</div>
<div class="card-body socialprofile filter-cards-view pt-2">
<div class="media">
<div class="avatar ratio"><img class="b-r-8 height-50 mr-3 img-80" src=#user.Image alt="#"></div>
<div class="media-body">
<h6 class="font-danger f-w-600">Hello, World!</h6>
<div>
<h5 class="d-inline"><i class="icofont icofont-social-snapchat"></i></h5>
<p class="d-inline">rwmGhw9El8cBMeyvzQ18fmZP2EbLaQhY</p>
</div>
<span class="card-text"><h5><i class="icofont icofont-social-instagram"></i></h5><p></p></span>
<p class="card-text mt-2">Testing the text lets see what it looks like.</p>
</div>
</div>
<div class="social-btngroup d-flex">
<button class="btn btn-primary w-100">Like</button>
</div>
<div class="likes-profile text-center">
<h5> <span> <i class="fa fa-heart font-danger"></i> 884</span></h5>
</div>
</div>
</div>
</div>
Your element need to be in display: block or display: inline-block.
/* Limit size of media-body */
.media-body {
max-width: calc(100% - 2rem);
}
.ellipsis {
display: block !important;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
<div>
<h5 class="d-inline"><i class="icofont icofont-social-snapchat"></i></h5>
<!-- Add the class ellipsis where you want -->
<p class="d-inline ellipsis">rwmGhw9El8cBMeyvzQ18fmZP2EbLaQhY</p>
</div>
I would like to style Bootsrap 4's card-header so that there is a heading and span vertically aligned on same line. The header should consist of a <hX> heading for expanding card's card-body and a <span> which is non-clicable and pulled to the right:
How can I ensure the "Non-clicable body summary" span appear on same line as the "Clicable header"?
.card-header h3 {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<div class="card">
<div class="card-header" id="headingOne">
<div class="float-right card-summary">Non-clicable body summary</div>
<a class="mb-0" data-toggle="collapse" data-target="#collapse" aria-expanded="true" aria-controls="collapse">
<h3>
Clicable header
</h3>
</a>
</div>
<div id="collapse" class="collapse" aria-labelledby="headingOne">
<div class="card-body">
<p>Some content</p>
</div>
</div>
</div>
Is this what you're looking for?
.card-header h3 {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<div class="card">
<div class="align-items-center card-header d-flex justify-content-between" id="headingOne">
<a class="mb-0" data-toggle="collapse" data-target="#collapse" aria-expanded="true" aria-controls="collapse">
<h3>
Clicable header
</h3>
</a>
<div class="card-summary">Non-clicable body summary</div>
</div>
<div id="collapse" class="collapse" aria-labelledby="headingOne">
<div class="card-body">
<p>Some content</p>
</div>
</div>
</div>
Removed floats and used default BS flexbox utility classes for
section.
first remove float-right class.
you can use the d-flex justify-content-between class in card-header div.
<div class="card-header d-flex justify-content-between" id="headingOne">
and also add align-self-center class. <div class="card-summary align-self-center">Non-clicable body summary</div>
Working Demo
Ref : Bootstrap utilities
I'm trying to create a profile page for a site I am currently working on. When viewing this in a desktop (anything above a tablet) I need it to show two cards next to each other, but there is a minimum width set due to the buttons in the card moving out of place when shrunk.
I have some empty column's on each side to pad the page out.
So I have made it so when you are in a large desktop, it pads the page out and the two cards are next to one another which is perfect.
But when I get less than 1175px, the two cards start to overlap each other, instead of this happening, I want the right card to jump right below the left card which has the profile information in it.
I have made it so it doesn't show up in mobile too, but I also want it to show with the two column's one underneath the other for mobile too.
HTML CODE:
<div class="container-fluid pcontainer">
<div class="row">
<div class="col-2 d-none d-lg-block">
test
</div>
<div class="col-4 d-none d-md-block">
<div class="card pc mw-100">
<div class="card-title">
<img class="pp card-img-top img-fluid rounded-circle" src="img/pp.jpg" alt="Card image cap">
<h4 class="pt card-title text-left text-justify"> Ryan Edwards </h4>
<button type="button" class="btn btn-sm btnp btn-outline-info">Add Friend</button>
<button type="button" class="btn btn-sm btn-outline-success">Message</button>
</div>
<div class="card-body">
<h3> My Biography: </h3>
<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>
<div class="col-4 d-none d-md-block">
<div class="card pc mw-100">
<div class="card-title">
<img class="pp card-img-top img-fluid rounded-circle" src="img/pp.jpg" alt="Card image cap">
<h4 class="pt card-title text-left text-justify"> Ryan Edwards </h4>
<button type="button" class="btn btn-sm btnp btn-outline-info">Add Friend</button>
<button type="button" class="btn btn-sm btn-outline-success">Message</button>
</div>
<div class="card-body">
<h3> My Biography: </h3>
<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>
<div class="col-2 d-none d-lg-block">
test
</div>
</div>
and here is the css:
.pc {
margin: 0px;
min-width:390px;
width: auto !important;
width:390px;
}
.pp {
max-width: 125px;
width: 125px;
max-height: 125px;
height: 125px;
margin-top: 40px;
margin-right: 40px;
float: right;
}
.pt {
margin-top: 50px;
margin-left: 50px;
}
.btnp {
margin-left: 50px;
}
.btn {
padding-top: 2px;
padding-bottom: 2px;
padding-left: 8px;
padding-right: 8px;
}
Have you tried to use a script tag?
You can set the distance and add a new class for your current one. Not sure if its going to help you :)
$(document).ready(() => {
$(document).scroll(() => {
if ($('.yourclass').offset().top - $(window).scrollTop() <= 25) {
if ($(window).scrollTop() < 400) {
$('.yourclass').removeClass('currentyclass');
} else {
$('.yourclass').addClass('newestclass');
}
}
})