html - 向已包含内容的 div 添加标题

标签 html css

我有一个 200 像素乘 200 像素的小 slider 。然而,我真正喜欢的是在右上角有一个 100px x 20px 的标题框,带有彩色背景和一个单词。问题是,因为我已经在框( slider )中有内容,所以如果我尝试添加标题 div,它不会位于现有内容的顶部,而是将 200px 的大小扭曲 200px div。

HTML

<div class="trigger">
    <div tabindex="0" class="maincontent static">
        <div class="slider2">
            <img src="client9.jpg" height="200" width="200" />
            <img src="client10.jpg" height="200" width="200" />
            <img src="client11.jpg" height="200" width="200" />
            <img src="client2.jpg" height="200" width="200" />
        </div>
    </div>
</div>

CSS

.trigger {
    width: 200px;
    height: 200px;
}

.static {
    position: relative;
    width: 200px;
    height: 200px;
    text-align: center;
    font-size: 0;
    border-style: solid;
    border-width: 1px;
    border-color: #CCCCB2;
    border-radius: 5px; 
}

最佳答案

试试这个:

添加到 CSS

.slider2 { position: relative; }

.caption-box {
  position: absolute;
  right: 0;
  height: 20px;
  width:100px;
  background-color: red; // change to suit
  color: #fff; // change to suit
}

您的 HTML 已更改:

<div class="caption-box">Caption</div><!-- added this -->
<div class="slider2">
        <img src="client9.jpg" height="200" width="200" />
        <img src="client10.jpg" height="200" width="200" />
        <img src="client11.jpg" height="200" width="200" />
        <img src="client2.jpg" height="200" width="200" />
    </div>

关于html - 向已包含内容的 div 添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559165/

相关文章:

html - 如何在响应式图像上垂直居中文本?

javascript - 如果选择了另一个 li,则设置字体粗细

javascript - 如何在 $(document).ready 之前将 jQuery.js 包含在自己的 .js 中?

javascript - Jquery 切换和隐藏

html - 将 div 中的图像与下方的文本对齐

javascript - 如何使用 jquery 操作元素,使它们匹配它们的 css-Selector "nth-of-type"

javascript - 如何在为每个输入 jquery 评估 .val() 之前运行一些特定代码

html - @font-face 自定义字体的问题

CSS div 有自己的想法 - 不遵守 Float 和 Width 值

html - 使用 wkhtmltopdf 生成 pdf 在分页符处插入空格