Reputation: 16244
I have a <div>
which needs to be auto adjusted according to the content in it. How can I do this? Right now my content is coming out of the <div>
The class I have used for the div is as follows
box-centerside {
background:url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float:left;
height:100px;
width:260px;
}
Upvotes: 88
Views: 497437
Reputation: 1
The modern approach is using min-content/max-content. This will auth width/height based on the content.
width: min-content;
height: min-content;
Upvotes: 0
Reputation: 11
You Can Try Using fit-content
This Works In Every Browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fit According To Div</title>
<style>
.test {
/* Use This Code */
width: fit-content;
block-size: fit-content;
/* You Can Also Try height: fit-content */
height: auto;
border-radius: 13px;
border-style: solid;
overflow: auto;
border-color: rgb(174, 174, 235);
}
</style>
</head>
<body>
<div class="test" autofocus>
<form>
<script src="https://checkout.razorpay.com/v1/payment-button.js" data- payment_button_id="pl_JfhYZeSvyVFaIf" async></script>
</form>
</div>
</body>
</html>
Upvotes: 1
Reputation: 61
There is no need to set such as min-height
any more, only set work well:
overflow: hidden
Upvotes: 2
Reputation: 471
Most of these are great answers, but I feel that they are leaving out one inevitable aspect of web-development which is the on-going page update. What if you want to come and add content to this div? Should you always come to adjust the div min-height? Well, whilst trying to answer these two questions, I tried out the following code instead:
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float: left;
height: auto; /* adjusts height container element according to content */
width: 260px;
}
furthermore, just for a sort of bonus , if you have elements around this div with properties like position: relative;
, they would consequently stack on top of this div, because it has property float: left;
To avoid such stacking, I tried the following code:
.parent {
display: grid;
grid-template-columns: repeat(3, 1fr); /* number depends on how many columns you want for a particular screen width. */
height: auto; /* adjusts height container element according to content */
}
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
height: auto; /* adjusts height container element according to content */
width: 100%;
}
.sibling 1 {
/* Code here */
}
.sibling 2 {
/* Code here */
}
My opinion is that, I find this grid method of displaying more fitting for a responsive website. Otherwise, there are many ways of achieving the same goals; But some of the important goals of programming are to make coding simpler and more readable as well as easier to understand.
Upvotes: 1
Reputation: 2167
For me after trying everything the below css worked:
float: left; width: 800px;
Try in chrome by inspecting element before putting it in css file.
Upvotes: 0
Reputation: 11
height:59.55%;//First specify your height then make overflow auto overflow:auto;
Upvotes: 0
Reputation: 867
You could try, div tag will auto fit height with content inside:
height: fit-content;
Upvotes: 15
Reputation: 10713
Try with the following mark-up instead of directly specifying height:
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float: left;
min-height: 100px;
width: 260px;
}
<div class="box-centerside">
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
</div>
Upvotes: 52
Reputation: 41
I just used
overflow: hidden;
And it was worked for me properly. This div had some of float lefted divs.
Upvotes: 1
Reputation: 790
Min- Height : (some Value) units
---- Use only this incase of elements where you cannot use overflow, like tooltip
Else you can use overflow property or min-height according to your need.
Upvotes: 1
Reputation: 128
Simple answer is to use overflow: hidden
and min-height: x(any) px
which will auto-adjust the size of div.
The height: auto
won't work.
Upvotes: 3
Reputation: 2146
I have fixed my issue by setting the position of the element inside a div to relative;
Upvotes: 0
Reputation: 636
Just write:
min-height: xxx;
overflow: hidden;
then div
will automatically take the height of the content.
Upvotes: 21
Reputation: 2468
I've used the following in the DIV that needs to be resized:
overflow: hidden;
height: 1%;
Upvotes: 16
Reputation: 1980
I have made some reach to do auto adjust of height for my project and I think I found a solution
[CSS]
overflow: auto;
overflow-x: hidden;
overflow-y: hidden;
This can be attached to prime div
(e.g. warpper, but not to body
or html
cause the page will not scroll down) in your css file and inherited by other child classes and write into them overflow: inherit;
attribute.
Notice: Odd thing is that my netbeans 7.2.1 IDE highlight overflow: inherit;
as Unexpected value token inherit
but all modern browser read this attribute fine.
This solution work very well into
I do not test it on previous versions of those browsers. If someone will check it, it will be nice.
Upvotes: 9
Reputation: 3547
Set a height to the last placeholder div.
<div class="row" style="height:30px;">
<!--placeholder to make the whole block has valid auto height-->
Upvotes: 1
Reputation: 4904
Just write "min-height: XXX;" And "overflow: hidden;" & you will be out of this problem Like this
min-height: 100px;
overflow: hidden;
Upvotes: 101
Reputation: 41
If you haven't gotten the answer yet, your "float:left;" is messing up what you want. In your HTML create a container below your closing tags that have floating applied. For this container, include this as your style:
#container {
clear:both;
}
Done.
Upvotes: 4
Reputation: 487
simply set the height to auto, that should fix the problem, because div are block elements so they stretch out to full width and height of any element contained in it. if height set to auto not working then simple don't add the height, it should adjust and make sure that the div is not inheriting any height from it's parent element as well...
Upvotes: 2
Reputation: 16377
<div>
should expand automatically. I guess that the problem is that you're using fixed height:100px;
, try replacing it with min-height:100px;
Upvotes: 0