gpt4 book ai didi

html - flexbox 和 CSS3 宽度/高度声明的垂直居中问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:05 25 4
gpt4 key购买 nike

我不确定我做错了什么,但我试图将导航元素垂直居中并将其粘贴到屏幕右侧,但是有一些东西将其向下推,使其偏离中心一点。

红色是 flex 容器,绿色是标题,灰色框是右对齐且垂直居中的框我想真正垂直居中而不是“偏离中心”。

我不知道是什么让它往下跳。

我创建了一个 JSFIDDLE这样您就可以看到我正在努力实现的目标。 (本质上,它是一个位于滚动页面上的固定 flex 容器。

<div class="navWrap"><!-- Place less important HTML after main content (My brain hurts!)-->

<header>

<nav>
</nav>

</header>

<nav class="pagesNav next">

<span class="arrow"></span>
<span class="arrow"></span>

</nav>

</div>

CSS:

.navWrap {
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Chrome */
display: flex;

-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;

-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;

-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;

position: fixed;
top: 0;
width: 100%;
height: 100vh;

background: red;
}

header {
-webkit-align-self: flex-start;
-ms-flex-item-align: start;
align-self: flex-start;
width: 100%;
height: 70px;
background: green;
}

nav:not(.pagesNav) {
}

.pagesNav {
margin-left: auto;

width: 80px;
height: 155px;

background: #414141;
}

最佳答案

你的标题放错地方了,试试这个

<div class="pageWrap"><!-- Primary content first for SEO purposes. Use CSS to rearrange secondary elements -->


<header>

<nav>
</nav>

</header>
<main role="main">

<section></section>
<section></section>
<section></section>
<section></section>

</main>

<footer>
</footer>

</div>

<div class="navWrap"><!-- Place less important HTML after main content (My brain hurts!)-->


<nav class="pagesNav next">

<span class="arrow"></span>
<span class="arrow"></span>

</nav>

</div>

CSS

    html {
color: #222;
font-size: 1em;
line-height: 1.4;
}

html, body {
height: 100%;
}

body {

}

/* Temp styles */

.pageWrap {

}

main {
max-width: 1920px;
height: 100%;
margin: 0 auto;
}

section {
height: 100vh;
width: 100%;
background: white;
}

section:first-child {
height: 95vh;
}

section:nth-child(even) {
background: yellow;
}

footer {
height: 300px;
background: red;
}

/* Less important info */
header{
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Chrome */
display: flex;

-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;

-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;

-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;

position: fixed;
top: 0;
width: 100%;
height: 100vh;

}
.navWrap {
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Chrome */
display: flex;

-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;

-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;

-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;

position: fixed;
top: 0;
width: 100%;
height: 100vh;
}

header {
-webkit-align-self: flex-start;
-ms-flex-item-align: start;
align-self: flex-start;
width: 100%;
height: 70px;
background: green;
}

nav:not(.pagesNav) {
}

.pagesNav {
margin-left: auto;

width: 80px;
height: 155px;

background: #414141;
}

这是 fiddle https://jsfiddle.net/L4u9t78m/2/

关于html - flexbox 和 CSS3 宽度/高度声明的垂直居中问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com