css - Why is text contained in div that follows a floated div appearing out of place?
问题描述
I have 4 divs in this order : #header , #content, #navigation, #footer.
#header {
background: lightblue;
height: 10%;
border: 1px solid black;
}
#content {
background: green;
opacity: 0.5;
width: 74%;
float: left;
height: 80%;
border: 1px solid black;
}
#navigation {
background: brown;
height: 80%;
width: 24%;
text-align: center;
border: 1px solid black;
}
#footer {
background: hotpink;
height: 10%;
border: 1px solid black;
}
body,html {
height:100%;
margin:0;
}
<div id="header">DEFAULT</div>
<div id="content">FLOAT</div>
<div id="navigation">NAVIGATION</div>
<div id="footer">CLEAR</div>
I am learning css, and in this scenario my understanding is that a non floated block level div named "navigation" will move in to take the place of a left floated div "content". The text 'NAVIGATION' inside of the div with id "navigation" is not hiding behind the #content div and is instead appearing inside of #footer div.
After going through this question Text in floated div I learnt that content in following div will float around this floated div.
Now since this #content div is only 75% wide, why is the NAVIGATION text not appearing right next to the #content div ? Why does it appear inside of the #footer div ?
解决方案
display:inline-block 是使用浮点数的更好方法
inline-block 比 float 更好,使用 float 方法不适合页面布局的原因是,float CSS 属性最初只是为了让文本环绕图像,并且从设计上来说不适合一般页面布局目的
哟可以这样做,先删除
向左飘浮;
在#content 并添加
显示:内联块;
并添加
显示:内联块;
#header {
background: lightblue;
height: 10%;
border: 1px solid black;
}
#content {
background: green;
opacity: 0.5;
width: 74%;
display: inline-block;
height: 80%;
border: 1px solid black;
}
#navigation {
background: brown;
height: 80%;
width: 24%;
display: inline-block;
text-align: center;
border: 1px solid black;
}
#footer {
background: hotpink;
height: 10%;
border: 1px solid black;
}
body,html {
height:100%;
margin:0;
}
<div id="header">DEFAULT</div>
<div id="content">FLOAT</div>
<div id="navigation">NAVIGATION</div>
<div id="footer">CLEAR</div>
推荐阅读
- php - 添加自定义 WordPress Rest API
- android-emulator - Android 模拟器太大,`scale` 参数被贬低
- typescript - JSDoc 与 Interace 的 Typescript 错误:错误:无法解析
- python - 运行应用程序时出错(ModuleNotFoundError: No module named 'tornado.ioloop')
- angular - mat-datepicker UI 停止正常工作
- javascript - 循环遍历 3 个不同的 RGB 滑块
- vba - 如何在电子邮件正文中包含发件人的详细信息?
- r - 减去数据框中的列 Shiny
- asp.net-core - dotnet publish commandline with platform parameter value of "Any CPU" for a .NET Core Web API Application
- python - A problem with compatibility of the script on Windows