在调整窗口大小时让div保持同一位置 [英] Making divs stay the same place when resizing window

查看:98
本文介绍了在调整窗口大小时让div保持同一位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先即时新的程序员,所以不要很快判断:)。无论如何,我一直在努力解决这个问题一段时间了。问题是,当我调整窗口的大小(缩放比例)时,它会混淆我的所有元素。在调整我的网站大小时,你应该比以前少看点。

以下是代码:



HTML:

 < div id =leftblock> 
< / div>



< div id =rightBlock>
< / div>

CSS:

  #leftblock {

margin-left:20%;
背景颜色:红色;
float:left;
height:100px;
width:3px;

}



#rightBlock {
margin-right:20%;
背景颜色:红色;
float:right;
height:100px;
width:3px;


}

确定我的意思,测试编辑器中的代码,然后尝试调整窗口大小。



jsFiddled

解决方案

如果您不想让他们四处移动,请将元素放置在固定大小的父级容器。



DEMO



CSS



  #container {
width:1000px;
}

#leftblock {
margin-left:20%;
背景颜色:红色;
float:left;
height:100px;
width:3px;
}

#rightBlock {
margin-right:20%;
背景颜色:红色;
float:right;
height:100px;
width:3px;



HTML



 < div id =container> 
< div id =leftblock>
< / div>

< div id =rightBlock>
< / div>
< / div>


First of all im new programmer, so dont judge quickly :). Anyways ive been struggling with this problem for some time now. The problem is when im resizing(scaling whatever) the window, it messes around with ALL my elements. When resizing my website, you should just see less, than before. Not move around elements.

Here's the code:

HTML:

<div id="leftblock">
   </div>



<div id="rightBlock">
</div>

CSS:

#leftblock {   

 margin-left: 20%;
 background-color: red;
 float: left;
 height: 100px;
 width: 3px;  

}



#rightBlock { 
 margin-right: 20%;
 background-color: red;
 float: right;
 height: 100px;
 width: 3px;


}

To be sure what i mean, test the code in an editor, and then try resize the window.

jsFiddled here

解决方案

If you don't want them to move around, place the elements in a fixed size parent container.

DEMO

CSS

#container {
 width: 1000px;
}

#leftblock {   
 margin-left: 20%;
 background-color: red;
 float: left;
 height: 100px;
 width: 3px;  
}

#rightBlock { 
 margin-right: 20%;
 background-color: red;
 float: right;
 height: 100px;
 width: 3px;
}

HTML

<div id="container">
    <div id="leftblock">
    </div>

    <div id="rightBlock">
    </div>
</div>

这篇关于在调整窗口大小时让div保持同一位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆