如何在背景图像中居中文字?

时间:2015-11-14 22:54:45

标签: html css

这段文字没有在div中正确居中。任何帮助都会很棒!这是CSS桌面文件http://www.cssdesk.com/Vc8yF正如您所看到的,文本位于左侧而非中心位置。请帮忙!谢谢。

代码段:

body {
  margin: 0;
  padding: 0;
}
.header {
  height: 50px;
  background-color: #191919;
}
.navcontainer {
  float: right;
  margin-right: 100px;
}
.nav li {
  display: inline;
  font-family: arial;
  color: white;
  font-family: 'Titillium Web', sans-serif;
  font-size: 14px;
  font-weight: 300;
  padding-left: 50px;
}
.billboard {
  height: 620px;
  width: 100%;
  background-repeat: no-repeat;
  background-size: 100%;
  text-align: center;
  background-position: center;
  background-image: url('http://i300.photobucket.com/albums/nn18/ojijimanuel/Midtown_Manhattan_and_Times_Square_district_2015_zpsqimz80ub.jpg');
}
h1 {
  margin: 0;
  text-align: center;
  color: white;
  font-family: arial;
  font-size: 50px;
}
<link href='https://fonts.googleapis.com/css?family=Titillium+Web:300' rel='stylesheet' type='text/css'>
</head>

<body>

  <div class="header">
    <div class="navcontainer">
      <ul class="nav">
        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
      </ul>
    </div>
  </div>


  <div class="billboard">
    <h1>SAMPLE TEXT</h1>
  </div>


</body>

</html>

3 个答案:

答案 0 :(得分:2)

我假设您想要垂直和水平居中文本:

查看result here

我使用修复程序创建了一个新的cssDesk。总而言之,我在广告牌div中添加了一个position: relative,然后添加了一个带有position:absolute的包装div,并带有一个小技巧来将文本置于其中。

代码段:

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
}
.header {
  height: 50px;
  background-color: #191919;
}
.navcontainer {
  float: right;
  margin-right: 100px;
}
.nav li {
  display: inline;
  font-family: arial;
  color: white;
  font-family: 'Titillium Web', sans-serif;
  font-size: 14px;
  font-weight: 300;
  padding-left: 50px;
}
.billboard {
  height: 620px;
  width: 100%;
  background-repeat: no-repeat;
  background-size: 100%;
  text-align: center;
  background-position: center;
  background-image: url('http://i300.photobucket.com/albums/nn18/ojijimanuel/Midtown_Manhattan_and_Times_Square_district_2015_zpsqimz80ub.jpg');
  position: relative;
}
.text-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 200px;
  margin-left: -200px;
  margin-top: -29px;
}
h1 {
  margin: 0;
  text-align: center;
  color: white;
  font-family: arial;
  font-size: 50px;
}
&#13;
<link href='https://fonts.googleapis.com/css?family=Titillium+Web:300' rel='stylesheet' type='text/css'>
</head>

<body>

  <div class="header">
    <div class="navcontainer">
      <ul class="nav">
        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
      </ul>
    </div>
  </div>


  <div class="billboard">
    <div class="text-wrapper">
      <h1>SAMPLE TEXT</h1>
    </div>
  </div>


</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是一种非常奇怪的行为,但与浮动一样。

这是因为你的导航系统对于它的容器而言太大了,所以浮动类型的扩展&#34;展开&#34;它的。

你可以添加clear:right;在你身上h1或身高:50px;在.navcontainer上,它将进行交易

答案 2 :(得分:0)

这是由导航的浮动造成的。导航高于它的包装,迫使元素波纹(广告牌)缩小。 有多种方法可以解决此问题,请选择以下方法之一:

  • 调整导航<ul>上的边距,使其适合标题

  • 添加溢出:隐藏到标题

  • 向广告牌添加明确:右/两者

相关问题