這篇文章主要介紹CSS如何實現兩欄布局,左邊固定,右邊自適應,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1. float+overflow:hidden
這種辦法主要通過 overflow 觸發 BFC,而 BFC 不會重疊浮動元素。由于設置 overflow:hidden 并不會觸發 IE6-瀏覽器的 haslayout 屬性,所以需要設置 zoom:1 來兼容 IE6-瀏覽器。具體代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.parent {
margin: 0 auto; // 使父容器水平居中顯示
overflow: hidden;
zoom: 1;
max-width: 1000px;
}
.left {
float: left;
margin-right: 20px;
width: 200px;
background-color: green;
}
.right {
overflow: hidden;
zoom: 1;
background-color: yellow;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
<p>left left left left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</body>
</html>2. float: left+ margin-left
float使左邊的元素脫離文檔流,右邊的元素可以和左邊的元素顯示在同一行,設置margin-left讓右邊的元素不覆蓋掉左邊的元素, 代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.parent {
margin: 0 auto;
max-width: 1000px;
}
.parent::after {
content: '';
display: table;
clear: both;
}
.left {
float: left;
width: 200px;
background-color: green;
}
.right {
margin-left: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
<p>left left left left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
<p>right</p>
</div>
</div>
</body>
</html>3. position: absolute + margin-left
左邊絕對定位,右邊設置margin-left,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.parent {
position: relative;
margin: 0 auto;
max-width: 1000px;
}
.left {
position: absolute;
width: 200px;
background-color: green;
}
.right {
margin-left: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
<p>left left left left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
<p>right</p>
</div>
</div>
</body>
</html>4. flex布局
flex布局可以使兩個子元素顯示在同一行,只要左邊的寬度固定,就可以實現效果, 代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.parent {
display: flex;
margin: 0 auto;
max-width: 1000px;
}
.left {
width: 200px;
background-color: green;
}
.right {
margin-left: 20px;
flex: 1;
background-color: yellow;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
<p>left left left left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
<p>right</p>
</div>
</div>
</body>
</html>以上是“CSS如何實現兩欄布局,左邊固定,右邊自適應”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。