【问题标题】:Bootstrap divider displaced on smaller screensBootstrap 分隔线在较小的屏幕上移位
【发布时间】:2014-10-01 07:13:47
【问题描述】:

我目前正在开发一个网站,这是我的第一个引导网站之一。

面临一个我已经尝试解决了几个小时的问题。

我正在将我在 Photoshop 中制作的分隔符或分隔符(png 图像)放在部分的边缘。

在桌面尺寸上看起来像这样:

https://www.dropbox.com/s/qxf7dheb0k79q2b/Screenshot%202014-09-30%2023.02.50.png?dl=0

但在较小的屏幕上会发生这种情况:

https://www.dropbox.com/s/w4je7milallfrqn/Screenshot%202014-09-30%2023.10.50.png?dl=0

我确定是因为我的 CSS 不好。

这是我的html和css:

<section class="hero-section text-center">
  <img class="separator img-responsive"src="images/separator.png">
  <div class="container">
    <h1>Download Now</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <hr>
    <button class="btn btncta">Download Now</button>
    <p class="small">*Needed for developers</p>
  </div>
</section>

CSS:

.separator{
display: block;
margin-left: auto;
margin-right: auto;
position: relative;
top:318px;

}

有什么办法可以解决这个问题?

是否使用图像作为分隔线已过时(可能是)?

【问题讨论】:

  • 为什么要使用相对定位?那就是问题所在。而是将其放在 html 中的正确位置(可能是 &lt;hr&gt; 当前所在的位置)。你也是正确的,你可以在没有图像的情况下做到这一点

标签: html css twitter-bootstrap


【解决方案1】:

使用那种分隔图像方法并没有错,但你做错了。只需像这样替换您的 CSS:

.separator{
display: block;
margin-left: auto;
margin-right: auto;
position: relative;
top:0px;
width:100%; height:1px;
}

你的 HTML 是这样的:

<section class="hero-section text-center">

  <div class="container">
    <h1>Download Now</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <hr>
    <button class="btn btncta">Download Now</button>
    <p class="small">*Needed for developers</p>
  </div>
  <img class="separator "src="images/separator.png">
</section>

我做了一个bootply,所以你可以看到(不需要背景颜色,只是为了让你看到效果,因为我没有你的图像)

根据新信息添加新方法:

/* CSS used here will be applied after bootstrap.css */
 body {
    background:#f00;
}
.separator {
    display: block;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    top:0px;
    background:#f00;
    width:100%;
    height:1px;
}
.hero-section {
    background:#fff url('http://www.customstairsandmouldings.com/images/circle.jpg') no-repeat center bottom; / we center the background and position it at the bottom of the div */
    padding-bottom: 100px; /* padding-bottom has to be enough to give room to the image in the background, so if image height is 80px, padding bottom should be at least 80px, or better 100px to add spacing */
}

new Bootply here

【讨论】:

  • 谢谢法比奥。不幸的是,它没有用。这是结果(我只改变了高度)dropbox.com/s/7njgpruugj84k3d/…
  • 那是因为我以为你只使用了一条直线,哈哈。所以只需删除宽度和高度属性就可以了,但老实说,在这种情况下,最好的办法是使用图像作为背景,这样它总是位于 div 的底部(例如下一个 div 的顶部)
  • 哦!它的工作原理啊哈哈哈这样一个菜鸟。谢了哥们。该方法(背景图像方法)的问题是我不知道如何同时获得图像和蓝色作为背景。
  • 啊,大声笑,我将其添加为对答案的编辑,因为我不想给您留下半答案,请查看编辑
  • 太棒了!非常感谢朋友,不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多