html - 重叠的盒子

标签 html css

我在尝试将盒子保留在容器 ID 内时遇到了麻烦,但由于某种原因,它们一直在右侧重叠。我对此很陌生,所以请对我放轻松。

Overlapping Boxes

<!DOCTYPE html>
<html>
<head>
	<title>Block-Level</title>
	<style type="text/css">
		#container{
		/* Auto margins used for centering elements horizontally */
		margin-right: auto; 
		margin-left: auto; 
		/* Box dimensions */
		width: 1000px;
		/* Colour of box */
		background-color: #AA4639;
		padding: 5px;
		border-radius: 10px;
		font-size: 0;
		}
		.box1{
			width: 100%;
			height: 200px;
			background-color: #81BBC9;
			border-radius: 10px;
			display: inline-block;
			font-size: 16px;
			margin: 5px;
			padding: 5px;
		}
	</style>
</head>
<body>
	<div id="container">
		<div class="box1"></div>
		<div class="box1"></div>
		<div class="box1"></div>
	</div>
</body>
</html>

最佳答案

删除边距、侧面填充,但不删除顶部/底部。如:

/* vertical | horizontal */
margin: 5px 0;
padding: 5px 0;

查看演示:

#container {
  /* Auto margins used for centering elements horizontally */  
  margin-right: auto;
  margin-left: auto;
  /* Box dimensions */
  width: 500px;
  /* Colour of box */
  background-color: #AA4639;
  padding: 5px;
  border-radius: 10px;
  font-size: 0;
}

.box1 {
  width: 100%;
  height: 200px;
  background-color: #81BBC9;
  border-radius: 10px;
  display: inline-block;
  font-size: 16px;
  margin: 5px 0; /* here */
  padding: 5px 0; /* here */
}
<div id="container">
  <div class="box1"></div>
  <div class="box1"></div>
  <div class="box1"></div>
</div>

关于html - 重叠的盒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599697/

相关文章:

javascript - 从索引中查找表中具有属性的 tr

python - if 和 elif 在模板 django 中不起作用

html - bootstrap 3 - 格式化 div 和 css 问题

javascript - 在网络摄像头视频之上添加内容,即颜色叠加

html - Sass CSS - 更改导航栏链接颜色

html - 歌剧溢出问题 css/html

html - 使用 CSS 在每个滚动 div 上放置固定行

html - 消除 HTML 中的额外高度

javascript - 偏移 html anchor 以调整固定标题

javascript - 垂直全页 slider ,如 Angular 中的 www.tumblr.com