gpt4 book ai didi

html - 在页面中间垂直居中文本

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:57 26 4
gpt4 key购买 nike

我想在页面中间垂直居中我的主页上的文本。现在我试着用百分比来做这个,但这不是正确的方法,因为当我在我的手机或 ipad 上打开网页时,文本不会居中。有谁知道我如何才能以正确的方式将它居中?

@charset "UTF-8";
/* CSS Document */

body {
background: white;
}

html, body{
width: 100%;
margin: 0;
padding: 0;
height:100%;
}

/* wrapper */
#wrapper {
min-height: 100%;
position: relative;
}

/* menu */
#header {
height: 80px;
width: 100%;
background: #000000;
}

li {
display: block;
float: left;
margin-left: 10px;
margin-top: 20px;
margin-right: 10px;
}

a {
position: reletive;
text-decoration: none;
color: #ffffff;
font-size: 24px;
font-family: 'Open Sans Condensed';
}

li:hover{
border-bottom: 1px solid white;
padding-bottom: 0px;
}

.home {
margin-left: 30px;
}

.contact {
float: right;
margin-right: 30px;
}

/*content*/
#content {
padding-bottom:80px;
}

/* homepage */
.anouk {
font-family: 'Oswald';
color: #000000;
font-size: 80px;
text-align: center;
margin-top: 15%;
}


/* footer */
#footer {
background: #000000;
width: 100%;
height: 80px;
position: absolute;
bottom: 0;
left: 0;
text-align: center;
}

.icoontjes {
margin-top: 15px;
margin-left: 10px;
margin-right: 10px;
height: 50px;
}

.icoontjes:hover {
opacity: 0.8;
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Anouk</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href="https://fonts.googleapis.com/css?family=Oswald|Open+Sans+Condensed:300" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="header">
<div class="menu">
<!--Home-->
<li class="home">
<a href="index.html">
Home
</a>
</li>
<!--Over Mij-->
<li class="over">
<a href="over.html">
Over Mij
</a>
</li>
<!--Portfolio-->
<li class="portfolio">
<a href="portfolio.html">
Portfolio
</a>
</li>
<!--Contact-->
<li class="contact">
<a href="contact.html">
Contact
</a>
</li>
</div>
</div>
<div id="content">
<p class="anouk">
Anouk Den Haag
</p>
</div>
<div id="footer">
<a href="mailto:#">
<img class="icoontjes" src="icoontjes/email.png" alt="email">
</a>
<a href="#" target="_blank">
<img class="#" src="icoontjes/facebook.png" alt="facebook">
</a>
<a href="#" target="_blank">
<img class="icoontjes" src="icoontjes/instagram.png" alt="instagram">
</a>
</div>
</div>
</body>
</html>

最佳答案

display 为您提供 2 种选择:表格布局或 flex 模型(如果内容增长,两者都会将页脚向下推)


在全页模式下测试片段并调整窗口大小


1) display:table/table-row/table-cell(应包括IE8及旧版浏览器CSS 2.1)

@charset "UTF-8";

/* CSS Document */

body {
background: white;
}

html,
body,
#wrapper {
width: 100%;
margin: 0;
padding: 0;
height: 100%;
}


/* wrapper */

#wrapper {
display: table;
position: relative;
}


/* menu */

#header {
height: 80px;
display: table-row;
background: #000000;
}

li {
display: block;
float: left;
margin-left: 10px;
margin-top: 20px;
margin-right: 10px;
}

a {
position: reletive;
text-decoration: none;
color: #ffffff;
font-size: 24px;
font-family: 'Open Sans Condensed';
}

li:hover {
border-bottom: 1px solid white;
padding-bottom: 0px;
}

.home {
margin-left: 30px;
}

.contact {
float: right;
margin-right: 30px;
}


/*content*/

#content {
vertical-align: middle;
display: table-cell;
}


/* homepage */

.anouk {
font-family: 'Oswald';
color: #000000;
font-size: 80px;
text-align: center;
}


/* footer */

#footer {
background: #000000;
width: 100%;
height: 80px;
display: table-row;
text-align: center;
}

.icoontjes {
margin-top: 15px;
margin-left: 10px;
margin-right: 10px;
height: 50px;
}

.icoontjes:hover {
opacity: 0.8;
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Anouk</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href="https://fonts.googleapis.com/css?family=Oswald|Open+Sans+Condensed:300" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="header">
<div class="menu">
<!--Home-->
<li class="home">
<a href="index.html">
Home
</a>
</li>
<!--Over Mij-->
<li class="over">
<a href="over.html">
Over Mij
</a>
</li>
<!--Portfolio-->
<li class="portfolio">
<a href="portfolio.html">
Portfolio
</a>
</li>
<!--Contact-->
<li class="contact">
<a href="contact.html">
Contact
</a>
</li>
</div>
</div>
<div id="content">
<p class="anouk">
Anouk Den Haag
</p>
</div>
<div id="footer">
<a href="mailto:#">
<img class="icoontjes" src="icoontjes/email.png" alt="email">
</a>
<a href="#" target="_blank">
<img class="#" src="icoontjes/facebook.png" alt="facebook">
</a>
<a href="#" target="_blank">
<img class="icoontjes" src="icoontjes/instagram.png" alt="instagram">
</a>
</div>
</div>
</body>
</html>


2) flex 模型(最新的浏览器)

@charset "UTF-8";

/* CSS Document */

body {
background: white;
}

html,
body,
#wrapper {
width: 100%;
margin: 0;
padding: 0;
height: 100%;
}


/* wrapper */

#wrapper, #content {
display: flex;/* triiger flex model prefixed might be needed for not so old browsers */
flex-direction:column /* here we need to stack elements */
}


/* menu */

#header {
height: 80px;
background: #000000;
}

li {
display: block;
float: left;
margin-left: 10px;
margin-top: 20px;
margin-right: 10px;
}

a {
position: reletive;
text-decoration: none;
color: #ffffff;
font-size: 24px;
font-family: 'Open Sans Condensed';
}

li:hover {
border-bottom: 1px solid white;
padding-bottom: 0px;
}

.home {
margin-left: 30px;
}

.contact {
float: right;
margin-right: 30px;
}


/*content*/

#content {
flex:1;/* take as much space avalaible */
justify-content:center;/* because it is display:flex too, you can horizontally center its contents */
align-items:center;/* because it is display:flex too, you can vertically center its contents */
}


/* homepage */

.anouk {
font-family: 'Oswald';
color: #000000;
font-size: 80px;
text-align: center;
}


/* footer */

#footer {
background: #000000;
height: 80px;
text-align:center;
}

.icoontjes {
margin-top: 15px;
margin-left: 10px;
margin-right: 10px;
height: 50px;
}

.icoontjes:hover {
opacity: 0.8;
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Anouk</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href="https://fonts.googleapis.com/css?family=Oswald|Open+Sans+Condensed:300" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="header">
<div class="menu">
<!--Home-->
<li class="home">
<a href="index.html">
Home
</a>
</li>
<!--Over Mij-->
<li class="over">
<a href="over.html">
Over Mij
</a>
</li>
<!--Portfolio-->
<li class="portfolio">
<a href="portfolio.html">
Portfolio
</a>
</li>
<!--Contact-->
<li class="contact">
<a href="contact.html">
Contact
</a>
</li>
</div>
</div>
<div id="content">
<p class="anouk">
Anouk Den Haag
</p>
</div>
<div id="footer">
<a href="mailto:#">
<img class="icoontjes" src="icoontjes/email.png" alt="email">
</a>
<a href="#" target="_blank">
<img class="#" src="icoontjes/facebook.png" alt="facebook">
</a>
<a href="#" target="_blank">
<img class="icoontjes" src="icoontjes/instagram.png" alt="instagram">
</a>
</div>
</div>
</body>
</html>

将 overflow:auto 添加到 #content ,如果需要它会显示一个滚动条,所以页脚不会被推下。

关于html - 在页面中间垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798954/

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