html - 如何仅使用 CSS 来创建这种效果?

标签 html css flexbox

因此,我拥有无法更改的 HTML,并且我拥有特定的设计目标。我还不能为此使用 JS。 enter image description here

请注意,容器实际上是屏幕上所有其他元素的父元素。然而,我之所以那样画是因为那是我想要的外观,如果可能的话,当我向下滚动时,唯一滚动的是元素 3 和 4,而 3 和 1 保持固定。目前容器是一个 flex 盒子。我拥有的 CSS 并没有真正完成我需要它做的事情,但它是:

html,
body {
  min-height: 100%;
  height: 100%;
}

#container {
  display: flex;
  flex-wrap: wrap;
  height: 100%;
}

#intro {
  background-color: yellow;
  width: 20%;
  order: 2;
}

#nav {
  background-color: red;
  width: 15%;
  order: 1;
}

#content {
  background-color: blue;
  width: 65%;
  order: 3
}

article {
  order: 4;
}

footer {
  background-color: magenta;
}
<div id="container">
  <section id="intro">...</section>
  <div id="content">...</div>
  <aside id="nav">...</aside>
  <article>
    <li><img src="http://via.placeholder.com/100x100"></li>
    <li><img src="http://via.placeholder.com/100x100"></li>
  </article>
</div>
<footer>...</footer>

最佳答案

as I scroll down the only thing that scrolls is element 3 and 4

使用 position: fixed 我们可以让 nav/intro/footercontent/article 滚动。

content/article 上的 align-items: flex-end; 将使它们保持右对齐,而 flex-grow : 1 on container/content 将使它们填充剩余空间。

堆栈片段

html {
  display: flex;                 /*  IE min-height bug fix  */
}

body {
  width: 100%;                   /*  using IE bug fix it need a width  */
  display: flex;
  flex-direction: column;
  min-height: 100vh;             /*  instead using precent all over  */
  margin: 0;
}

#container {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

#nav {
  position: fixed;
  left: 0;
  top: 0;
  width: 15%;
  height: calc(100% - 30px);     /*  make up for footer  */
  background-color: red;
}

#intro {
  position: fixed;
  left: 15%;
  top: 0;
  width: 20%;
  height: calc(100% - 30px);     /*  make up for footer  */
  background-color: yellow;
}

#content {
  background-color: lightblue;
  width: 65%;
  flex-grow: 1;
}

article {
  margin-bottom: 30px;           /*  make up for footer  */
}

article li {
  list-style: none;
}

footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 30px;
  background-color: magenta;
}
<div id="container">
  <section id="intro">Intro</section>
  <div id="content">
    Content along with below article that will scroll and leave "Nav"/"Intro" fixed. <br>
    Content along with below article that will scroll and leave "Nav"/"Intro" fixed. <br>
  </div>
  <aside id="nav">Nav</aside>
  <article>
    <li><img src="http://via.placeholder.com/100x100"></li>
    <li><img src="http://via.placeholder.com/100x100"></li>
  </article>
</div>
<footer>Footer</footer>

关于html - 如何仅使用 CSS 来创建这种效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921549/

相关文章:

html - 自动以半步包裹 Flexbox 元素

html - 如何在页面底部创建停靠类型工具栏?

javascript - 使用 Aurelia 有条件地在选择选项中添加属性

Jquery 动画 - 在悬停时动画文本阴影 css 以实现从模糊到清晰文本的缓慢效果

css - React-native 使用 flexbox 定义组件高度

css - CSS3 flexbox negative-flex 是如何工作的?

css - 高度 :100% works in Chrome but not in Safari

python - 使用 BeautifulSoup 获取没有标签的文本?

javascript - 使用 Javascript 更改 CSS 值

html - 如何在 HTML 链接标签中添加::after