How can I make a Bootstrap 5 modal fit image content? - bootstrap-5

I have a webpage that shows an image up to size 400x400px, if they click on the image then I want it to display in a modal that sizes to accommodate the actual size of the image
The modal comes up but it defaults to 500px whatever the size of the image (from https://getbootstrap.com/docs/5.2/components/modal/#fullscreen-modal). If the image is larger than that I want the modal to be larger, and if smaller I want the modal to be smaller, I cannot see how to do this. I notice you can also have a fullscreen image by using modal-fullscreen but that doesn't help me.
Surely possible for the modal to size to accommodate the data
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div>
<a data-bs-toggle="modal" data-bs-target="#full_size_image_dialog">
<img src="https://via.placeholder.com/400" class="figure-img img-fluid border border-dark">
</a>
<div id="full_size_image_dialog" class="modal">
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h5>
Full Size artwork
</h5>
</div>
<div class="modal-body">
<img src="https://via.placeholder.com/500" class="figure-img img-fluid border border-dark">
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
Edit
So I went though the answers at How to resize Twitter Bootstrap modal dynamically based on the content
The only answer that sort of worked was adding style="max-width : 1200px;"to the div with bootstrap class of modal-dialog
<div id="full_size_image_dialog" class="modal">
<div class="modal-dialog modal-dialog-scrollable" style="max-width : 1200px;">
<div class="modal-content">
<div class="modal-header">
<h5>
Full Size artwork
</h5>
</div>
<div class="modal-body">
<img src="http://172.17.22.1:9790/minimserver/*/Music/PopRock/Lily*20Allen/Alright,*20Still/01*20-*20Smile.wav/$!picture-34755692-444226.jpg" class="figure-img img-fluid border border-dark">
<figcaption>
1200 x 1200
</figcaption>
</div>
</div>
</div>
</div>
The trouble is that if the actual height of the image is larger than the height of the browser window then the image has to be scrolled vertically
What I actually want is the full image to be displayed full size but only if enough room in browser, otherwise display the max size it can be displayed without vertical scrolling. I can set a lower value for max-width so it works on my window, but then that is not going to be great for users with bigger resolution windows.
It does handle preventing sideways scrolling okay, so if the width of window is too small to show whole image it will shrink image accordingly, and we can reasonably assume images will usually be square.
So my attempted solution was to use Bootstraps container system to set different max-width size for different screen widths using col-md, col-xl ectera. But the trouble is it seems from the help that modal windows need to use their own container within the modal-body div but this isn't going to help because the max-width style has to be applied at the modal div so is outside of modal-body scope.
I tried just utilising the main page container, but it just uses the first dialog code each time, i.e it never uses more than 700px max width
<div class="row g-0">
<div class="col p-2">
<div id="full_size_image_dialog" class="modal">
<div class="modal-dialog modal-dialog-scrollable" style="max-width : 700px;">
<div class="modal-content">
<div class="modal-header">
<h5>
Full Size artwork
</h5>
</div>
<div class="modal-body">
<img src="http://172.17.22.1:9790/minimserver/*/Music/PopRock/Lily*20Allen/Alright,*20Still/01*20-*20Smile.wav/$!picture-34755692-444226.jpg" class="figure-img img-fluid border border-dark">
<figcaption>
1200 x 1200
</figcaption>
</div>
</div>
</div>
</div>
</div>
<div class="col-md p-2">
<div id="full_size_image_dialog" class="modal">
<div class="modal-dialog modal-dialog-scrollable" style="max-width : 1200px;">
<div class="modal-content">
<div class="modal-header">
<h5>
Full Size artwork
</h5>
</div>
<div class="modal-body">
<img src="http://172.17.22.1:9790/minimserver/*/Music/PopRock/Lily*20Allen/Alright,*20Still/01*20-*20Smile.wav/$!picture-34755692-444226.jpg" class="figure-img img-fluid border border-dark">
<figcaption>
1200 x 1200
</figcaption>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row g-0">
<div class="col-7 col-md-6 col-lg-4 col-xl-3 p-2">
<a data-bs-toggle="modal" data-bs-target="#full_size_image_dialog">
<img src="http://172.17.22.1:9790/minimserver/*/Music/PopRock/Lily*20Allen/Alright,*20Still/01*20-*20Smile.wav/$!picture-34755692-444226.jpg" class="img-fluid border border-dark">
</a>
</div>

At the moment, this is the best solution I could come up with using only CSS:
.modal-dialog {
max-width: max-content !important;
}
.modal-body {
display: flex;
}
.modal-body img {
object-fit: contain;
}
Only problem is that white bars appear to the left and right of the image when screen height is less then image height.
This is because of the object-fit: contain; style which I added to the image to maintain the original aspect ratio. If you remove this style then then white bars will disappear but the image will not keep it's initial aspect ratio when it's height is shrinking.
JS Fiddle: https://jsfiddle.net/hvaosw5j/

Related

Bootstrap 5.2 Equal height cards with three cards, two in the same column

I'm not sure how to properly word this question, hence the sketchy title.
I'm using Bootstrap 5.2.3, two-column grid to display three cards. There is one card in the first column and two cards, one per row, in the second column.
Code
<div class="container-fluid ">
<!-- Content starts here-->
<div class="row mt-3 mb-3">
<div class="col-md-2 mb-3">
<div class="card shadow h-100 mb-3">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Students</h6>
</div>
<div class="card-body">
Box C
</div>
</div>
</div>
<div class="col-md-10 mb-3">
<div class="card shadow h-100 mb-3 ">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Tests</h6>
</div>
<div class="card-body">
content box a
</div>
</div>
<div class="card shadow h-100 mb-3">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Questions</h6>
</div>
<div class="card-body">
content box b
</div>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
https://jsfiddle.net/nbrvqmat/
I've used the h-100 successfully in other parts of my project when there is one on the left and one on the right.
In this scenario, the bottom of the left card, Students, is only extends to the bottom of the first card, Tests. It should be the bottom of the last card, Questions.
When I inspect the Row element, it does not contain the Questions card. The height ends at the bottom of the Students and Tests cards.
If I remove the h-100 from both cards, they are contained within the row.
How can I get bottom of the Student card to match the bottom of the Questions card?
In col-md-10 you set the first card as 100% height of its column, which causes the second card to overflow out of col-md-10 because there is no more vertical space left to allocate.
Try setting overflow:hidden on col-md-10 and you will see it be cut off.
Therefore row will only grow in height that of the first card in col-md-10. Understanding that, you should realize the only thing you need to do is making the Students card a 100% height of its parent; col-md-2 which' own height is determined by the height of row.
In the example below (or see JSFiddle) I set a min-height on .card to give the cards some height for demonstration purposes, but in the end the Students card will always grow/shrink to the combined height of its two neighboring cards, except of course when they wrap at the smaller screen size.
.card {
min-height: 10rem;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<div class="container-fluid ">
<div class="row mt-3">
<div class="col-md-2 mb-3">
<div class="card shadow h-100">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Students</h6>
</div>
<div class="card-body">
Box C
</div>
</div>
</div>
<div class="col-md-10 mb-3">
<div class="card shadow mb-3">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Tests</h6>
</div>
<div class="card-body">
content box a
</div>
</div>
<div class="card shadow">
<div class="card-header">
<h6 class="m-0 font-weight-bold text-primary">Questions</h6>
</div>
<div class="card-body">
content box b
</div>
</div>
</div>
</div>
</div>

Bootstrap 5 Layout: Column heights, responsive positions

I'm trying to create the following layout:
When the page is smaller - the blue column should go under the red column.
No matter what I try - the issue is the red box always wants to be the same height as the blue box.
https://codepen.io/LeeBow/pen/eYKGzdb
<div class="container-fluid">
<div class="row">
<div class="col-md-8 order-md-1">
<div
style="height:100px;width:100px;background-color:red"></div>
</div>
<div class="col-md-4 order-md-3"><div style="height:300px;width:100px;background-color:blue"></div></div>
</div>
<div class="row">
<div class="col-md-4 order-md-2"><div style="height:100px;width:100px;background-color:green"></div></div>
</div>
</div>
I would like my green box to move under the red box.
The red box always stays the same height as the blue box. It moves under the red box correctly when the screen is smaller.
But the green box will not move under the red box.
Make two blue boxes:
one for desktop (use classes d-md-block d-none) and
one for mobile (use classes d-md-none d-block, which is the opposite from above).
Those classes are Bootstrap classes to manipulate display CSS property.
See the snippet below.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<div style="height:100px;width:100%;background-color:red"></div>
<div class="d-md-none d-block" style="height:300px;width:100%;background-color:blue"></div> <!-- Show on mobile -->
<div style="height:100px;width:100%;background-color:green"></div>
</div>
<div class="col-md-4 d-md-block d-none">
<div style="height:300px;width:100%;background-color:blue"></div> <!-- Show on desktop -->
</div>
</div>
</div>

How does one change the opacity of a Bootstrap 5.2 toast?

My toast overlaps some buttons and the default opacity is too low and it makes reading the message difficult.
Here is my toast markup:
<div class="toast-container p-3 top-0 end-0" id="toastPlacement" data-original-class="toast-container p-3">
<div id="root" class="toast align-items-center fade animate" data-bs-autohide="false" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body" id="toastMessage">
Hello World
</div>
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
I have tried simply setting style="opacity: 1" on the 'root' element, but that did nothing.
I've looked around online, but none of the suggestions I found work.
Also, is there any finer grained control of the position of the Toast other than Left, middle, Right? I'd like to position it by a percentage or fixed value if possible.
Did you tried..?
<div class="opacity-100">...</div>
<div class="opacity-75">...</div>
<div class="opacity-50">...</div>
<div class="opacity-25">...</div>
https://getbootstrap.com/docs/5.2/utilities/opacity/
Ok, for anyone not already a web/bootstrap guru, the trick here is the background color on the .toast class is set to be translucent (85%).
So in your own css file, make a style like so:
.bg-solid-white {
background-color: whitesmoke !important;
}
Obviously use whatever color and class name you like, then:
<div class="toast-container p-3 top-0 end-0" id="toastPlacement" data-original-class="toast-container p-3">
<div id="root" class="toast align-items-center fade animate bg-solid-white" data-bs-autohide="false" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body" id="toastMessage">
${this.message}
</div>
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
And there you have it, a solid background.

MVC 5 / Bootstrap App - Modal Dialog Auto expand?

I've got the following HTML in an MVC 5 / Bootstrap app. As you can see, I am attempting to display a photo in the modal dialog. This works fine. But, if the photo is wider than the dialog, it extends beyond the bounds of the dialog. Is there a way I can have the dialog expand, to a certain point, then display scroll bars, based on the size of the contents (image)?
<div class="modal fade" id="viewPhotoDialog" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Ask the Experts</h4>
</div>
<div class="modal-body">
<img src="#Url.Action("GetPhoto", new { askTheExpertId = #Model.AskTheExpertId })" alt="" title=""/>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
You can add .img-responsive class to the img (gives max-width:100% and auto height).
About modal width, there are 2 extra option classes modal-lg and modal-sm that you can add at .modal-dialog
http://getbootstrap.com/javascript/#modals

How to spread div which is inside content to wider than its content?

Ex. Code below is represented as Blue Box, I want to expand 2 grid to Yellow one. But, the problem is; This .. need to be inside content but it always have margin left and right. I don't know how to solve this.
<div data-role="content" data-theme="d" >
<div class="ui-grid-a" >
<div class="ui-block-a"><strong>Time:</strong></div>
<div class="ui-block-b" id="price-order-timestamp">yyyy-mm-dd hh24:mi:ss</div>
</div>
<div style="width:100%;"> <!-- Here-->
<div class="ui-grid-d" id="data-table-header" >
<div class="ui-block-a">Label A</div>
<div class="ui-block-b">Label B</div>
<div class="ui-block-c">Label C</div>
<div class="ui-block-d">Label D</div>
<div class="ui-block-e">Label E</div>
</div>
<div class="ui-grid-d" id="data-table" >
<div class="ui-block-a">A</div>
<div class="ui-block-b">B</div>
<div class="ui-block-c">C</div>
<div class="ui-block-d">D</div>
<div class="ui-block-e">E</div>
</div>
</div>
</div>
If you want to remove the default padding added to the data-role=content tag, add the following override in your custom.css
#myPageId .ui-content{
padding-left:0;
padding​-right:0;
}​
http://jsfiddle.net/nirmaljpatel/LW5aC/

Resources