html - 尽管有内容,但反应 div 标签高度为零,
问题描述
在我的项目中,我有一个Pokedex 类标签,其中包含一些 Pokecard 组件(在图片上可见),但是,div 标签的高度始终为零,只有在我在 CSS 中将其设置为 fe height=1000px; 高度设置正确。例如查看屏幕
我的代码:CSS:
.Pokecard img {
margin-left: auto;
margin-right: auto;
display: block;
}
.Pokecard p {
text-align: center;
margin: 3px;
}
.Pokecard {
border: 1px solid black;
width: 20%;
max-width: 180px;
min-width: 100px;
border-radius: 10px;
padding: 10px;
background-color: rgb(235, 199, 238);
float: left;
margin: 2%;
}
.Pokedex {
margin: auto;
max-width: 700px;
width: 95%;
background-color: cornsilk;
height: max-content;
}
图鉴组件:
myProps = this.props.prokeList;
render() {
return (
<div className="Pokedex">
{[...this.myProps].map((e, index) => {
return (
<Pokecard
key={`pk_${index}`}
name={e.name}
id={e.id}
baseExperience={e.base_experience}
type={e.type}
/>
);
})}
</div>
);
}
}
扑克牌组件
class Pokecard extends React.Component {
render() {
return (
<div className="Pokecard">
<h3>{this.props.name}</h3>
<img
src={'https://raw.githubusercontent.com/PokeAPI/sprites/master/' +
`sprites/pokemon/${this.props.id}.png`} alt={this.props.name}/>
<p>Type: {this.props.type}</p>
<p>EXP: {this.props.baseExperience}</p>
</div>
)
}
}
为什么会发生这种情况,我在浏览器检查器中看到 div.Pokedex 包含组件但高度为 0px?
已解决参考 Danko 的回答,我将这个clearfix 添加到我的 CSS 中:
.Pokedex::after {
content: "";
clear: both;
display: table;
}
解决方案
您的父元素已折叠,因为子元素是浮动的。使用清除修复。
推荐阅读
- python - 如何将所有维基百科图像添加到我的 docx 文件中?
- python - Sympy 中的半整数值
- python - Numpy 连接 void 二进制缓冲区
- node.js - 如何为外部字体请求设置 AWS Cloudfront 缓存?
- python - 在 python 3.9 中通过 pip install 运行 jupyterlab 时出错
- flutter - Flutter - 谷歌地图可以在模拟器上运行,但不能在手机上运行
- php - 在 laravel 8 中调用未定义函数 App\Http\Controllers\get_option()
- azure-devops - 阻止用户在 Azure DevOps 中创建新的工作项
- node.js - 在多个 websocket 服务器上设置字符串值
- c++ - 编译 MATIO 库时出错:“未定义对 'Mat_Open' 的引用”