即使调整窗口大小,如何确保元素正确对齐?

时间:2013-05-22 21:28:21

标签: javascript html css

在浏览器中查看全屏时,我的网页已正确对齐,但如果调整大小,则会出现严重错误对齐的情况。 这是html。

<html>
<head>
<style type='text/css'>
  textarea {
    height:500px;
    width:500px;
    font-size:20;
    font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
  }
  input[type="text"] {
    width: 450px;
    height: 30px;
  }
  #chatArea {
    width:600px;
    margin: 10px auto;
  }
  h1
  {
    text-align: center;
    width:600px;
    margin-left:280px;
    margin-right:20px;
    color: brown;
  }
  .greyText
  {
    color:grey;
  }
</style>
</head>

<body>
<h1>Suggestion Box</h1>
<div id="chatArea">
<textarea id='chatHistory' value="Type your suggestion here." ></textarea>
<br/><br/>
<form id= 'chatForm' onsubmit="return false">
    <input name= 'newMessageString' id="newMessageString" type="text" />
    <input type="submit" value='send'/>
</form>
</div>
</body>
</html>

如何确保页面调整大小但元素保持在中心位置?

5 个答案:

答案 0 :(得分:2)

将textarea的宽度更改为600px以填充整个居中的#chatArea div,并将h1的中心位置更改为:{/ p>

margin-left: auto;
margin-right: auto;

完整代码:

<html>
<head>
<style type='text/css'>
  textarea {
    height:500px;
    width:600px;
    font-size:20;
    font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
  }
  input[type="text"] {
    width: 450px;
    height: 30px;
  }
  #chatArea {
    width:600px;
    margin: 10px auto;
  }
  h1
  {
    text-align: center;
    width:600px;
    margin-left: auto;
    margin-right: auto;
    color: brown;
  }
  .greyText
  {
    color:grey;
  }
</style>
</head>

<body>
<h1>Suggestion Box</h1>
<div id="chatArea">
<textarea id='chatHistory' value="Type your suggestion here." ></textarea>
<br/><br/>
<form id= 'chatForm' onsubmit="return false">
    <input name= 'newMessageString' id="newMessageString" type="text" />
    <input type="submit" value='send'/>
</form>
</div>
</body>
</html>

答案 1 :(得分:1)

你需要看看响应式设计

有很多FW可以提供帮助

所有内容都基于媒体查询 @media(max-width:1023px),IE8不支持您可以查看IE8 support for CSS Media Query 或使用http://code.google.com/p/css3-mediaqueries-js/

修改

我忘了告诉你,大部分widths都是34.333%

我希望这可以帮助你并使你走上正确的轨道

答案 2 :(得分:1)

以下是代码:

<html><head>
<style type="text/css">
  textarea {
    height:500px;
    width:500px;
    font-size:20;
    font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
  }
  input[type="text"] {
    width: 450px;
    height: 30px;
  }
  #chatArea {
    text-align:center;
    width:600px;
    margin: 10px auto;
  }
  h1
  {
    text-align: center;
    color: brown;
  }
  .greyText
  {
    color:grey;
  }
</style>
</head>

<body cz-shortcut-listen="true">

<div id="chatArea">
<h1>Suggestion Box</h1><textarea id="chatHistory" value="Type your suggestion here."></textarea>
<br><br>
<form id="chatForm" onsubmit="return false">
    <input name="newMessageString" id="newMessageString" type="text">
    <input type="submit" value="send">
</form>
</div>

`</body></html>

答案 3 :(得分:1)

您可以使用百分比来适应浏览器上的布局,看看我所做的更改http://coding.smashingmagazine.com/2009/06/09/smart-fixes-for-fluid-layouts/

 <style type='text/css'>
              textarea {
                height:500px;
                width:500px;
                font-size:20;
                font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
              }
              input[type="text"] {
                width: 450px;
                height: 30px;
              }
              #chatArea {
                width:80%;
                margin: 10px auto;
                display:block;
                text-align:center;
              }
              h1
              {
                text-align: center;
                width:80%;
                margin-left:auto;
                margin-right:auto;
                color: brown;
              }
              .greyText
              {
                color:grey;
              }
            </style>

答案 4 :(得分:1)

这样做的一般方法是将您的身体内容包裹在一个固定宽度的容器中。右边距设置为“自动”以使内容居中。

<强> HTML

<body>
    <div class="container">
        <!--the rest of your content goes here-->
    </div>
</body>

<强> CSS

.container {
    width:960px; /* or whatever width you would like to set */
    margin: 0 auto;
}
相关问题