html - 如何动态更改 <h1> 文本的宽度和高度?

标签 html css

我正在尝试更改 <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 divheight as 80px ,希望这一切正常,这正是您所期望的。

Updated Demo

已添加 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/

相关文章:

css - 使用 MVC 3 在 IIS 6 上动态创建 Site.css。在 dev 中工作,prod 失败。为什么?

html - 覆盖单个元素的 css 样式

html - Css id 使用了两次

javascript - 继续之前进行 API 验证

jquery - 如何对齐占位符内的文本

css - 如何为动态下拉列表设置表格列宽?

css - sticky-top 不适用于 Angular8/safari

html - CSS 框阴影 - 悬停类

javascript - 生成PDF时如何检测长表的分页符

javascript - 如何在屏幕上实时显示用户的游戏分数,而不是在最后提醒?