【发布时间】:2020-02-16 08:28:52
【问题描述】:
我正在学习 Flexbox,并尝试通过创建演示组合来实现。
我试过这个answer 来创建一个固定的侧边栏。然后我尝试使用 flex: 1 使我的投资组合的各个部分具有相同的维度,最初它不起作用,然后我发现了这个 answer 但它也不起作用。
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs < 12)
greet = 'Good Morning';
else if (hrs >= 12 && hrs <= 17)
greet = 'Good Afternoon';
else if (hrs >= 17 && hrs <= 24)
greet = 'Good Evening';
document.getElementById('greet').innerHTML =
greet + ', visitor! <br><br> Welcome.';
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Comfortaa', sans-serif;
font-size: 1.4em;
}
.container {
display: flex;
flex-flow: row nowrap;
height: 100vh;
}
.sidebar {
flex-basis: 25%;
background-color: #bbbbeb;
display: flex;
flex-flow: column;
padding: 10px;
}
.logo {
text-align: center;
font-weight: bold;
}
.navbar {
margin: auto;
}
.content-container {
flex-basis: 75%;
display: flex;
flex-flow: column nowrap;
text-align: center;
overflow: auto;
height: 100vw;
}
section {
padding: 10px;
}
.home {
background-color: red;
flex: 1;
}
.about {
background-color: orange;
flex: 1;
}
.skills {
background-color: orangered;
flex: 1;
}
.contact {
background-color: crimson;
flex: 1;
}
li {
list-style: none;
margin: 10px 0;
text-align: center;
}
a {
color: black;
display: inline-block;
padding: 2px;
text-decoration: none;
}
a:hover {
transform: scale(1.2);
font-weight: bold;
border-bottom: 3px solid black;
text-shadow: 1px 1px 2px rgb(122, 71, 122);
}
<div class="container">
<section class="sidebar">
<div class="logo"><a href="#home"><logo></a></div>
<nav class="navbar">
<ul class="nav-links">
<li><a href="#home">home</a></li>
<li><a href="#about">about me</a></li>
<li><a href="#skills">skills</a></li>
<li><a href="#contact">contact</a></li>
</ul>
</nav>
</section>
<main class="content-container">
<section class="home" id="home">
<h2 id="greet"><span></h2>
</section>
<section class="about" id="about">
<h2>ABOUT</h2>
<h3>
<span>Name</span>
</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</section>
<section class="skills" id="skills">
<h2>SKILLS</h2>
<h3>Lorem ipsum dolor sit amet.</h3>
<ul class=tech-skills>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<h3>Lorem</h3>
<ul class="languages">
<li>1</li>
<li>2</li>
</ul>
<h3>Lorem, ipsum.</h3>
<ul class="learning-skills">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</section>
<section class="contact" id="contact">
<h2>CONTACT</h2>
<p>handle</p>
<p>phone</p>
<p>email</p>
</section>
</main>
</div>
正如您在 CSS 中看到的,.content-container 的高度为 100vw,如果我将其更改为 100vh,那么 flex:1 在 .home、.about、.skills 和 .contact 内将不起作用,但在使用 100vw 作为高度时,flex:1 有效,但固定侧边栏不再固定。
这是因为.content-container 上的flex-flow: column nowrap 而发生的吗?
【问题讨论】: