css - Bootstrap 网格中的右边距错误

标签 css margin

我正在使用 Bootstrap 设计网页。我定位了一个绝对 <div> (透明文本覆盖)在我编码为在 Bootstrap 列框架内响应的图像上。我的文字叠加在绝对<div>与左边距的图像对齐但超出右边距(即不适合右侧设置的 Bootstrap 列)。关于如何使透明文本叠加层的右边界在列框架内对齐以响应图像的任何建议?

参见 https://cgriffith5.github.io/research.html

HTML:

<div class="container-fluid">
    <div class="row">  
        <div class="col-md-5 container">    
        <img src="assets/img.jpg" class="img-rounded" style="width:100%"> 
        <div class="overlay">Text</div>   
        </div>

CSS:

.container {
    position: relative; 
    max-width: 410px; /* Maximum width */
    margin: 0 auto; /* Center it */
}

.overlay {
    position: absolute; /* Position the background text */
    overflow: hidden;
    bottom: 0; /* At the bottom. Use top:0 to append it to the top */
    background: rgba(0, 0, 0, 0.5); /* Black background with 0.5 opacity */
    color: #f1f1f1; /* Grey text */
    width: 100%; /* Full width */
    padding: 15px; /* Some padding */
}`

最佳答案

像这样制作你的 HTML 代码:

<div class="row">  
    <div class="col-md-5 container">
      <div style="position: relative"> <!-- Make a div and give it a position of relative -->
         <img src="assets/img.jpg" class="img-rounded" style="width:100%"> 
         <p class="overlay">Text</p>
      </div>
    </div>
</div>

我们将包含 img 和叠加层的整个 div 包装为相对位置,并在其中使叠加层具有绝对位置。

关于css - Bootstrap 网格中的右边距错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703166/

相关文章:

css - 负相对定位firefox chrome兼容性

html - 多重自​​动 margin

html - 滚动条显示在页面底部

css - 如何仅使用 CSS 动态调整布局

javascript - 获取iframe的内容

html - 如何在 Html 中使用内联 block 显示表格和图像?

html - CSS3 背景尺寸 :cover showing unexpected behavior

html - 翻转卡片,背面卡片上的图像在第一次翻转时出现缓慢

html - CSS min-height 添加到子元素的底部边距

html - 边距插入父元素而不是自身