我有一个动态输入控件,其宽度根据业务条件而变化。输入控件在表中。输入控件中有一个日历图标(glyphicon),其边距也应根据条件而更改。它还应该支持响应性。现在,当输入控件的宽度改变时,日历的位置也会改变,并且它不会停留在一个位置。有时它在输入控件的中间,有时在它的外部。基本上,不管输入控件的宽度如何,日历都必须在一个专用的位置。
现在的代码是这样的,它在1900x1200和1366x768上运行良好。但它不适用于其他分辨率和某些设备。在某些情况下,媒体查询似乎会停止。有没有一种方法可以在不使用媒体查询的情况下,将日历位置固定在输入控件内的最右边。Jquery/CSS解决方案可以,只要我不必为每个解决方案修复日历图标的margin-right属性。
这里是Demo
下面是代码片段

input.c-text-field[type=text], input[type=email] {
    display: block;
    width: 276px;
    height: 36px;
    margin-top: 20px;
    padding: 7px 10px;
    border: 1px solid rgba(0,0,0,.6);
    outline: 0;
    background: #FFF;
}
.glyphicon {
    float: right;
    width: 36px;
    margin-right: 5px;
    margin-bottom: -36px;
    color: #0379d3!important;
}
.glyphicon-calendar {
    pointer-events: none;
    margin-top:20px;
}
.glyphicon-calendar:after {
    font-family: MWF-MDL2;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    content: "\E787";
    display: block;
    font-size: 26px;
    height: 36px;
    line-height: 36px;
    text-align: right;
}
.c-checkbox label.c-label, input.c-text-field[type=text], table .c-checkbox label.c-label {
    margin-top: 0;
}

<link rel="stylesheet" href="//assets.onestore.ms/cdnfiles/external/mwf/long/v1/v1.15.0/css/mwf-west-european-default.min.css">
<table>
<tbody>
  <tr>
    <td>
    <!-- I do not want to put margin-right hardcoded like this. It should come dynamically or fix with other CSS properties -->
      <i class="glyphicon glyphicon-calendar" style="margin-right:15%;"></i>
<input value="04/29/2018 12:00 AM" class="sfr-ip-start-date c-text-field Control x-hidden-focus" id="StartDate1" type="text" style="cursor: pointer;width:250px;font-size:13px">
    </td>
    <td>
    <!-- I do not want to put margin-right hardcoded like this. It should come dynamically or fix with other CSS properties -->
      <i class="glyphicon glyphicon-calendar" style="margin-right:20%;"></i>
<input value="04/29/2018 12:00 AM" class="sfr-ip-start-date c-text-field Control" id="StartDate2" type="text" style="cursor: pointer;width:170px;font-size:13px">
    </td>
  </tr>
</tbody>
</table>

任何帮助都将不胜感激。谢谢!

最佳答案

通过将position: relative属性添加到td中(它是.glyphicon的父级),并将图标定位在absoulte属性中,因此.glyphicon被限制在td中。我们不再需要margin-right了,所以我把它们都删除了。设置right属性以确定.glyphicontd右侧的距离jsfiddle

td {
  position: relative;
}

input.c-text-field[type=text],
input[type=email] {
  display: block;
  width: 276px;
  height: 36px;
  margin-top: 20px;
  padding: 7px 10px;
  border: 1px solid rgba(0, 0, 0, .6);
  outline: 0;
  background: #FFF;
}

.glyphicon {
  position: absolute;
  right: 5px;
  width: 36px;
  color: #0379d3!important;
}

.glyphicon-calendar {
  pointer-events: none;
  margin-top: 20px;
}

.glyphicon-calendar:after {
  font-family: MWF-MDL2;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  content: "\E787";
  display: block;
  font-size: 26px;
  height: 36px;
  line-height: 36px;
  text-align: right;
}

.c-checkbox label.c-label,
input.c-text-field[type=text],
table .c-checkbox label.c-label {
  margin-top: 0;
}

<link rel="stylesheet" href="//assets.onestore.ms/cdnfiles/external/mwf/long/v1/v1.15.0/css/mwf-west-european-default.min.css">
<table>
  <tbody>
    <tr>
      <td>
        <!-- I do not want to put margin-right hardcoded like this. It should come dynamically or fix with other CSS properties -->
        <i class="glyphicon glyphicon-calendar"></i>
        <input value="04/29/2018 12:00 AM" class="sfr-ip-start-date c-text-field Control x-hidden-focus" id="StartDate1" type="text" style="cursor: pointer;width:250px;font-size:13px">
      </td>
      <td>
        <!-- I do not want to put margin-right hardcoded like this. It should come dynamically or fix with other CSS properties -->
        <i class="glyphicon glyphicon-calendar"></i>
        <input value="04/29/2018 12:00 AM" class="sfr-ip-start-date c-text-field Control" id="StartDate2" type="text" style="cursor: pointer;width:170px;font-size:13px">
      </td>
    </tr>
  </tbody>
</table>

10-05 20:41