简体   繁体   English

如何使用CSS将3个色块HTML放入CSS

[英]How do I put 3 color blocks HTML with CSS

I want my background to be 3 equally high blocks of color (say the German flag). 我希望我的背景是3个同样高的色块(例如德国国旗)。 I created an HTML file with header, main, and footer. 我创建了一个带有页眉,主要和页脚的HTML文件。 And then added the CSS below. 然后在下面添加CSS。 But this merely creates 3 small blocks at the top of the page. 但这仅在页面顶部创建3个小块。 I tried replacing min-height with height and max-height. 我尝试用height和max-height替换min-height。 No luck. 没运气。 I'm missing something elementary. 我缺少基本的东西。 What? 什么?

* {
  margin: 0;
  padding: 0;
}
body    {
    height: 100%;
}
header  {
    background-color: black;
    min-height: 33%;
}
main    {
    background-color: red;
    min-height: 33%;
}
footer  {
    background-color: gold;
    min-height: 33%;
}

The % of height doesn't work since the body doesn't have defined height. 高度的%不起作用,因为身体没有定义的高度。 HTML doesn't understand - 100% of what?. HTML无法理解-100%是什么?

You should define the body height in vh ( viewport height units , 1 vh = 1/100 of viewport height): 您应该以vh定义主体高度( 视口高度单位 ,1 vh =视口高度的1/100):

 * { margin: 0; padding: 0; } body { height: 100vh; } header { background-color: black; min-height: 33%; } main { background-color: red; min-height: 33%; } footer { background-color: gold; min-height: 33%; } 
 <header></header> <main></main> <footer></footer> 

CSS has limited support for getting the screen size. CSS对获取屏幕尺寸的支持有限。

Javascript on the other hand has the ability to do this and it is supported by all browsers as long as js is enabled for them. 另一方面,JavaScript可以执行此操作,并且只要所有浏览器都启用了js,它就受所有浏览器的支持。

<script>
function setBodyHeight(){
  document.body.style.height = window.innerHeight + "px";
}
setBodyHeight();
onresize = setBodyHeight;
</script>

add this to the bottom of the html body and your current css should work as expected. 将其添加到html主体的底部,您当前的CSS应该可以正常工作。

Learn flexbox from early on 尽早学习flexbox

.main {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: center;
 -ms-flex-pack: center;
justify-content: center;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
height: 300px;
}

You need a height declaration somewhere though, but other than this, check the pen 虽然您需要在某处声明高度,但是除此之外,请检查笔

http://codepen.io/damianocel/pen/VjxrWN http://codepen.io/damianocel/pen/VjxrWN

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

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