javascript - 在固定位置 Div 中水平居中文本

标签 javascript html css position

我在下面的 div 中有文本,但我似乎无法让它居中对齐:

<div class="banner_tron">
            <div class="bg-box-100-grey">
                    <span class="SansFontBold ex-lrg-60 center color-white">Hello World</span>
                    <div class="div-wrapper">
                    <p class="SansFontBold ex-lrg-20 center color-white">Have a Wonderful Day</p>
                    </div>
            </div>
        </div>

此代码的 CSS 如下:

.banner_tron{
  bottom: 0;
  height: 150px;
  left: 0;
  margin: auto;
  position: absolute;
  top: 0;
  width: 300px;
  white-space: nowrap;
}     
   .center {
    margin-left: auto;
    margin-right: auto;
    margin-top: auto;
    margin-bottom:auto;
    } 

/* background-boxing */
.bg-box-100-grey{
    position: fixed;
    left: 0;
    right:0;
    width: 100%;
    background-color:  rgba(108,108,108, .7);
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border: none;
}
.ex-lrg-60 {
    font-size: 60px;
    font-weight: bold;
}

我已经为 .center 类尝试了几种不同的建议,但似乎没有任何效果(javascript 定位、对齐元素和 webkit 转换)。关于为什么这不起作用以及我可以做些什么来解决它的任何建议?

最佳答案

text-align:center 添加到您的 .banner_tron

.banner_tron{
  bottom: 0;
  height: 150px;
  left: 0;
  margin: auto;
  position: absolute;
  top: 0;
  width: 300px;
  white-space: nowrap;
  text-align:center;
}     
   .center {
    margin-left: auto;
    margin-right: auto;
    margin-top: auto;
    margin-bottom:auto;
    } 

/* background-boxing */
.bg-box-100-grey{
    position: fixed;
    left: 0;
    right:0;
    width: 100%;
    background-color:  rgba(108,108,108, .7);
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border: none;
}
.ex-lrg-60 {
    font-size: 60px;
    font-weight: bold;
}
<div class="banner_tron">
  <div class="bg-box-100-grey">
    <span class="SansFontBold ex-lrg-60 color-white">Hello World</span>
    <div class="div-wrapper">
      <p class="SansFontBold ex-lrg-20 center color-white">Have a Wonderful Day</p>
    </div>
  </div>
</div>

希望对您有所帮助:)

关于javascript - 在固定位置 Div 中水平居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682980/

相关文章:

CSS:清除 3 列 Div 布局后的内容

html - <html> 宽度小于其背景

html - 在 HTML 中为按钮设置 Action

css - 如何在浏览器大小中调整变换比例的大小?

javascript - React Redux 调度有效负载到达带有数据的情况,但不保存它

javascript - 使用 JavaScript/ActionScript 监视来自同一域的浏览器窗口

javascript - 如何使用类似于 PHP 的 preg_match_all() 的 JavaScript 中的正则表达式匹配多次出现?

javascript - 将 ES6 类方法作为要在函数内部调用的函数参数传递

css - 覆盖孙元素的父 .resizable 属性,也许使用 css?

javascript - 如何检查网格的特定位置是否有元素?