javascript - 根据里面的信息调整div的大小

标签 javascript jquery html css resize

我有一个容器,我想在其中保存信息(会定期更新)。但有时,我可能希望此信息或长或短,为此我无法在容器上设置固定高度,因此应根据文本/信息的数量调整大小。这是我的脚本。

HTML:

<html>
<head>
<title>LoL Champs</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
<link href='http://fonts.googleapis.com/css?family=Sansita+One' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Belleza' rel='stylesheet' type='text/css'>
</head>
<body>

<!-- NAVIGATION --> 
<div id="nav-bar">
    <div id="nav-bar-title">LoL Champs</div>
    <ul>
    <li><a href="">Champs</a></li>
    <li><a href="">Info</a></li>
    <li><a href="">Guides</a></li>
    <li><a href="">Model Viewer</a></li>
    <li><a href="">Lists</a></li>
    </ul>
</div>
<!-- END NAVIGATION -->

<div id="main-body">
<div id="nav-body-divider"></div>

<p id="home-body-title">News</p>
<!-- News Container -->
<div id="news-container">

</div>

</div>

</body>
</html>

和 CSS:

/** MAIN PAGE CONFIG CSS **/

html, body {
    width: 100%;
    height: 100%;
    margin: 0 auto;
    max-width: 1920px;
    max-height: 1050px;
    font-size: 100%;
}

html { 
  background: url(../images/JinxBG.jpg) 0 0 fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

p {
    color: white;
}

a:link {color: inherit;}
a:visited {color: inherit;}
a:active {color: inherit;}
a {text-decoration: none;}
/** MAIN CSS **/

/**NAVIGATION**/
#nav-bar {
    width: 100%;
    height: 10%;
    background-color: #A62D2D;
    position: fixed;
    box-shadow: 0px 2px 9px black;
    font-size: 100%;
    max-height: 10%;
    max-width: 100%;
}

#nav-bar-title {
    font-family: 'Sansita One';
    color: #262424;
    position: absolute;
    width: 20%;
    height: 100%;
    left: 0.3%;
    font-size: 1.8em;
    max-width: 16%;
    margin-top: 1.2%;
}

#nav-bar ul {
    list-style-type: none;
    color: #2E2C2C;
    font-family: 'Belleza';
    font-size: 1.5em;
    position: absolute;
    top: 0;
    max-width: 100%;
    max-height: 100%;
    left: 15%;
    line-height: 0%;
    margin-top: 0;
    margin-bottom: 10%;
    top: 50%;
}
#nav-bar li {
    margin-right: 45px;
    display: inline;
    height: 100%;
    -webkit-transition: color 0.5s ease;
}
#nav-bar li:hover {
    color: #C7C7C7;
    -webkit-transition: color 0.5s ease;
}

/** END NAVIGATION **/

/** MAIN TEXT BODY **/
#nav-body-divider {
    width: 100%;
    height: 1px;
    background: #B55050;

}

#main-body {
    max-width: 100%;
    max-height: 90%;
    background: #A62D2D;
    width: 70%;
    height: 100%;
    margin: auto;
    position: relative;
    top: 10%;
}

/** MAIN BODY NEWS **/

#home-body-title {
    font-size: 2.4em;
    font-family: Belleza;
    color: #C98A5D;
    position: relative;
    left: 3%;
    top: 0%;
}

#news-container {
    width: 45%;
    height: 40%;
    background: #CF4040;
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    position: relative;
    left: 3%;
}

我想调整 news-container 的大小,以便它适应其中的内容量,但只根据高度调整它的大小。宽度应保持不变。另外,如果 div 更大,我如何移动该 div 下面的对象以容纳页面的更下方并为大容器腾出空间?

干杯,尼克

最佳答案

你不应该在任何东西上设置高度。始终让内容自然增长。 http://jsfiddle.net/LQkj3/

<div id="news-container">
  <p>
    This is the news section! height will automatically grow depending on the amount of data! you should NEVER control height with pixels or percentage! always let the content grow it automatically.
  </p>
</div>


<style>
#news-container { width: 45%; background: #CF4040; -webkit-border-radius: 7px; -moz-border-radius: 7px; border-radius: 7px; position: relative; left: 3%;  padding: .5em 1em; }
</style>

...除非您当然需要一个可滚动的容器(如@griegs 建议的那样)。 ;)

然后你可以设置一个 max-width: 10em;或类似的东西。

关于javascript - 根据里面的信息调整div的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554250/

相关文章:

html - SVG 背景颜色不适用于 Firefox

html - 像在浏览器中一样加载网页,但使用 Ruby

javascript - 正则表达式匹配单个特殊字符或特定序列

html - IE7 上的 ASP WebForms DropDownList 怪异行为

javascript - GitHub 在浏览存储库时如何实现其奇特的页面删除效果?

javascript - 当键有多个条目时,如何处理二维数组搜索?

javascript - 单击添加/删除隐藏的溢出

javascript - jquery,将单击事件附加到函数并与一串 JavaScript 调用相结合?

javascript - 隐藏零值的图例

javascript - 带传单的多边形标签