【问题标题】:Place several divs one after another width full width and adjusted width and height for image background一个接一个地放置几个div宽度全宽并调整图像背景的宽度和高度
【发布时间】:2018-03-30 23:15:46
【问题描述】:

我想一个接一个地显示 div #botone#bottwo#botthree。我还希望在#botone div 上将背景图像调整为 100% 宽度和比例高度。

现在最后两个 Div 出现在 #botone Div 后面,#botone div 将 49px 添加到来自 .topnav Div 的垂直位置。

我还想将.topnav Div 固定在顶部。

** body {
  margin: 0px 0px;
}


/* Add a black background color to the top navigation */

.topnav {
  background-color: #000;
  overflow: hidden;
  height: 49px;
}


/* Style the links inside the navigation bar */

.topnav a {
  float: right;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}


/* Change the color of links on hover */

.topnav a:hover {
  background-color: #ddd;
  color: black;
}


/* Add a color to the active/current link */

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}

.block {
  float: left;
  width: 40px;
}

.blocka {
  float: right;
  width: 250px;
}

#botone {
  position: absolute;
  height: 100%;
  width: 100%;
  background-image: url("arc.jpg");
  background-size: cover;
}

#bottwo {
  background-color: blue;
  height: 400px;
  width: 100%;
}

#botthree {
  background-color: yellow;
  height: 600px;
  width: 100%;
}
<div class="topnav">
  <div class="block">
    <a href="#oi">Oi</a>
  </div>
  <div class="blocka">
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
  </div>
</div>
<div id="botone">testtext
</div>
<div id="bottwo">testtext
</div>
<div id="botthree">testtext
</div>

【问题讨论】:

  • #botone 是绝对定位的,而其他则不是。这意味着#botone不在文档流中,不会影响其他2个div的放置
  • @zgood 谢谢。但是如何使背景图像适应#botone 上窗口的完整高度和宽度。如果位置不是绝对的,height 属性似乎不起作用。
  • 如果您想要可视窗口(视口)的高度,可以尝试将高度设置为100vh
  • @zgood。谢谢!有效。现在只有我想固定在页面顶部的 topnav div。位置:固定;好像没用。

标签: html css


【解决方案1】:

如果我正确理解您的问题,这是否完成了您想要做的事情?

HTML:

<div class="topnav">
  <div class="block">
      <a href="#oi">Oi</a>
  </div>
  <div class="blocka">
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
  </div>
  </div>
  <div id="botone">testtext
  </div>
  <div id="bottwo">testtext
  </div>
  <div id="botthree">testtext
</div>

CSS:(底部的变化)

body {
margin: 0px 0px;
}

/* Add a black background color to the top navigation */
.topnav {
background-color: #000;
overflow: hidden;
height: 49px;

}

/* Style the links inside the navigation bar */
.topnav a {
float: right;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color:white;
}

.block { float:left;width: 40px; }
.blocka { float:right;width: 250px; }

#botone {
position: absolute;
height: 100%;
width: 100%;
background-image: url("arc.jpg");
background-size: cover;
}
#bottwo {
background-color:blue;
height: 400px;
width: 100%;

}
#botthree {
background-color:yellow;
height: 600px;
width: 100%;
}


/*--- Fixes --*/

.topnav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
}

#botone {
  /*-- Test --*/
  background-image: url("https://images.pexels.com/photos/948358/pexels-photo-948358.jpeg?auto=compress&cs=tinysrgb&h=650&w=940");

  background-position: center;
  height: 100vh;
  margin-top: 49px;
  position: relative;
}

link to example

【讨论】:

    【解决方案2】:

    对您的需求描述有点困惑,但我认为这是您想要实现的目标:

    .topnav {
      background-color: #000;
      overflow: hidden;
      height: 49px;
      position:fixed; //add this
      width:100%; //add this
    }
    
    #botone {
      position: absolute;
      margin-top: 49px; //add this
      height: 100%;
      width: 100%;
      background-image: url("arc.jpg");
      background-size: cover;
    }
    

    要固定顶部导航或标题,请使用position:fixed。您必须将宽度定义为 100%,否则它会自动采用 width: auto

    为确保以下 div 直接显示在固定元素的下方,请为其设置与固定元素高度相对应的 margin-top。如果您使用的是 SCSS,这在响应式设计中会更好,因为您可以创建一个同时将高度应用于 margin-top 的函数。但如果您使用 CSS,只需检查您的 @media 查询以确保高度和边距顶部一致。

    目前,蓝色元素是固定元素之后的元素。如果您想在蓝色元素和标题(即幻灯片或横幅)之间添加一些内容,请按照现在正在处理的蓝色元素进行处理。给它 margin-top 并将其从蓝色元素中删除,因为蓝色元素不再直接位于固定标题之下。

    DEMO

    或者sn-p中的全部代码

    body {
      margin: 0px 0px;
    }
    
    
    /* Add a black background color to the top navigation */
    
    .topnav {
      background-color: #000;
      overflow: hidden;
      height: 49px;
      position:fixed;
      width:100%;
      top:0;
    }
    
    
    /* Style the links inside the navigation bar */
    
    .topnav a {
      float: right;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    
    
    /* Change the color of links on hover */
    
    .topnav a:hover {
      background-color: #ddd;
      color: black;
    }
    
    
    /* Add a color to the active/current link */
    
    .topnav a.active {
      background-color: #4CAF50;
      color: white;
    }
    
    .block {
      float: left;
      width: 40px;
    }
    
    .blocka {
      float: right;
      width: 250px;
    }
    
    #botone {
      position: absolute;
      margin-top: 49px;
      height: 100%;
      width: 100%;
      background-image: url("arc.jpg");
      background-size: cover;
    }
    
    #bottwo {
      background-color: blue;
      height: 400px;
      width: 100%;
    }
    
    #botthree {
      background-color: yellow;
      height: 600px;
      width: 100%;
    }
    <div class="topnav">
      <div class="block">
        <a href="#oi">Oi</a>
      </div>
      <div class="blocka">
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
      </div>
    </div>
    <div id="botone">testtext
    </div>
    <div id="bottwo">testtext
    </div>
    <div id="botthree">testtext
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多