面试:常用上下固定中间自适应——flexbox 解决以及相关问题

flexbox 解决方案

html 部分

         <article class="top-center-bottom">
             <div class="top"></div>
             <div class="center">
                <h2> felxbox 解决方案</h2>
                <p>中间拉伸,顶部和底部固定</p>
                <p>中间拉伸,顶部和底部固定</p>
             </div>
             <div class="bottom"></div>
         </article>

css

        html * {
            padding: 0;
            margin: 0;
        }
        html,
        body,
        .top-center-bottom {
            height: 100%;
        }
        .top-center-bottom {
            display: flex;
            flex-direction: column;
        }
        .top-center-bottom .top {
            height: 100px;
            background-color: red;
        }
        .top-center-bottom .center {
            flex: 1;
            background-color: yellow;
        }
        .top-center-bottom .bottom {
            height: 100px;
            background-color: olivedrab;
        }

常见错误解析

1.在整个外面包了一层 div,如:

<div>
         <article class="top-center-bottom">
             <div class="top"></div>
             <div class="center">
                <h2> felxbox 解决方案</h2>
                <p>中间拉伸,顶部和底部固定</p>
                <p>中间拉伸,顶部和底部固定</p>
             </div>
             <div class="bottom"></div>
         </article>
</div>

原因解析:

在外层的div 是没有宽度高度的,需要靠的是子元素撑开,里面的 <article> 是的高度是 css 中设置的topbottom两个 100px 和 center 的高度。而center 的高度是根据内容撑开的。

这里延伸一个问题,在这里的的css 中的 flexbox 的设置到底有没有起作用呢?如果要修改的话该如何修改样式才能生效?欢迎评论处探讨。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,642评论 1 45
  • 学会使用CSS选择器熟记CSS样式和外观属性熟练掌握CSS各种选择器熟练掌握CSS各种选择器熟练掌握CSS三种显示...
    七彩小鹿阅读 6,332评论 2 66
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,330评论 0 11
  • 蓝蓝的天,暖暖的阳光洒在大地上,树上的鸟儿叽叽喳喳的歌唱,小花穿着一条花裙子,背着书包蹦蹦跳跳的去上学。 妈妈告诉...
    色妖妖阅读 315评论 0 1