简体   繁体   English

如何在容器内获取3个div以水平对齐,高度为100%

[英]How to get 3 divs to align horizontally, with height of 100%, inside a container

EDIT: Alignment fixed by adding floats. 编辑:通过添加浮点数固定的对齐方式。 Height still doesn't fill 100% though. 身高仍然没有达到100%。 Updated look: https://gyazo.com/4030d76c62c106fae5fbbb07f062efdd 更新外观: https : //gyazo.com/4030d76c62c106fae5fbbb07f062efdd

I have a footer container in which I want to have 3 columns (I have made them green, white and red for now so it's easier for you to see). 我有一个页脚容器,其中要有3列(我现在将它们设置为绿色,白色和红色,以便您更轻松地看到它们)。 Currently they're stacking vertically but I want them to be beside eachother and have a height of 100% to fill the height of whatever the container's height is. 目前,它们是垂直堆叠的,但我希望它们彼此相邻,并具有100%的高度,以填充无论容器高度如何的高度。 Here is an image of what it looks liek at the moment, please ignore the large black boxes, these are just so I can see where stuff is on the page but will evenutally be transparent etc. https://gyazo.com/12d0642e5fd9518a663606668ec06311 这是目前看起来很喜欢的图像,请忽略大的黑匣子,这些只是为了我可以看到页面上的内容,但最终还是透明的等。https: //gyazo.com/12d0642e5fd9518a663606668ec06311

They each have a width of 33% because I want to follow responsive practices and I've currently tried removing all padding and margins etc from every div. 它们每个的宽度为33%,因为我想遵循快速响应的做法,而且我目前已尝试从每个div删除所有填充和边距等。

Any help would be greatly appreciated, thank you. 任何帮助将不胜感激,谢谢。

HTML: HTML:

<div id="Page">

        <div id="content" class="wrapper">

        </div>      

        <div id="footer">
            <div id="footerContainer">
                <div id="footerLeft">
                    <p>
                    Test
                    </p>
                </div>
                <div id="footerCenter">
                    <p>
                    Test
                    </p>
                </div>
                <div id="footerRight">
                    <p>
                    Test
                    </p>
                </div>
            </div>    <!-- footerContainer -->
        </div> <!-- Footer -->

    </div> <!-- Page -->

CSS: CSS:

#content {
    background-color: black;
}

.wrapper {
    width: 60%;
    height: 100%;
    margin: 0 auto;
}

#footer {
    position: absolute;
    width: 100%;
    height: 300px;
    background-color: black;
}

#footerContainer {
    width: 60%;
    max-height: 100%;
    margin: 0 auto;
}

#footerLeft {
    width: 33%;
    height: 100%;
    float: left;
    background-color: darkolivegreen;
    padding: 0;
}

#footerCenter {
    width: 33%;
    height: 100%;
    float: left;
    background-color: white;
    padding: 0;
}

#footerRight {
    width: 33%;
    height: 100%;
    float: left;
    background-color: firebrick;
    padding: 0;
}

Add display: flex to the #footerContainer rule, and change max-height: 100%; 添加display: flex#footerContainer规则,并更改max-height: 100%; to height: 100%; height: 100%; in #footerContainer #footerContainer

 #content { background-color: black; } .wrapper { width: 60%; height: 100%; margin: 0 auto; } #footer { position: absolute; width: 100%; height: 300px; background-color: black; } #footerContainer { display: flex; width: 60%; height: 100%; margin: 0 auto; } #footerLeft { width: 33%; background-color: darkolivegreen; } #footerCenter { width: 33%; background-color: white; } #footerRight { width: 33%; background-color: firebrick; } 
 <div id="Page"> <div id="content" class="wrapper"> </div> <div id="footer"> <div id="footerContainer"> <div id="footerLeft"> <p> Test </p> </div> <div id="footerCenter"> <p> Test </p> </div> <div id="footerRight"> <p> Test </p> </div> </div> <!-- footerContainer --> </div> <!-- Footer --> </div> <!-- Page --> 


Here is a version where I simplified both markup and CSS 这是我简化标记和CSS的版本

 #content { background-color: black; } .wrapper { width: 60%; height: 100%; margin: 0 auto; } #footer { position: absolute; width: 100%; height: 300px; background-color: black; display: flex; justify-content: center; } #footer > div { width: 20%; } #footerLeft { background-color: darkolivegreen; } #footerCenter { background-color: white; } #footerRight { background-color: firebrick; } 
 <div id="Page"> <div id="content" class="wrapper"> </div> <div id="footer"> <div id="footerLeft"> <p> Test </p> </div> <div id="footerCenter"> <p> Test </p> </div> <div id="footerRight"> <p> Test </p> </div> </div> <!-- Footer --> </div> <!-- Page --> 


One for older browsers, using display: table 一种用于较旧的浏览器,使用display: table

 #content { background-color: black; } .wrapper { width: 60%; height: 100%; margin: 0 auto; } #footer { position: absolute; width: 100%; height: 300px; background-color: black; } #footerContainer { display: table; width: 60%; height: 100%; margin: 0 auto; } #footerContainer > div { display: table-cell; width: 33%; } #footerLeft { background-color: darkolivegreen; } #footerCenter { background-color: white; } #footerRight { background-color: firebrick; } 
 <div id="Page"> <div id="content" class="wrapper"> </div> <div id="footer"> <div id="footerContainer"> <div id="footerLeft"> <p> Test </p> </div> <div id="footerCenter"> <p> Test </p> </div> <div id="footerRight"> <p> Test </p> </div> </div> <!-- footerContainer --> </div> <!-- Footer --> <!-- Footer --> </div> <!-- Page --> 

Add float: left to the 3 elements. 添加float: left在3个元素的float: left
Don't repeat the properties, combine the selectors with comma. 不要重复属性,将选择器与逗号组合。

To stretch the elements you will need to change max-height to height on #footerContainer . 要拉伸元素,您将需要在#footerContainer max-height更改为height

 #content { background-color: black; } .wrapper { width: 60%; height: 100%; margin: 0 auto; } #footer { position: absolute; width: 100%; height: 300px; background-color: black; } #footerContainer { width: 60%; height: 100%; margin: 0 auto; } #footerLeft, #footerCenter, #footerRight { float: left; width: 33%; height: 100%; padding: 0; } #footerLeft { background-color: darkolivegreen; } #footerCenter { background-color: white; } #footerRight { background-color: firebrick; } 
 <div id="Page"> <div id="content" class="wrapper"> </div> <div id="footer"> <div id="footerContainer"> <div id="footerLeft"> <p> Test </p> </div> <div id="footerCenter"> <p> Test </p> </div> <div id="footerRight"> <p> Test </p> </div> </div> <!-- footerContainer --> </div> <!-- Footer --> </div> <!-- Page --> 

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#container {
    width:100%;
    height:100%;
} 
#div1 {
    width:33%;
    display:inline-block;
    float:left;
    height:100%;
}
#div2 {
    width:33%;
    display:inline-block;
    height:100%;
}
#div3 {
    width:33%;
    display:inline-block;
    float:right;
    height:100%;   
}
</style>
</head>
<body>
<div id="container">
<div id="div1"><!--
--><div id="div2"><!--
--><div id="div3">
</div>
</body>
</html>

If the second div isn't perfectly aligned, try setting #container text-align to center. 如果第二个div不能完全对齐,请尝试将#container文本对齐设置为居中。 Is very important to use the comments to delete the space between the divs. 使用注释删除div之间的空间非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM