简体   繁体   English

设置显示:内联2格不起作用

[英]Setting display: inline for 2 divs doesn't work

I trying to make a div display beside another div using "display: inline". 我试图使用“ display:inline”在另一个div旁边显示一个div。 However, I can't seem to make it work. 但是,我似乎无法使其工作。 I have a parent div (parent-and-guardian) with 2 divs inside (student-parent-container and student-guardian-other-container). 我有一个父母div(父母和监护人),里面有2个div(学生-父母-容器和学生监护人-其他容器)。 I am trying for this 2 divs to be displayed side by side. 我正在尝试将这2个div并排显示。

<div id="parent-and-guardian" class="tab">
    <div id="student-parent-container">
        <fieldset id="student-father-info">
            <legend class="form-legend">Father Information</legend>
            <div>
                <label class="form-label">Name:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
            <div>
                <label class="form-label">Contact No.:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
        </fieldset>
        <fieldset id="student-mother-info">
            <legend class="form-legend">Mother Information</legend>
            <div>
                <label class="form-label">Name:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
            <div>
                <label class="form-label">Contact No.:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
        </fieldset>
    </div>
    <div id="student-guardian-other-container">
        <fieldset id="student-guardian-info">
            <legend class="form-legend">Guardian Information</legend>
            <div>
                <label class="form-label">Name:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
            <div>
                <label class="form-label">Relation:</label>
                <input type="text" class="form-input form-input-long"></input>
            </div>
        </fieldset>
    </div>
</div>

Here is the CSS: 这是CSS:

#student-parent-container {
    display: inline;
}

#student-guardian-other-container {
    display: inline;
}

I'd like for the Guardian Information to be on the right side of Father Information and Mother Information. 我希望监护人信息位于父亲信息和母亲信息的右侧。

Here's a link to jsFiddle: Test 这是jsFiddle的链接: 测试

For native-block elements it's better to apply display: inline-block; 对于本机块元素,最好应用display: inline-block; than element will still take block element display effect (like setting height for inline elements does nothing, while block elements height does change). 元素仍将显示块元素显示效果(例如,内联元素的设置高度什么都不做,而块元素的高度却发生变化)。 Also your blocks may lead to different top position on page. 同样,您的方块可能会导致页面上的其他顶部位置不同。 That's why I added vertical-align: top (for inline and inline-block elements only): 这就是为什么我添加了vertical-align: top (仅适用于内联和内联块元素):

 #student-parent-container { display: inline-block; vertical-align: top; } #student-guardian-other-container { display: inline-block; vertical-align: top; } 
 <div id="parent-and-guardian" class="tab"> <div id="student-parent-container"> <fieldset id="student-father-info"> <legend class="form-legend">Father Information</legend> <div> <label class="form-label">Name:</label> <input type="text" class="form-input form-input-long"></input> </div> <div> <label class="form-label">Contact No.:</label> <input type="text" class="form-input form-input-long"></input> </div> </fieldset> <fieldset id="student-mother-info"> <legend class="form-legend">Mother Information</legend> <div> <label class="form-label">Name:</label> <input type="text" class="form-input form-input-long"></input> </div> <div> <label class="form-label">Contact No.:</label> <input type="text" class="form-input form-input-long"></input> </div> </fieldset> </div> <div id="student-guardian-other-container"> <fieldset id="student-guardian-info"> <legend class="form-legend">Guardian Information</legend> <div> <label class="form-label">Name:</label> <input type="text" class="form-input form-input-long"></input> </div> <div> <label class="form-label">Relation:</label> <input type="text" class="form-input form-input-long"></input> </div> </fieldset> </div> </div> 

#parent-and-guardian {
    display: flex;
}

it will cause it to display side by side. 它将使其并排显示。 Check out https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties 查看https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties

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

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