css div中心多行文本垂直和水平与背景图像

时间:2016-01-20 23:26:26

标签: html css html5 image center

我搜索过Stackoverflow,找不到有效的解决方案。

似乎在div中垂直和水平居中很难。

我正在使用这个css:

.title {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 400%;
}

.subtitle {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 150%;
}

.subnote {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 75%;
}

.outer {
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
}

.middle {
    display: table-cell;
    vertical-align: middle;
}

.innie {
    width: 1000px;
    height: 515px;
    background-image: url("bkgrnd3.png");
    position:absolute;
    left:50%;
    top:50%;
    margin: -257px 0 0 -500px;
    text-align: center;
}

和这个html:

<!DOCTYPE html>
<html>
    <head>
        <title>Timothy Eldon Official</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="eldon.css">
    </head>
    <body>
        <div class="outer">
            <div class="middle">
                <div class="innie">
                    <span class="title">
                        Timothy Eldon
                    </span>
                    <br/>
                    <span class="subtitle">
                        Author
                    </span>
                    <br/>
                    <span class="subnote">
                        (amateur)
                    </span>
                </div>
            </div>
        </div>
    </body>
</html>

它将页面上的所有内容相对集中,但我无法将文本垂直居中。

似乎有很多想法,但我已经尝试了几个,但没有任何作用。

3 个答案:

答案 0 :(得分:3)

margin: -257px 0 0 -500px; left:50%; top:50%; width: 1000px; height: 515px;上有一些不必要的css,例如.innie,这会导致你的中心对齐。

我想这就是你想要的。

<强>已更新

<强> CSS

.title {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 400%;
}

.subtitle {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 150%;
}

.subnote {
    font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
    font-size: 75%;
}

.outer {
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
    background:url('https://placeimg.com/600/300/any')no-repeat;
    background-position: center;
}

.middle {
    display: table-cell;
    vertical-align: middle;
}

.innie {
    text-align: center;
}

jsFiddle演示 - https://jsfiddle.net/8dphnor5/

答案 1 :(得分:2)

flex可以帮助你:

.title {
  font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
  font-size: 400%;
}
.subtitle {
  font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
  font-size: 150%;
}
.subnote {
  font-family: "Book Antiqua", "Palatino Linotype", Palatino, serif;
  font-size: 75%;
}
.outer {} .middle {} .innie {
  width: 1000px;
  height: 515px;
  background-image: url("http://lorempixel.com/1000/515");
  text-align: center;
  text-shadow: 0 0 1px white;
}
html,
body {
  height: 100%;/* min-height is buggie in IE */
}
body,
.innie {
  display: flex;
  flex-direction: column;
  /* now centers things in X,Y axis */
  align-items: center;
  justify-content: center;
}
<div class="outer">
  <div class="middle">
    <div class="innie">
      <span class="title">
                        Timothy Eldon
                    </span>
      <br/>
      <span class="subtitle">
                        Author
                    </span>
      <br/>
      <span class="subnote">
                        (amateur)
                    </span>
    </div>
  </div>
</div>

答案 2 :(得分:1)

这里很简单{{3}} ..希望它有所帮助。

<强> HTML

<div>
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>

<强> CSS

div {
  display: table;
  width: 250px;
  height: 500px;
  text-align: center;
  border: 1px solid red;
}

span {
  display: table-cell;
  vertical-align: middle;

}