html - 如何在一个 Div 中正确地居中一个 Div

标签 html css twitter-bootstrap

它需要响应,所以它似乎可以工作,直到您调整到较小的屏幕宽度并且元素开始不成比例。这是一个 JSfiddle 链接:https://jsfiddle.net/gn8L0rxu/

我需要在中间轻拍“home-slider-caption col-sm-12”,并根据不同的浏览器大小正确调整大小。

section.home-slider {
  background-repeat: no-repeat;
  background-position: center;
  background-color: black;
  -moz-background-size: cover;
  -o-background-size: cover;
  -webkit-background-size: cover;
  background-size: cover;
  min-height: 630px;
  position: relative;
}

section.home-slider .home-slider-caption {
  width: 60%;
  position: absolute;
  top: 50%;
  right: 25%;
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  z-index: 1;
}
section.home-slider h1 {
  color: #fff;
  letter-spacing: -4px;
  font-size: 5em;
  font-weight: 100;
  line-height: 1;
  text-align: center;
}
section.home-slider p {
  font-size: 1.3em;
  color: #fff;
  margin-bottom: 20px;
  text-align: center;
}
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <section class="home-slider" id="top">
        <div class="container">
            <div class="home-slider-caption col-sm-12">
                <h1>Sell Your Textbooks</h1>
                <p>We'll pay top dollar for your used books!</p>
                <a href="sell.html" class="btn btn-outline btn-primary btn-icon active btn-lg btn-rounded">Start Selling!<span class="fa fa-angle-right icon-with-btn"></span></a>
            </div>
        </div>
    </section>

最佳答案

如果我真的理解问题,你必须这样做:

section.home-slider .home-slider-caption {
  position: absolute;
  top: 50%;
  left: 50%;
  -moz-transform: translate(-50%,-50%);  -ms-transform: translate(-50%,-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);
  z-index: 1;
}

不要在没有 .row 的情况下使用 col-*

关于html - 如何在一个 Div 中正确地居中一个 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39786982/

相关文章:

javascript - 保存位置的可排序 Div

javascript - IE后台闪烁

css - 如何更改 Bootstrap Datatable 标题列的宽度

html - Bootstrap 输入组插件样式

html - 如何使用 Bootstrap 添加图像?

javascript - 如何在 HTML5 中拖动时更改光标图标?

jquery - 无法处理 JQuery 形式的 Spring MVC 中的 JSON 数据

javascript - 当通过 github.io 部署时,jQuery 在 Codeacademy 上可以运行,但在 Firefox 中则不行

css - CSS 中类似 OO 的行为 : inheritance possible?

CSS:如何使水平图像滚动,每列有两个图像?