【问题标题】:How can I get my div the same height as my previous div sibling如何让我的 div 与我以前的 div 兄弟的高度相同
【发布时间】:2017-09-12 15:52:06
【问题描述】:

我想让我的一个 div 与我以前的 div 具有相同的高度。我正在使用宽度和高度的百分比(所以它在移动设备上看起来也更好。我也尝试将父元素设置为 100% 的宽度和高度(在一些论坛帖子中看到,但它没有工作)。我也尝试过使用 display: table-cell,但也没有用。这就是我的设置的样子:

index.php:

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "UTF-8"/>
        <title>SiteTitle</title>
        <link rel = "stylesheet" type = "text/css" href = "styles/index.css"/>
    </head>
    <body>
        <div id = "allContent">
            <div id = "header">
                <div id = "siteLogo">
                    <h1 id = "title">Site_Title</h1>
                </div>
                <div id = "shortMenu">
                    <a href = "#">Login</a>
                </div>
            </div>
        </div>
    </body>
</html>

index.css:

body {
    background-color: rgb(0, 0, 0);
    margin: 0px;
    padding: 0px;
    color: rgb(192, 74, 0);
}

div#allContent {
    width: 80%;
    margin: auto;
}

div#allContent div#header {
    margin: 0px;
    padding: 0px;
}

div#allContent div#header div#siteLogo {
    margin: 0px;
    padding: 0px;
    width: 70%;
    border: 1px solid blue;
    float: left;
}

div#allContent div#header div#siteLogo h1#title {
    margin: 0px;
    padding: 0px;
    font-size: 300%;
    font-family: Sans-Serif;
}

div#allContent div#header div#shortMenu {
    width: 20%;
    border: 1px solid green;
    float: left;
}

div#allContent div#header div#shortMenu a {
    font-family: Sans-Serif;
    color: rgb(204, 204, 204);
    text-decoration: none;
}

不确定现在要检查什么.. 我基本上希望Login 链接的底部与网站标题的底部对齐。稍后我可能会将其更改为徽标图像,因此高度可能会更改,并且如果我决定在路上多次更改它,我不想更改这些值。现在Login 链接位于页面顶部。

编辑:我添加了以下代码行,但它为每个 div 提供了 50% 的宽度

div#allContent div#header {
  margin: 0px;
  padding: 0px;
  display: flex;
}

div#allContent div#header div {
  flex: 1;
}

【问题讨论】:

    标签: html css height percentage siblings


    【解决方案1】:

    试试这个代码

    使用display:table-cell

    body {
        background-color: rgb(0, 0, 0);
        margin: 0px;
        padding: 0px;
        color: rgb(192, 74, 0);
    }
    
    div#allContent {
        width: 80%;
        margin: auto;
    }
    
    div#allContent div#header {
        margin: 0px;
        padding: 0px;
    }
    
    
    div#allContent div#header div#siteLogo h1#title {
        margin: 0px;
        padding: 0px;
        font-size: 300%;
        font-family: Sans-Serif;
    }
    
    
    div#allContent div#header div#shortMenu a {
        font-family: Sans-Serif;
        color: rgb(204, 204, 204);
        text-decoration: none;
    }
    div#allContent div#header div#shortMenu {
        width: 30%;
        border: 1px solid green;
        display: table-cell;
        vertical-align: middle;
    }
    
    div#allContent div#header div#siteLogo {
        margin: 0px;
        padding: 0px;
        width: 70%;
        border: 1px solid blue;
        display: table-cell;
    }
    div#allContent div#header {
        margin: 0px;
        padding: 0px;
        width: 100%;
        display: table;
    }
    <div id = "allContent">
                <div id = "header">
                    <div id = "siteLogo">
                        <h1 id = "title">Site_Title</h1>
                    </div>
                    <div id = "shortMenu">
                        <a href = "#">Login</a>
                    </div>
                </div>
            </div>

    使用display:flex

    替换这个css代码

    div#allContent div#header div {
      flex: 1 auto;
    }
    

    body {
        background-color: rgb(0, 0, 0);
        margin: 0px;
        padding: 0px;
        color: rgb(192, 74, 0);
    }
    
    div#allContent {
        width: 80%;
        margin: auto;
    }
    
    div#allContent div#header {
        margin: 0px;
        padding: 0px;
    }
    
    div#allContent div#header div#siteLogo {
        margin: 0px;
        padding: 0px;
        width: 70%;
        border: 1px solid blue;
        float: left;
    }
    
    div#allContent div#header div#siteLogo h1#title {
        margin: 0px;
        padding: 0px;
        font-size: 300%;
        font-family: Sans-Serif;
    }
    
    div#allContent div#header div#shortMenu {
        width: 20%;
        border: 1px solid green;
        float: left;
    }
    
    div#allContent div#header div#shortMenu a {
        font-family: Sans-Serif;
        color: rgb(204, 204, 204);
        text-decoration: none;
    }
    div#allContent div#header {
      margin: 0px;
      padding: 0px;
      display: flex;
    }
    
    div#allContent div#header div {
      flex: 1 auto;
    }
    <div id = "allContent">
                <div id = "header">
                    <div id = "siteLogo">
                        <h1 id = "title">Site_Title</h1>
                    </div>
                    <div id = "shortMenu">
                        <a href = "#">Login</a>
                    </div>
                </div>
            </div>

    【讨论】:

    • 我想出了 flex 样式,但我更喜欢使用表格单元格,因为我觉得我有更多的控制权。不知道我之前在尝试什么,但它有效。又得看CSS了,大概三四年了吧哈哈。
    【解决方案2】:

    我更改了以下选择器。请注意 display: inline-block;是为了防止ie11可能出现的bug。此外,为简洁起见,我没有包括供应商前缀样式。

    div#allContent div#header div {
      flex: 1;
      display: inline-block;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多