默认情况下,块元素将消耗其父级的全部宽度。这就是他们如何满足他们的设计要求,即垂直堆叠。
然而,这种行为并没有扩展到高度。默认情况下,大多数元素是其content(height: auto)的高度。
因此,请记住这两点:
- 除非你想要全宽,你需要定义一个块元素的宽度
- 除非您想要内容高度,否则需要定义元素的高度
具体实现代码如下:
<style>
.Contact {
display: flex; /* full width by default */
min-height: 100vh; /* use full height of viewport, at a minimum */
}
.left {
flex: 0 0 60%;
background-color: tomato;
}
.right {
flex: 1;
background-color: pink;
}
body { margin: 0; } /* remove default margins */
</style>
<div class=\"Contact\">
<section class=\"left\">
<div class=\"\">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</h1>
</div>
</section>
<section class=\"right\">
<img />
</section>
</div>
第二种方法:
<style>
.wrapper{
position: relative;
width: 200px;
}
.wrapper:before,
.wrapper:after{
content: \"\";
display: block;
height: 100%;
width: 40%;
border: 2px solid blue;
position: absolute;
top: 0;
}
.wrapper:before{
left: 0;
background-color: red;
}
.wrapper:after{
right: 0;
background-color: green;
}
.div1, .div2{
width: 40%;
display: inline-block;
position: relative;
z-index: 1;
}
.div1{
margin-right: 20%;
}
</style>
<div class=\"wrapper\">
<div class=\"div1\">Content Content Content Content Content Content Content Content Content
</div><div class=\"div2\">Other</div>
</div>
继续阅读与本文标签相同的文章
-
性能优化之卡顿延迟
2026-05-14栏目: 教程
-
第16问:Filecoin从DSN角度解读
2026-05-14栏目: 教程
-
C/C+从零基础到精通,究竟是如何快速完成的?其实只需要这6步!
2026-05-14栏目: 教程
-
谷歌再爆重大安全漏洞!华为却成最大赢家?网友:这谁还敢用!
2026-05-14栏目: 教程
-
Excel崩溃文件如何找回
2026-05-14栏目: 教程
