HTML常见的布局问题
两个盒子左边固定宽度右边自适应填满屏幕
一、使用浮动和外边距
首先给左边的元素进行左浮动,给想处于右边的盒子设置左边图片宽的外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left{
float: left;
width: 100px;
height: 100px;
background-color: red;
}
.rigth{
margin-left: 100px;
height: 100px;
background-color: aqua;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="left"></div>
<div class="rigth"></div>
</body>
</html>
结果显示如下图
二、使用overflow+浮动
首先给左边的盒子设置向左浮动,右边的盒子不设置宽度,但是设置overflow:hidden
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left{
float: left;
width: 100px;
height: 100px;
background-color: red;
}
.rigth{
overflow: hidden;
height: 100px;
background-color: aqua;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="left"></div>
<div class="rigth"></div>
</body>
</html>
效果图和上图一样
三、使用绝对定位+外边距
首先给左边的盒子设置position:absolute,这样就可以脱离标准流了,然后给右边的盒子设置左边距为左边盒子宽度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left{
position: absolute;
width: 100px;
height: 100px;
background-color: red;
}
.rigth{
height: 100px;
margin-left: 100px;
background-color: aqua;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="left"></div>
<div class="rigth"></div>
</body>
</html>
四、使用display:flex属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
display: flex;
}
.left{
width: 100px;
height: 100px;
background-color: red;
}
.rigth{
height: 100px;
background-color: aqua;
flex: 1;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
<div class="rigth"></div>
</div>
</body>
</html>
上面的方法是为两个盒子设置一个父元素,其中父元素设置一个display:flex,这样两个子元素就可以横向了,给右边的盒子设置一个flex:1,就可以平摊剩下的空间了
水平垂直居中的方法
在有固定宽度的情况下
一、使用position定位和外边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: relative;
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
width: 500px;
height: 500px;
}
.left{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
本代码中因为需要居中的盒子是带有宽度的,所以我们给父元素设置一个相对定位,子元素设置一个绝对定位,然后让子元素进行left:50%和top:50%,这样盒子左上角就会居中,我们最后使用margin调整盒子的中心居中
二、使用margin+position
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: relative;
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
width: 500px;
height: 500px;
}
.left{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
上面的代码中,我们使用position和margin进行调整
三、使用position+calc
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: relative;
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
width: 500px;
height: 500px;
}
.left{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: calc(50% - 50px);
top:calc(50% - 50px);
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
上述代码中的calc是css3中的新属性,可以自动帮你进行计算
不固定宽度的情况下
一、使用display:flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.left{
width: 20%;
height: 100px;
background-color: red;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
在上述代码中我们对父元素使用display:flex,然后给父元素中的子元素强行规定水平和垂直都居中
二、使用表格的方法进行水平垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
height: 500px;
width: 500px;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.left{
width: 20%;
height: 20%;
display: inline-block;
background-color: saddlebrown;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
三、使用display:grid进行居中,但是这个属性的兼容性不是很好
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
height: 500px;
width: 500px;
display: grid;
}
.left{
width: 20%;
height: 20%;
align-self: center;
justify-self: center;
background-color: saddlebrown;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>
四、使用position+transform
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
border: solid 2px saddlebrown;
/* 为了方便看见父元素 */
height: 500px;
width: 500px;
position: relative;
}
.left{
width: 20%;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
background-color: salmon;
}
/* 添加背景颜色的原因是更方便观看 */
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
</div>
</body>
</html>