如何让我的div与我之前的div兄弟一样高

时间:2017-04-17 02:59:43

标签: html css height percentage siblings

我想让我的一个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;
}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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