html - 将一排 div 居中在容器中
问题描述
我有一个小问题将我的 div 放在一个容器中。我尝试了所有我能找到的东西,但问题仍然存在。目标是将绿色盒子放在黄色盒子的中间。它应该是响应式的。主要问题是浮动框没有居中。我也不明白为什么黄色空间仍然存在于小视图中。绿色框应该在整个地方(示例 belove 看起来很奇怪)。
谢谢你的帮助
body,html{
width:100%;
height:100%;
}
#hans{
background:red;
width:50rem;
height:28%;
display:table;
}
.test-center{
height:30rem;
width:90vw;
display: block;
margin: 0 auto;
background: yellow;
}
.content-block{
background: green;
width:45rem;
height:90%;
margin-left:1%;
5margin-top: 8rem;
display: inline-block;
5padding-top:2rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet">
<link href="test.css" rel="stylesheet">
<!--TzITT-->
<title>Test right site</title>
</head>
<body>
<div class="container" id="hans">
<div class= "test-center">
<div class = "content-block">
<p>fwfwf</p>
</div>
<div class = "content-block">
<p>fwfwf</p>
</div>
<div class = "content-block">
<p>fwfwf</p>
</div>
</div>
</div>
<div class = "footer">
</div>
</body>
</html>
解决方案
body,html{
width:100%;
height:100%;
}
#hans{
background:red;
width:50rem;
height:28%;
display:table;
}
.test-center{
height:30rem;
width:90vw;
display: block;
margin: 0 auto;
background: yellow;
display:flex;
justify-content:center;
align-items:center;
}
.content-block{
flex:1;
background: green;
height:90%;
display: inline-block;
margin:1%;
}
@media screen and (max-width:728px){
.test-center {
flex-direction:column;
}
.content-block{
width:100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:300" rel="stylesheet">
<link href="test.css" rel="stylesheet">
<!--TzITT-->
<title>Test right site</title>
</head>
<body>
<div class="container" id="hans">
<div class= "test-center">
<div class = "content-block">
<p>fwfwf</p>
</div>
<div class = "content-block">
<p>fwfwf</p>
</div>
<div class = "content-block">
<p>fwfwf</p>
</div>
</div>
</div>
<div class = "footer">
</div>
</body>
</html>
推荐阅读
- c - 在 C 中使用 printf() 和 fork() 重复输出
- python - 树节点计数器算法:请逐步解释此递归
- jquery - 使用 Jquery 和 Materialize css 在 HTML 中添加选项在 laravel 刀片中不起作用
- c++ - 如何将具有十六进制值的 std::string 转换为 std::vector
- javascript - 如何从 jQuery.fn.init() 和 jQuery.fn.init 中取出特定值 [prevObject: jQuery.fn.init()]
- javascript - 在数组数组上使用 math.random 的问题
- highcharts - Highcharts 组织结构图的拖放功能
- python - 从 json 文件中提取 URL
- javascript - 如何比较两个对象数组并删除 JavaScript 中的重复项?
- javascript - 如何在我的网站上的光标上制作十字准线效果