Why the div is not maintained their aspect ratio while displaying image? - html

This is html code snippet:
my_button.onclick = () => {
image_holder_div.classList.remove('display_none_class');
let imgTag = `<img id="nnn" src="${fileURL}" class="mx-auto d-block" alt="..."></img>`;
image_holder_div.innerHTML = imgTag;
}
.container-fluid{
max-width: 650px;
width: 95%;
padding: 30px;
background-color: rgb(250, 252, 253);
border-radius: 4px;
border: 1px solid #dfe1e5;
/* box-shadow: 2px 2px 2px; */
position: relative;
}
.first{
height: 400px;
border: 2px dashed #8b8e96;
background-color: #fffce5;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 10px auto;
}
.image_holder_div{
height: 400px;
width: 100%;
border: 2px dashed #8b8e96;
background-color: #b4a429;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 10px auto;
position: absolute;
top: 30px;
}
<div class="container-fluid">
<div class="first">
</div>
<div class="image_holder_div display_none_class">
</div>
</div>
When I click on my_button to display image, the image_holder_div not maintained aspect ratio while displaying image. It's showing like this I upload the image here
image_holder_div
Can anyone please tell me why it's happening and how can I fix this issue...

Explanation of the cause:
It's totally clear from the code, that you have set the width of image_holder-div to 100% of its parent container. Since its parent element is .container-fluid, it would attain its width. It is seen that you have added padding: 30px in .container-fluid, hence, you see a space of 30 pixels between the parent and image holder div.
Solution:
You can add the following styling on the image_holder_div:
width: calc(100% - 60px);
Adding this line would use some CSS calculations and set the height of the div equal to 100% of the parent element minus 60px, i.e. 30px from left and right both. (Don't forget to add space before and after the minus sign)
For more about the calc() function:
https://www.w3schools.com/cssref/func_calc.asp

Related

CSS / HTML: How to center div horizontally? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 28 days ago.
I know this question has been asked hundreds of times, but for some reason I cant get it to work.
Both my HTML and CSS is rather simple, but I cant seem to center the div (livechat-wrapper) horizontally.
I just want the div to be as wide as the textarea, and placed just above the textarea, but it is "stuck" to the left side of my view.
Any ideas on how to do this?
<body >
<div class="livechat-wrapper">
</div>
<form>
<textarea maxlength="400" rows="1"id="input_field" class="input_field" type="text" name="q" placeholder="Write a message..."></textarea>
</form>
</body>
* {
box-sizing: border-box;
font-family: "Nunito", sans-serif;
}
html, body {
background: linear-gradient(to bottom right, #FDFCFB, #E2D1C3);
/*linear-gradient(to bottom right, #FDABDD, #374A5A);*/
width: 800px;
height: 600px
}
form {
display: flex;
flex-direction: row;
justify-content: center;
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding-bottom: 10px;
}
.input_field {
background: rgba(255, 255, 255, 0.4);
border-radius: 10px;
width: 90%;
border: none;
padding: 1.2em;
outline: none;
font-weight: 400;
box-shadow: 1px 1px 1px black, -0.5px -0.5px white;
border: none;
resize: none;
outline: none;
}
.livechat-wrapper {
background: rgba(255, 255, 255, 0.4);
box-shadow: 1px 1px 1px black, -0.5px -0.5px white;
height: 80%;
width: 90%;
border-radius: 10px;
border: 0.05em solid red;
display: flex;
flex-direction: row;
justify-content: center;
}
I tried using on the div, but with no luck
display: flex;
flex-direction: row;
justify-content: center;
There are several ways to center a div horizontally using CSS. Here are a few examples:
Using margin: auto:
div {
width: 50%; /* or any other width */
margin: 0 auto;
}
This method works by setting the left and right margins to auto, which will push the div to the center of the parent element.
Using Flexbox:
div {
display: flex;
justify-content: center;
}
This method works by using the justify-content property to center the div within the parent element
Using Grid:
div {
display: grid;
place-items: center;
}
This method works by using the place-items property to center the div within the parent element.
Using transform: translate:
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
The body has a width of 800px however that doesn't affect the form due to absolute positioning. So you will want to remove the width on the body. Also the div is limited to 90% width. So you can either change that to 100% and add box-sizing: border-box; to correct for margins or add margin: auto; which should center it on the page.
Try adding this to the your CSS
body {
display: flex;
justify-content: center;
width: 800px;
}
.livechat-wrapper {
margin: auto;
width: 90%;
}

Putting three images side by side and fill the parent

I want to have three images side by side with one condition I'm unable to reach without a little bit help:
.picture-container {
position: absolute;
top: 10%;
width: 90%;
height: 70%;
left: 5%;
border-style: dotted;
}
.picture-container .img-container.three-image {
justify-items: center;
height: 100%;
gap: 20px;
display: grid;
grid-template-columns: auto auto auto;
}
.picture-container .img-container.three-image * {
height: 298px;
}
.picture-container .img-container.three-image .img-frame {
width: 100%;
height: 42%;
object-fit: cover;
}
.img-frame {
display: flex;
border: 5px solid #e8e8e8;
box-shadow: 1px 7px 20px 9px rgb(0 0 0 / 75%);
margin: 3rem auto 3rem;
flex: 1 0 45%;
border-radius: 5px;
cursor: pointer;
margin: 0.4rem;
background: #dfe4ea;
user-select: none;
transition: 0.5s;
height: 100%;
object-fit: contain;
}
<div class="picture-container">
<div class="img-container three-image ">
<img class="img-frame" src="https://images.pexels.com/photos/4876243/pexels-photo-4876243.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<img class="img-frame" src="https://images.pexels.com/photos/1386454/pexels-photo-1386454.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<img class="img-frame" src="https://images.pexels.com/photos/9646282/pexels-photo-9646282.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500">
</div>
</div>
I want these images to :
fill the whole area of the picture-container parent. they should fill the width and the height of the picture-container.
All the images should have the same dimensions. I don't want to have different sizes of them.
Note that source of the each image has its own dimensions.
The images should not be Cropped and they can be stretched...
The frame should be intact...
How can I do this?
I couldn't get it to work without wrapping the images in a DIV and setting a width and height on the images themselves.
.img-container{display:flex;max-height:100vh}
.img-container div{flex-grow:1}
.img-frame{width:calc(100% - 29px - 0.4rem);height:calc(100% - 29px - 0.4rem)}
Example:
https://jsfiddle.net/540scey8/1/

After removing border, text not in center anymore

.home {
height: 100vh;
border: 0px solid rgb(255, 255, 255);
background-image: url(../img/b.jpg);
background-size: cover;
}
.home-content {
height: 80vh;
/* border: 4px solid red; */
}
.heading {
/* border: 2px solid red; */
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 100px;
}
<section class="home">
<div class="home-content">
<div class="heading">
<div>Shubham karwal</div>
</div>
</div>
</section>
After removing border from .home the text Shubham karwal which was previously centered shifted towards left a bit.
What to do?
Please do help
You just have to add four lines to your css file:
.heading div {
height: 100%;
text-align: center;
}
Here is the example with the css added:
.home {
height: 100vh;
border: 0px solid rgb(255, 255, 255);
background-image: url(../img/b.jpg);
background-size: cover;
}
.home-content {
height: 80vh;
/* border: 4px solid red; */
}
.heading {
/* border: 2px solid red; */
display: flex;
height: 100%;
justify-content: center;
align-items: center;
font-size: 100px;
}
.heading div {
height: 100%;
text-align: center;
}
<section class="home">
<div class="home-content">
<div class="heading">
<div>Shubham karwal</div>
</div>
</div>
</section>
It depends on where the border is being added/removed. And also whether any other CSS settings are keeping the standard padding/margins that a browser might add to some elements or not.
The height is set at 100vh. If the box-sizing has not been altered it will default to value content, which means any padding and any borders are not included in the browser's calculation of the size of an element.
In the snippet given in the question neither the default padding/margins nor the default setting of box-zising are altered. Hence the height overall is greater than 100vh (what with the default top padding and the addition of a border) so space for a scroll bar is created on the right hand side and the text (well, the elements it is in) all shift a bit to the left.
Quite often you'll see these settings at the top of a style sheet:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
which means all three of those settings are set as given for all relevant elements unless subsequently set.
Try this in your 'real' situation to see if it cures the problem.

How to convert fixed margins in percent?

Here is a wrapper div with 450 px. If I set the height to 100vh, then the bottom margin of the title should adapt to the viewport.
.title,
.subtitle {
border: 1px solid #000;
}
.wrapper {
display: flex;
flex-direction: column;
width: 16%;
height: 450px;
margin-right: 42%;
margin-left: 42%;
border: 1px solid #000;
}
.title {
margin-bottom: 1rem;
}
<div class="wrapper">
<div class="title">Title</div>
<div class="subtitle">Subtitle</div>
</div>
https://codepen.io/anon/pen/ZNoZVm
Summary: The margins of the elements in wrapper should remain in proportion to the viewport.
I don't 100% understand (I know, I shouldn't be answering if I don't understand) but could it be that you're trying to say that you'd like the height of the wrapper to be the height of the viewport (screen height).
If that's the case then you're doing everything right, except...
On default the body has a margin of 8px. You simply have to remove that and you will get the result you're looking for.
body {
margin: 0;
}
/* =================== */
.title,
.subtitle {
border: 1px solid #000;
}
.wrapper {
display: flex;
flex-direction: column;
width: 16%;
height: 100vh;
margin-right: 42%;
margin-left: 42%;
border: 1px solid #000;
}
.title {
margin-bottom: 1rem;
}
<div class="wrapper">
<div class="title">Title</div>
<div class="subtitle">Subtitle</div>
</div>
(If you're wandering why you're still able to scroll, it's because "border" adds to the height (and width) so if your border-bottom is 3px and your height like 10px, you have an overall height of 13px)

How to set height to a child div?

i have a div in another div whose height is 48px and position relative. now i have a child div in the div whose height is 48px. i want the child div max-height to be set to 80% and min-height set to 40%. in doing so, the child divs height is just 48px.
Below is the code,
<div class="top_div">
<div class="drawer">
<div class="menu">
<header>
<Svg>
<button></button>
</header>
<ul>
<li></li>
</ul></div></div></div>
.top_div {
z-index: 1;
display: flex;
position: relative;
width: 100%;
height: 48px;
padding: 0 12px 0 12px;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
align-items: center;
}
.drawer {
display: flex;
flex-direction: column;
background-color: $white;
position: absolute;
width: 380px;
top: 55px;
right: 8px;
min-height: 40%;
max-height: 80%;
header {
height: 41px;
border-bottom: 1px solid #CCCCCC;
display: flex;
align-items: center;
justify-content: space-between;
padding-right: 0px;
padding-top: 2px;
svg {
margin-left: 16px;
}
}
ul {
overflow-x: hidden;
}
::after {
content: " ";
position: absolute;
bottom: 100%;
left: 83%;
margin-left: -5px;
border-width: 14px;
border-style: solid;
border-color: transparent transparent $white transparent;
}
}
If i remove position : relative for top_div then it works well but remove the box-shadow for topbar...how can i fix this. I want the box-shadow to be there. or if i keep position:relative for top_div then i want the height of the drawer to be 80%.
How can i fix this. could someone help me. thanks.
I inserted the code into an html document, and set the position of your child's div to relative. For me this solved the issue, and I got exacly what you want to do.
Try this.
I hope this can help, for me it helped.