Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

javascript - Safari includes border for height input?

Why i have this difference in height on my input when i check the website on Safari? I can see that Safari includes the border line also in the heigth, i'm not sure if that's the problem or how to fix that.

Input on Chrome Input on Safari

<div className="input_city">
    <input
       id="input_basic"
       placeholder="Search city"
       onChange={handleInput}
       value={searcher}
     />
     <button className="button_search" onClick={handleSubmit}>
      Search
     </button>
</div>
.input_city {
  display: table;
  align-items: center;
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 4;
  grid-row-end: 6;
  margin-bottom: -10px;
  text-align: end;
}

#input_basic {
  padding: 8.5px 5px;
  font-size: 1rem;
  border-radius: 3px 0px 0px 3px;
  border: 1px solid #ccc;
  border-right: none;
  vertical-align: bottom;
}

#input_basic:focus {
  outline: none;
}


.button_search {
  border-radius: 0px 3px 3px 0px;
  padding: 10px 5px;
  border: 1px solid #ccc;
  width: 5rem;
  background-color: #ec6e4c;
  color: #fff;
  border-left: none;
  cursor: pointer;
}

.button_search:focus {
  outline: none;
}

.button_search:hover {
  background: #ec6f4cc7;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of problems with trying to align the input and search button elements.

Safari puts a small shadow at the top under the border which gets it out of alignment with the search button border. Also, for any browser the heights of the two elements are not necessarily exactly the same.

To get round these two problems we first get the browser to remove as much of its own formatting as we can and then we use rems as the base unit, setting a specific height on the two elements so we don't have to rely on how the browser treats the font for setting the elements' heights.

Here's the code.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
input[type=text] {   
    /* remove browser formatting */
    /* note that Safari uses attribute type so we've added it in the HTML */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;

    outline: none;
    }
    
.input_city {
  display: table;
  align-items: center;
  grid-column-start: 2;
  grid-column-end: 3;
  grid-row-start: 4;
  grid-row-end: 6;
  margin-bottom: -10px;
  text-align: end;
}

#input_basic {
  font-size: 1rem;
  border-radius: 3px 0px 0px 3px;
  border: 1px solid #ccc;
  border-right: none;
  vertical-align: bottom;
  
  /*added*/
  line-height: 1.2rem;
  padding: 0.25rem;
  height: 2rem;
}

#input_basic:focus {
  outline: none;
}

.button_search {
  border-radius: 0px 3px 3px 0px;
  border: 1px solid #ccc;
  width: 5rem;
  background-color: #ec6e4c;
  color: #fff;
  border-left: none;
  cursor: pointer;
  
    /*added*/
  line-height: 1.2rem;
  padding: 0.25rem;
  height: 2rem;
}

.button_search:focus {
  outline: none;
}

.button_search:hover {
  background: #ec6f4cc7;
}
<div class="input_city">
    <input type="text" id="input_basic" placeholder="Search city" onChange="" value=''/>
     <button class="button_search" onClick="">
      Search
     </button>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...