1

我有一个如下所示的 DIV。我的问题是,只要滚动条可见,它就会显示在 300px 宽度之外。但无论滚动条如何,我都需要为 DIV 固定 300px 宽度。我怎样才能做到这一点?

<div style="width:300px; overflow:auto; float:left;"></div>

谢谢,

4

3 回答 3

1

我要么将 div 放在 300px 容器中,然后将溢出应用到内部 div。这应该将其限制为您想要的尺寸。如果这不是可取的,您可以尝试一下,看看是否应用box-sizing:border-box,但旧版本的 IE 不支持。

于 2013-03-18T14:49:49.237 回答
0

希望我能通过这个答案正确理解这个问题,如果不是,请指出。

http://jsfiddle.net/hmCFh/1/

HTML

<div>
Lorem ipsum dolor sit amet, vehicula vulputate, velit sapien suspendisse, ipsum nunc, integer velit aptent non quam. Erat sit consequat lectus, velit risus taciti, in sed, risus tincidunt. Egestas lacus vel voluptatem in volutpat. Sapien lacus suspendisse nec velit. Integer molestie erat, erat lectus luctus ipsum. Nulla massa netus. Odio nunc maecenas modi tortor cras in. Quis nonummy ridiculus vel sed, luctus nulla lectus bibendum justo donec.

Lorem ipsum dolor sit amet, vehicula vulputate, velit sapien suspendisse, ipsum nunc, integer velit aptent non quam. Erat sit consequat lectus, velit risus taciti, in sed, risus tincidunt. Egestas lacus vel voluptatem in volutpat. Sapien lacus suspendisse nec velit. Integer molestie erat, erat lectus luctus ipsum. Nulla massa netus. Odio nunc maecenas modi tortor cras in. Quis nonummy ridiculus vel sed, luctus nulla lectus bibendum justo donec.    

Lorem ipsum dolor sit amet, vehicula vulputate, velit sapien suspendisse, ipsum nunc, integer velit aptent non quam. Erat sit consequat lectus, velit risus taciti, in sed, risus tincidunt. Egestas lacus vel voluptatem in volutpat. Sapien lacus suspendisse nec velit. Integer molestie erat, erat lectus luctus ipsum. Nulla massa netus. Odio nunc maecenas modi tortor cras in. Quis nonummy ridiculus vel sed, luctus nulla lectus bibendum justo donec.        
</div>

CSS

div {
    width:300px; 
    height:300px;
    overflow-y:scroll; 
    float:left;
    background:orange;    
}
于 2013-03-18T14:51:39.347 回答
0

您可以使用overflow-y: scroll这样滚动条将始终可见,然后将 div 的宽度设置284px为考虑大约。滚动条的宽度。

于 2013-03-18T14:52:31.857 回答