私は、左右の浮動要素をナビゲーションバーに垂直に整列しようとしています。 ( '折りたたまれた' Nav BarがClearfix Hackを適用していた場所)。ナビゲーションバーの高さは、左浮動タイトル(デフォルトでh2、2em)によって決まります。しかし、右に浮かんだ要素(入力とボタンのあるフォーム)は、アイテムを垂直方向にセンタリングする変換アプローチを試していても、中央に座っていませんか? トランスフォームの垂直方向のアラインメントを解除すると、フォームがさらに上に移動します(下方向ではなく中央に配置されます)。垂直整列 - 浮動小数点型の要素navbar
CodePen(https://codepen.io/yunti/pen/xdvpQK)
https://codepen.io/yunti/pen/xdvpQK
.header {
background-color: darkorange;
}
.header-title {
float: left;
padding-left: 10px;
color: white;
font-weight: 400;
}
.form-header {
float: right;
padding-right: 10px;
/*position: relative;*/
/*top: 50%;*/
/*transform: translateY(-50%);*/
}
.clearfix:after {
content: "";
clear: both;
display: table;
}
input,
button {
vertical-align: middle;
}
.form-control {
margin: 10px;
height: 34px;
width: 180px;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 18px;
color: #555;
}
.form-control::placeholder {
color: grey;
}
.btn {
margin-left: 10px;
height: 34px;
padding-left: 12px;
padding-right: 12px;
line-height: 1.42857143;
white-space: nowrap;
border: 1px solid transparent;
border-radius: 4px;
background-color: forestgreen;
font-size: 14px;
font-weight: 400;
color: white;
cursor: pointer;
}
<div class="header clearfix">
<h1 class="header-title">Weather App</h1>
<div class="form-header">
<form class="form-group">
<input class="form-control" type="text" placeholder="St. George, Utah" />
<button class="btn">Get Weather</button>
</form>
</div>
</div>
答えのためのThansk(私はie9をサポートする必要があるので、フレックスボックスはできませんが、他の答えに感謝します)。 tranformのアプローチがうまくいかない理由は何ですか? (他のSOの答えは – Yunti
ですhttps://developer.mozilla.org/en-US/docs/Web/CSS/position?v=control '相対配置された要素の場合、topまたはbottomプロパティは法線からの垂直オフセットを指定します'%'を使用しているときは、 'fixed'や' absolute'や 'sticky'の位置だけに使われます。相対位置要素の%は何もしません。代わりに '50%'の代わりに '50px'を使用すると動作します。 –
相対的な質問のアプローチではありませんか? – Yunti