html - 为什么我的边框会超过我的表格?

标签 html css

当我缩小浏览器窗口时,边框不会停留在表单周围,而是会重叠?我的判决也是如此。如何使边框保持在表单周围,并根据带有填充的表单长度调整边框高度?

#form_border{
    margin: auto;
    width: 400px; 
    
    border-style: solid;
    border-color: black;
    border-width: 1px;
  
    
    
}
#form {
      
    width: 50%;
    height: 50%;
    margin: auto;
    padding-top: 2em;
    text-align: center;
                    
}
#form form{display: block;}
#existing_account{ text-align: center;
                   text-decoration: none;
                   color: black;
}
    


input[type=submit] {
                    background:white;
                    border:1px solid;
                    border-color: #292929;
                    cursor:pointer;
                    border-radius: 5px; }   


input[type=submit] {
                    background:white;
                    border:1px solid;
                    border-color: #292929;
                    cursor:pointer;
                    border-radius: 5px; }
<div id="form_border">
    <form id="form" method="post" action="">
        <p>Username:<br> <input type="text" name="user_name" /> <br></p>
        <p>Password:<br> <input type="password" name="user_pass"><br></p>
        <p>Password again:<br> <input type="password" name="user_pass_check"><br></p>
        <p>E-mail:<br> <input type="email" name="user_email"><br></p>
        <input type="submit" value="Sign Up" />
     </form>
        <a id="existing_account" href="sign_in.php">Already have an account?</a>
    </div>

最佳答案

尝试使用这段代码:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<style>
#form_border{
    margin: auto;
    width: 400px; 
    border-style: solid;
    border-color: black;
    border-width: 1px;
}
#form {
    width: 50%;
    height: 50%;
    margin: auto;
    padding-top: 2em;
    text-align: center;
}
#form form{
    display: block;
}
#existing_account{ 
    text-align: center;
    text-decoration: none;
    color: black;
}
input[type=submit] {
    background:white;
    border:1px solid;
    border-color: #292929;
    cursor:pointer;
    border-radius: 5px; 
}   
@media screen and (max-width:399px){
    input[type="text"],
    input[type="password"],
    input[type="email"]{
        width:90%;
    }
    #form_border{
        width:98%;
    }
    #form {
        width: 50%;
        height: 50%;
    }
}
</style>
</head>
<body>
<div id="form_border">
    <form id="form" method="post" action="">
        <p>Username:<br> <input type="text" name="user_name" /> <br></p>
        <p>Password:<br> <input type="password" name="user_pass"><br></p>
        <p>Password again:<br> <input type="password" name="user_pass_check"><br></p>
        <p>E-mail:<br> <input type="email" name="user_email"><br></p>
        <input type="submit" value="Sign Up" />
     </form>
        <a id="existing_account" href="sign_in.php">Already have an account?</a>
    </div>
</body>
</html>

您将观察到:

  1. 我引入了媒体查询来管理宽度小于 400 像素的屏幕的表单大小。
  2. 表单和表单元素的宽度已在媒体查询中得到处理。

改进范围:

  1. 重构 HTML,使其更易于管理。

  2. 更高效地使用媒体查询。

  3. 更有效地使用 width 值。

你用这个jsfiddle.net演示链接。

这里有一些更常用的媒体查询断点,您可以考虑将其包含在您的 CSS 中

/* Smartphones (portrait and landscape) ----------- */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */
}
/**********
iPad 3
**********/
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
/* Desktops and laptops ----------- */
@media only screen  and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen  and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

/* iPhone 5 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6 ----------- */
@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6+ ----------- */
@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S3 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S4 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

/* Samsung Galaxy S5 ----------- */
@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

关于html - 为什么我的边框会超过我的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30822100/

相关文章:

javascript - 如何将样式仅应用于 HTML 中的一个 div?

css - 谷歌字体 Yanone Kaffeesatz Light 不显示

html - 如果我想隐藏我的文本区域,我该怎么做?

javascript - 获取背景图片的全高

CSS:text-align-last 在 chrome 中不起作用?

javascript - iframe 中元素的 CKEditor 内联编辑器

css - MVC 5 Bootstrap 复选框 CSS 问题

javascript - 动态改变背景颜色

JavaScript - 未捕获的类型错误 : Cannot read property 'src' of undefined

html - flex 盒布局 : shrink and grow at the same time