英文:
Divide html in three parts with different heights
问题
以下是翻译好的部分:
我试图将页面分为三个不同高度的部分,分别是HEADER、CONTAINER和FOOTER。
HEADER占整体高度的10%,CONTAINER占70%,FOOTER占20%。
我尝试使用flex完成这个任务,但无法使用百分比。请帮忙。
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: space-between;
}
.header {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
height: 10%;
padding: 5px 10px;
border: 1px solid blue;
}
.container {
width: 100%;
height: 70%;
border: 1px solid red;
}
.footer {
width: 100%;
height: 20%;
border: 1px solid green;
}
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
希望这能帮助您。
英文:
I tried to divide the body into three parts with different sizes, HEADER, CONTAINER, and FOOTER.
Header with 10% of the height, a container with 70% of the height, and a footer with 20%.
I tried to complete this task with flex but I can't do it with percentages.
Please help.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: space-between;
}
.header {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
height: 10%;
padding: 5px 10px;
border: 1px solid blue;
}
.container {
width: 100%;
height: 70%;
border: 1px solid red;
}
.footer {
width: 100%;
height: 20%;
border: 1px solid green;
}
<!-- language: lang-html -->
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
<!-- end snippet -->
答案1
得分: 0
要实现这一效果,请使用CSS的flex属性,其中包括flex-base和flex-growth的值。请查看下面的代码片段:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
display: flex;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
padding: 5px 10px;
border: 1px solid blue;
flex: 0 0 10%;
}
.container {
border: 1px solid red;
flex: 1 0 70%;
}
.footer {
border: 1px solid green;
flex: 0 0 20%;
}
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
英文:
To achieve this use the CSS flex property with the values flex-base and flex-growth. See the code snippet down below
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
display: flex;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
padding: 5px 10px;
border: 1px solid blue;
flex: 0 0 10%;
}
.container {
border: 1px solid red;
flex: 1 0 70%;
}
.footer {
border: 1px solid green;
flex: 0 0 20%;
}
<!-- language: lang-html -->
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
<!-- end snippet -->
答案2
得分: 0
你可以使用 flex 属性
来修改你的代码以实现你想要的输出。
此外,我已添加 flex-shrink: 0;
以防止在视口宽度减小时页眉和页脚收缩。
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
display: flex;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
padding: 5px 10px;
border: 1px solid blue;
flex: 0 0 10%;
flex-shrink: 0;
}
.container {
border: 1px solid red;
flex: 1 0 70%;
}
.footer {
border: 1px solid green;
flex: 0 0 20%;
flex-shrink: 0;
}
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
英文:
You can modify your code with the flex property
to achieve your desired output.
In extra, I have added flex-shrink: 0;
to prevent the header and footer from shrinking when the viewport width is reduced.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: skyblue;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
display: flex;
justify-content: center;
align-items: center;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
background-color: black;
color: rgb(225, 150, 0);
font-weight: bold;
font-style: italic;
padding: 5px 10px;
border: 1px solid blue;
flex: 0 0 10%;
flex-shrink: 0;
}
.container {
border: 1px solid red;
flex: 1 0 70%;
}
.footer {
border: 1px solid green;
flex: 0 0 20%;
flex-shrink: 0;
}
<!-- language: lang-html -->
<div class="header">
<p>HEADER</p>
</div>
<div class="container">
<p>CONTAINER</p>
</div>
<div class="footer">
<p>FOOTER</p>
</div>
<!-- end snippet -->
答案3
得分: 0
以下是代码部分的中文翻译:
有两种实现方式,一种是使用网格(grid),第一种是使用弹性布局(flex):
第一种,使用网格:
/* 简单的重置,去除默认的外边距和内边距,并使用
box-sizing 以便所有浏览器使用相同的尺寸算法,
以便在声明的尺寸中包括填充和边框宽度: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* 在块轴上(块元素放置的轴)占用100个视口高度单位: */
block-size: 100vh;
/* 使用网格布局: */
display: grid;
/* 在相邻的网格项之间设置0.5rem的间隔: */
gap: 0.5rem;
/* 定义三行:第一行占10%,第三行占20%,
第二行占所有剩余空间的1份: */
grid-template-rows: 10% 1fr 20%;
}
section > * {
/* 对演示无关紧要,只是为了将网格项的内容居中: */
display: grid;
place-content: center;
}
/* 对演示无关紧要,只是为了给各个元素上色: */
header {
background-color: hsl(50deg 90% 70%);
}
main {
background-color: hsl(100deg 90% 70%);
}
footer {
background-color: hsl(150deg 90% 70%);
}
第二种,使用弹性布局:
/* 简单的重置 - 如上所述 - 去除默认的外边距和
内边距,并将box-sizing包括在声明的尺寸中: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* 将<section>的尺寸设置为占用100个视口高度单位: */
block-size: 100vh;
/* 使用弹性布局: */
display: flex;
/* 将弹性布局的主轴设置为纵向: */
flex-direction: column;
/* 在相邻的弹性项之间设置0.5rem的间隔: */
gap: 0.5rem;
}
/* 对演示无关紧要,只是为了将弹性项的内容居中: */
section > * {
display: grid;
place-content: center;
}
header {
background-color: hsl(50deg 90% 70%);
/* 将flex-basis(定义元素的基本尺寸)设置为父元素尺寸的10%: */
flex-basis: 10%;
}
main {
background-color: hsl(100deg 90% 70%);
/* 指定该元素应该增长以占用可用空间: */
flex-grow: 1;
}
footer {
background-color: hsl(150deg 90% 70%);
/* 将flex-basis设置为父元素尺寸的20%: */
flex-basis: 20%;
}
如果您需要更多翻译,请告诉我。
英文:
There are two ways of achieving this, with grid and flex; first, with grid:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* a simple reset, to remove default margin and padding, and using
box-sizing to have all browsers use the same sizing algorithm,
in order to have padding and border widths included within the
declared sizing: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* taking 100 units of viewport-height on the block axis (the
axis on which block elements are placed): */
block-size: 100vh;
/* using grid layout: */
display: grid;
/* setting a 0.5rem gap between adjacent grid-items: */
gap: 0.5rem;
/* defining the three rows: the first 10%, the third 20% and
the second taking 1 fraction of all the remaining space: */
grid-template-rows: 10% 1fr 20%;
}
section > * {
/* irrelevant to demo, but is just to center the content of
the grid-items: */
display: grid;
place-content: center;
}
/* irrelevant to the demo, just to colour the various elements: */
header {
background-color: hsl(50deg 90% 70%);
}
main {
background-color: hsl(100deg 90% 70%);
}
footer {
background-color: hsl(150deg 90% 70%);
}
<!-- language: lang-html -->
<section>
<header>
<p>Heading</p>
</header>
<main>
<p>Main content</p>
</main>
<footer>
<p>Footer</p>
</footer>
</section>
<!-- end snippet -->
And, second, using flex:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* simple reset - as above - to remove default margins and
padding, and to have box-sizing include the padding and
borders in the declared sizes: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* sizing the <section> to take 100 viewport-height units: */
block-size: 100vh;
/* using flex layout: */
display: flex;
/* setting the main axis of the flex layout to columnnar: */
flex-direction: column;
/* setting a 0.5rem gap between adjacent flex-items: */
gap: 0.5rem;
}
/* irrelevant to the demo, just centering the content of the
flex-items: */
section > * {
display: grid;
place-content: center;
}
header {
background-color: hsl(50deg 90% 70%);
/* setting the flex-basis - that defines the base-size
of the element - to 10% of its parent's size: */
flex-basis: 10%;
}
main {
background-color: hsl(100deg 90% 70%);
/* specifying that the element should grow to take
available space: */
flex-grow: 1;
}
footer {
background-color: hsl(150deg 90% 70%);
/* setting the flex-basis to 20% of the parent's size: */
flex-basis: 20%;
}
<!-- language: lang-html -->
<section>
<header>
<p>Heading</p>
</header>
<main>
<p>Main content</p>
</main>
<footer>
<p>Footer</p>
</footer>
</section>
<!-- end snippet -->
I feel it's worth pointing out that – despite your stated requirements – you don't really want to use those sizes as declared. A layout in which the elements are sized according to percentage can be great, but it also exposes the possibility of the content being displayed at the extremes, from ridiculously small (phones, watches) to ridiculously large (large screen displays).
With that said, I'd like to offer a slight revision of the above, taking advantage of the clamp()
function; this allows us to set a "preferred" size, but with a specified minimum, and maximum:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* a simple reset, to remove default margin and padding, and using
box-sizing to have all browsers use the same sizing algorithm,
in order to have padding and border widths included within the
declared sizing: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* taking 100 units of viewport-height on the block axis (the
axis on which block elements are placed): */
block-size: 100vh;
/* using grid layout: */
display: grid;
/* setting a 0.5rem gap between adjacent grid-items: */
gap: 0.5rem;
/* defining the three rows,
row 1: a minimum size of 4rem, a maximum size of 150px, with a
preferred size of 10% (as specified in the question):,
row 2: as before, set to take all available space once the
elements with a specific defined size have been laid out,
row 3: a mimum size of 8rem, a maximum of 250px, and a preferred
size of 20%: */
grid-template-rows: clamp(4rem, 10%, 150px) 1fr clamp(8rem, 20%, 250px);
}
section > * {
/* irrelevant to demo, but is just to center the content of
the grid-items: */
display: grid;
place-content: center;
}
/* irrelevant to the demo, just to colour the various elements: */
header {
background-color: hsl(50deg 90% 70%);
}
main {
background-color: hsl(100deg 90% 70%);
}
footer {
background-color: hsl(150deg 90% 70%);
}
<!-- language: lang-html -->
<section>
<header>
<p>Heading</p>
</header>
<main>
<p>Main content</p>
</main>
<footer>
<p>Footer</p>
</footer>
</section>
<!-- end snippet -->
And, second, using flex:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* simple reset - as above - to remove default margins and
padding, and to have box-sizing include the padding and
borders in the declared sizes: */
*,::before,::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
/* sizing the <section> to take 100 viewport-height units: */
block-size: 100vh;
/* using flex layout: */
display: flex;
/* setting the main axis of the flex layout to columnnar: */
flex-direction: column;
/* setting a 0.5rem gap between adjacent flex-items: */
gap: 0.5rem;
}
/* irrelevant to the demo, just centering the content of the
flex-items: */
section > * {
display: grid;
place-content: center;
}
header {
background-color: hsl(50deg 90% 70%);
/* setting the flex-basis to a minimum of 4rem, a maximum of
150px, with a preferred size of 10% within those constraints: */
flex-basis: clamp(4rem, 10%, 150px);
}
main {
background-color: hsl(100deg 90% 70%);
/* specifying that the element should grow to take
available space: */
flex-grow: 1;
}
footer {
background-color: hsl(150deg 90% 70%);
/* setting the flex-basis to a minimum of 8rem, a maximum of
250px, with a preferred size of 20% within those constraints: */
flex-basis: clamp(8rem, 20%, 250px);
}
<!-- language: lang-html -->
<section>
<header>
<p>Heading</p>
</header>
<main>
<p>Main content</p>
</main>
<footer>
<p>Footer</p>
</footer>
</section>
<!-- end snippet -->
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论