【问题标题】:shrink-to-fit-div containing more elements?缩小以适应 div 包含更多元素?
【发布时间】:2012-05-24 13:26:32
【问题描述】:

基本问题是:如何在元素包含其他元素的同时缩小以适应元素?

目标是在(居中)图像上设置(居中)菜单,其宽度和高度应与图像尺寸相关。 所有这些都是响应式的,这意味着没有绝对的尺寸!

这是示例代码:

<div id="menu">
    <img src="picture.jpg" />
    <div id="left">
        test1
    </div>
    <div id="right">
        test2
    </div>
</div>
#menu{
position:relative;
display: table; /*tried inline-block as well */
text-align: center;
line-height: 1;
}

#menu img{
height: 90%;
position:relative;
}

#left{
width: 46%;
background-color: #ffcdcc;
float: left;
text-align: right;
}

#clear{
clear: both;
}

#right{
width: 46%;
background-color: #324344;
float: right;
text-align: left;
}

这就是它的样子:

____________________________________
|                                  |
|  ------------------------------  |
|  |                            |  |
|  |     p i c t u r e          |  |
|  |                            |  |
|  |                            |  |
|  |                            |  |
|  |                            |  |
|  |    left <button> right     |  |
|  |                            |  |
|  ------------------------------  |
|                                  |
------------------------------------

图片的高宽比始终相同。不过,它的总大小取决于用户窗口。

我只是不能让“菜单”div 环绕在“左”和“右”div 上同时定位。

这甚至可能吗?我什至还没有谈论浏览器兼容性......

【问题讨论】:

  • 这里是你的代码:jsfiddle.net/sdvnh
  • 除非您对子元素使用“position:absolute”,否则您不需要使用“position:relative”。

标签: html css


【解决方案1】:

看看这是否有效:http://jsfiddle.net/sdvnh/1/

变化:

#menu {
display: block;
}

#menu img{
height: 90%;
width: 90%;
}

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多