简体   繁体   English

内联块:无法水平显示元素

[英]inline-block: cannot display elements horizontally

I am trying to display three tables as three separated elements, displayed horizontally. 我试图将三个表显示为三个单独的元素,水平显示。 This means that they should appear one alongside each other, from left to right. 这意味着它们应该从左到右并排出现。 I tried to use inline-block and set the margins, borders but it doesn't work: 我尝试使用inline-block并设置页边距,边框,但是它不起作用:

        <div style="border:2px solid black; width:485px;">
            <h1 align= "center" style="color:blue"> Interaction </h1>
            <p style="display:inline-block; width:180px; margin:10px; padding:20px;">
                <h3> Light </h3>
                <table border="1">
                    <tr> 
                        <td> Color </td>
                        <td> <input type="text" id="light-color" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Type </td>
                        <td> 
                            <select id="light-type">
                                <option value="point"> Point Light </option>
                                <option value="spot"> Spot Light </option>
                                <option value="ambient"> Ambient Light </option>
                                <option value="area"> Area Light </option>
                                <option value="directional"> Directional Light </option>
                            <select>
                        </td>
                    </tr>
                </table>
            </p>
            <p style="display:inline-block; width:180px; margin:10px; padding:20px;">
                <h3> Material </h3>
                <table border="1">
                    <tr>
                        <td> Diffuse </td>
                        <td> <input type="text" id="material-diffuse" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Ambient </td>
                    <td> <input type="text" id="material-ambient" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Emissive </td>
                        <td> <input type="text" id="material-emissive" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Specular </td>
                        <td> <input type="text" id="material-specular" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Shininess </td>
                        <td> <input type="text" id="material-shininess" value="0xffffff"> </input> </td>
                    </tr>
                    <tr>
                        <td> Type </td>
                        <td> 
                            <select id="material-type">
                                <option value="lambert"> Lambert </option>
                                <option value="normal"> Normal </option>
                                <option value="phong"> Phong </option>
                            </select>
                        </td>
                    </tr>
                </table>
            </p>
            <p style="display:inline-block; width:180px; margin:10px; padding:20px;">
                <h3> Object </h3>
                <table border="1">
                    <tr>
                        <td> Type </td>
                        <td>
                            <select>
                                <option value= "sphere"> Sphere </option>
                                <option value= "cube"> Cube </option>
                                <option value= "cylinder"> Cylinder </option>
                            </select>
                        </td>
                    </tr>
                </table>
            </p>
        </div>

The tables are still displayed vertically. 表格仍垂直显示。

My suggestion would be to change the containing p tags to divs and then float each one left and then clear them at the end. 我的建议是将包含p的标签更改为div,然后将每个浮动左,然后在末尾清除它们。 This will also be a more cross-browser solution. 这也将是一个跨浏览器的解决方案。

<div style="border:2px solid black; width:485px;">
        <h1 align= "center" style="color:blue"> Interaction </h1>
        <div style="float:left; display:inline-block; width:180px; margin:10px; padding:20px;">
            <h3> Light </h3>
            <table border="1">
                <tr> 
                    <td> Color </td>
                    <td> <input type="text" id="light-color" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Type </td>
                    <td> 
                        <select id="light-type">
                            <option value="point"> Point Light </option>
                            <option value="spot"> Spot Light </option>
                            <option value="ambient"> Ambient Light </option>
                            <option value="area"> Area Light </option>
                            <option value="directional"> Directional Light </option>
                        <select>
                    </td>
                </tr>
            </table>
        </div>
        <div style="float:left; display:inline-block; width:180px; margin:10px; padding:20px;">
            <h3> Material </h3>
            <table border="1">
                <tr>
                    <td> Diffuse </td>
                    <td> <input type="text" id="material-diffuse" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Ambient </td>
                <td> <input type="text" id="material-ambient" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Emissive </td>
                    <td> <input type="text" id="material-emissive" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Specular </td>
                    <td> <input type="text" id="material-specular" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Shininess </td>
                    <td> <input type="text" id="material-shininess" value="0xffffff"> </input> </td>
                </tr>
                <tr>
                    <td> Type </td>
                    <td> 
                        <select id="material-type">
                            <option value="lambert"> Lambert </option>
                            <option value="normal"> Normal </option>
                            <option value="phong"> Phong </option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
        <div style="float:left; display:inline-block; width:180px; margin:10px; padding:20px;">
            <h3> Object </h3>
            <table border="1">
                <tr>
                    <td> Type </td>
                    <td>
                        <select>
                            <option value= "sphere"> Sphere </option>
                            <option value= "cube"> Cube </option>
                            <option value= "cylinder"> Cylinder </option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
      <div style="clear:both;"></div>
    </div>
  1. Use float: left for first table. 使用float: left为第一张桌子。
  2. Separate the next two tables with a left margin. 用左页边距分隔接下来的两个表。 eg margin-left: 120px; 例如, margin-left: 120px;

It's a famous problem. 这是一个著名的问题。 For your future references. 供您将来参考。 http://alistapart.com/article/holygrail http://alistapart.com/article/holygrail

First, replace the paragraph tags with divs and then add the following class to each div; 首先,将段落标记替换为div,然后将以下类添加到每个div中;

.nextToEachOther {
 float:left;
 display:inline-block;
}

The issue that you're having is miss-understanding or the property inline-block . 您遇到的问题是误解或属性inline-block

If you simply use inline then the elements would float inline, defying any margin but still border would work. 如果仅使用inline则元素将以内联方式浮动,不带任何margin但仍然可以使用border

If you use inline-block (same as your case) the elements would get the margins too, so I guess that is the issue. 如果您使用inline-block (与您的情况相同),这些元素也会获得页边距,所以我想这就是问题所在。

What I would ask you to do is to either: 我要你做的是:

  1. Remove all the margin s and use inline-block . 删除所有的margin并使用inline-block This way the elements will be placed as a inline and block . 这样,元素将作为inlineblock放置。 block is a display property's value. blockdisplay属性的值。 You can learn about it here: http://www.w3schools.com/cssref/pr_class_display.asp or go to Mozilla Developer Network here: https://developer.mozilla.org/en-US/docs/Web/CSS/display 您可以在这里了解有关信息: http : //www.w3schools.com/cssref/pr_class_display.asp或在以下位置访问 Mozilla开发人员网络: https : //developer.mozilla.org/en-US/docs/Web/CSS/显示

  2. You can remove the -block and use it simply as inline . 您可以删除-block并将其简单地用作inline This way even if you are having margin s they won't be applied. 这样,即使您有margin ,也不会应用它们。 And they all would be in a line. 他们全都在一条线上。

One more thing: 还有一件事:

You should always note the width of the elements, if any element gets a width more then the width of viewport, it will be placed under the other elements. 您应该始终注意元素的宽度,如果任何元素的宽度大于视口的宽度,它将被放置在其他元素的下面。 So try using a width in percentage such as width: 10% this way browser will automatically shrink the element. 因此,请尝试使用百分比形式的宽度,例如width: 10%这样浏览器将自动缩小元素。

There is a specialty in the <p> element of HTML which allows it to be used without a closing tag. HTML的<p>元素中有一项特殊功能,它可以在没有结束标记的情况下使用它。 The next display:block tag (here: the <h3>) will auto-close the <p>, your closing </p> will be ignored. 下一个display:block标记(此处为<h3>)将自动关闭<p>,您的关闭</ p>将被忽略。

Simplest solution is replacing <p> with <div> 最简单的解决方案是将<p>替换为<div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM