弹性盒子布局实例

弹性盒子布局实例

1.1 基本网络布局

布局效果:
在这里插入图片描述
.html文件中代码如下:

    <div class="dd">
        <div class="d1">1/2</div>
        <div class="d1">1/2</div>
    </div>
    <div class="dd">
        <div class="d1">1/3</div>
        <div class="d1">1/3</div>
        <div class="d1">1/3</div>
    </div>
    <div class="dd">
        <div class="d1">1/4</div>
        <div class="d1">1/4</div>
        <div class="d1">1/4</div>
        <div class="d1">1/4</div>
    </div>
    <div class="dd">
        <div class="d2">......</div>
        <div class="d2">......</div>
    </div>

CSS样式如下:

.dd {
    /* 设置为弹性盒子 */
    display: flex;
    /* 设置文字居中对齐 */
    text-align: center;
    /* 设置盒子到顶部的外边距距离为10像素 */
    margin-top: 10px;
}
.d1, .d2 {
    /* 设置项目放大比例,都同时放大 */
    flex-grow: 1;
    /* 设置项目缩小比例,当空间不足时都同时缩小 */
    flex-shrink: 1;
    /* 设置背景颜色为粉色 */
    background-color: pink;
    /* 设置每个项目的左外边距距离为8像素 */
    margin-left: 8px;
    ;
}
.d2 {
    /* 设置高度为200像素 */
    height: 200px;
}

1.2 圣杯布局

指的是一种最常见的网站布局。页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分成三栏,从左到右为:导航、主栏、副栏。

布局效果:
在这里插入图片描述
.html文件中代码如下:

<body class="grail">
    <header>#header</header>
    <div class="grail-body">
        <main class="grail-content">#center</main>
        <nav class="grail-nav">#side</nav>
        <aside class="grail-aside">#right</aside>
    </div>
    <footer>#footer</footer>
</body>

CSS样式如下:

.grail {
    /* 设置弹性盒子 */
    display: flex;
    /* 排列方向从上至下 */
    flex-direction: column;
    /* 设置最低高度 vh视窗高度的百分比 视窗高度为96% */
    min-height: 96vh;
}
header, footer {
    /* 设置自动放大和缩小 */
    flex-grow: 1;
    flex-shrink: 1;
    /* 设置背景颜色为紫色 */
    background-color: plum;
    /* 设置文字自动居中 */
    text-align: center;
    /* 设置文字行高11vh */
    line-height: 11vh;
}
.grail-body {
    /* 设置弹性盒子布局 */
    display: flex;
    /* 设置自动放大 */
    flex-grow: 1;
    /* 设置自动缩小 */
    flex-shrink: 1;
    text-align: center;
    /* 设置文字行高 */
    line-height: 75vh;
}
.grail-content {
    flex-grow: 1;
    flex-shrink: 1;
    background-color: powderblue;
}
.grail-nav, .grail-aside {
    /* 设置两边宽度为12em */
    flex: 0 0 12em;
    height: 75vh;
}
.grail-nav {
    background-color: peachpuff;
    /* 设置顺序在最左边 */
    order: -1;
}
.grail-aside {
    background-color: pink;
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些简单的代码示例和说明来帮助您完成这个任务。 首先,我们需要创建一个HTML文件,包含主页和至少2个子页面的链接。例如: ```html <!DOCTYPE html> <html> <head> <title>My Website</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header> <main> <!-- 主页内容 --> </main> <footer> <p>© 2021 My Website</p> </footer> </body> </html> ``` 接下来,我们需要编写CSS样式来设计我们的页面。我们可以使用浮动布局、定位布局弹性盒子来实现我们的设计。 以下是一个使用浮动布局的示例: ```css header { background-color: #333; color: #fff; padding: 20px; } nav ul { list-style: none; margin: 0; padding: 0; } nav li { float: left; margin-right: 20px; } nav a { color: #fff; text-decoration: none; } main { float: left; width: 70%; padding: 20px; } aside { float: right; width: 30%; padding: 20px; } footer { background-color: #333; color: #fff; padding: 20px; clear: both; } ``` 在这个示例中,我们使用了浮动布局来将导航和内容区域分别放置在左侧和右侧。我们还使用了清除浮动来确保页脚在页面的底部。 如果您想使用定位布局,可以使用`position`属性和`top`、`right`、`bottom`、`left`属性来定位元素。以下是一个使用定位布局的示例: ```css header { background-color: #333; color: #fff; padding: 20px; position: fixed; top: 0; left: 0; width: 100%; } main { position: absolute; top: 100px; left: 0; width: 70%; padding: 20px; } aside { position: absolute; top: 100px; right: 0; width: 30%; padding: 20px; } footer { background-color: #333; color: #fff; padding: 20px; position: absolute; bottom: 0; left: 0; width: 100%; } ``` 在这个示例中,我们将页眉固定在页面的顶部,并使用绝对定位将主内容区域和侧边栏定位在页面的左侧和右侧。我们还将页脚定位在页面的底部。 最后,如果您想使用弹性盒子布局,可以使用`display: flex`属性来定义容器,并使用`flex-direction`、`justify-content`、`align-items`等属性来控制元素的排列方式。以下是一个使用弹性盒子布局的示例: ```css body { display: flex; flex-direction: column; min-height: 100vh; } header { background-color: #333; color: #fff; padding: 20px; } nav ul { list-style: none; margin: 0; padding: 0; display: flex; } nav li { margin-right: 20px; } nav a { color: #fff; text-decoration: none; } main { flex-grow: 1; display: flex; } article { flex-basis: 70%; padding: 20px; } aside { flex-basis: 30%; padding: 20px; } footer { background-color: #333; color: #fff; padding: 20px; } ``` 在这个示例中,我们使用弹性盒子布局将页面分为头部、主内容区域和页脚。我们还使用了`flex-grow`和`flex-basis`属性来控制内容区域和侧边栏的宽度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值