简体   繁体   English

使用 div 在 CSS 元素周围创建空白

[英]Create white space around CSS elements with div

I want to put all of my elements in a white box.我想把我所有的元素放在一个白盒子里。 The code before is this:之前的代码是这样的:

<form action="/login" method="post">
            <div class='boxput'>
                <input autocomplete="off" autofocus name="username" placeholder="Username" type="text">
            </div>
            <div class='boxput'>
                <input style='position: relative; top: 30px;' name="password" placeholder="Password" type="password">
            </div>
            <button style='position: relative; top: 60px; height: 30px; width: 90px; font-size: 16px;' type="submit">Log In</button>
</form>
    
<div style='position: absolute; top: 28%; transform: translate(-50%, -50%); left: 50%;'>
            <h1 id='log'>Login:</h1>
</div>
    
<div style='position: absolute; display: inline-block; left: 50%; top: 69%; transform: translate(-50%, -50%);'>
            <a href='/signup'>Don't have an account? Sign up here.</a>
</div>

and I have just put a div around it like such:我刚刚在它周围放了一个div,如下所示:

<div>
        <form action="/login" method="post">
            <div class='boxput'>
                <input autocomplete="off" autofocus name="username" placeholder="Username" type="text">
            </div>
            <div class='boxput'>
                <input style='position: relative; top: 30px;' name="password" placeholder="Password" type="password">
            </div>
            <button style='position: relative; top: 60px; height: 30px; width: 90px; font-size: 16px;' type="submit">Log In</button>
        </form>
    
        <div style='position: absolute; top: 28%; transform: translate(-50%, -50%); left: 50%;'>
            <h1 id='log'>Login:</h1>
        </div>
    
        <div style='position: absolute; display: inline-block; left: 50%; top: 69%; transform: translate(-50%, -50%);'>
            <a href='/signup'>Don't have an account? Sign up here.</a>
        </div>
</div>

Upon further notice I found out that the new div I created was on the top left corner of the screen width a height of 0, which is not where I want it to be.经过进一步通知,我发现我创建的新 div 位于屏幕宽度的左上角,高度为 0,这不是我想要的位置。 How do I surround my elements with my new div?如何用我的新 div 包围我的元素?

Your <div> is working fine, the problem is using of position: absolute , add position: relative;您的<div>工作正常,问题是使用position: absolute ,添加position: relative; to your <div> to control your chlidren and change left and top children styles to fix your design.到您的<div>来控制您的孩子并更改左侧和顶部孩子 styles 以修复您的设计。 read this: https://css-tricks.com/absolute-positioning-inside-relative-positioning/阅读: https://css-tricks.com/absolute-positioning-inside-relative-positioning/

You have used relative and absolute positioning quite liberally in the code.您在代码中相当自由地使用了相对和绝对定位。 This can cause issues when you want to surround something with a div since the absolute positioned elements will jump out of the layout structure.当您想用 div 包围某些内容时,这可能会导致问题,因为绝对定位的元素会跳出布局结构。 Try using some margins to give distance between the elements:尝试使用一些边距来给出元素之间的距离:

<div style="background-color: white; position: relative; min-height: 16rem">
        <form action="/login" method="post" style="padding-left: 10px">
            <div class='boxput'>
                <input style=' margin-top: 30px;' autocomplete="off" autofocus name="username" placeholder="Username" type="text">
            </div>
            <div class='boxput'>
                <input style=' margin-top: 30px' name="password" placeholder="Password" type="password">
            </div>
            <button style='height: 30px; width: 90px; font-size: 16px; margin-top: 30px' type="submit">Log In</button>
        </form>
    
        <div style='position: absolute; top: 8rem; text-align: center; transform: translate(-50%, -50%); left: 50%;'>
            <h1 id='log'>Login:</h1>
            <a href='/signup'>Don't have an account? Sign up here.</a>
        </div>
    </div>

Here's a link to see how it looks这是一个链接,看看它的外观

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

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