我正在尝试更改 <h1>
和 <p>
文本的宽度和高度是动态的。我想得到 parent 的高度和宽度,然后使用百分比来改变 child 的高度和宽度。但是我所有的尝试都失败了。
这是我的 CSS 示例代码:
#main-holder #category1 {
height: 80px;
width: 15%;
float: left;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
border: thin solid #999;
margin-top: 5%;
margin-right: 2%;
margin-bottom: 0px;
margin-left: 6%;
text-align: center;
vertical-align: top;
}
#main-holder #category1 h1 {
font-weight: bold;
color: #FFF;
text-align: center;
vertical-align: middle;
line-height: 20%;
text-decoration: none;
}
#main-holder #category1 h1 a {
color: #FFF;
text-decoration: none;
}
#main-holder #category1 h1 a:hover {
color: #999;
text-decoration: none;
}
#main-holder #category1 p {
color: #CCC;
}
在 html 中我是这样使用的:
<div id="category1">
<h1><a href="category1.html">MOVIES</a></h1>
<p>FOR ALL AGES</p>
</div>
我像下面这样添加了宽度和高度,但没有帮助:
#main-holder #category1 h1 {
height: 50%;
width: 50%;
}
最佳答案
在你的代码中,你没有 #mainholder
,但使用了所有的 css。所以没有应用任何样式。所以我从 css 中删除了它并使用了 line-height as 100%
随着高度。作为parent div
有height as 80px
,希望这一切正常,这正是您所期望的。
已添加 font-size:2.9vw;
对于 h1
#category1 {
height: 80px;
width: 15%;
float: left;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
border: thin solid #999;
margin-top: 5%;
margin-right: 2%;
margin-bottom: 0px;
margin-left: 6%;
text-align: center;
vertical-align: top;
background-color:#666;
}
#category1 h1 {
font-weight: bold;
color: #FFF;
text-align: center;
vertical-align: middle;
line-height: 100%;
height: 100%;
text-decoration: none;
font-size:2.9vw;
}
#category1 h1 a {
color: #FFF;
text-decoration: none;
}
#category1 h1 a:hover {
color: #999;
text-decoration: none;
}
#category1 p {
color: #CCC;
}
您可以使用视口(viewport)值代替 ems、pxs 或 pts。
1vw = 1% of viewport width
1vh = 1% of viewport height
更新: Demo
对于 <p>
高度对齐问题,为<h1>
使用背景和边框以及像这样的高度和行高:
CSS:
#category1 {
height: 80px;
width: 15%;
float: left;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
margin-top: 5%;
margin-right: 2%;
margin-bottom: 0px;
margin-left: 6%;
text-align: center;
vertical-align: top;
}
#category1 h1 {
font-weight: bold;
color: #FFF;
text-align: center;
vertical-align: middle;
line-height: 80px;
height: auto;
text-decoration: none;
font-size:2.9vw;
background-color:#666;
border: thin solid #999;
}
关于html - 如何动态更改 <h1> 文本的宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674962/