如何动态地将兄弟div垂直对齐父高度

时间:2017-03-12 20:39:59

标签: html css html5 css3

我知道可以解决类似问题的许多解决方案,我尝试过但直到现在都没有用。

我想动态地将以下代码段中的元素垂直对齐到其父高度,但无法找到工作方法。有什么建议吗?

.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:"\201C" "\201D" "\2018" "\2019"}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>

2 个答案:

答案 0 :(得分:2)

CSS表

您可以使用display: tabledisplay: 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;
}

&#13;
&#13;
.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: "\201C" "\201D" "\2018" "\2019"
}

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
-------------------------------------------------- */
&#13;
<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>
&#13;
&#13;
&#13;

Flexbox的

如果您不介意使用CSS flexbox(添加供应商前缀后最多可获得97%的浏览器支持),只需移除浮动并使用display: flexalign-items: center .wrap选择器也可以正常工作:

.wrap {
  margin: 0 auto;
  max-width: 1140px;
  display: flex;        /* Added */
  align-items: center;  /* Added */
}

&#13;
&#13;
.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: "\201C" "\201D" "\2018" "\2019"
}

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
-------------------------------------------------- */
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<强>静

给.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;
}

https://jsfiddle.net/b3y2pjtr/1/