-1

Below, the left: 50% in the css does not move the input element in any way. Anyone know why? Is this just not possible? Do I have anything blocking it? I don't want to use margins because it will look different on different computers. If left: is not possible, how can this be centered either way?

(I put the left: 50% at the bottom of css for the searchbar)

   #searchbar{
         
  border-radius: 4px;
  background-color: transparent;
  cursor: pointer;
         
  color: transparent;
  background: #FFF;
  padding: 1px 1px 1px 5px;

width: 30px;
  height: 30px;
  outline: none;
  border: 1px solid #CCCCCC;
  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  transition: all .5s;
  text-align: center;
         background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
         background-size: contain;
         border:none;
right: 50% 
    
     }
     
 <input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">
Michael McQuade
  • 3,234
  • 1
  • 19
  • 31

2 Answers2

1

Try,

 <input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">

CSS

    #searchbar{
    position:absolute;
    left:50%;
    border-radius: 4px;
    background-color: transparent;
    cursor: pointer;
    color: transparent;
    background: #FFF;
    padding: 1px 1px 1px 5px;
    width: 30px;
    height: 30px;
    outline: none;
    border: 1px solid #CCCCCC;
    -webkit-transition: all .5s;
    -moz-transition: all .5s;
    transition: all .5s;
    text-align: center;
    background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
    background-size: contain;
    border:none;
    }

OR

    <div id="searchbardiv">
    <input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">
    </div>

CSS

 #searchbardiv{
 width:100%;
 display:flex;
 position relative
 } 

 #searchbar{
 position:absolute;
 left:50%;
 border-radius: 4px;
 background-color: transparent;
 cursor: pointer;
 color: transparent;
 background: #FFF;
 padding: 1px 1px 1px 5px;
 width: 30px;
 height: 30px;
 outline: none;
 border: 1px solid #CCCCCC;
 -webkit-transition: all .5s;
 -moz-transition: all .5s;
 transition: all .5s;
 text-align: center;
 background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
 background-size: contain;
 border:none;
 }
Shasha
  • 971
  • 5
  • 19
0

You can use a flex box to center content.

.flex {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

#searchbar {
  border-radius: 4px;
  background-color: transparent;
  cursor: pointer;
  color: transparent;
  background: #FFF;
  padding: 1px 1px 1px 5px;
  width: 30px;
  height: 30px;
  outline: none;
  border: 1px solid #CCCCCC;
  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  transition: all .5s;
  text-align: center;
  background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
  background-size: contain;
  border: none;
}
<div class="flex">
  <input id="searchbar" name="search" size="1" autocomplete="off" maxlength="27">
</div>
Michael McQuade
  • 3,234
  • 1
  • 19
  • 31