如何强制父div按子div宽度/填充/边距/框扩展? [英] How to force parent div to expand by child div width/padding/margin/box?

查看:121
本文介绍了如何强制父div按子div宽度/填充/边距/框扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用子div扩展div父级,但是我不知道这是否可行以及如何实现.

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

我准备了小提琴,并包含了一些代码.

I prepare a fiddle and included some code.

CSS

body {
    background: gray;
}
div.page {
    color: white;
    background: black;
    max-width: 72em;
    margin: auto;
    padding: 1em;
}
div.one {
    background-color: red;
    width: 40%;
}
div.two {
    background-color: green;
    width: 120%;
}

HTML

<body>
    <div class="page">
        <div class="one">One</div>
        <div class="two">Two</div>
    </div>
</body>

推荐答案

解决您问题的关键方法是使用display:inline-block;

HTML

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

HTML

<div class="page">
    <div class="one">One</div>
    <div class="two">Two</div>
</div>

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;
}

这是工作中的小提琴

这篇关于如何强制父div按子div宽度/填充/边距/框扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆