CSS width in Percent related to the grand parent

心已入冬 提交于 2019-12-06 16:35:05

问题


How to relate width in percent to the grand parent?


回答1:


You could simply relate width of li s to the parent ul which in turn it relates to the grand parent div:

div{
    overflow: hidden;
    width: 100%;
}
ul{
    width: 1000%;
}
li{
    width: 3%;
}



回答2:


Use javascript:

var g = document.getElementById('grandparent');
var w = g.clientWidth;
if (w > 400)
    w *= .3;
var c = document.getElementsByClassName('child');
for (var i=0; i < c.length; i++) {
    c[i].style.width = w+'px';
}

I have a jsfiddle example.



来源:https://stackoverflow.com/questions/31317438/css-width-in-percent-related-to-the-grand-parent

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