【问题标题】:trying to get a div to shrink width on window resize试图让 div 在调整窗口大小时缩小宽度
【发布时间】:2011-09-03 00:35:25
【问题描述】:

我现在觉得自己像个白痴。无论如何 - 该页面有三列。左右是固定宽度,必须保持不变。我需要在浏览器窗口调整大小时调整中心列的大小。我知道我知道,这应该不难,但这对我来说并没有发生。这是html:

<div class="bottom_content">

<div class="left_col">

<p>Keeping up with Commpro.Biz</p>

<div class="s_n_icons">

<img src="images/facebook.png" alt="facebook" />

<img src="images/flickr.png" alt="flickr" />

<img src="images/twitter.png" alt="twitter" />

<img src="images/youtube.png" alt="youtube" />

</div>

<p>User Name:<input ="text" size="20" name="username" value="" /></p>

<p>Password:<input ="text" size="20" name="password" value="" /></p>

<input type="button" value="submit" name="submit" />

<p><a href="#">Forgot Username or Password?</a>

<p>Click <a href="#">here</a> to sign up to receive the Commpro.Biz newsletter.</p>

<div id="twitter"></div>

</div>

<div class="center_col">

<p>What's Hot on Commpro.Biz</p>

<div class="hot_item">
hot item hot item hot item hot item hot item hot item hot itemhot item hot item
</div>

</div>

<div class="right_col">

<p>Our Partners</p>

<img src="images/BW.jpg" alt="Warren Buffett" />

<img src="images/IR_PRNewswire.gif" alt="newswire" />

<img src="images/Sysomo.gif" alt="Sysomo" />

</div>

<div id="footer">
    <p>Copyright 2011 by CommproBiz</p>
</div>
</div>

和 .CSS

.left_col{
float:left;
width:190px;
height:auto;
padding-right:5px;
}

.center_col{
float:left;
width:auto;
padding:0 10px;
}

.hot_item{
width:auto;
background-color:red;
}

.hot_item a{
text-decoration:none;
}

.hot_item img{
float: right;
vertical-align: top;
}

.right_col{
float:right;
width:250px;
height:auto;
padding-left:5px;
}

.right_col img{
margin:5px;
}

发生的情况是,中心列中的内容很少,三列的行为。一旦超过一定数量的内容进入它,它就会下降到左列下方,而右列下降。

【问题讨论】:

    标签: html css


    【解决方案1】:

    如果你浮动一个元素,你必须指定一个宽度。如果您不这样做,则没有指定浏览器的行为方式,因此每个浏览器的行为可能不同。

    您可以做的是以百分比形式给列指定宽度,例如左边是 10%,中间是 70%,右边是 20%。

    如果你想要右列和左列的固定宽度。您应该将 right-col 的 html 移到 center-col 上方。然后在 css 中,您应该删除 center-col 上的浮动并给它一个 margin-left:195px;和margin-right:255px;

    【讨论】:

      【解决方案2】:

      我使用clear:both; 属性将您的版权信息降低到左侧边栏下方,当页面充满内容时,版权会随之降低。

      这是固定的 HTML:

      <div class="bottom_content">
      <div class="left_col">
      <p>Keeping up with Commpro.Biz</p>
      <div class="s_n_icons">
      <img src="images/facebook.png" alt="facebook" />
      <img src="images/flickr.png" alt="flickr" />
      <img src="images/twitter.png" alt="twitter" />
      <img src="images/youtube.png" alt="youtube" />
      </div>
      <p>User Name:<input ="text" size="20" name="username" value="" /></p>
      <p>Password:<input ="text" size="20" name="password" value="" /></p>
      <input type="button" value="submit" name="submit" />
      <p><a href="#">Forgot Username or Password?</a>
      <p>Click <a href="#">here</a> to sign up to receive the Commpro.Biz newsletter.</p>
      <div id="twitter"></div>
      </div>
      <div class="right_col">
      <p>Our Partners</p>
      <img src="images/BW.jpg" alt="Warren Buffett" />
      <img src="images/IR_PRNewswire.gif" alt="newswire" />
      <img src="images/Sysomo.gif" alt="Sysomo" />
      </div>
      <div class="center_col">
      <p>What's Hot on Commpro.Biz</p>
      <div class="hot_item">hot item hot item hot item hot item hot item hot item hot itemhot item hot item</div>
      </div>
      <div id="footer">
      <p>Copyright 2011 by CommproBiz</p>
      </div>
      </div>
      </body>
      

      然后是固定的CSS:

      .left_col{
      float:left;
      width:190px;
      height:auto;
      padding-right:5px;
      }.center_col{
      margin-left:195px;
      margin-right:254px;
      width:auto;
      padding:0 10px;
      }.hot_item{
      width:auto;
      background-color:red;
      }.hot_item a{
      text-decoration:none;
      }.hot_item img{
      float: right;
      vertical-align: top;
      }.right_col{
      float:right;
      width:250px;
      position:relative;
      left:1px;
      padding-left:5px;
      }.right_col img{
      margin:5px;
      }#footer{
      clear:both;
      }
      

      希望有帮助!

      【讨论】:

      • 这是我需要帮助并从 Gerben 那里得到答案的中心 div,但谢谢。
      猜你喜欢
      • 1970-01-01
      • 2013-02-11
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2014-11-10
      • 1970-01-01
      • 2013-12-22
      相关资源
      最近更新 更多