Skip to content

Instantly share code, notes, and snippets.

@jasonzhouu
Last active March 11, 2020 05:21
Show Gist options
  • Save jasonzhouu/5f3682fdbde423233ba41c6b6e0d67eb to your computer and use it in GitHub Desktop.
Save jasonzhouu/5f3682fdbde423233ba41c6b6e0d67eb to your computer and use it in GitHub Desktop.
inline, block, inline-block的尺寸设置的区别
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>1</div>
<div></div>
<div></div>
</body>
</html>

inline无法设置尺寸(宽度、高度),它的尺寸是有它的内容决定的,有多少内容就占多少空间。即使设置了尺寸,也不会生效。

block, inline-block则可以设置尺寸。

div {
width: 1em;
height: 1em;
}
div:nth-of-type(1) {
display: inline;
background-color: red;
}
div:nth-of-type(2) {
display: block;
background-color: green;
}
div:nth-of-type(3) {
display: inline-block;
background-color: blue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment