欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

html使多个
在同一行 博客分类: html  

程序员文章站 2024-02-10 11:56:58
...

       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>