This question already has answers here:
Make floating divs the same height
                                
                                    (5个答案)
                                
                        
                                4年前关闭。
            
                    
首先,我要说我是HTML和CSS的新手。到目前为止,我有一个包含div的容器,里面有两个div,一个向左浮动,一个向右浮动。向右浮动的一个包含一个400px图像,向左浮动的一个应包含一些文本,但是我希望背景color的高度保持与图像相同的高度。因为图像是由百分比而不是像素高度定义的,所以我不能仅将高度设置为无法缩放的高度。

这是HTML:

<div id="about">
    <div id="abouttext">
        djfd
    </div>
    <div id="aboutpic">
        <img src="images/photoph.png" alt="Yeah, that's me">
    </div>
</div>


以下是相关的CSS:

#about
{
    width: 70%;
    height: auto;
    margin: 0 auto;
}

#aboutpic
{
    width: 40%;
    float: right;
}

#abouttext
{
    width: 50%;
    background-color: #2A1F33;
    opacity: 0.6;
    height: auto;
    float: left;
    padding-top: 5%;
    padding-left: 5%;
    padding-right: 5%;
}


任何帮助深表感谢。

最佳答案

这里的一般原则是将包装器定义为display: table,将包含元素的定义为display: table-cell。请参阅以下内容:

img {

    width: 100%;
}

#about {
    width: 100%;
    height: auto;
    margin: 0 auto;
    display: table;
}
#aboutpic {
    background-color: blue;
    width: 45%;
    display: table-cell;
}
#abouttext {
    color: white;
    width: 45%;
    background-color: #2A1F33;
    display: table-cell;;
}

<div id="about">
    <div id="abouttext">djfd</div>
    <div id="aboutpic">
        <img src="http://www.freestockphotos.name/wallpaper-original/wallpapers/download-images-of-gentle-dogs-6866.jpg" alt="Yeah, that's not me" />
    </div>
</div>

10-05 20:58
查看更多