CSS拾遗+技巧

1、实现尖角符号。

这是内联inline-block标签独有的特性。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .a{
            border-top:30px solid #000000 ;
            border-bottom: 30px solid #2c78d5;
            border-left:30px solid #1dd537 ;
            border-right: 30px solid #ff3021;
            display: inline-block;
        }
        .b{
            border-top:30px solid #000000 ;
            /*border-bottom: 30px solid #2c78d5;*/
            border-left:30px solid #1dd537 ;
            border-right: 30px solid #ff3021;
            display: inline-block;
        }
        .c{
            border: 30px solid transparent;
            /*transparent为透明色*/
            margin-top:20px ;
            display: inline-block;
            border-top:30px solid #000000 ;
        }
        .c:hover{
            margin-top:-10px ;
            border: 30px solid transparent;
             border-bottom:30px solid #000000;
        }
        .bb{
            border-top:30px solid #000000 ;
            /*border-bottom: 30px solid #2c78d5;*/
            border-left:30px solid #1dd537 ;
            border-right: 30px solid #ff3021;
            display: inline-block;
        }
        /*bb为自己想的方法,不需要改变自身的位置即可。*/
        /*bb和b为长方形,其他为正方形*/
        .bb:hover{
            margin-top:15px ;
            border: 0;
            border-bottom: 30px solid #2c78d5;
            border-left:30px solid #1dd537 ;
            border-right: 30px solid #ff3021;
        }
        .a:hover{

        }
    </style>
</head>
<body>
    <div class="a"></div>
<div class="b"></div>
    <div class="bb"></div>
<div style="background-color: #ff3021;height: 70px">
    <div class="c"></div>
</div>
</body>
</html>

鼠标临幸前:

鼠标临幸后:

2、实现输入框最后有小图标

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .login{
 8             position: relative;
 9             /*父级标签 position为 relative时,子标签才会根据父级标签定位。否则一级一级找,找不到就根据body定位*/
10         }
11         .login input{
12             width: 170px;
13             padding-right: 20px;
14             /*达到输入到R处就不增长的效果*/
15             height: 30px;
16         }
17         .ren{
18             position: absolute;
19             /*根据父标签 来定位。*/
20             top: 8px;
21             left: 180px;
22         }
23     </style>
24 </head>
25 <body>
26 <div class="login">
27     <input type="text">
28     <span class="ren">R</span>
29 </div>
30 </body>
31 </html>

输入前:

输入后:

  文字不会超过“R”的位置,通过设置padding-right.

3、实现购物加减按钮

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .left{
            float: left;
        }
        /*公用left*/
        .w{
            width: 96px;
            border: 1px solid #ddd;
            height: 22px;
        }
        .jia{
            text-align: center;
            line-height: 22px;
            height: 22px;
            width: 22px;
            cursor: pointer;
        /*鼠标放上去时,变成小手*/
        }
        .text{
            height: 22px;
            width: 50px;
            padding: 0;
            border: 0;
            border-left: 1px solid #ddd ;
            border-right: 1px solid #ddd ;
        }
        /*输入框左右2边各1px边框。*/
    </style>
</head>
<body>
<div class="w">
    <div class="jia left">+</div>
        <input type="text" class="text left">
    <div class="jia left">-</div>
</div>
</body>
</html>

4、页面布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .top{
            height: 48px;
            width: 100%;
            background-color: #7d7d7d;
        }
        .left{
            position: absolute;
            top:48px;
            left: 0px;
            width: 180px;
            bottom: 0;
            background-color: #1dd537;
        }
        .right{
            position: absolute;
            top:48px;
            right: 0px;
            left: 183px;
            bottom: 0;
            background-color: #1c6a9e;
            overflow: auto;
            /*如果内容超过自动长度,就会生成一个滚动条*/
        }
    </style>
</head>
<body>
<div class="top">

</div>
<div class="left">
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>
<div class="right">
    <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1>
     <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1> <h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1><h1>333</h1>
</div>
</body>
</html>

这里最重要的就是:overflow: auto;如果内容超过自动长度,就会生成一个滚动条.

图上这个滚动条是属于蓝色背景的,非页面的滚动条

5、实现模态对话框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*分为3层 1、最底层内容  2、一个遮罩层 3、对话框层*/
        .zhezhao{
            background-color: rgba(0,0,0,0.4);
            position: fixed;
            top:0;
            left: 0;
            bottom: 0;
            right: 0;
            z-index: 2;
            /*优先级 比较低*/
        }
        .kuang{
            width: 350px;
            height: 250px;
            /*5行*/
            position: fixed;
            top:50%;
            left: 50%;
            margin-left: -175px;
            margin-top:-125px;
            /*这5行  实现了真正的居中,margin的是框长宽的一半*/
            background-color: white;
            z-index: 3;
            /*优先级 比较高 在最上边显示*/
        }
    </style>
</head>
<body>
    <div><h1>Mr很大的龙</h1></div>
<div class="zhezhao">
</div>
<div class="kuang"></div>
</body>
</html>

6、伪类选择器补充

伪类选择器 可以对别的标签样式修改,例如:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .touch{
            background-color: #1c6a9e;
            width: 300px;
            height: 300px;
            overflow: hidden;
            position: relative;
            /*用来给子标签提供定位参考*/
        }
        .touch .content{
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom:0;
            /*占满父级标签*/
            background:rgba(0,0,0,0.6);
            /*设置透明度*/
            color: white;
            text-align: center;
            visibility: hidden;
            /*隐藏起来*/
        }
        .touch:hover .content{
            visibility: visible;
        }
        /*当touch被鼠标临幸时,修改content为显示*/
        .touch .c1{
            font-size: 32px;
            /*padding: 60px 0;*/
            line-height: 300px;
        }
    </style>
</head>
<body>
    <div class="touch">
        <div class="img"><img src="http://pic.iqshw.com/d/file/qita/weixin/2016/07/04/74af10abec5420f74c6c172dab6969a4.jpg"></div>
        <div class="content c1">
            大龙
        </div>
    </div>
</body>
</html>

鼠标临幸前:

鼠标临幸后:

7、css代码保护样式不被修改(优先级)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .a{
            color: #ff3021 !important;
            /*当一句css代码 后边跟!important的时候,下边的普通代码改变不了*/
            font-size:50px ;
        }
        .b{
            color: #1dd537 ;
            /*试图改变颜色*/
        }
    </style>
</head>
<body>
<div class="a b">Mr很大的龙</div>
</body>
</html>

按照CSS执行顺序来说,文字应该是绿色,但文字为红色,因为被后边的“!important”保护

8、用position进行定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div style="height: 2000px;width: 1000px">
<div style="width: 500px;height: 200px;margin: 0 auto;background-color: #2c78d5">
    <!--有一定的宽度在设置margin:0 auto 可以让该标签左右居中-->
    <div style="width: 300px;height: 100px;margin: 0 auto;background-color:#Fc78d5;position: relative">
        <!--父级设置了relative-->
        <div style="width: 30px;height: 30px;margin: 0 auto;background-color:#f7fc42;position:absolute;bottom: 0;left: 0"></div>
        <!--子标签会根据最近的relative来进行定位-->
    </div>
</div>
<div style="width: 50px;height: 50px;margin: 0 auto;background-color: #1dd537;position: fixed;top: 30px;right: 50px;">
    <!--fixed 用来绝对定位,根据窗口来定位,滑动滚轮,位置也不会改变-->
</div>
</body>
</html>

当滚动条在最顶端时:

当滚动条在下边时:可以看到绿色块保持的位置是不变的。这就是position的 fixed属性

时间: 2024-08-25 15:36:12

CSS拾遗+技巧的相关文章

2天驾驭DIV+CSS (技巧篇)(转)

这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是技巧篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” 将你引入两大误区[知识三] 什么是W3C标准?[基础一] CSS如何控制页面[基础二] CSS选择器[基础三] CSS选择器命名及常用命名[基础四] 盒子模型[基础五] 块状元素和内联元素 实战篇[第一课] 实战小热身[第二课] 浮动[第三课] 清除浮动[第四课] 导航条(上) | 导航条(下)[大练

CSS小技巧

CSS 小技巧 总结的网络上的资源,以备自己后续参考... 使用:not()去除导航上不需要的边框 为body添加行高 垂直居中任何元素 逗号分离的列表 使用负nth-child选择元素 使用SVG图标 文本显示优化 在纯CSS幻灯片上使用max-height 继承box-sizing 表格单元格等宽 使用Flexbox摆脱边界Hack 使用属性选择器选择空链接 使用:not()添加/去除导航上不需要的边框 添加边框… /* 添加边框 */ .nav li { border-right: 1px

CSS hack技巧大全

CSS hack技巧大全[转]:http://www.duitang.com/static/csshack.html 常用CSS书写技巧和CSS HACK技巧[转]:http://www.cnblogs.com/pittzh/archive/2009/07/23/1529769.html

css使用技巧

1) 网站上经常会出现用户输入一大段字符和字母以至于文字无法正常折行,把版式破坏,这样我们就要参考以下样式:word-wrap:break-word; overflow:hidden; 当然必须得有宽度属性值. 2) 文字过长要出现省略号样式如下: white-space: nowrap; text-overflow:ellipsis; overflow:hidden; 3) 一个完整的表单应该包括: <fieldset><legend></legend><lab

Web之CSS开发技巧: CSS 居中大全

<center> text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:middle 垂直居中 inline 文字,inline 元素,配合 display:table ,display:table-cell,有奇效. line-height 与 height 联手,垂直居中文字 margin:auto 示例: <style> #ex2_container { width:200px; background

10个CSS简写技巧让你永远受用

转自:http://blog.bingo929.com/10-css-shorthand-tips.html 前言: CSS简写就是指将多行的CSS属性声明化成一行,又称为CSS代码优化.CSS简写的最大好处就是能够显著减少CSS文件的大小,其实还有很多其他益处.臃肿而杂乱的CSS样式表会使你遇到问题是难以调试.尤其是当一个团队在进行设计的时候,你的臃肿的CSS代码会使你的团队其他成员的工作效率下降. 今天,暴风彬彬整理了一些CSS简写技巧,它们其实是CSS最常用的写法,但是太多的人使用Drea

css兼容技巧

CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middle; 将行距增加到和整个DIV一样高 line-height:200px; 然后插入文字,就垂直居中了.缺点是要控制内容不要换行 http://www.php100.com 2. margin加倍的问题 设置为float的div在ie下设置的margin会加倍.这是一个ie6都存在的bug.解决方案

css sprite技巧详解

Css  sprite做为一个前端人员来说必须要掌握,但是这个对于刚学习的同学来说是由困难的,下面我就专门为这个内容做一次分享,这里没有网络图片地址,若内容显示不全面你可以到我的个人博客中?1. [代码][CSS]代码    CSSSprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢慢显示出来了.对于当前网络流行的速度而言,不高于200KB的单张图片的所需载

css 简写技巧

一些css  简写技巧 1.font 的简写  font 有可选选项和必选选项 如 font-style , font -weight , font-variant ,可写可不写, 但如果写的话必须前置 放到 font-size 和 font-family 前面. 而必选的  font-family 必须放到最后面 p{ font-style:italic; font-weight:bold; font-variant:small-caps; font-size :12px; font-fami