How to force parent div to expand by child div width/padding/margin/box?

后端 未结 3 1113
生来不讨喜
生来不讨喜 2020-12-28 14:52

I want to expand div parent with child div but I don\'t know if that\'s possible and how to do it.

\"enter

3条回答
  •  长发绾君心
    2020-12-28 15:08

    The key solution to your problem is to use display:inline-block;

    HTML

    One
    Two

    CSS

    body {
        background: gray;
    }
    div.page {
        color: white;
        background: black;
        margin: auto;
        padding: 1em;
        display:inline-block;
    }
    div.one {
        background-color: red;
        width: 10em;
        display:inline-block;
    }
    div.two {
        background-color: green;
        width: 40em;
        display:inline-block;
    }
    

    Here is the working fiddle

提交回复
热议问题