我有一个稍微不寻常的CSS挑战需要克服.
我有一个两列布局,其中左列的宽度由主图像的宽度设置,右边允许填充剩余的空间.主图像下方有一个容器,其自然宽度可能大于主图像.但是,我希望这个div与主图像的宽度相同,并且要隐藏溢出.以下是我尝试这样做的努力:
.outer {
margin-right: 5px;
position: relative;
}
.left {
float: left;
}
.right {
width: auto;
overflow: hidden;
}
.contentOuter {
overflow: hidden;
}
.content {
width: 500px;
}
.inner {
background-color: grey;
color: white;
width: 100%;
height: 150px;
}
但正如您所看到的,无论我尝试什么,.contentOuter都会延伸到其内容的宽度.
我有一个主要的警告是,除了具有固定宽度的.content之外,我不希望我的CSS中有任何其他硬编码宽度;一切都应该是完全流动的,并且列的尺寸由.image img的尺寸决定.
所以,我在视觉上看起来像这样,但没有.content上的硬编码最大宽度:
.outer {
margin-right: 5px;
position: relative;
}
.left {
float: left;
}
.right {
width: auto;
overflow: hidden;
}
.contentOuter {
overflow: hidden;
}
.content {
max-width: 350px; /* Hard-coded for demo purposes */
}
.inner {
background-color: grey;
color: white;
width: 100%;
height: 150px;
}
最佳答案
解决这个问题的一种快速方法是简单地使用一些jQuery.它只需要两行代码即可实现.
var imgWidth = $('.image').width();
$('.content').width(imgWidth);
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!