开发者

How to auto adjust the <div> height according to content in it?

开发者 https://www.devze.com 2023-01-01 19:40 出处:网络
I have a <div> which needs to be auto adjusted according to the conte开发者_StackOverflownt in it.How can I do this?Right now my content is coming out of the <div>

I have a <div> which needs to be auto adjusted according to the conte开发者_StackOverflownt 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;
}


Just write "min-height: XXX;" And "overflow: hidden;" & you will be out of this problem Like this

min-height: 100px;
overflow: hidden;


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>


Just write:

min-height: xxx;
overflow: hidden;

then div will automatically take the height of the content.


I've used the following in the DIV that needs to be resized:

overflow: hidden;
height: 1%;


You could try, div tag will auto fit height with content inside:

height: fit-content;


just use following

height:auto;


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.


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.


Don't set height. Use min-height and max-height instead.


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.


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...


Set a height to the last placeholder div.

    <div class="row" style="height:30px;">
    <!--placeholder to make the whole block has valid auto height-->


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.


I just used

overflow: hidden;

And it was worked for me properly. This div had some of float lefted divs.


<div> should expand automatically. I guess that the problem is that you're using fixed height:100px;, try replacing it with min-height:100px;


use min-height instead of height


I have fixed my issue by setting the position of the element inside a div to relative;


height:59.55%;//First specify your height then make overflow auto overflow:auto;


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.


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.


There is no need to set such as min-height any more, only set work well:

overflow: hidden


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消