首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >100%宽度导致div在容器中时从屏幕上消失

100%宽度导致div在容器中时从屏幕上消失
EN

Stack Overflow用户
提问于 2017-04-24 18:00:54
回答 3查看 31.7K关注 0票数 3

因此,我在将容器中的div与Bootstrap对齐时遇到了一些问题。

我想做的是让div在容器宽度内扩展全宽。

它可以在容器的左侧工作,但不能在右侧工作。它会消失在屏幕之外。

HTML:

代码语言:javascript
复制
<!-- Image With Action Section -->
    <section class="special_section">
        <div class="container-fluid" style="max-height: 25%">
            <div class="row">
                <div class="col-sm-12">
                    <div class="caption-2">
                        <img src="background-image-url" class="img-full-width img-responsive" alt="" />
                        <div class="container">
                            <div class="action-footer">
                                <div class="col-sm-10">
                                    <h4 class="caption-text">Title</h4>
                                </div>
                                <div class="col-sm-2">
                                    Link
                                </div>
                            </div>
                            <div class="caption">
                                <h4>Title</h4>
                                <p>Content</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

CSS:

代码语言:javascript
复制
.caption-2 .action-footer {
    margin: -1px 0 0 0;
    padding: 10px;
    font-size: 15px;
}
@media (min-width:500px) {
    .caption-2 .action-footer {
        margin:0;
        position: absolute;
        bottom: 0;
        height: 10%;
        width: 100%;
    }
}

.action-footer .col-md-12 {
    margin-top: -10px;
}

.action-footer div是我需要在父容器中保持100%宽度的那个。相反,它的宽度太大了。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-24 18:10:46

你的主要问题实际上在这里:

代码语言:javascript
复制
@media (min-width:500px) {
    .caption-2 .action-footer {
        margin:0;
        position: absolute;
        bottom: 0;
        height: 10%;
        width: 100%;
    }
}

将其更改为:position: relative;考虑到您是为移动设备构建的,它应该可以完成这项工作。每当使用container-fluid,时,大多数元素都有一个相对位置( relative ),因此它们可以与屏幕宽度重新对齐。

票数 3
EN

Stack Overflow用户

发布于 2017-04-24 18:02:40

您需要应用CSS属性box-sizing: border-box;

代码语言:javascript
复制
.action-footer {
    box-sizing: border-box;
}
票数 11
EN

Stack Overflow用户

发布于 2017-04-24 18:15:16

只需替换containeraction-footer的顺序就可以得到想要的结果,还可以将left:0px添加到ation-footer中,这样它总是从body的左侧开始。

代码语言:javascript
复制
@media (min-width:500px) {
  .caption-2 .action-footer {
    margin: 0;
    position: absolute;
    bottom: 0;
    height: 10%;
    width: 100%;
    left:0px;
  }
}

下面是工作代码:

代码语言:javascript
复制
.caption-2 .action-footer {
  margin: -1px 0 0 0;
  padding: 10px;
  font-size: 15px;
}

@media (min-width:500px) {
  .caption-2 .action-footer {
    margin: 0;
    position: absolute;
    bottom: 0;
    height: 10%;
    width: 100%;
    left:0px;
  }
}

.action-footer .col-md-12 {
  margin-top: -10px;
}
代码语言:javascript
复制
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="special_section">
  <div class="container-fluid" style="max-height: 25%">
    <div class="row">
      <div class="col-sm-12">
        <div class="caption-2">
          <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" class="img-full-width img-responsive" alt="" />
          <div class="action-footer">
            <div class="container">
              <div class="col-sm-10">
                <h4 class="caption-text">Title</h4>
              </div>
              <div class="col-sm-2">
                Link
              </div>
            </div>
            <div class="container">
            <div class="caption">
              <h4>Title</h4>
              <p>Content</p>
            </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43585134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档