using position:absolute to set an inputs width

后端 未结 4 425
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 12:21

A simple yet annoying one - I am tryign to set an input[type=text] width by using absolute positioning (ie right:10px;left:10px) yet I cant get it to play ball.

Does

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 12:53

    Actually, what you're doing works fine. You just need to remember to set the parent element's position as well.

    Then CSS it:

    div {
        width: 400px;
        position: relative;
    }
    input {
        position: absolute;
        left: 5px;
        right: 5px;
    }
    

    This would result in the input box being 390px.

    If you set the div to be flexible, then the input box would be too.

    edit: seems to only work in Chrome, so you'd need to put the input inside another element. This works in FF and IE too:

    with this CSS:

    #parent {
        position: relative;
        width: 400px;
    }
      #parent div {
          position: absolute;
          left: 5px;
          right: 5px;
      }
        #parent div input {
            width: 100%;
        }
    

    This has the expected effect. A bit hack-ish, maybe...

提交回复
热议问题