Collapse a DIV hiding its content even if defined in percentage

纵然是瞬间 提交于 2019-12-08 04:48:05

问题


I'm trying to obtain a DIV that could collapse on click. For this simple example, click is triggered directly on the entire DIV.

<div id='fixed'>
    <input type='text'>
</div>
#fixed { width: 200px; } 
#fixed input { width: 180px; }
.short_fixed { width: 50px !important; }
$('#fixed').click(function(){
    $(this).toggleClass('short_fixed');
});

My example shows 2 cases: first one use "fixed" width object, while the second one use "percentage" width.

When clicked, first DIV truncates without resizing its content, resulting in hiding overflow content, but it requires to have px based width that is not so desirable

Instead, second one adapt content according to container's width, it lets me to use %, but does not hyde content on collapse as I would like.

So, I would like to set content width in % of container's width (that could have % width too) like in second example, BUT with behaviour of first one in case of container's collapsing.


回答1:


add

#variable input { width:100%; min-width: 180px;}

to your css, so it will keep percentage but content will hide on collapse

See demo here




回答2:


toggling bettween display: block and display: none with the toggle function seems to be yout best bet



来源:https://stackoverflow.com/questions/28068724/collapse-a-div-hiding-its-content-even-if-defined-in-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!