我怎样才能将文字和文字集中在一起如何制作<div>背景图片?

时间:2017-03-15 11:16:39

标签: html css

Q1

如何将文本<h1>Welcome to Liam Docherty's Digital Portfolio</h1>置于导航栏文本的中心位置?

Q2

如何让<DIV>代表背景图片?

我不想因为各种原因而将第1部分设为背景图像,这就是为什么我要创建一个<DIV>它自己的背景图像。我尝试将HTML div class="hero"></div>与下面的CSS一起使用。然而,没有任何反应。

.hero {
      background-image: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg");
      background-attachment: fixed;
    }

HTML

 <body>
        <nav class="navbar navbar-default navbar-fixed-top"> 
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#"><img src="http://placehold.it/60x60" alt="Your Brand Name"></a>
                    <h1 class="nav-title">Welcome to Liam Docherty's Digital Portfolio</h1>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                            <li><a href="#">Home</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">Action</a>
                                    </li>
                                    <li><a href="#section3">Contact</a>
                                    </li>
                                    <li><a href="#">Something else here</a>
                                    </li>
                                    <li><a href="#">Separated link</a>
                                    </li>
                                    <li><a href="#">One more separated link</a>
                                    </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <span class="caret"></span></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">Action</a>
                                    </li>
                                    <li><a href="#">Another action</a>
                                    </li>
                                    <li><a href="#">Something else here</a>
                                    </li>

                                    <li><a href="#">Separated link</a>
                                    </li>

                                    <li><a href="#">One more separated link</a>
                                    </li>
                                </ul>
                            </li>
                            <li><a href="#">Clients</a>
                            </li>
                            <li><a href="#contact-me">Contact Me</a>
                            </li>
                        </ul>
                      </div><!-- /.navbar-collapse -->
                    </div><!-- /.container-fluid -->
        </nav>


    <section id="section1" class="section1">
      <div class="hero"></div>

      <a href="#section2"><i class="fa fa-angle-down" style="font-size:100px;"></i></a>

    </section>
    <section id="section2" class="section2">


      <a href="#contact-me"> <i class="fa fa-angle-down" style="font-size:100px;"></i></a>

    </section>

    <section id="contact-me" class="contact_section section3">

      <a href="#section1"> <i class="fa fa-angle-up" style="font-size:100px;"></i></a>

    </section>
    <script   src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<footer>
    <div class="page">
        <h1 class="footer">Copyright © 2017 Liam Docherty's Site. All rights reserved.</h1>
    </div>
 </footer>     
</body>

CSS

<style>
    body {
      margin: 0;
      padding: 0;
    }
    .navbar.navbar-default {
      background-color: #4D5061;
      height: 10vh;
      z-index: 100;
    }
    .navbar.navbar-default ul {
      list-style-type: none;
      text-align: right;
    }
    .navbar.navbar-default ul li {
      display: inline-block;
    }
    .dropdown-menu li { 
        text-align:center 
    }
    .dropdown .dropdown-menu {
      background-color: #4D5061;
    }
    .dropdown .dropdown-menu a {
      color: white;
    }
    .navbar.navbar-default ul li a {
      display: inline-block;
      padding: 3.5vh 8px 4px;
      color: white;
      text-decoration: none;
      font-size: 14pt;
      font-family: 'Roboto', sans-serif;
    }
    .navbar.navbar-default ul li:after {
      content: '';
      position: absolute;
      right: 50%;
      bottom: 0;
      left: 50%;
      height: 3px;
      background-color: #FFFFFF;
      border-radius: 9px;
      transition: all .2s;
    }
    .navbar.navbar-default ul li a:hover {
      color: white;
    }
    .nav.navbar-nav,
    .nav.navbar-nav>li {
      float: none;
    }
    .navbar.navbar-default ul li:hover:after {
      right: 0;
      left: 0;
    }
    .nav>li>a:hover {
    background-color: rgba(17, 17, 17, 0.2);
    }
    #logo {
      padding-top: 2vh;
      padding-left: 20px;
      float: left;
    }
    nav-title {
        display: block;
        margin: 0 auto; /*auto will do the centering trick*/
        width: 80%; /*or any width*/
        position: relative;
    }
    .hero {
      background-image: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg");
      background-attachment: fixed;
      position:absolute;
      width: 100%;
      width: 100vw;
      height: 100%;
      height: 70vh;
    }
    section {
      position: relative;
      height: 95vh;
    }
    .section1 {
      height: 100vh;
      text-align: center;
      color: white;
    }
    .section2 {
      //height: 95vh;
      background-color: #11B5E4;
      text-align: center;
      color: white;
    }
    .section3 {
      //height: 95vh;
      background-color: #EDF7F6;
      text-align: center;
      color: white;
    }
    .fa-angle-down {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    .fa-angle-up {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    .footer {
      height: 5vh;
      background-color: #4D5061;
      text-align: center;
      padding:0;
      right:0;
      bottom:0;
      left:0:
    }
    h1{
      font-size: 14pt;
      margin:0;
      text-align: center;
      color: white;
      font-family: 'Roboto', sans-serif;
    }
    .navbar.navbar-default ul.dropdown-menu li,
    .navbar.navbar-default ul.dropdown-menu li a {
      position: relative;
      display: block;
    }
    </style>

2 个答案:

答案 0 :(得分:0)

首先考虑你的第二个问题 - 你拥有的英雄元素没有任何高度 - 这就是图像不显示的原因。如果您希望图像在整个高度上显示为背景,则可以将这些元素添加到.hero样式中: position:absolute; height:100%; width:100%;

当我将代码复制并粘贴到普通文件中时,导航不会显示扩展,因此很难回答问题1. H1中的文本已经针对页面本身居中。如果您可以提供URL或代码集,则更容易回答这一点。

答案 1 :(得分:0)

Q2: 没有任何反应的原因是因为默认情况下任何DIV高度和宽度都是0px,所以你必须明确提到高度和宽度。 如果你想要英雄形象是100%的高度和宽度的100%,我建议你在CSS中使用vw(视口宽度)和vh(视口高度值),其中100vw表示100%的视口宽度(高度相同的逻辑)。 像这里:

.hero {
  background-image: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg");
  background-attachment: fixed;
  position: fixed;
  width: 100%;
  width: 100vw;
  height: 100%;
  height: 100vh;
}

百分比值是为了向后兼容。

Q1: 将h1置于div中并将其置于中心位置:

<div>
  <h1>Welcome to Liam Docherty's Digital Portfolio</h1>
</div>

CSS:

div{
 display: block;
 margin: 0 auto; /*auto will do the centering trick*/
 width: 80%; /*or any width*/
 position: relative;
}

div必须在导航容器之外。