How to auto adjust the <div> height according to content in it?
CssHtmlCss Problem Overview
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;
}
Css Solutions
Solution 1 - Css
Just write "min-height: XXX;" And "overflow: hidden;" & you will be out of this problem Like this
min-height: 100px;
overflow: hidden;
Solution 2 - Css
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>
Solution 3 - Css
Just write:
min-height: xxx;
overflow: hidden;
then div
will automatically take the height of the content.
Solution 4 - Css
I've used the following in the DIV that needs to be resized:
overflow: hidden;
height: 1%;
Solution 5 - Css
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
- firefox 18+
- chorme 24+
- ie 9+
- opera 12+
I do not test it on previous versions of those browsers. If someone will check it, it will be nice.
Solution 6 - Css
just use following
height:auto;
Solution 7 - Css
You could try, div tag will auto fit height with content inside:
height: fit-content;
Solution 8 - Css
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.
Solution 9 - Css
Don't set height
. Use min-height
and max-height
instead.
Solution 10 - Css
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...
Solution 11 - Css
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.
Solution 12 - Css
Set a height to the last placeholder div.
<div class="row" style="height:30px;">
<!--placeholder to make the whole block has valid auto height-->
Solution 13 - Css
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.
Solution 14 - Css
I just used
overflow: hidden;
And it was worked for me properly. This div had some of float lefted divs.
Solution 15 - Css
<div>
should expand automatically. I guess that the problem is that you're using fixed height:100px;
, try replacing it with min-height:100px;
Solution 16 - Css
use min-height
instead of height
Solution 17 - Css
I have fixed my issue by setting the position of the element inside a div to relative;
Solution 18 - Css
height:59.55%;//First specify your height then make overflow auto overflow:auto;
Solution 19 - Css
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.
Solution 20 - Css
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.
Solution 21 - Css
There is no need to set such as min-height
any more, only set work well:
overflow: hidden