为什么文本不会在 <section> 背景中居中对齐?

Why won't text will not align to center in <section> background?

我正在学习 HTML 和 CSS。我的 <h3> 文本不会与页面中心对齐。我知道这与背景宽度为 50% 有关,但我需要这样才能使页面有两种颜色。有什么解决办法吗?

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Oswald:300,400" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Prociono" rel="stylesheet">
<link rel="stylesheet" href="font-awesome/css/font-awesome.css">

</head>
<body>
    <!-- HEADER -->
    <section id="header">

        <h1 class="name">Jessica Shae</h1>


        <div class="container heading">
            <div class="row">

                <div class="col-md-4">
                    <img src="img/7.jpg" class="display">
                </div>
                <div class="col-md-4">
                    <img src="img/2.jpg" class="display">
                </div>
                <div class="col-md-4">
                    <img src="img/9.jpg" class="display">
                </div>

                <div class="row">
                    <div class="col-md-12 text-xs-center">
                        <a href="#gallery"<i class="fa fa-chevron-down fa-3x" aria-hidden="true"></i></a> 

            </div>
        </div>


    </section>

    <!-- Gallery -->

    <section id="gallery">

        <h2 class="title">The Dark Room</h2>

        <div class="container photo-collection">
            <div class="row">

                <div class="col-md-4 affect">
                    <img src="img/1.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/10.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/4.jpg" class="work">
                </div>
            </div>

            <div class="row">

                <div class="col-md-4 affect">
                    <img src="img/18.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/6.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/8.jpg" class="work">
                </div>
            </div>

            <div class="row">

                <div class="col-md-4 affect">
                    <img src="img/12.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/11.jpg" class="work">
                </div>
                <div class="col-md-4 affect">
                    <img src="img/14.jpg" class="work">
                </div>
            </div>

        </div>




    </section>

    <section class="contact-me">
        <div class="contact">
            <h3>Contact Me</h3>
        </div>


    </section>

还有我的CSS:

* {
     /*background-color: rgb(0, 0, 0);*/
     background: #070606;
}

/* HEADER */

.display {
  height: auto;
  width: 500px;
  box-sizing: border-box;
  overflow: hidden;
  overflow-x: hidden;
  max-width: 100%;
  border: 4px solid white;
  border-radius: 6%;
}

.heading {
max-width: 100%;
}

.name {
      font-family: 'Oswald', sans-serif;
      text-transform: uppercase;
      font-size: 500%;
      font-weight: 100;
      text-align: center;
      color: whitesmoke;
      width: 100%;
      margin-bottom: 20px;
      margin-top: 15px;
}

h1:after {
      display: block;
      height: 2px;
      background-color: #e62222;  /*Great way to give single line color */
      content: " ";
      width: 100px;
      margin: 0 auto;
      margin-top: 20px;
}

.fa {
      margin-top: 18px;
}

.fa:link, /*Prevents color change when clicked */
.fa:visited {
              text-decoration: none;
              color: #bdc3c7;
}

.fa:hover,
.fa:active {
            color: #ebedee;
}


/* GALLERY */


.work {
       width: 300px;
       height: 100%;
       margin-top: 60px;
       margin-bottom: 60px;
       border: 3px solid white;
       }

 .title {
       font-family: 'Prociono', serif;
       font-size: 350%;
       color: whitesmoke;
       text-align: center;
       padding-top:40px;

 }

.affect img {
           opacity: 0.2;
           background-color: #070606;
           transition: opacity .35s, transform .35s;
           transform: scale(1.0);
}

   .affect:hover img {

    opacity: 1;
    transform: scale(1.15);

}

/* CONTACT */

.contact {
  background: linear-gradient(to right, black 50%, gray 50%);
}

h3 {
  color: white;
  text-align: center;
}

您的文字没有居中,因为 text-align:center 指的是父级宽度,所以这意味着它在 50% 宽处居中 div。为了使其居中,您可以将 contact 和 h3 包装在具有相对位置的包装器中。然后将position absolute 设置为h3(你需要将它移出.contact div)并为h3 设置100% 宽度。它应该以这种方式工作。请记住,如果您使用 absolute 定位,则需要为父级设置高度。

.contact {
        background-color: black;
        background-attachment: fixed;
        background-size: cover;
        width: 50%;
        overflow-x: hidden;
        position: absolute;
        height:100%;
    }

h3 {
        text-align: center;
        font-size: 300%;
        position: absolute;
        width:100%;
 }
.wrapper{
position:relative;
  height:200px;
}
    <section class="wrapper">
    <div class="contact"></div>
    <h3>Contact me</h3>
    </section>

但是这个例子并不是最好的方法。在这种情况下,最好的方法是为具有 50/50 颜色的父容器制作渐变背景(如评论片段中所示)。这不会使您的 HTML 结构和 CSS 代码混乱。

您 CSS 中的这条规则是将每个元素的背景颜色设置为 #070606,因为您使用的是星号,它是一个可以捕获所有内容的通配符选择器。

* { background: #070606; }

如果您只想将页面的背景颜色设置为该颜色,请使用 body 而不是 *