如何动态地将兄弟 div 垂直对齐到父高度
How to align sibling divs vertically to parent height dynamically
我知道有许多可用于类似问题的解决方案,我也尝试过它们,但直到现在都没有奏效。
我想动态地将以下代码段中的元素垂直对齐到它们的父高度,但找不到可行的方法。有什么建议吗?
.wrap {
margin: 0 auto;
max-width: 1140px;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
float: left;
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
float: right;
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}audio,canvas,video{display:inline-block}audio:not([controls]){display:none;height:0}[hidden]{display:none}html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a:link,
a:visited,a:hover,a:active,a:focus{outline:0 none;text-decoration:none}h1{font-size:2em;margin:.67em 0}abbr[title]{border bottom:1px dotted}b,strong{font-weight:bold}dfn{font-style:italic}hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}mark{background:#ff0;color:#000}code,kbd,pre,samp{font-family:monospace,serif;font-size:1em}pre{white-space:pre-wrap}q{quotes:"1C" "1D" "18" "19"}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-0.5em}sub{bottom:-0.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:0}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{border:0;padding:0}button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}button,input{line-height:normal}button,select{text-transform:none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}textarea{overflow:auto;vertical-align:top}table{border-collapse:collapse;border-spacing:0}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
CSS table
您可以使用display: table
和display: table-cell
的组合来实现垂直倾斜的效果:
.wrap {
margin: 0 auto;
max-width: 1140px;
display: table; /* Added */
}
.left {
display: table-cell; /* Added */
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
display: table-cell; /* Added */
width: 50%;
text-align: left;
padding-left: 20px;
}
.wrap {
margin: 0 auto;
max-width: 1140px;
display: table;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
display: table-cell;
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
display: table-cell;
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
display: block
}
audio,
canvas,
video {
display: inline-block
}
audio:not([controls]) {
display: none;
height: 0
}
[hidden] {
display: none
}
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%
}
body {
margin: 0
}
a:link,
a:visited,
a:hover,
a:active,
a:focus {
outline: 0 none;
text-decoration: none
}
h1 {
font-size: 2em;
margin: .67em 0
}
abbr[title] {
border bottom: 1px dotted
}
b,
strong {
font-weight: bold
}
dfn {
font-style: italic
}
hr {
-moz-box-sizing: content-box;
box-sizing: content-box;
height: 0
}
mark {
background: #ff0;
color: #000
}
code,
kbd,
pre,
samp {
font-family: monospace, serif;
font-size: 1em
}
pre {
white-space: pre-wrap
}
q {
quotes: "1C" "1D" "18" "19"
}
small {
font-size: 80%
}
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline
}
sup {
top: -0.5em
}
sub {
bottom: -0.25em
}
img {
border: 0
}
svg:not(:root) {
overflow: hidden
}
figure {
margin: 0
}
fieldset {
border: 1px solid silver;
margin: 0 2px;
padding: .35em .625em .75em
}
legend {
border: 0;
padding: 0
}
button,
input,
select,
textarea {
font-family: inherit;
font-size: 100%;
margin: 0
}
button,
input {
line-height: normal
}
button,
select {
text-transform: none
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
-webkit-appearance: button;
cursor: pointer
}
button[disabled],
html input[disabled] {
cursor: default
}
input[type="checkbox"],
input[type="radio"] {
box-sizing: border-box;
padding: 0
}
input[type="search"] {
-webkit-appearance: textfield;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
-webkit-appearance: none
}
button::-moz-focus-inner,
input::-moz-focus-inner {
border: 0;
padding: 0
}
textarea {
overflow: auto;
vertical-align: top
}
table {
border-collapse: collapse;
border-spacing: 0
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
弹性框
如果您不介意使用 CSS flexbox(添加供应商前缀后浏览器支持高达 97%),只需删除浮点数并在 [=] 上使用 display: flex
和 align-items: center
20=] 选择器也能正常工作:
.wrap {
margin: 0 auto;
max-width: 1140px;
display: flex; /* Added */
align-items: center; /* Added */
}
.wrap {
margin: 0 auto;
max-width: 1140px;
display: flex;
align-items: center;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
display: block
}
audio,
canvas,
video {
display: inline-block
}
audio:not([controls]) {
display: none;
height: 0
}
[hidden] {
display: none
}
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%
}
body {
margin: 0
}
a:link,
a:visited,
a:hover,
a:active,
a:focus {
outline: 0 none;
text-decoration: none
}
h1 {
font-size: 2em;
margin: .67em 0
}
abbr[title] {
border bottom: 1px dotted
}
b,
strong {
font-weight: bold
}
dfn {
font-style: italic
}
hr {
-moz-box-sizing: content-box;
box-sizing: content-box;
height: 0
}
mark {
background: #ff0;
color: #000
}
code,
kbd,
pre,
samp {
font-family: monospace, serif;
font-size: 1em
}
pre {
white-space: pre-wrap
}
q {
quotes: "1C" "1D" "18" "19"
}
small {
font-size: 80%
}
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline
}
sup {
top: -0.5em
}
sub {
bottom: -0.25em
}
img {
border: 0
}
svg:not(:root) {
overflow: hidden
}
figure {
margin: 0
}
fieldset {
border: 1px solid silver;
margin: 0 2px;
padding: .35em .625em .75em
}
legend {
border: 0;
padding: 0
}
button,
input,
select,
textarea {
font-family: inherit;
font-size: 100%;
margin: 0
}
button,
input {
line-height: normal
}
button,
select {
text-transform: none
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
-webkit-appearance: button;
cursor: pointer
}
button[disabled],
html input[disabled] {
cursor: default
}
input[type="checkbox"],
input[type="radio"] {
box-sizing: border-box;
padding: 0
}
input[type="search"] {
-webkit-appearance: textfield;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
-webkit-appearance: none
}
button::-moz-focus-inner,
input::-moz-focus-inner {
border: 0;
padding: 0
}
textarea {
overflow: auto;
vertical-align: top
}
table {
border-collapse: collapse;
border-spacing: 0
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
静态:
给 .container 一个固定的高度,并给它的子元素一个 100% 的高度:
.container{
height:140px;
}
.wrap, .left, .right{
height:100%;
}
https://jsfiddle.net/b3y2pjtr/
动态:
忽略.container。在 .wrap、.left 和 .right 上使用 flexbox 属性:
.wrap{
display:flex;
width:100%;
}
.left, .right{
flex:1;
}
我知道有许多可用于类似问题的解决方案,我也尝试过它们,但直到现在都没有奏效。
我想动态地将以下代码段中的元素垂直对齐到它们的父高度,但找不到可行的方法。有什么建议吗?
.wrap {
margin: 0 auto;
max-width: 1140px;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
float: left;
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
float: right;
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}audio,canvas,video{display:inline-block}audio:not([controls]){display:none;height:0}[hidden]{display:none}html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a:link,
a:visited,a:hover,a:active,a:focus{outline:0 none;text-decoration:none}h1{font-size:2em;margin:.67em 0}abbr[title]{border bottom:1px dotted}b,strong{font-weight:bold}dfn{font-style:italic}hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}mark{background:#ff0;color:#000}code,kbd,pre,samp{font-family:monospace,serif;font-size:1em}pre{white-space:pre-wrap}q{quotes:"1C" "1D" "18" "19"}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-0.5em}sub{bottom:-0.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:0}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{border:0;padding:0}button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}button,input{line-height:normal}button,select{text-transform:none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}textarea{overflow:auto;vertical-align:top}table{border-collapse:collapse;border-spacing:0}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
CSS table
您可以使用display: table
和display: table-cell
的组合来实现垂直倾斜的效果:
.wrap {
margin: 0 auto;
max-width: 1140px;
display: table; /* Added */
}
.left {
display: table-cell; /* Added */
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
display: table-cell; /* Added */
width: 50%;
text-align: left;
padding-left: 20px;
}
.wrap {
margin: 0 auto;
max-width: 1140px;
display: table;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
display: table-cell;
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
display: table-cell;
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
display: block
}
audio,
canvas,
video {
display: inline-block
}
audio:not([controls]) {
display: none;
height: 0
}
[hidden] {
display: none
}
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%
}
body {
margin: 0
}
a:link,
a:visited,
a:hover,
a:active,
a:focus {
outline: 0 none;
text-decoration: none
}
h1 {
font-size: 2em;
margin: .67em 0
}
abbr[title] {
border bottom: 1px dotted
}
b,
strong {
font-weight: bold
}
dfn {
font-style: italic
}
hr {
-moz-box-sizing: content-box;
box-sizing: content-box;
height: 0
}
mark {
background: #ff0;
color: #000
}
code,
kbd,
pre,
samp {
font-family: monospace, serif;
font-size: 1em
}
pre {
white-space: pre-wrap
}
q {
quotes: "1C" "1D" "18" "19"
}
small {
font-size: 80%
}
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline
}
sup {
top: -0.5em
}
sub {
bottom: -0.25em
}
img {
border: 0
}
svg:not(:root) {
overflow: hidden
}
figure {
margin: 0
}
fieldset {
border: 1px solid silver;
margin: 0 2px;
padding: .35em .625em .75em
}
legend {
border: 0;
padding: 0
}
button,
input,
select,
textarea {
font-family: inherit;
font-size: 100%;
margin: 0
}
button,
input {
line-height: normal
}
button,
select {
text-transform: none
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
-webkit-appearance: button;
cursor: pointer
}
button[disabled],
html input[disabled] {
cursor: default
}
input[type="checkbox"],
input[type="radio"] {
box-sizing: border-box;
padding: 0
}
input[type="search"] {
-webkit-appearance: textfield;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
-webkit-appearance: none
}
button::-moz-focus-inner,
input::-moz-focus-inner {
border: 0;
padding: 0
}
textarea {
overflow: auto;
vertical-align: top
}
table {
border-collapse: collapse;
border-spacing: 0
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
弹性框
如果您不介意使用 CSS flexbox(添加供应商前缀后浏览器支持高达 97%),只需删除浮点数并在 [=] 上使用 display: flex
和 align-items: center
20=] 选择器也能正常工作:
.wrap {
margin: 0 auto;
max-width: 1140px;
display: flex; /* Added */
align-items: center; /* Added */
}
.wrap {
margin: 0 auto;
max-width: 1140px;
display: flex;
align-items: center;
}
.container {
background-color: #f7f7f7;
padding: 20px 5%;
}
.container p {
margin: 0;
}
.left {
width: 50%;
text-align: right;
padding-right: 20px;
}
.right {
width: 50%;
text-align: left;
padding-left: 20px;
}
a.button {
background-color: #333;
border-bottom-width: 0;
border-radius: 5px;
color: #fff;
display: inline-block;
font-size: 18px;
font-weight: 300;
line-height: 1;
padding: 15px 20px;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
display: block
}
audio,
canvas,
video {
display: inline-block
}
audio:not([controls]) {
display: none;
height: 0
}
[hidden] {
display: none
}
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%
}
body {
margin: 0
}
a:link,
a:visited,
a:hover,
a:active,
a:focus {
outline: 0 none;
text-decoration: none
}
h1 {
font-size: 2em;
margin: .67em 0
}
abbr[title] {
border bottom: 1px dotted
}
b,
strong {
font-weight: bold
}
dfn {
font-style: italic
}
hr {
-moz-box-sizing: content-box;
box-sizing: content-box;
height: 0
}
mark {
background: #ff0;
color: #000
}
code,
kbd,
pre,
samp {
font-family: monospace, serif;
font-size: 1em
}
pre {
white-space: pre-wrap
}
q {
quotes: "1C" "1D" "18" "19"
}
small {
font-size: 80%
}
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline
}
sup {
top: -0.5em
}
sub {
bottom: -0.25em
}
img {
border: 0
}
svg:not(:root) {
overflow: hidden
}
figure {
margin: 0
}
fieldset {
border: 1px solid silver;
margin: 0 2px;
padding: .35em .625em .75em
}
legend {
border: 0;
padding: 0
}
button,
input,
select,
textarea {
font-family: inherit;
font-size: 100%;
margin: 0
}
button,
input {
line-height: normal
}
button,
select {
text-transform: none
}
button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
-webkit-appearance: button;
cursor: pointer
}
button[disabled],
html input[disabled] {
cursor: default
}
input[type="checkbox"],
input[type="radio"] {
box-sizing: border-box;
padding: 0
}
input[type="search"] {
-webkit-appearance: textfield;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
-webkit-appearance: none
}
button::-moz-focus-inner,
input::-moz-focus-inner {
border: 0;
padding: 0
}
textarea {
overflow: auto;
vertical-align: top
}
table {
border-collapse: collapse;
border-spacing: 0
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.wrap:before {
content: " ";
display: table;
}
.wrap:after {
clear: both;
content: " ";
display: table;
}
/*
Baseline Normalize and Float Clearing
-------------------------------------------------- */
<div class="container">
<div class="wrap">
<div class="left">
<p>This is an example of a sentence</p>
</div>
<div class="right">
<a class="button" href="#">Button</a>
</div>
</div>
</div>
静态:
给 .container 一个固定的高度,并给它的子元素一个 100% 的高度:
.container{
height:140px;
}
.wrap, .left, .right{
height:100%;
}
https://jsfiddle.net/b3y2pjtr/
动态:
忽略.container。在 .wrap、.left 和 .right 上使用 flexbox 属性:
.wrap{
display:flex;
width:100%;
}
.left, .right{
flex:1;
}