同一部分的两个div

时间:2017-07-06 13:41:25

标签: html css

在下面的HTML代码中,为什么他们使用两个div,一个带有类,另一个带有ID,而不是只使用其中一个来提供属性?

.header {
  background-image: url("https://s3.amazonaws.com/codecademy-content/courses/web-101/unit-5/htmlcss1-img_canyon.jpeg");
  background-position: center center;
  background-size: cover;
  height: 700px;
  width: 100%;
}

#header-text {
  margin: 0 auto;
  position: relative;
  text-align: center;
  top: 25%;
  width: 60%;
}
 <div class="header">
    <div  id="header-text">
      <h1>Travel Like Never Before</h1>
      <h2>Whether you're looking for adventure or luxury, let us help you plan your perfect getaway.</h2>
    </div>
 </div>

1 个答案:

答案 0 :(得分:0)

原因是因为.header类定义了背景图像的区域,#header-text id定义了图像顶部的文本。由于文本居中并位于较大的标题区域内,因此需要一个单独的容器,以便可以移动并适当设置样式。

相关问题