具有百分位边距的HTML流体布局

时间:2013-05-15 16:15:59

标签: html5 margins margin

如何将黄色框布局放在一行中。

Html(已更新 - 包含数字的小方框必须为内嵌):

<table style='table-layout: fixed; border-collapse: collapse; width: 100%;'>
            <tr>
                <td style='width: 34%; border: 1px solid #ccc; display: inline-block;'>

                    <!--commented out doesn't work-->
                    <!--div style="margin-right: 55%; margin-left: -55%; background-color: yellow;">One</!--div-->

                    <div style="margin-right: 50%; margin-left: -50%; background-color: yellow;">
                        <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">1</div>
                    </div>
                    <div style="margin-right: -50%; margin-left: 50%; background-color: yellow;">
                        <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">2</div>
                    </div>
                </td>
                <td style='width: 33%; border: 1px solid #ccc;'>
                    <div style="margin-right: -50%; margin-left: 50%; background-color: yellow;">
                        <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">3</div>
                    </div>
                </td>
                <td style='width: 33%; border: 1px solid #ccc;'>
                    <div style="margin-right: -50%; margin-left: 50%; background-color: yellow;">
                        <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">4</div>
                    </div>
                </td>
            </tr>
        </table>

1 个答案:

答案 0 :(得分:1)

这适用于这个具体的例子。 http://jsfiddle.net/bdJUL

<table style='table-layout: fixed; border-collapse: collapse; width: 100%;'>
        <tr>
            <td style='width: 34%; border: 1px solid #ccc; position: relative;'>

                <!--commented out doesn't work-->
                <!--div style="margin-right: 55%; margin-left: -55%; background-color: yellow;">One</!--div-->

                <div style="margin-right: 50%; margin-left: -50%; background-color: yellow;">
                    <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">1</div>
                </div>
                <div style="margin-right: -50%; margin-left: 50%; background-color: yellow; position: absolute; top: 1px; width: 100%;">
                    <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">2</div>
                </div>
            </td>
            <td style='width: 33%; border: 1px solid #ccc;'>
                <div style="margin-right: -50%; margin-left: 50%; background-color: yellow;">
                    <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">3</div>
                </div>
            </td>
            <td style='width: 33%; border: 1px solid #ccc;'>
                <div style="margin-right: -50%; margin-left: 50%; background-color: yellow;">
                    <div style="border: 1px solid #ccc; width: 20px; margin-left: auto; margin-right: auto; text-align: center;">4</div>
                </div>
            </td>
        </tr>
    </table>