Different height of select with HTML5 and Bootstra

2019-08-27 05:59发布

问题:

I have a HTML select element that uses Bootstrap Select. If I use HTML5, the button element, that is generated by Boostrap Select to render the select, has a correct height of 24.

If I do not use HTML5, it then has a height of 8.875 on Blink (Opera 56.0) and of 8.8833 on Gecko (Firefox 60.3,) resulting in a broken display.

By using HTML5, I mean having (or not) the DOCTYPE <!doctype html> as the first line (no server config as I tested from files on the FS).

Any idea of what precisely is causing this behaviour?

Here is a complete repro (just save it as a file, and either keep or remove the first line):

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bootstrap-select.min.css">
    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
    <div class="container">
      <div class="form-group row">
        <label for="foobar" class="col-sm-2 col-form-label">Sauce</label>
        <div class="col-sm-10">
          <select class="form-control selectpicker">
            <option>Mustard</option>
            <option>Ketchup</option>
            <option>Barbecue</option>
          </select>
        </div>
      </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/js/bootstrap-select.min.js"></script>
  </body>
</html>

回答1:

If you are not specifying a correct doctype, the browser switches to quirks mode. It is a fallback solution for older sites whose stylesheets were written before CSS was specified in the current way. MDN provides a "rough" list of differences in layouting in firefox, but there are subtle differences between browsers.

I didn't find a specific rule causing the bug you found, but it has something to do with the percentage height calculation with absolutely positioned children. If you remove position: absolute from .bootstrap-select .dropdown-toggle .filter-option, the height gets calculated correctly (even though you would have to adjust the placement of the carret afterwards).