gpt4 book ai didi

css - 如何使我的整个网页居中,包括背景

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

我希望带边框的部分在所有屏幕分辨率上居中。这可能与 margin-topmargin-bottom 属性有关吗?

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Site</title>
</head>

<body>

<div id="container">
<div id="body">
<img src="2.png" width="1000" height="100" />
<center>
<ul class="navbar">
<li><a href="index.html">Home</a></li><li><a href="about.html">About</a></li><li><a href="contact.html">Contact</a></li><li><a href="services.html">Services</a></li><li><a href="biography.html">Biography</a></li>
</ul>
</center>
</div>
</div>
</body>
</html>

CSS:

@charset "utf-8";

html {
text-align: center
}

#container {
margin-left:auto;
margin-right:auto;
margin-top:50 auto;
margin-bottom:50 auto;
width:960px;
background-color:#666666;
}

#body {
background-color:#666666;
width:1000px;
height:1000px;
border:3px solid #FFFFFF;
margin-top:50px auto;
}

.navbar {
margin:0px;
background-color:#66FF33;
text-align:center;
list-style:none;
border-bottom:none;
padding-left:0px;
}


ul.navbar li {
width:20%;
display:inline-block;
}

ul.navbar a {
display:block;
width:100%;
margin:0px;
padding:10px 0px;
text-decoration:none;
}

ul.navbar a:hover {
background-color:#33FFD7;
}

body {
background-color:#333333;
}

最佳答案

尝试:

#container{
width:960px;
margin:0 auto;
/*more CSS*/
}

您可以将 0 更改为任何值,例如 50px。不过,您的图片宽度大于 960 像素。

也许你想要这样的东西:

var pre = onload; // window is implicit
onload = function(){ // I personally don't indent directly inside the onload
if(pre)pre(); // execute old window.onload if it existed
var doc = document, IE = parseFloat(navigator.appVersion.split('MSIE')[1]);
function E(e){
return doc.getElementById(e);
}
function alignTop(id){
var e = E(id), w, h;
if(IE){
h = parseInt(e.currentStyle.height);
}
else{
h = parseInt(getComputedStyle(e).getPropertyValue('height'));
}
w = innerHeight || doc.documentElement.clientHeight || doc.body.clientHeight;
e.style.marginTop = w/2-h/2+'px';
}
alignTop('container');
}

您应该将脚本标记放在头部,以便在某些较旧的浏览器中定义 body。我会使用外部 JavaScript,所以它被缓存了,比如:

<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>Your Title Here</title>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='keywords' content='some words to help SEO here' />
<style type='text/css'>
@import 'common.css'; @import 'thisPage.css';
</style>
<script type='text/javascript' scr='someName.js'></script>
</head>
<body>
<div>
<div id='container'>Example Only</div>
</div>
</body>
</html>

要查看工作模型,请访问 http://jsfiddle.net/MeMQz/2/ .

关于css - 如何使我的整个网页居中,包括背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606180/

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