bind:clientHeight grows endlessly - html

I've created the following layout:
desktop: two columns
mobile: one column
There are 4 grey cards and one (the first) red. The dimensions of the grey cards depends on their content, the red one whould have the same height of the near card. To compure the size, I used svelte bind:clientWidth and bind:clientHeight.
It works but for some reason I needed to create a relative and then an absolute div, otherwise height grows endlessly.
So, this works perfectly on desktop (2 columns) but obviously not on mobile because of the absolute div.
Here the demo and below some relevant code:
<script>
let containerWidth = 0;
let containerHeight = 0;
$: width = containerWidth;
$: height = containerHeight;
</script>
<div class="col-12 col-md-6 mb-3 border">
<div
class="position-relative w-100 h-100 p-3"
bind:clientWidth={containerWidth}
bind:clientHeight={containerHeight}
style="width: {width}px; height: {height}px; background-color: red"
>
<div
class="position-absolute top-0 start-0"
style="width: {width}px; height: {height}px; background-color: #f7f9fa"
>
<div
class="d-flex justify-content-center align-items-center"
style="width: {width}px; height: {height}px; background-color: red"
>
</div>
</div>
</div>
</div>
In that case the red card has small height, it should have height = 150px but how can I do that?
On mobile probably is not necessary to bind height... How can I avoid to use relative/absolute to get the right dimensions of a div?

Related

Display image inside the box without stretching it

I developed an image gallery.
My problem is that I am using 100% height and sometimes the image stretches, ending up losing quality.
Is there a way to place the image in the center of the box without stretching it and in the center if it is not tall enough to fill the box completely?
Demo
My code
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="row">
<div class="drop dropp">
<div class="abc">
<ngb-carousel style="outline: none;" id="carousel" #carousel *ngIf="data" data-interval="false"
data-ride="carousel" data-pause="hover" (slide)="change($event)">
<ng-template *ngFor="let imgIdx of data; let i = index" [id]="i" ngbSlide>
<div class="picsum-img-wrapper">
<img [src]="imgIdx.image" style="border-radius: 8px; object-fit: fill;" class="img-responsive">
</div>
</ng-template>
</ngb-carousel>
</div>
</div>
<div class="row">
<div class="Upcard" *ngFor="let imgIdx of belowImageData; let i = index">
<img class="img-responsive" [src]="imgIdx.image" style="width: 100%; object-fit: fill; height: 100%; border-radius: 8px;">
</div>
</div>
</div>
</div>
**My Problem **
As you can see in the image, it fills the whole box but is deformed :( I want it not to lose quality and if it doesn't have enough height, stay centered on the box. I want the box to have a fixed height.
If you don't care about the old browsers you can use object-fit property on the img directly.
for example:
object-fit: cover;
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
Otherwise you can add an empty div, give it the full size of the box, and use the image as a background of that div. Then use background-size property.
Hope it helps.
I have tested the code, you can try adding the below code in css.edit and take a look
.img-responsive {
object-fit: contain; height:200px!important;
}
or to need all images to fit properly you have to remove the div global height 100 percentage. and set the height specific for each div. so you don't need height parameter in .img-responsive class.

Why cant i increase the height of this div (using bootstrap)?

Im trying to increase the height of the sidebar, i want it to be the same height as the page.picture of page
I want the box on the left to touch te bottom but when i use the class h-100 it does not work.
The html code of the element:
<div class="container-fluid mt-5">
<div class="row">
<div class="col-2 pt-5 sidebar h-100 sticky-top w100">
...
Thanks in advance
h-100 will work only if the parent div has 100% height !
you can use 100vh instead of above if you cant set the height to the parent
1.If you want to set height of a div first of all you have to make the div as display-inline-block. Because inline element doesn't allows height and div is a inline element. Set height manually using css
.container-fluid {
height: 100px;
}
If h-100 means height: 100% according to the content of the div or child elements. So the height will be only the text height.
<div class="container-fluid mt-5">
<div class="row">
<div class="col-2 pt-5 sidebar h-100 w-100 d-inline-block sticky-top">
test text
Your sidebar actually have the same height of your page. Is your page that is not touching the bottom of the screen.
You have two ways to fix this.
1. set body height to 100vh in your styles.css
body {
height: 100vh;
}
2. wrap your page in a div
<div style="height: 100vh">
<div class="container-fluid mt-5">
<div class="row">
<div class="col-2 pt-5 sidebar h-100 sticky-top w100">
...
</div>
add this code to your css file
#media screen and (min-width: 1024px) {
myNewDivHeight{
height:250px;
}
}
add this class to your div
class="col-md-12 myNewDivHeight"

Overflow causing entire page to scroll

I am trying to create a page that doesn't scroll. Certain child elements on the page can scroll, but I'm trying to prevent the page as a whole from scrolling. I have a very nested child element that, when overflowed, receives a scroll bar, but also causes the main document to grow and receive a scroll bar as well.
This is the heart of the issue, but there are a few more nested levels that may be a factor.
<div class="h-100 d-flex flex-column">
<div class="d-flex align-items-center justify-content-center bg-red" style="height: 7%">
</div>
<div class="d-flex align-items-center justify-content-center bg-red" style="height: 3%">
</div>
<div class="bg-green" style="max-height: 75%; height: 75%; overflow-y: auto;">
<div class="bg-gray m-4" style="height: 2000px;">
The height of this content causes BOTH scroll bars to appear. I only want a bar on the
'green section'
</div>
</div>
<div class="bg-red flex-grow-1">
</div>
</div>
This code pen demonstrates how my app is set up. The overflowing element is nested deep within many flex displays (coming from bootstrap 4 utility classes).
https://codepen.io/averyferrante/pen/YMdNpO
I want only the green section from the code pen to scroll, not the entire document to grow/scroll as well.
The problem is that a couple of your containers are missing height definitions. As a result, the children of those containers ignore the percentage heights applied to them.
Add this to your code:
<div class="flex-grow-1" style="height: calc(100% - 48px);">
This height rule gives the container a defined height while compensating for the height of its sibling.
Another height rule was missing three levels down:
<div class="d-flex flex-column w-100" style="height: 100%;">
revised codepen
More detailed explanations:
Working with the CSS height property and percentage values
Chrome / Safari not filling 100% height of flex parent
Did you try playing with position: absolute? You can then set width and height as needed and the red box will see its scrollbar disappear!

Max-width 760px for block in Bootstrap4?

I need to set block content width to 760px (best for line-lenght) and center it horizontal on the page. Is it possible in pure Boostrap 4 without own CSS?
With the "col-8" class the block content is 730px wide.
With "col-10 px-5" classes the content of the block is 853,984px wide.
In CSS is it easy:
div{
margin:0 auto;
max-width:760px
}
Could you show your code? if the div is inside a div with class container-fluid, just add col to your div class, it's take all width possible in the screen.
The divs container should have the d-flex justify-content-center classes which will made it to be in center. The max-width means the div's MAXIMUM width will be 760px only if the inner content is that wide. You need just width for that.
OR in boostrap the row class is display:flex by default, you can use that too. Just pay attention to the default margins. (margin-left/margin-right: -8px)
<div class="container d-flex justify-content-center">
<div> // with the style="width: 760px;"
</div>
</div>
<div class="row justify-content-center mx-0">
<div> // with the style="width: 760px;"
</div>
</div>

How to divide html page vertically and set a full height background?

note: i'm using bootstrap, if it could help somehow.
.html, css, container {
height: 100%;
}
My problem is, i cant set it in html or body class (like some solutions suggest), because i dont want it to every pages. I just need this in one page.
I've tried to aplly it to my custom DIV but it wont work if I dont have content inside. Height is set to 100% but stills 0px, unless i put some content inside that div.
You can add a class to the <html> tag for the page you wish to have 100% height
<html class="full-height-page">
Then use this CSS below
.full-height-page,
.full-height-page body {
height: 100%;
min-height: 100%; /* this is necessary */
}
First, add this style to this particular page
body, html {
height: 100%;
}
Then use this. This code divides the page into 2 columns vertically and vertically align the contents. Content on the left is center-aligned. Only bootstrap classes are used. Call bootstrap 4 beta ver to apply the code.
<div class="container-fluid h-100">
<div class="row h-100">
<div class="col-sm-6 styled-bg">
<div class="d-flex align-items-center justify-content-center h-100">
<div class="col-sm-6 text-center">
This is left column
</div>
</div>
</div>
<div class="col-sm-6">
<div class="d-flex align-items-center h-100">
<div class="col-sm-6">
This is Right column
</div>
</div>
</div>
</div>
</div>