在 header 中居中搜索
Centering search from in header
在我的 header 中,左上角有一个不应移动的徽标,还有一个我想水平居中的搜索输入表单(即使浏览器调整大小),但我发现很难这样做("Go" 按钮也应该在它旁边。
我尝试过自动保证金,但都无法正常工作。我已经包含了代码和一个 jsfiddle。
谢谢!
html:
<header>
<h1 id="logo">Test Logo</h1>
<form method="get" id="search-form">
<input type="text" id="search" placeholder="Search Here" autocomplete="off" />
<ul id="suggestions"></ul>
<input type="submit" value="Go" />
</form>
</header>
css:
body {
font: 14px/1.5 "Open Sans", "PT Sans", "Source Sans Pro",
"Helvetica Neue", Helvetica, Arial, Sans-Serif;
color: #333;
height: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
}
header {
position: relative;
height: 50px;
background-color: black;
padding-top: 10px;
}
#logo {
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
margin-left:20px;
text-align:left;
color:#ECF0F1;
display:inline
}
#search {
width: 350px;
padding: 4px 8px;
outline: none;
display:inline;
}
#search-form input[type='submit'] {
display:inline;
padding: 5px 8px;
border: 1px solid #aaa;
background-color: #eee;
background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#ddd));
background-image: -webkit-linear-gradient(top, #eee, #ddd);
background-image: -moz-linear-gradient(top, #eee, #ddd);
background-image: -o-linear-gradient(top, #eee, #ddd);
background-image: linear-gradient(to bottom, #eee, #ddd);
}
#search-form {
position: relative;
margin-bottom: 20px;
display:inline;
}
见fiddle
已更改 CSS
header {
position: relative;
height: 50px;
background-color: black;
padding-top: 10px;
display: inline-block;
width: 100%;
text-align: center;
}
#logo {
float: left;
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
margin-left:20px;
text-align:left;
color:#ECF0F1;
display:inline
}
#search-form {
text-align: center;
position: relative;
margin-bottom: 20px;
display:inline;
}
我所做的是,我将您的 header
的显示设置为 inline-block
,并将 text-align:center
用于 header
。然后,为了使 logo
始终保持在左侧,我使用了 float:left
。而且,为了使搜索表单居中,我将其 display
设置为 inline-block
。
在我的 header 中,左上角有一个不应移动的徽标,还有一个我想水平居中的搜索输入表单(即使浏览器调整大小),但我发现很难这样做("Go" 按钮也应该在它旁边。
我尝试过自动保证金,但都无法正常工作。我已经包含了代码和一个 jsfiddle。
谢谢!
html:
<header>
<h1 id="logo">Test Logo</h1>
<form method="get" id="search-form">
<input type="text" id="search" placeholder="Search Here" autocomplete="off" />
<ul id="suggestions"></ul>
<input type="submit" value="Go" />
</form>
</header>
css:
body {
font: 14px/1.5 "Open Sans", "PT Sans", "Source Sans Pro",
"Helvetica Neue", Helvetica, Arial, Sans-Serif;
color: #333;
height: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
}
header {
position: relative;
height: 50px;
background-color: black;
padding-top: 10px;
}
#logo {
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
margin-left:20px;
text-align:left;
color:#ECF0F1;
display:inline
}
#search {
width: 350px;
padding: 4px 8px;
outline: none;
display:inline;
}
#search-form input[type='submit'] {
display:inline;
padding: 5px 8px;
border: 1px solid #aaa;
background-color: #eee;
background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#ddd));
background-image: -webkit-linear-gradient(top, #eee, #ddd);
background-image: -moz-linear-gradient(top, #eee, #ddd);
background-image: -o-linear-gradient(top, #eee, #ddd);
background-image: linear-gradient(to bottom, #eee, #ddd);
}
#search-form {
position: relative;
margin-bottom: 20px;
display:inline;
}
见fiddle
已更改 CSS
header {
position: relative;
height: 50px;
background-color: black;
padding-top: 10px;
display: inline-block;
width: 100%;
text-align: center;
}
#logo {
float: left;
margin-bottom: 10px;
font-weight: bold;
margin-top: 0px;
margin-left:20px;
text-align:left;
color:#ECF0F1;
display:inline
}
#search-form {
text-align: center;
position: relative;
margin-bottom: 20px;
display:inline;
}
我所做的是,我将您的 header
的显示设置为 inline-block
,并将 text-align:center
用于 header
。然后,为了使 logo
始终保持在左侧,我使用了 float:left
。而且,为了使搜索表单居中,我将其 display
设置为 inline-block
。