开发者

Making a vertically scrolling parent div which does not cause overflow of children

开发者 https://www.devze.com 2023-01-04 11:30 出处:网络
I have something as follows: <div style=\"float: left; height: 20px; overflow-y: scroll\"> <div>hello this is a test</div>

I have something as follows:

<div style="float: left; height: 20px; overflow-y: scroll">
  <div>hello this is a test</div>
  <div>hello this is another test</div>
  <div>t1</div>
  <div>t2</div>
  <div>t3</div>
</div>

The parent div grows to the appropriate width to fit the largest child (which is what I want). However, the vertical scrollbar c开发者_运维百科auses the inner width to be smaller, which causes the largest child to wrap.

Is there a way to get the parent to grow to the width of the largest child + the width of the scroll bar? I've tried every margin trick I know, but nothing.


Found one solution, but open to any other suggestions.

By adding a right-float child to the longest child (or, if unsure which is the longest child, to every element):

<style type="text/css">
.stretcher { float: right; right: -50px; width: 50px; height: 1px; }
</style>

<div style="float: left; height: 20px; overflow-y: scroll">
  <div>hello this is a test</div>
  <div>hello this is another test<div class="stretcher"></div></div>
  <div>t1</div>
  <div>t2</div>
  <div>t3</div>
</div>


Adding some horizontal padding is sufficient in my tests with IE6/7

<div style="float: left; height: 50px; overflow-y: scroll; padding: 0 50px 0 4px;">
  <div>hello this is a test</div>
  <div>hello this is another test lorem ipsum</div>
  <div>t1</div>
  <div>t2</div>
  <div>t3</div>
</div>


I had the same issue earlier but I dont think its possible to increase the size of an element dynamically using CSS. But what you can try is adjusting the height dynamically using Javascript.

In jQuery you can use height() method everytime you add or remove a child element to the parent.

I did this in one of my projects but it still had issues with Opera (IE was fine ;) ). But since we don't have many clients using Opera, I ignored the issue.

FYI: I just skimmed thru your question. Please correct me if I didn't understand your problem properly

0

精彩评论

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