Skip to content

Instantly share code, notes, and snippets.

@linxuedong
Created December 16, 2017 08:01
Show Gist options
  • Save linxuedong/9dc11c9bfa4ad39dabe2840cabd28e72 to your computer and use it in GitHub Desktop.
Save linxuedong/9dc11c9bfa4ad39dabe2840cabd28e72 to your computer and use it in GitHub Desktop.
CSS anonymous(匿名) boxes 影响元素显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Why two lines?</title>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'>
<style>
* {
box-sizing: border-box;
font-family: 'Source Sans Pro', sans-serif;
font-weight: bold;
}
.blue {
background-color: #02b3e4;
}
.boxed {
border: 2px solid #2e3d49;
}
.container {
background-color: #dbe2e8;
padding: 8px;
}
.child {
display: inline-block;
width: 50%;
}
</style>
</head>
<body>
<div class="container">
<div class="blue boxed child">Child 1</div>
<div class="blue boxed child">Child 2</div>
</div>
</body>
</html>
@linxuedong
Copy link
Author

html page

Reason

The whitespace between the two .child divs creates a new element.

Detail

Believe it or not, tabs, spaces and newlines in HTML can actually become elements! The resulting elements are called "anonymous boxes," which get created in a number of scenarios. This is just one example of an anonymous box. Usually, anonymous boxes from whitespace don't affect the way a page displays, but this is an example of where they do!

@linxuedong
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment