调整大小/放大/缩小页面时,Div会移动

时间:2014-03-28 21:05:36

标签: html css resize

我知道这个问题被问了很多次,但我找到的解决方案都没有给我工作。我有这个网站,当我放大和缩小网页时,div元素会移动。这是HTML代码:

<!doctype html>
<html>    
    <head>
        <!-- Header -->
        <center>
            <img src="http://i1349.photobucket.com/albums/p742/lucasgame13/header_zps8f446598.png" alt="Shawn&amp;LucasWeb">
        </center>
        <!-- Header -->
        <title>Shawn & Lucas Web Development!</title>
        <link rel="stylesheet" href="style.css">
    </head>    
    <body>
        <center>
            <h1>Welcome to Shawn & Lucas Web Development!</h1>
        </center>
        <!-- Left Box -->
        <div id="leftbox">
             <h3> Info </h3>

            <p>This is a test. -</p>
            <p>- <a href="Red.html">Click here!</a>
            </p>
        </div>
        <!-- End of Left Box -->
        <!-- Middle Box -->
        <div id="box">
             <h3> Menu </h3>

            <p>Navigate around the website!</p>
            <center><a href="about.html">About Us</a> 
            </center>
        </div>
        <!-- End of Middle Box -->
        <!-- Right Box -->
        <div id="rightbox">
             <h3> Menu </h3>

            <p>Navigate around the website!</p>
        </div>
        <!-- End of Right Box -->
        <footer>
            <p class="us"><font color="black"> Shawn & Lucas WEB - Custom Websites &copy; </p>
         </footer>
     </body>
</html> 

CSS代码是这样的:

body {
    background:#61c9f5;
}
h1 {
    color:#000000;
    font-family:"Impress BT" arial verdana;
    position:relative;
    bottom:37px;
}    
p.us {
    tetx-align:center;
    color:white;
    font:bold 400%
}

/* Middle Box */
#box {
    border-width: 0.139in;
    border-color: rgb( 75, 75, 75 );
    border-style: dashed;
    background-color: rgb( 115, 115, 115 );
    width: 309px;
    height: 296px;
    z-index: 1;
    display:block;
    margin: auto auto;      
}    
#box h3 { 
    font-size:180%;
    text-align:center;
    position:relative;
    bottom:30px;        
}    
#box p {
    position:relative;
    bottom:60px;
    text-align:center;      
}    
#box a:link, a:visited {
    display:block;
    font-weight:bold;
    color:#FFFFFF;
    background-color:#778899;
    width:120px;
    text-align:center;
    padding:4px;
    text-decoration:none;
    text-align:center;
    border:1px solid black; 
}    
#box a:hover, a:active {
    background-color:#708090
}

/* End of Middle Box */

/* Left Box */ 
#leftbox {
    border-width: 0.139in;
    border-color: rgb( 75, 75, 75 );
    border-style: dashed;
    background-color: rgb( 115, 115, 115 );
    width: 309px;
    height: 296px;
    z-index: 1;
    position:absolute;
    left:70px 
}  
#leftbox h3 {
    font-size:180%;
    text-align:center;
    position:relative;
    bottom:30px;
}
#leftbox p {
    position:relative;
    bottom:60px;
    text-align:center;
}
/* End of Left Box */

/* Right Box */
#rightbox {
    border-width: 0.139in;
    border-color: rgb( 75, 75, 75 );
    border-style: dashed;
    background-color: rgb( 115, 115, 115 );
    width: 309px;
    height: 296px;
    z-index: 1;
    position:absolute;
    top:395px;
    right:70px;     
}
/* End of Right Box */

div {                    
    border-radius:10px;
}   

我没有链接,因为它只在我的电脑上,但我有截图:

http://prntscr.com/34ypmu - 以下是100%缩放的普通网站

http://prntscr.com/34yq9w - 这是放大时的效果。

我无法发布超过2个链接,因为我没有10个声望,但是当它缩小时 盒子彼此远离。

我知道该网站并不是一个很好的网站,但它只是一个测试,因为当我放大时,没有任何其他网站可以工作。请帮助! (我确实尝试过包装但它没有用)

2 个答案:

答案 0 :(得分:0)

你滥用绝对定位。经验法则:你需要绝对定位吗?否。

所以,使用float:

float: left;
width: 33%;

请记住使宽度合适,以免下降到下一行。如果您不想考虑填充/边框,请使用:

box-sizing: border-box;
-moz-box-sizing: border-box;

它使边框在宽度范围内(而不仅仅是内容)。

答案 1 :(得分:0)

如果您使用text-align: center在包装盒周围添加包装,并在框中添加display: inline-block,则它们将在中心旁边彼此对齐。

http://jsfiddle.net/borglinm/JFkYL/

HTML

<div class="boxes">
    <div class="box">Content</div>
    <div class="box">Content</div>
    <div class="box">Content</div>
</div>

CSS

.boxes {
    text-align: center;
}
/* All the boxes */
.box {
    vertical-align: top;
    margin: 0 10px;
    text-align: left;
    display: inline-block;
}
相关问题