【问题标题】:div width not workingdiv宽度不起作用
【发布时间】:2014-11-01 15:41:37
【问题描述】:

我尝试在所有方向上按像素设置 div 的宽度和填充,以使它们组合在一起,但我尝试的任何方式都不起作用。当我放大和缩小时,一切的宽度都会发生变化。我觉得我做错了,还有其他更简单的方法来调整页面中元素的大小和位置吗?

JsFiddle:http://jsfiddle.net/ruavcstx/

<!DOCTYPE html>

<html>
    <head>
        <style>
        .background {
        background-color: #2ECCFA;
}
#signupform {
            clear: both;
            background-color: #e9c85d;
            display: table;
            padding: 33px 29px 33px 29px;
}

#loginform { 
            background-color: #4daf7c;
            display: table;
            clear: both;
            padding: 0px;
}

.username {
            padding: 15px 35px ;
            display: table;
            text-align: center;
            margin: 0px;
            width: 150px;
            clear: both;
}

.hidden {
        display: none !important;
}

.password {
            padding: 15px 35px ;
            display: table;
            text-align: center;
            margin: 0px;
            width: 150px;
            margin: 0;
}
#loginbtn {
            padding: 15px 39px ;
            background-color:#404241;
            display: table;
            text-align: center;
            margin: 0px;
            width: 150px;
            cursor: pointer;
            display: block !important;


}
#logintab {
        border: 5px solid #4daf7c;
        background-color: #4daf7c;
        padding: 15px 20px;
        float: left;
        cursor: pointer;
        margin: 0px;
}
#signuptab {
        border: 5px solid #e9c85d;
        background-color: #e9c85d;
        padding: 15px 14px;
        float: left;
        cursor: pointer;
        margin: 0px;
        display: inline;
}
#skiptab {
        border: 5px solid #404241;
        background-color: #404241;
        padding: 15px 12px 15px 12px;
        float: left;
        cursor: pointer;
        margin: 0px;
        color: red;
}



#signupbtn {
            padding: 5px 39px ;
            background-color:#404241;
            display: table;
            text-align: center;
            width: 150px;
            cursor: pointer;
            display: block !important;
            color: red;

}

#signupform p, #loginform p {
                            display: inline;
                            color: red;

}
        </style>

    </head>

    <body class="background">

        <div id="loginsignupform">
            <div class="tabs">
                <div id="logintab"> login </div>
                <div id="signuptab"> sign up </div>
                <div id="skiptab"> skip </div>          
            </div>

            <div>
                <div id="loginform"> 

                    <div class="username">
                        <input type="text" placeholder="username" name="username"/>
                    </div>

                    <div class="password">

                        <input type="password" placeholder="password" name="password"/>
                        <p> invalid username or password! </p>
                    </div>

                    <p id="loginbtn"> login </p>

                </div>

                <div id="signupform" >
                    <input type="text" placeholder="full name" name="name" /><br>
                    <p> full name is required </p><br>
                    <input type="username" placeholder="username" name="username" /><br>
                    <input type="text" placeholder="e-mail"/><br>                   
                    <input type="password" placeholder="password" name="password" /><br>
                    <input type="password" placeholder="re-enter password" name="password1" /><br>
                </div>

                <div id="signupbtn">    
                        <p > sign up </p>
                </div>

            </div>
        </div>

    </body>
</html>

【问题讨论】:

  • 你能放小提琴并提供链接
  • 老实说,看起来一切正常:jsfiddle.net/ruavcstx
  • @Khaleel 我已经添加了
  • @CBauer 绿色登录 div 未与顶部的选项卡对齐
  • 您已将该 div 中的子元素设置为 150px 宽度,并在左侧和右侧为它们提供 35px 的填充。这迫使父级比控件顶部的选项卡更宽。

标签: html css width padding


【解决方案1】:

您还没有为tabs提供相应的填充

检查这个更新的小提琴:http://jsfiddle.net/ruavcstx/2/

查看 CSS 更改。希望对你有帮助

【讨论】:

  • 感谢 Khaleel,但它在缩放和在不同浏览器上使用时不起作用:s21.postimg.org/76gzis0cn/upload.png
  • @MohamedAmrSalem 欢迎您!对于缩放场景,有许多解决方法可以找到浏览器缩放/调整大小事件。使用它,您可以检测浏览器缩放级别并使用 CSS 转换缩放整个渲染页面,因此它看起来相同但更大。
【解决方案2】:

你可以试试吗?

代替float:left,使用display:inline;并且脚测试确实对齐中心。并且,调整它的大小。它对我来说工作正常。

HTML

<div id="loginsignupform" align="center">

到

<div id="loginsignupform">

CSS

#signuptab {
  float: left;
}

到

#signuptab {
    display: inline;
}

【讨论】:

    【解决方案3】:

    好的,试试这些变化:http://jsfiddle.net/ruavcstx/1/

    我变了—— 用户名/密码类

    • 显示:块
    • 上边距:35px; (没有其他设置)
    • 宽度:100%;

    我在 html 中做了一个小改动(在密码文本框之后放置了一个 &lt;br/&gt;,因此错误出现在下一行)。

    .background {
                background-color: #2ECCFA;
     }
     #signupform {
          clear: both;
          background-color: #e9c85d;
          display: table;
          padding: 33px 29px 33px 29px;
    }
    
    #loginform { 
                background-color: #4daf7c;
                display: table;
                clear: both;
                padding: 0px;
        width: 150px;
    }
    
    .username {
        padding-top: 15px
        display: block;
                text-align: center;
                margin: 0px;
                width: 100%;
                clear: both;
    }
    
    .hidden {
            display: none !important;
    }
    
    .password {
                padding-top: 15px;
        display: block;
                text-align: center;
                margin: 0px;
                width: 100%;
    }
    #loginbtn {
                padding: 15px 39px ;
                background-color:#404241;
                display: table;
                text-align: center;
                margin: 0px;
                width: 150px;
                cursor: pointer;
                display: block !important;
    
    
    }
    #logintab {
            border: 5px solid #4daf7c;
            background-color: #4daf7c;
            padding: 15px 20px;
            float: left;
            cursor: pointer;
            margin: 0px;
    }
    #signuptab {
            border: 5px solid #e9c85d;
            background-color: #e9c85d;
            padding: 15px 14px;
            float: left;
            cursor: pointer;
            margin: 0px;
            display: inline;
    }
    #skiptab {
            border: 5px solid #404241;
            background-color: #404241;
            padding: 15px 12px 15px 12px;
            float: left;
            cursor: pointer;
            margin: 0px;
            color: red;
    }
    
    
    
    #signupbtn {
                padding: 5px 39px ;
                background-color:#404241;
                display: table;
                text-align: center;
                width: 150px;
                cursor: pointer;
                display: block !important;
                color: red;
    
    }
    
    #signupform p, #loginform p {
                                display: inline;
                                color: red;
    
    }
    

    HTML:

        <div id="loginsignupform">
            <div class="tabs">
                <div id="logintab"> login </div>
                <div id="signuptab"> sign up </div>
                <div id="skiptab"> skip </div>          
            </div>
    
            <div>
                <div id="loginform"> 
    
                    <div class="username">
                        <input type="text" placeholder="username" name="username"/>
                    </div>
    
                    <div class="password">
    
                        <input type="password" placeholder="password" name="password"/><Br/>
                        <p> invalid username or password! </p>
                    </div>
    
                    <p id="loginbtn"> login </p>
    
                </div>
    
                <div id="signupform" >
                    <input type="text" placeholder="full name" name="name" /><br>
                    <p> full name is required </p><br>
                    <input type="username" placeholder="username" name="username" /><br>
                    <input type="text" placeholder="e-mail"/><br>                   
                    <input type="password" placeholder="password" name="password" /><br>
                    <input type="password" placeholder="re-enter password" name="password1" /><br>
                </div>
    
                <div id="signupbtn">    
                        <p > sign up </p>
                </div>
    
            </div>
        </div>
    
    </body>
    

    【讨论】:

    • @MohamedAmrSalem 尝试在多个浏览器中打开小提琴。您会看到我在上面发布的代码可以在 IE、Chrome 和 firefox 中运行。您的文件中的其他地方可能有其他 css 覆盖了我在此处发布的内容并导致其显示不正确。
    • 你用的是什么浏览器?您是否打开了我帖子顶部的 jsfiddle 链接?它适用于所有浏览器。
    • 我试过 IE 和 chrome 都出现了与此屏幕截图中相同的错误postimg.org/image/gyfbk2pcv@CBauer
    • 你是在打开我答案顶部的 jsfiddle 吗?
    猜你喜欢
    • 2012-10-25
    • 2012-06-27
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多