如何防止100vh图像扩大视点?

时间:2019-04-11 05:10:28

标签: html css css3 flexbox css-grid

对于我的#l-splash图片,我遇到了height: 100vh超出视点范围的问题。

我尝试更改溢出和不同的最大高度。我希望我的宽度占据左侧网格的100%,以便它恰好占据屏幕的一半。我怀疑问题是我的导航栏是如何粘贴的,但理想情况下,我需要它以继续粘贴在屏幕顶部。感谢您的帮助

https://jsfiddle.net/mtgcosxd/1/

header {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: row;
  background: #fffaf0;
  position: sticky;
  width: 100%;
  top: 0;
  font-family: 'Montserrat', sans-serif;
  /*font-family: 'Gotham-Light', gotham;*/
  font-weight: 300;
  font-size: 1vw;
}

body {
  max-width: 100%;
  overflow-x: hidden;
  padding: 0px;
  margin: 0px;
  color: #fffaf0;
  font-family: 'Work Sans', sans-serif;
  font-weight: 300;
}

.nav {
  position: -webkit-sticky;
  position: sticky;
  top: 0%;
  color: #80985d;
}

.navLink {
  padding: 0 10px;
  font-weight: 300;
  text-transform: uppercase;
  cursor: pointer;
  text-align: center;
}

#logo {
  margin-top: 4px;
  margin-bottom: 4px;
  width: 4%;
  height: 4%;
  cursor: pointer;
}

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr
}

.content {
  grid-column: content;
  background: #2f6e84;
}

#l-splash {
  width: 100%;
  height: 100vh;
  overflow: auto;
}
<div class="nav">
  <header>
    <div class="navLink" id="story-scroll">Our Story</div>
    <div class="navLink" id="menu-scroll">Menu</div>
    <img src="https://placekitten.com/200/300" id="logo" lt="logo">
    <div class="navLink" id="press-scroll">Press</div>
    <div class="navLink" id="contact-scroll">Contact</div>
  </header>
</div>

<div class="container">

  <div class="content">
    <div id="splash container">
      <img id="l-splash" src="https://images.unsplash.com/photo-1526069631228-723c945bea6b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80">
    </div>

2 个答案:

答案 0 :(得分:1)

您不需要在图像上设置height: 100vh-因为您的布局中已经有 flexbox grid ,所以可以body上使用 column flexbox 包装器继承高度,以下是更改:

  • 为您body创建了一个列flexbox ,并提供了100vh的高度,

  • 允许container使用nav上的flex: 1来填充container剩余的空间,

  • height: 100%添加到img容器中,

  • 使用height: 100%object-fit将图像填充到其容器中,

body {
  max-width: 100%;
  overflow-x: hidden;
  padding: 0px;
  margin: 0px;
  color: #fffaf0;
  font-family: 'Work Sans', sans-serif;
  font-weight: 300;
  /* made a flexbox */
  display: flex;
  flex-direction: column;
  height: 100vh; /* full-height */
}

header {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: row;
  background: #fffaf0;
  position: sticky;
  width: 100%;
  top: 0;
  font-family: 'Montserrat', sans-serif;
  /*font-family: 'Gotham-Light', gotham;*/
  font-weight: 300;
  font-size: 1vw;
}

.nav {
  position: -webkit-sticky;
  position: sticky;
  top: 0%;
  color: #80985d;
}

.navLink {
  padding: 0 10px;
  font-weight: 300;
  text-transform: uppercase;
  cursor: pointer;
  text-align: center;
}

#logo {
  margin-top: 4px;
  margin-bottom: 4px;
  width: 4%;
  height: 4%;
  cursor: pointer;
}

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr;
  flex: 1; /* added */
}

.content {
  grid-column: content;
  background: #2f6e84;
}

.content > div {
  height: 100%; /* added */
}

#l-splash {
  width: 100%;
  /*height: 100vh;*/
  height: 100%;
  object-fit: cover; /* added */
  overflow: auto;
  display: block; /* remove inline element whitespace */
}
<div class="nav">
  <header>
    <div class="navLink" id="story-scroll">Our Story</div>
    <div class="navLink" id="menu-scroll">Menu</div>
    <img src="https://placekitten.com/200/300" id="logo" lt="logo">
    <div class="navLink" id="press-scroll">Press</div>
    <div class="navLink" id="contact-scroll">Contact</div>
  </header>
</div>
<div class="container">
  <div class="content">
    <div id="splash container">
      <img id="l-splash" src="https://images.unsplash.com/photo-1526069631228-723c945bea6b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80">
    </div>


但是您仍然溢出-现在怎么办?

请参见下面的演示

body {
  max-width: 100%;
  overflow-x: hidden;
  padding: 0px;
  margin: 0px;
  color: #fffaf0;
  font-family: 'Work Sans', sans-serif;
  font-weight: 300;
  /* made a flexbox */
  display: flex;
  flex-direction: column;
  height: 100vh; /* full-height */
}

header {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: row;
  background: #fffaf0;
  position: sticky;
  width: 100%;
  top: 0;
  font-family: 'Montserrat', sans-serif;
  /*font-family: 'Gotham-Light', gotham;*/
  font-weight: 300;
  font-size: 1vw;
}

.nav {
  position: -webkit-sticky;
  position: sticky;
  top: 0%;
  color: #80985d;
}

.navLink {
  padding: 0 10px;
  font-weight: 300;
  text-transform: uppercase;
  cursor: pointer;
  text-align: center;
}

#logo {
  margin-top: 4px;
  margin-bottom: 4px;
  width: 4%;
  height: 4%;
  cursor: pointer;
}

.container {
  display: grid;
  grid-template-columns: [content] 1fr [images] 1fr;
  flex: 1; /* added */
  min-height: 0; /* added */
}

.content {
  grid-column: content;
  background: #2f6e84;
  min-height: 0; /* added */
}

.content > div {
  height: 100%; /* added */
}

#l-splash {
  width: 100%;
  /*height: 100vh;*/
  height: 100%;
  object-fit: cover; /* added */
  overflow: auto;
  display: block; /* remove inline element whitespace */
}
<div class="nav">
  <header>
    <div class="navLink" id="story-scroll">Our Story</div>
    <div class="navLink" id="menu-scroll">Menu</div>
    <img src="https://placekitten.com/200/300" id="logo" lt="logo">
    <div class="navLink" id="press-scroll">Press</div>
    <div class="navLink" id="contact-scroll">Contact</div>
  </header>
</div>
<div class="container">
  <div class="content">
    <div id="splash container">
      <img id="l-splash" src="https://images.unsplash.com/photo-1526069631228-723c945bea6b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80">
    </div>

答案 1 :(得分:0)

您可以从#l-splash div的高度中减去导航/标题的高度。例如

header { 
  ...
  height: 50px; 
  max-height: 50px; 
}

#l-splash {
  ...
  height: calc(100vh - 50px);
}

如果您的资产净值或其他股利具有边距,则可能需要在高度计算中包括这些边距。例如

header { 
  ...
  height: 50px; 
  margin-bottom: 5px;
}

#l-splash {
  ...
  height: calc(100vh - 55px);
}