div with unspecified width (absolute positioning)

前端 未结 7 1521
我寻月下人不归
我寻月下人不归 2020-12-14 15:42

I\'ve got an absolutely positioned div I\'m working with, and I put some text inside of it just to be able to find it on the page, namely:

7条回答
  •  再見小時候
    2020-12-14 16:35

    I solved this by adding a wrapper div that is also absolutely positioned, with the width I want my div to expand up to.

    E.g.,

    And css,

    .wrapper {
        position: absolute;
        width: 500px;
    }
    
    .my-div {
        position: absolute;
        whitespace: pre-wrap;
    }
    

    You can add whitespace: pre-wrap to allow wrapping lines, which makes this more flexible than the nowrap solution.

提交回复
热议问题