前端面试之CSS相关

栏目: CSS · 发布时间: 6年前

内容简介:说实话,看到这道题,刚开始我是自信满满的,可算到后面自己就越来越不确定了,为什么会出这么简单的题呢?由此可见,自己的基本功真不扎实呀!为了展示,我加了两个背景,效果见
  1. 写出以下 % 分别有多少 px
<div class="box">
    <div class="box-item"></div>
</div>
<style>
    .box {
        position: relative;
        width: 1000px;
        height: 500px;
    }
    .box-item {
        position: absolute;
        top: 50%;
        bottom : 50%; 
        left: 50%;
        right: 50%;
        width: 50%;
        height: 50%;
        padding-top: 10%;
        padding-bottom: 10%;
        margin-right: 10%;
        margin-top: 10%;
    }
</style>

说实话,看到这道题,刚开始我是自信满满的,可算到后面自己就越来越不确定了,为什么会出这么简单的题呢?由此可见,自己的基本功真不扎实呀!为了展示,我加了两个背景,效果见 CodePen

解析

  1. 首先明确box-item块会相对于box块定位,并且box块是box-item块的包含块;
  2. top, bottom, left, right, width, height, padding, margin这些属性的值为 % 时,计算的规则如下:

    • top, bottom, height: 基于包含元素的高度;
    • left, right, width, padding-left, padding-right, margin-left, margin-right, padding-top , padding-bottom , margin-top , margin-bottom : 基于包含元素的宽度;
  3. 最容易混淆以致出错的就是 padding-top , padding-bottom , margin-top , margin-bottom ,也是本题的主要考察点: margin和padding四个方向的值为 % 时,都是基于包含元素的宽度计算的 ,一定要记住!

答案

<div class="box">
    <div class="box-item"></div>
</div>
<style>
    .box {
        position: relative;
        width: 1000px;
        height: 500px;
    }
    .box-item {
        position: absolute;
        top: 50%; /* 250px; */
        bottom : 50%; /* 250px; */
        left: 50%; /* 500px; */
        right: 50%; /* 500px; */
        width: 50%; /* 500px; */
        height: 50%; /* 250px; */
        padding-top: 10%; /* 100px; */
        padding-bottom: 10%; /* 100px; */
        margin-right: 10%; /* 100px; */
        margin-top: 10%; /* 100px; */
    }
</style>

以上所述就是小编给大家介绍的《前端面试之CSS相关》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax开发精要

Ajax开发精要

柯自聪 / 电子工业出版社 / 2006 / 45.00

书籍目录: 概念篇 第1章 Ajax介绍 2 1.1 Ajax的由来 2 1.2 Ajax的定义 3 1.3 Web应用程序的解决方案 5 1.4 Ajax的工作方式 7 1.5 小结 8 第2章 B/S请求响应机制与Web开发模式 9 2.1 HTTP请求响应模型 9 2.2 B/S架构的请求响应机......一起来看看 《Ajax开发精要》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试