user7445644
user7445644

Reputation:

Position search bar at the right side of div and centered

I want to position a search bar inside a div so it looks something like this:

At the moment it doesn't even sit within the div.

enter image description here

HTML:

.sb-search {
  float: right;
  overflow: hidden;
}

.sb-search-input {
  border: none;
  outline: none;
  background: #fff;
  width: 100%;
  height: 60px;
  margin: 0;
  z-index: 10;
  padding: 20px 65px 20px 20px;
  font-family: inherit;
  font-size: 20px;
  color: #2c3e50;
}
<div class="searchbar" style="position: relative;">
  <h1>Welcome Solictior...</h1>
  <div id="sb-search" class="sb-search">
    <form>
      <input class="sb-search-input" placeholder="Search..." type="text" value="" name="search" id="search" />
      <input class="sb-search-submit" type="submit" value="" />
      <span class="sb-icon-search"></span>
    </form>
  </div>
</div>

Upvotes: 0

Views: 5967

Answers (3)

Greg
Greg

Reputation: 1003

It's very easy to do with Flexbox

.searchbar {
  background-color: #8BCAC7;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

https://jsfiddle.net/cb3fu9wj/

justify-content specifies the alignment along the main axis. It defines how the free space is distributed between elements.

align-items specifies the alignment along the cross axis.

Here's the complete guide.

Upvotes: 1

Nutshell
Nutshell

Reputation: 8537

Here's a possible example of what you're looking for : See this fiddle

CSS :

.searchbar { background-color: teal;  color: #fff; overflow: hidden; padding: 0 10px;}
.searchbar .sb-search { float: right; margin: 5px 0; }
.searchbar h1 { float: left; font-size: 14px; margin: 10px 0 5px; }

Upvotes: 1

user7236046
user7236046

Reputation:

I think your problem here is the h1 as it's a block level element.

Add this to your h1 CSS and it should let the search bar go to the right of it.

h1 {
    display: inline-block;
}

Keep the float: right; on your .sb-search element too.

Upvotes: 2

Related Questions