如何使容器在 HTML/CSS 中居中?

2023-12-03

作为我任务的一部分,我开发了一个网站。由于我是 html/css 新手,我无法从代码中找出一些问题。分配规范规定屏幕尺寸应在一定尺寸内,以便大多数浏览器都可以打开它,并且用户不应体验到任何滚动活动。所以我用div来划分整个页面以适应大小。但是,容器(在我的代码中名为“box”)设置在浏览器主体的左侧,我必须将其设置为居中,但我不知道如何修复它。谁能给我一些帮助,请,谢谢。

My HTML:

<!DOCTYPE html>
<html>
<head>
<title>habibe Naby</title>
<link rel="stylesheet" href="Websystems.css">
</head>
<body>
<div id="box">
    <div id="header">
     <img id="image" src="spring.jpeg">
     <p id="text">Welcome to My Homepage</p>
    </div>

    <div id="under">
            <div id="upper">
                    <div id="leftbar">
                            <div class="list">
                            <ul>
                                    <li><a href="../index.html">Home</a></li>
                                            <li><a href="past.html">Past</a></li>
                                            <li><a href="future.html">Future</a></li>
                                            <li><a href="comments.html">Comments</a>   

                                    </li>
                             </ul>
                            </div>
                    </div>
                    <div id="rightbar">
                    <div  id="title">Habibe Naby</div>
                       <p>this is my name and                           

                        I<spanclass="special">Danny</span>.Ihave    a .. </p>
                    </div>
            </div>
  <div id="footer">copyrights&copy</div>
  </div>

My CSS:

body
{
    height: 750px;
    margin: 2px;
    padding: 2px;
    width: 1000px;
}


#box 
{
    width: 1000px;
    margin-left: auto;
    margin-right: auto;
    height:100%;
    border:1px solid #8D8D8D;
}


#header 
{
    height: 150px; 
    width: 100%;
    position: relative;
}


#image 
{
    height: 150px; 
    width: 1000px;
}


#text 
{
    z-index: 100; 
position: absolute;    
color: darkolivegreen; 
    font-style: italic;
font-size: 32px;
font-weight: bolder;
left: 300px; 
top: 25px;
}

#leftbar
{
    float: left;
    width: 200px;
    height: 560px; 
    margin: 0px;
    padding: 0px;
    background-color: azure;
}

.list
{ 
    margin-top: 40px;
    margin-left: auto;  
    text-decoration: underline;
    color:blueviolet; 
    font-size: 20px; 
}


.list ul 
{
    list-style: none; 
}

#rightbar 
{
    float: right;
    width: 800px;
    height: 560px;
    margin: 0px;
    padding: 0px;
    background: mintcream;
}

#title 
{
    margin-left: 80px; 
    margin-top: 30px;
    font-size: 28px;
    font-weight: normal;
    font-style: italic;
    color: blueviolet;
}


#footer 
{
    clear: both;
    height: 40px;
    text-align: center;
    background-color: oliveDrab;
    margin 0px;
    padding: 0px;
    color: white;
}

.special
{
    font-size: 20px; 
    font-weight: bold;
    font-family: "New Century Schoolbook", Times, sans-serif;
    color: dodgerblue;
} 


p, pre
{
    padding: 10px 20px;
    margin-left: 50px;
    margin-right: 50px;
    color: darkslateblue;
 }

杰斯小提琴:http://jsfiddle.net/2gtsK/show/

删除了宽度body, Added margin:0 auto to #box

margin:0 auto与以下相同:

margin-top: 0;
margin-right: auto;
margin-bottom: 0;
margin-left: auto;

.

body
{
    height: 750px;
    margin: 2px;
    padding: 2px;
}


#box 
{
    width: 1000px;
    margin: 0 auto;
    height:100%;
    border:1px solid #8D8D8D;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使容器在 HTML/CSS 中居中? 的相关文章