Centering search from in header

1.1k views Asked by At

In my header, I have a logo top left that shouldn't move, and a search input form that I want to center horizontally (even when the browser resizes) but am finding hard to do so (the "Go" button should be next to it as well.

I attempted to auto margin and all but could not get this to work. I have included the code and a jsfiddle.

Thank you!

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

http://jsfiddle.net/31zoco4c/1/

1

There are 1 answers

3
Lal On BEST ANSWER

See the fiddle

Changed 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;
}

What I have done is, I've set the display of your header to inline-block and used text-align:center for the header. Then, to make the logo stick to the left always, I have used float:left. And, to make the searchform centered, I set its display to inline-block.