html - 无论两侧的div如何,如何对齐div中心
问题描述
这是基本的 html,.left 和 .right div 我无法控制它们的宽度,它们可能比我现在拥有的更小或更大,但无论它们的宽度如何,我都需要将中心 div 始终保持在.内部容器。
因为左边只有 200px 宽,右边是 400px 宽,所以 .center 容器不在中心。
.header{
background-color: blue;
height: 60px;
width: 100%;
}
.inner{
width: 1200px;
margin: 0 auto;
height: 60px;
background-color: pink;
}
.left{
width: 200px;
min-width: 0px;
max-width: 500px;
height: 60px;
background-color: red;
display: inline-block;
opacity: 0.2;
}
.center{
width: 100px;
height: 60px;
background-color: blue;
display: inline-block;
margin: 0 auto;
opacity: 1;
}
.right{
width: 400px;
min-width: 0px;
max-width: 500px;
height: 60px;
background-color: red;
float: right;
opacity: 0.2;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="header">
<div class="inner">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</div>
</body>
</html>
欢迎任何指示或解决方案
解决方案
问题的约束:目标是.center
在容器中居中。请注意,确保这 100% 涵盖了所有内容,但至少应该是一个良好的开端。请注意每个设置的 CSS 中的所有注释。更详细地显示上下文并添加边框和颜色来说明。
- 中心必须以定义为
.inner
类的包含元素为中心 - 容器
.inner
有宽度 .left
和.right
元素有不同的内容- 使用最大
1200px
宽度,左右手取 1200 像素中的 1100 像素,或每个最多 550 像素。 - 注意
.left,.right
设置:flex-grow: 0;/* set to 1 to make them take/fill max-width */
比较两个片段,这是唯一的变化
.header {
background-color: blue;
height: 60px;
border: solid 1px blue;
display: flex;
flex-basis: 1200px;/* how much */
flex-direction: row; /* across */
}
.inner {
display: flex;
flex-direction: row; /* across */
flex-wrap: nowrap;
flex-grow: 5;/* relative number */
flex-shrink: 5;/* relative number */
/* flex-basis: 1200px;/* how much, sets width if un-comment */
/*min-width: 1200px;*//* override basis */
/* max-width: 1200px;*/ /* override basis */
/*width:100%;*/
background-color: pink;
border: solid lime 4px;
}
.left,
.center,
.right {
display: flex;
/*flex:1;*/ /*display: inline-block;*/
flex-direction: column;
border: 2px solid;
}
.left,
.right {
flex: 0 1 0px; /* shorthand for next 3 */
flex-grow: 0;/* set to 1 to make them take/fill max-width */
flex-shrink: 1;
flex-basis: 100px;/* effective min width */
min-width: 0px; /* limits applied overrides basis if greater */
max-width: 550px; /* limits applied overrides basis */
}
.left {
background-color: lime;
border-color: magenta;
}
.center {
display: flex;
flex: 0 0 100px; /* shorthand for next 3 */
flex-grow: 0;
flex-shrink: 0;
flex-basis: 100px;
min-width: 100px; /* limits applied overrides basis */
max-width: 100px; /* limits applied overrides basis */
justify-content: left;
flex-direction: row;
/* width: 200px; ignored due to flex-basis but messes with border and .inner containment if set */
background-color: lightblue;
border-color: blue;
}
.right {
background-color: orange;
border-color: cyan;
}
.showcenter {
text-align: center;
/* min-width: 1200px; /* override width min */
max-width: 1200px; /* set max */
width: 100%;/* overridden by min/max */
border: dashed 2px blue;
}
.centerthing {
border-right: 1px dotted;
border-left: 1px dotted;
width: 100px;
display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="header">
<div class="inner">
<div class="left">lefty</div>
<div class="center">middle</div>
<div class="right">righty</div>
</div>
</div>
<div class="showcenter"><span class="centerthing">centered 100px element just to provide a measure</span></div>
</body>
</html>
.header {
background-color: blue;
height: 60px;
border: solid 1px blue;
display: flex;
flex-basis: 1200px;/* how much */
flex-direction: row; /* across */
}
.inner {
display: flex;
flex-direction: row; /* across */
flex-wrap: nowrap;
flex-grow: 5;/* relative number */
flex-shrink: 5;/* relative number */
/* flex-basis: 1200px;/* how much, sets width if un-comment */
/*min-width: 1200px;*//* override basis */
/* max-width: 1200px;*/ /* override basis */
/*width:100%;*/
background-color: pink;
border: solid lime 4px;
}
.left,
.center,
.right {
display: flex;
/*flex:1;*/ /*display: inline-block;*/
flex-direction: column;
border: 2px solid;
}
.left,
.right {
flex: 0 1 0px; /* shorthand for next 3 */
flex-grow: 1;/* set to 1 to make them take/fill max-width */
flex-shrink: 1;
flex-basis: 100px;/* effective min width */
min-width: 0px; /* limits applied overrides basis if greater */
max-width: 550px; /* limits applied overrides basis */
}
.left {
background-color: lime;
border-color: magenta;
}
.center {
display: flex;
flex: 0 0 100px; /* shorthand for next 3 */
flex-grow: 0;
flex-shrink: 0;
flex-basis: 100px;
min-width: 100px; /* limits applied overrides basis */
max-width: 100px; /* limits applied overrides basis */
justify-content: left;
flex-direction: row;
/* width: 200px; ignored due to flex-basis but messes with border and .inner containment if set */
background-color: lightblue;
border-color: blue;
}
.right {
background-color: orange;
border-color: cyan;
}
.showcenter {
text-align: center;
/* min-width: 1200px; /* override width min */
max-width: 1200px; /* set max */
width: 100%;/* overridden by min/max */
border: dashed 2px blue;
}
.centerthing {
border-right: 1px dotted;
border-left: 1px dotted;
width: 100px;
display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="header">
<div class="inner">
<div class="left">lefty</div>
<div class="center">middle</div>
<div class="right">righty</div>
</div>
</div>
<div class="showcenter"><span class="centerthing">centered 100px element just to provide a measure</span></div>
</body>
</html>
推荐阅读
- javascript - 加载数千张图片以在 p5.js 中使用
- python - 如何根据键从类列表中选择类 - 字典中的“喜欢”?
- python - 如何使用 ctypes.create_string_buffer 将其作为 C 函数的 char * 参数传递?
- r - 使用 googledrive R 包获取文件的大小
- javascript - 跨域 GET 请求 CORS 错误与数据功率
- app-store - MS InTune:是否可以从 AppStore 获得以前的应用程序版本?
- drupal - 在 Drupal 中为特定页面创建块
- reactjs - 运行管道时将 create-react-app 发布到 Azure 时出现 git checkout 错误我该如何解决?
- r - glht() 输出中的“估计”列是什么意思?(多压缩)
- php - select2 的错误没有正确显示