【问题标题】:Div Positioning & Positioning at resizing of page {Html}页面 {Html} 调整大小时的 Div 定位和定位
【发布时间】:2021-01-06 00:27:30
【问题描述】:

我是网络编程的初学者。我正在尝试克隆谷歌搜索页面。我实现了构建克隆网站的结构。

第一个问题

但是,我对 div 的定位有疑问。我正在使用表单类来搜索书面文本,并且我想在此表单上方添加带有特定边距的谷歌徽标图片。我使用 div 来划分页面来实现这一点。但是,当我更改 google 框架 div 位置时,所有 div 都会随着此更改而更改。例如,当我更改#oneGoogleBar 边距时,表单和#otherlinks 正在发生变化。我可以通过更改其边距值来更改#otherlinks,但这不是可靠的解决方案(例如,每次我都需要更改它(如果我有多个 div,这种方法将非常乏味)。

第二个问题

当我调整大小(缩小网页)时,#otherlinks 开始向下移动。我也不明白这个原因。

我在同一个问题中提出了这些问题,因为这些问题可能是初学者级别的问题。您可以在下面找到代码。

HTML

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<link rel="stylesheet" href="index_css.css">
<html lang="en">
    <head>
        <title>Search</title>
    </head>
    <body>
        <div id="oneGoogleBar">
    <iframe id="backgroundImage" frameBorder = "0"
        src="https://www.google.com.tr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
    </iframe>
       </div>
        <div id="form_div">
        <form action="https://google.com/search" class="form">
            <input type ="text" name="q"> <br><br>
            <input type ="submit" value="Google Search">
            <input type ="submit" value ="I'm Feeling Lucky">
        </form>
        </div>
        <div id = "other_links">
        <a href="googleImage.html" class=google_image_page>Google Image Search</a>
        <a href="googleAdvanced.html" class=google_advanced_page>Google Advanced Search</a>
        </div>
    </body>
</html>

CSS

@import url('https://fonts.googleapis.com/css?family=Open+Sans');
.form{
    text-align: center;
    padding-top:150px;
}
input[type=submit]{
    
    background-color: #f8f9fa;
    border: 1px solid #f8f9fa;
    border-radius: 4px;
    border-radius:inhterit;
    color: #3c4043;
    font-family: arial,sans-serif;
    font-size: 14px;
    margin: 11px 4px;
    padding: 0 16px;
    line-height: 27px;
    height: 36px;
    min-width: 54px;
    text-align: center;
    cursor: pointer;
    user-select: none;
    padding-left: 25px;
    outline:none;
}

input[type=text]:hover{
    border-color:#9DD9F3;
}


input[type=text]{
    width : 385px;
    height: 25px;
    border-radius: 15px;
    outline:none;
    
}
.google_image_page{
    font-family: arial,sans-serif;
    color:black;
    text-decoration:none;
    font-size:13px;
    
    
}

.google_advanced_page{
    font-family: arial,sans-serif;
    color:black;
    text-decoration:none;
    font-size:13px;
    
}


#other_links{
     margin-top : -40%;
     margin-left : 80%;
     margin-bottom: -85%
 }

 #oneGoogleBar{
     position: relative;
     margin-left : 42%;
     margin-top: 15%;
     max-width:10%;
     max-height: 10%
     
     
 }

【问题讨论】:

    标签: html css web


    【解决方案1】:

    编辑:

    更改列表:

    • 我在 google.com 中找不到任何“Google 图片搜索”,所以我只是假设您希望它作为标题。所以我把它放在了上面。
    • 我将名称“index_css.css”更改为“style.css”
    • 我将名称“oneGoogleBar”更改为“one-google-bar”
    • 我将名称“form_div”更改为“form-div”
    • 我将名称“other_links”更改为“其他链接”

    我按照您提出的相同顺序回答您的问题:

    1- Html 中的第一个元素是“one-google-bar”。在 Html 中,第一个元素在第二个元素之上,第二个在第三个元素之上,......基本上我的意思是元素出现在页面上,取决于它们在代码中的位置。\

    您的 Html 结构:
    1) 一个谷歌酒吧
    2) 表单-div
    3) 其他链接

    如果您更改“one-google-bar”的位置,其下方的位置也会更改。

    解决方案:

    你必须按顺序工作。从顶部元素开始,然后向下移动。

    更改列表:

    • 我将“其他链接”放在“one-google-bar”上方,因为就像我之前所说的,我不知道您想把它放在哪里。我假设您希望它位于页面的顶部。
    • 我从您的 CSS 中删除了“其他链接”。因为这个元素已经在顶部了。
    • 我对您的“one-google-bar”css 文件进行了一些更改。变化如下:
      • 我删除了“位置:相对”。因为您希望它与什么相关?

      • 我假设你首先给它'margin-left: 50%' 并且由于它没有被放置在中间(因为图像也占据了一些位置)你将它更改为'margin-left: 42%'。

        有一个巧妙的技巧可以将元素放在中间:

    边距:0 自动;

    • 这样,您可以将上下边距设置为 0。浏览器会自动设置左右边距,使元素位于中间。
      现在在它下面,随意更改顶部和底部边距。
    • 我还将“最大宽度:10%”更改为:

    宽度:适合内容;

    不是使用最大宽度的正确方法。

    2- 你说你没有意识到为什么当你调整页面大小时所有元素都会移动。
    在您的“one-google-bar”中,您有:

    上边距:150px;

    % 就是发生这种情况的原因。这意味着将当前宽度的 15% 添加到上边距,并且由于您更改了宽度,因此它也会更改。因此整个页面移动(因为元素是按顺序放置的......)。

    解决方案:

    只需使用 px 代替 %。

    更改列表:

    • 我将 'margin-top: 15%' 更改为:

    保证金最高:15%;

    Index.html:

    <!DOCTYPE html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css">
    <html lang="en">
        <head>
            <title>Search</title>
        </head>
        <body>
            <div id = "other-links">
                <a href="googleImage.html" class=google_image_page>Google Image Search</a>
                <a href="googleAdvanced.html" class=google_advanced_page>Google Advanced Search</a>
            </div>
            <div id="one-google-bar">
                <iframe id="backgroundImage" frameBorder = "0"
                    src="https://www.google.com.tr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
                </iframe>
           </div>
            <div id="form-div">
                <form action="https://google.com/search" class="form">
                    <input type ="text" name="q"> <br><br>
                    <input type ="submit" value="Google Search">
                    <input type ="submit" value ="I'm Feeling Lucky">
                </form>
            </div>
            
        </body>
    </html>
    

    style.css:

    @import url("https://fonts.googleapis.com/css?family=Open+Sans");
    .form {
      text-align: center;
      padding-top: 150px;
    }
    input[type="submit"] {
      background-color: #f8f9fa;
      border: 1px solid #f8f9fa;
      border-radius: 4px;
      border-radius: inhterit;
      color: #3c4043;
      font-family: arial, sans-serif;
      font-size: 14px;
      margin: 11px 4px;
      padding: 0 16px;
      line-height: 27px;
      height: 36px;
      min-width: 54px;
      text-align: center;
      cursor: pointer;
      user-select: none;
      padding-left: 25px;
      outline: none;
    }
    
    input[type="text"]:hover {
      border-color: #9dd9f3;
    }
    
    input[type="text"] {
      width: 385px;
      height: 25px;
      border-radius: 15px;
      outline: none;
    }
    .google_image_page {
      font-family: arial, sans-serif;
      color: black;
      text-decoration: none;
      font-size: 13px;
    }
    
    .google_advanced_page {
      font-family: arial, sans-serif;
      color: black;
      text-decoration: none;
      font-size: 13px;
    }
    
    /* #other-links {           //REMOVED
      margin-top: -40%;         //REMOVED
      margin-left: 80%;         //REMOVED
      margin-bottom: -85%;      //REMOVED
    } */
    
    #one-google-bar {
      /* position: relative;     //REMOVED       
      margin-left: 42%;          //REMOVED   
      margin-top: 15%;           //REMOVED           
      max-width: 10%;            //REMOVED       
      max-height: 10%;           //REMOVED  
      */
      width: fit-content;        /*ADDED*/                    
      margin: 0 auto;            /*ADDED*/                    
      /* margin-top: 15%;        //REMOVED*/
      margin-top: 150px;         /*ADDED*/   
    }
    

    【讨论】:

      【解决方案2】:

      您的 html 文档非常复杂,这不是必需的。而不是&lt;iframe&gt;,您可以简单地使用&lt;img&gt; 标签。在您的 CSS 中,您使用 % 作为边距属性,这使对齐变得混乱。 看看这个。

      HTML 文件

      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1.0" />
          <title>Google</title>
          <link rel="stylesheet" href="style.css" />
        </head>
        <body>
          <nav>
            <ul>
              <li>Gmail</li>
              <li>Images</li>
            </ul>
          </nav>
          <main>
            <img
              class="logo"
              src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
              alt=""
            />
            <form action="">
              <input type="text" /> <br />
              <br />
              <input type="submit" value="Google Search" />
              <input type="submit" value="I'm Felling Lucky" />
            </form>
          </main>
        </body>
      </html>
      
      

      CSS 文件

      body {
        margin: 0;
        padding: 0;
      }
      
      nav ul {
        list-style: none;
        display: flex;
        flex-direction: row-reverse;
      }
      nav ul li {
        margin: 20px;
      }
      
      main {
        align-items: center;
      }
      .logo {
        display: block;
        margin-top: 100px;
        margin-left: auto;
        margin-right: auto;
      }
      form {
        text-align: center;
      }
      input[type="text"] {
        margin-top: 20px;
        width: 385px;
        height: 25px;
        border-radius: 15px;
        outline: none;
      }
      
      

      【讨论】:

      • 感谢您的友好反馈伙伴。但是,我希望有一个与 Google 本身接近的网站。
      【解决方案3】:

      您基本上是在为每个 div 应用边距,因此它可以到达您想要的位置。嗯,这不是一个很好的做法。
      而不是这样做,只需使用一个

      display: flex, ...
      

      实现你想要的。 基本上在 Html 中,元素是根据它们在代码中的位置来显示的。意思是,如果您想要一个包含“Google 图片搜索”的标题,请先编写它。

      调整页面大小时元素位置发生变化的原因是,您在整个地方都使用了边距。它发生是很自然的。


      我会避免使用 iframe,因为没有理由使用它。只需在你的 html 中使用一个简单的 img 标签。


      索引.html

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
      <link rel="stylesheet" href="style.css">
      <html lang="en">
          <head>
              <title>Search</title>
          </head>
          <body>
          <main>
              <img src="https://www.google.com.tr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="google-img" />
              <div class="form-div">
                  <form action="https://google.com/search" class="form">
                      <input type ="text" id="search-field">
                      <div>
                          <button>Google Search</button>
                          <button>I'm Feeling Lucky</button>
                      </div>
                  </form>
              </div>
          </main>        
          </body>
      </html>
      

      样式.css:

      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      
      body {
        width: 100%;
      }
      
      main {
        display: flex;
        flex-direction: column;
        width: 100%;
        align-items: center;
        margin-top: 200px;
      }
      
      header {
        display: flex;
        width: 100%;
        align-items: right;
        justify-content: right;
      }
      
      .form {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
      }
      
      .form-div {
        width: 100%;
        margin-top: 35px;
      }
      
      .form div {
        margin-top: 20px;
      }
      
      .form div button {
        background-color: #f8f9fa;
        border: 1px solid #f8f9fa;
        border-radius: 4px;
        color: #3c4043;
        font-family: arial, sans-serif;
        font-size: 14px;
        margin: 11px 4px;
        padding: 0 16px;
        line-height: 27px;
        height: 36px;
        min-width: 54px;
      }
      .form div button:hover {
        cursor: pointer;
      }
      
      #search-field {
        width: 80%;
        height: 44px;
        border-radius: 24px;
        border: 1px solid #dfe1e5;
        padding-left: 30px;
      }
      #search-field:focus {
        outline: none;
      }
      

      只是一些注释:

      • 在 Html 和 css 中,您不使用 camelCaseNaming,您只需在不同部分之间放置一个 -。所以 oneGoogleBar 应该是 one-google-bar
      • 不要使用 iframe。
      • 您不应该使用
        来对齐iteams。看看我是如何使用 flex box 做到这一点的。
      • 将您的 css 文件命名为 Style.css。没有规则,但通常这是人们编写代码的方式。
      • 如果您想构建一个已经存在的页面,检查页面是一个非常好的做法。你可以从中学到很多东西。
      • 最后也是最重要的事情:继续努力。

      编辑:

      更改列表:

      • 我在 google.com 中找不到任何“Google 图片搜索”,所以我只是假设您希望它作为标题。所以我把它放在了上面。
      • 我将名称“index_css.css”更改为“style.css”
      • 我将名称“oneGoogleBar”更改为“one-google-bar”
      • 我将名称“form_div”更改为“form-div”
      • 我将名称“other_links”更改为“其他链接”

      我按照您提出的相同顺序回答您的问题:

      1- Html 中的第一个元素是“one-google-bar”。在 Html 中,第一个元素在第二个元素之上,第二个在第三个元素之上,......基本上我的意思是元素出现在页面上,取决于它们在代码中的位置。\

      您的 Html 结构:
      1) 一个谷歌酒吧
      2) 表单-div
      3) 其他链接

      如果您更改“one-google-bar”的位置,其下方的位置也会更改。

      解决方案:

      你必须按顺序工作。从顶部元素开始,然后向下移动。

      更改列表:

      • 我将“其他链接”放在“one-google-bar”上方,因为就像我之前所说的,我不知道您想把它放在哪里。我假设您希望它位于页面的顶部。
      • 我从您的 CSS 中删除了“其他链接”。因为这个元素已经在顶部了。
      • 我对您的“one-google-bar”css 文件进行了一些更改。变化如下:
        • 我删除了“位置:相对”。因为您希望它与什么相关?

        • 我假设你首先给它'margin-left: 50%' 并且由于它没有被放置在中间(因为图像也占据了一些位置)你将它更改为'margin-left: 42%'。

          有一个巧妙的技巧可以将元素放在中间:

      边距:0 自动;

      • 这样,您可以将上下边距设置为 0。浏览器会自动设置左右边距,使元素位于中间。
        现在在它下面,随意更改顶部和底部边距。
      • 我还将“最大宽度:10%”更改为:

      宽度:适合内容;

      不是使用最大宽度的正确方法。

      2- 你说你没有意识到为什么当你调整页面大小时所有元素都会移动。
      在您的“one-google-bar”中,您有:

      上边距:150px;

      % 就是发生这种情况的原因。这意味着将当前宽度的 15% 添加到上边距,并且由于您更改了宽度,因此它也会更改。因此整个页面移动(因为元素是按顺序放置的......)。

      解决方案:

      只需使用 px 代替 %。

      更改列表:

      • 我将 'margin-top: 15%' 更改为:

      保证金最高:15%;

      Index.html:

      <!DOCTYPE html>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
      <link rel="stylesheet" href="style.css">
      <html lang="en">
          <head>
              <title>Search</title>
          </head>
          <body>
              <div id = "other-links">
                  <a href="googleImage.html" class=google_image_page>Google Image Search</a>
                  <a href="googleAdvanced.html" class=google_advanced_page>Google Advanced Search</a>
              </div>
              <div id="one-google-bar">
                  <iframe id="backgroundImage" frameBorder = "0"
                      src="https://www.google.com.tr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
                  </iframe>
             </div>
              <div id="form-div">
                  <form action="https://google.com/search" class="form">
                      <input type ="text" name="q"> <br><br>
                      <input type ="submit" value="Google Search">
                      <input type ="submit" value ="I'm Feeling Lucky">
                  </form>
              </div>
              
          </body>
      </html>
      

      style.css:

      @import url("https://fonts.googleapis.com/css?family=Open+Sans");
      .form {
        text-align: center;
        padding-top: 150px;
      }
      input[type="submit"] {
        background-color: #f8f9fa;
        border: 1px solid #f8f9fa;
        border-radius: 4px;
        border-radius: inhterit;
        color: #3c4043;
        font-family: arial, sans-serif;
        font-size: 14px;
        margin: 11px 4px;
        padding: 0 16px;
        line-height: 27px;
        height: 36px;
        min-width: 54px;
        text-align: center;
        cursor: pointer;
        user-select: none;
        padding-left: 25px;
        outline: none;
      }
      
      input[type="text"]:hover {
        border-color: #9dd9f3;
      }
      
      input[type="text"] {
        width: 385px;
        height: 25px;
        border-radius: 15px;
        outline: none;
      }
      .google_image_page {
        font-family: arial, sans-serif;
        color: black;
        text-decoration: none;
        font-size: 13px;
      }
      
      .google_advanced_page {
        font-family: arial, sans-serif;
        color: black;
        text-decoration: none;
        font-size: 13px;
      }
      
      /* #other-links {           //REMOVED
        margin-top: -40%;         //REMOVED
        margin-left: 80%;         //REMOVED
        margin-bottom: -85%;      //REMOVED
      } */
      
      #one-google-bar {
        /* position: relative;     //REMOVED       
        margin-left: 42%;          //REMOVED   
        margin-top: 15%;           //REMOVED           
        max-width: 10%;            //REMOVED       
        max-height: 10%;           //REMOVED  
        */
        width: fit-content;        /*ADDED*/                    
        margin: 0 auto;            /*ADDED*/                    
        /* margin-top: 15%;        //REMOVED*/
        margin-top: 150px;         /*ADDED*/   
      }
      

      【讨论】:

      • 亲爱的 Parsa,感谢您的评论和友好的反馈。但是,我们的设计完全不同。如果您复制并粘贴我的代码并查看我正在尝试做什么,您可能会更有帮助。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多