html - 内容取决于另一个 <div> 下 <div> 的宽度

标签 html css

Example

您好,我是 HTML 和 CSS 新手。我想让 div 宽度取决于其中内容的大小,并且它们必须显示在彼此下方。我尝试使用 display: inline-block,但这会使描述移动到标题。如果我不使用 display: inline-block,则 div 的宽度不正确。如果我将其设置为 auto,则 div 的宽度将变为 bottom_part 的宽度。

<div class="slider_test">
    <div class="slide_test">
        <a class="slide_img_link" href="http://google.com">
            <img src="cat.png" class="slide_img">
        </a>
        <div class="bottom_part">
            <div class="bottom_text1">
                <h4>Header</h4>
            </div>
            <div class="bottom_text2">
                <p1>Description</p1>
            </div>
        </div>
    </div>
</div>

.slider_test {
    width: 600px;
    height: 300px;
}

.slide_test {
    position: relative;
    height: 100%
}

.slide_img_link {
    position: absolute;
    width: 100%;
    height: 100%;
}

.slide_img {
    height: 100%;
    width: 100%;
}

.bottom_part {
    position: absolute;
    height: 50%;
    width: 50%;
    bottom: 0;
}

.bottom_text1 {
    background-color: red;
    vertical-align: top;
    width: auto;
    bottom: 0;
}

.bottom_text1 h4 {
    margin: 0;
}

.bottom_text2 {
    background-color: aqua;
    vertical-align: top;
    width: auto;
    bottom: 0;
}

有谁知道我如何才能按预期显示它?

最佳答案

Description div 放入另一个 div 中,并设置两个 Headerdisplay code> 和 Description divinline-block

.slider_test {
width: 600px;
height: 300px;
}

.slide_test {
position: relative;
height: 100%
}

.slide_img_link {
position: absolute;
width: 100%;
height: 100%;
}

.slide_img {
height: 100%;
width: 100%;
}

.bottom_part {
position: absolute;
height: 50%;
width: 50%;
bottom: 0;
}

.bottom_text1 {
background-color: red;
vertical-align: top;
width: auto;
bottom: 0;
display: inline-block;
}

.bottom_text1 h4 {
margin: 0;
}

.bottom_text2 {
background-color: aqua;
vertical-align: top;
width: auto;
bottom: 0;
display: inline-block;
}
<div class="slider_test">
<div class="slide_test">
    <a class="slide_img_link" href="http://google.com">
        <img src="cat.png" class="slide_img">
    </a>
    <div class="bottom_part">
        <div class="bottom_text1"><h4>Header</h4>
        </div>
      <div>
        <div class="bottom_text2">
            <p1>Description</p1>
        </div>
      </div>
    </div>
</div>
</div>

关于html - 内容取决于另一个 <div> 下 <div> 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472358/

相关文章:

javascript - HTML 列表并计算总价

css - 如何使用 DropZone.js 获得质量更好的缩略图?

javascript - 如何动态更改字体大小以使其始终适合其父容器

javascript - 在 AngularJS 中为每个第 n 个元素自定义 ng-repeat

html - 如何使 Div 居中并使其与 HTML 中的其他元素重叠

javascript - 在现代浏览器上双击选择框不可能吗?

java - 使用文本文件中的数据创建重定向链接

html - CSS - 如何将样式应用于静态生成列表中的第一个元素

css - CSS 段落元素问题

jquery-ui 可拖动 : change clone helper's css margin correctly