html - 替代 a 标签内的 div,并使用 text-align right 进行右填充

标签 html css text-align

我有以下 html:

<div id="image">
 <a href="link" title="some link"><img src="someimage"><p id="imagecaption">Some Link</p></a>
</div>

和CSS:

#mainimage a {
text-decoration: none;
border-bottom: 0;
}

#imagecaption {
position: absolute;
bottom: 0;
width: 100%;
text-align: right;
background-color: #000;
background-color: rgba(0, 0, 0, 0.6);
}

这在图像底部提供了一个漂亮的半透明条,我用它来描述图像中的内容。图像和栏都是可点击的,并且会转到同一个地方。

不过,我目前有两个问题。首先显然你不能使用像 <p> 这样的东西和 <div><a> 里面标签。我应该改用什么?

其次,理想情况下,我想将栏中的文本从右侧边缘填充 10 像素,这样文本就不会紧靠右侧。我不能只使用 padding-right: 10px因为我已经指定了宽度,所以会导致边框模型出现问题。

有什么想法吗?谢谢。

最佳答案

考虑使用 <span>而不是 <p>

#imagecaption {
    position:absolute;
    left:0; right:0; bottom:0;
    padding-right:10px; /* avoids box sizing issue */
    text-align:right;
    background:#000; /* for compatibility, use background not background-color */
    background:rgba(0,0,0,0.6);
}

关于html - 替代 a 标签内的 div,并使用 text-align right 进行右填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21366302/

相关文章:

html - 应用 "word-break"css 属性后文本对齐受到干扰

php - 在 HTML 中嵌入 <form> 的输出

javascript - Element.matches 选择器限制

CSS : text-align:justify doesn't work in textarea for Internet Explorer

javascript onchange 属性

css - 如何仅使用 CSS 在鼠标悬停时在单独的 div 中弹出图像?

html - 按钮中间的文字 - 无法居中

JavaScript/jQuery 类修改

javascript - 如何平移 map ,以便当 div 部分覆盖 map 时所有标记始终可见? (谷歌地图 API v3)

css在两个div之间添加一条分割线