Python Web开发实战:边玩Minecraft边学CSS定位

这堂课非常有趣,侯老师把上一堂课中的代码进行了简单修改,就成了minecraft中的三层砖墙:实在是用心良苦!这种方法讲解CSS样式的相对定位和绝对定位,让人印象深刻!

最终效果:

把一幅画挂在墙的正中

我的代码(html 部分):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Learn css with blocks</title>
        <link rel="stylesheet" href="block.css" media="screen" title="no title" charset="utf-8">
    </head>
    <body>
      <div class="bg">
            <div class="flower">
                <div class="point">

                </div>

            </div>
            <div class="block-1"></div>

            <div class="yellow-flower">
                  <div class="point">

                  </div>

            </div>

            <div class="block-2"></div>

            <div class="block-3"></div>
      </div>

    </body>
</html>

我的代码(CSS部分):

.block-1 {

        background: url('images/grass.png');
        background-size: contain;
        box-sizing:border-box;
        width: 64px;
        height:64px;

}

.block-2 {

        background: url('images/grass.png');
        background-size: contain;
        box-sizing:border-box;
        width: 128px;
        height:64px;

}

.block-3 {

        background: url('images/grass.png');
        background-size: contain;
        box-sizing:border-box;
        width: 192px;
        height:64px;

}

.flower {
        background: url('images/rose.png');
        background-size: contain;
        box-sizing:border-box;
        width: 64px;
        height:64px;
        position: relative;
        left: 64px;
        top: 64px;

}

.yellow-flower {
        background: url('images/flower.png');
        background-size: contain;
        box-sizing:border-box;
        width: 64px;
        height:64px;
        position: absolute;
        left: 128px;

}

.point  {

        width: 8px;
        height: 8px;
        background: rgb(194, 142, 28);
      }

.bg {

      background: rgb(18, 64, 227);
      width: 320px;
      height: 256px;
      position: absolute;
      left: 50%;
      top:50%;
      transform: translate(-50%,-50%);
      border: solid 8px yellow;
}

body {

  margin:0;
  background: url('images/brick.jpg');
  background-size: 150px 150px;
}

总结:

  1. 相对定位(position:relative;)可以使用偏移量(left:?px;top:?px;)实现定位;
  2. 绝对定位(position:absolute;)可以使用百分比来实现居中;
  3. 每个css语句后面的;千万不能少,会导致后面的语句失效;
  4. 使用Ctrl+/可以把选中的语句屏蔽掉(实际上是认为该语句是注释);
  5. 背景background可以使用颜色和图片{background: url('images/flower.png');}两种填充方式
  6. 使用background-size: 150px 150px;或者background-size: contain;来实现精细的背景(固定像素大小或者不缩放)。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,805评论 1 92
  • CSS基础 本文包括CSS基础知识选择器(重要!!!)继承、特殊性、层叠、重要性CSS格式化排版单位和值盒模型浮动...
    廖少少阅读 3,181评论 0 40
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,805评论 0 2
  • 本文主要是起笔记的作用,内容来自慕课网. 认识CSS样式 CSS全称为“层叠样式表 (Cascading Styl...
    0o冻僵的企鹅o0阅读 2,663评论 0 30
  • 看了《好好学习》,开始写反思日记,所以简书更新也慢了。相对于简书的书写,我觉得在我现在的阶段,反思日记对我作用更大...
    行动君阅读 4,692评论 0 6