【问题标题】:How to space between username and password forms如何在用户名和密码表格之间留出空格
【发布时间】:2017-02-23 17:48:07
【问题描述】:

我正在为我的实习创建一个网页,我需要一个登录名和密码表单。我像这样为文本框设置动画:

input[type=text] {
    z-index: 10;
    width: 30px;
    position: absolute;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    height: 30px;
    display: block;
}
input[type=text]:focus {
    width: 130px;
}
<input type="text" name="username" placeholder="User.." size="30" />

然后我将表格插入到一个 div 中并对密码做了同样的事情

#login {
    position: absolute;
    font-family: "Times New Roman", Times, serif;
    font-weight: bold;
    text-align: left;
    right:350px;
    bottom:5px;
    height: 200px;
    width:200px;
}


input[type=text] {
    z-index: 10;
    width: 30px;
    position: absolute;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    height: 30px;
    display: block;
}
input[type=text]:focus {
    width: 130px;
}



input[type=password] {
    z-index: 10;
    width: 30px;
    position: absolute;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-image: url('searchicon.png');
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    display: block;
    height: 30px;
}
input[type=password]:focus {
    width: 130px;
}
<div id="login">
            
         <form name="frmLogin" method="post" action="">
            <br><br>
            <table width="100" align="center">
                
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" placeholder="User.." size="30" /></td>
                </tr>
                
                
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" placeholder="Pass.." size="30" /></td>
                </tr>
                
                
            </table>
            <input type="hidden" name="acao" value="login" />
        </form>

所以现在您可以在 sn-p 中看到我的问题,登录和密码表单相互重叠,我希望它们之间有一点空间。

【问题讨论】:

  • 为什么需要使用position:absolute
  • 添加边距:10px;给你密码字段 css 应该做的!
  • 您可以在 css 的末尾添加表格 {border-spacing: 10px},这样可以在它们之间留出一些空间

标签: html css


【解决方案1】:

#login {
    /*position: absolute;*/
    font-family: "Times New Roman", Times, serif;
    font-weight: bold;
    text-align: left;
    right:350px;
    bottom:5px;
    height: 200px;
    width:200px;
}


input[type=text] {
    z-index: 10;
    width: 30px;
    /*position: absolute;*/
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    height: 30px;
    display: block;
}
input[type=text]:focus {
    width: 130px;
}



input[type=password] {
    z-index: 10;
    width: 30px;
    /*position: absolute;*/
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-image: url('searchicon.png');
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    display: block;
    height: 30px;
}
input[type=password]:focus {
    width: 130px;
}
<div id="login">
            
         <form name="frmLogin" method="post" action="">
            <br><br>
            <table width="100" align="center">
                
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" placeholder="User.." size="30" /></td>
                </tr>
                
                
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" placeholder="Pass.." size="30" /></td>
                </tr>
                
                
            </table>
            <input type="hidden" name="acao" value="login" />
        </form>

这里的问题是position:absolute。根据您的使用情况,您也可以使其与绝对位置一起使用,但您必须声明高度。目前,我刚刚删除了该职位。希望这会有所帮助。

【讨论】:

  • 非常感谢答案,但我需要绝对位置,因为 div 位于另一个 div 的顶部,即相对定位
  • 然后尝试将高度添加到父 td。
  • 怎么样?在表格中?
【解决方案2】:

从两个输入 css 中删除绝对位置

#login {
    position: absolute;
    font-family: "Times New Roman", Times, serif;
    font-weight: bold;
    text-align: left;
    right:350px;
    bottom:5px;
    height: 200px;
    width:200px;
}


input[type=text] {
    z-index: 10;
    width: 30px;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    height: 30px;
    display: block;
}
input[type=text]:focus {
    width: 130px;
}



input[type=password] {
    z-index: 10;
    width: 30px;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    background-image: url('searchicon.png');
    background-position: 10px 10px; 
    background-repeat: no-repeat;
    padding: 12px 20px 12px 40px;
    -webkit-transition: width 0.4s ease-in-out;
    transition: width 0.4s ease-in-out;
    display: block;
    height: 30px;
}
input[type=password]:focus {
    width: 130px;
}
<div id="login">
            
         <form name="frmLogin" method="post" action="">
            <br><br>
            <table width="100" align="center">
                
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" placeholder="User.." size="30" /></td>
                </tr>
                
                
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" placeholder="Pass.." size="30" /></td>
                </tr>
                
                
            </table>
            <input type="hidden" name="acao" value="login" />
        </form>

【讨论】:

  • 非常感谢答案,但我需要绝对位置,因为 div 位于另一个 div 的顶部,即相对位置
【解决方案3】:

您可以删除 position:absolute,position:absolute 使您的元素脱离页面上的正常定位。在这种情况下,它删除了它们之间的间距,并将输入框放置在低于表格标签的位置。

如果您愿意,可以通过更改密码字段的边距来解决此问题,例如评论中提到的 Keerthana Prabhakaran。

【讨论】:

  • 但我需要绝对位置,因为 div 被放置在另一个 div 的顶部,即相对定位
猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 2018-07-05
  • 2021-01-08
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2014-10-29
相关资源
最近更新 更多