div是块级元素,是独占一行的一般情况下,两个相邻的div是不会处于同一行的。
例如:
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>aaa</title>
<link rel="stylesheet" type="text/css" href="../css/0001.css">
</head>
<body>
<div style="height: 40px;width: 80px;background-color: #006ba4">a</div>
<div style="height: 40px;width: 80px;background-color: #f5f5f5">b</div>
<div style="height: 40px;width: 80px;background-color: #b2b2b2">c</div>
</body>
</html>
让div在同一行有两种方法:
第一种是在div里加float: left;
如
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>aaa</title>
<link rel="stylesheet" type="text/css" href="../css/0001.css">
<style>
div {
float: left;
}
</style>
</head>
<body>
<div style="height: 40px;width: 80px;background-color: #006ba4">a</div>
<div style="height: 40px;width: 80px;background-color: #f5f5f5">b</div>
<div style="height: 40px;width: 80px;background-color: #b2b2b2">c</div>
</body>
</html>
第二种方法是加display: inline-block;
如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>aaa</title>
<link rel="stylesheet" type="text/css" href="../css/0001.css">
<style>
div {
display: inline-block;
}
</style>
</head>
<body>
<div style="height: 40px;width: 80px;background-color: #006ba4">a</div>
<div style="height: 40px;width: 80px;background-color: #f5f5f5">b</div>
<div style="height: 40px;width: 80px;background-color: #b2b2b2">c</div>
</body>
</html>