Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a text box inside a div width:100% leaving some margin in all sides

I need to put a text box inside a div that has 100% width leaving margin in all sides.
My problem is that I wasn't able to put right margin, cause the 100% width of the text box (that is necessary!).
So the textbox and the div container must have 100% width (cause responsive) and the textbox needs 8px margin in all sides.

.search-cont {
    background-color: #c8c7cc;
    width:100%;
    height:44px;
    overflow:hidden;
    transition: all 0.5s;
    z-index:997;
    position:fixed;
}

.search-text {
    border-radius:5px;
    border:0px;
    height:28px;
    padding:0px;
    padding-left:6px;
    width: 100% !important;
    margin: 8px !important;
    box-sizing:border-box;
    display:block;
    background-color:#fff;
}

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}
<div id="search-cont" class="search-cont">
  <div><input type="text" id="search-text" class="search-text" placeholder="Search" /></div>
</div>

There is the example --> DEMO

like image 458
Higo709 Avatar asked Nov 22 '25 06:11

Higo709


1 Answers

You can use padding on the parent tag instead of margin on the element.

You need to add box-sizing:border-box; to the parent (the best practice is to add this to any element using * selector).

* {
  box-sizing:border-box;  
}

body {
  margin:0;  
}

.search-cont {
  background-color: #c8c7cc;
  width:100%;
  height:44px;
  overflow:hidden;
  transition: all 0.5s;
  z-index:997;
  position:fixed;
  padding:8px;
}

.search-text {
    border-radius:5px;
    border:0px;
    height:28px;
    padding:0px;
    padding-left:6px;
    width: 100% !important;
    /*margin: 8px !important;*/
    box-sizing:border-box;
    display:block;
    background-color:#fff;
}

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}
<div id="search-cont" class="search-cont">
  <div><input type="text" id="search-text" class="search-text" placeholder="Search" /></div>
</div>
like image 96
Mosh Feu Avatar answered Nov 23 '25 20:11

Mosh Feu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!