关于css:学习CSS行内样式

37次阅读

共计 4036 个字符,预计需要花费 11 分钟才能阅读完成。

一、CSS 在页面中能够帮忙用户实现如下工作:

  • 显隐特效:借助 CSS 的 display 和 visibility 实现。再联合渐隐、渐显和各种动画序列,能够设计出简单的视觉效果
  • 尺寸缩放:应用 CSS 的 width 和 height 实现。在变形动画中常见,如设计图像、栏目、页面开展、膨胀等;动态控制指标对象的大小,设计变动的页面布局等
  • 对象定位:应用 CSS 的 position、left、top、right、和 bottom 等,在位移动画中常见,如开发网页游戏
  • 视图管制:应用 CSS 的 clip、overflow、visibility,能够动静的管制页面对象的显示方式和显示内容
  • 通明设计:应用 CSS 的 opacity,个别与显隐、缩放、位移动画配合应用,加强渐进、渐弱成果
  • 对象成果:动静扭转字体款式、文本版式、网页布局版式等,设计图像特效等
  • UI 交互:配合 CSS+HTML,设计弱小性能的交互表格、交互表单,设计富裕变动的网页皮肤

二,引入 css 样式表的三种形式:

1)行内款式(内联款式)
概念:称行内款式、行间款式,是通过标签的 style 属性来设置元素的款式;

根底语法

< 标签名 style="属性 1:属性值 1;属性 2:属性值 2;"> 内容 </ 标签名 >

毛病:没有实现款式和构造的拆散

2)外部样式表(内嵌样式表)

是将 css 代码集中写在 HTML 文档的 head 头部标签中,并且用 style 标签定义;

根底语法:

<head>
    <style type="text/css">
        div{
            color: red;
            font-size: 12px;
        }
    </style>
</head>

毛病:没有彻底拆散

3)内部样式表(外链式)

概念:是将所有的款式放在一个或多个以.css 为扩展名的内部样式表文件中,通过 link 标签将内部样式表文件链接到 HTML 文档中;

根本语法:

<head>
    <link rel="stylesheet" type="text/css" href="css 文件门路">
</head>

留神:

  • link 是一个单标签;
  • link 标签要放在 head 头部标签中,并且指定 link 的三个属性;

属性:

  • rel:定义以后文档与被链接文档之间的关系,在这里须要指定为 ”stylesheet”,示意被链接的文档是一个样式表文件;
  • type:定义所链接文档的类型;
  • href:定义所有链接内部样式表文件的 url,能够是绝对路径也能够是相对路径;

毛病:须要引入

次要讲讲行内款式

三、操作行内款式;

CSS 的很多属性都时有复合名的,应用连字符“-”,如 border-right、margin-left 等,但在脚本中(js 代码)连字符会被定义为减号,会主动参数表达式的运算;解决这个问题 须要应用 小驼峰命名:borderRight、marginLeft 等

1,应用 style 对象:

elementNode.style.fontFamily="Arial,Helvetica,sans-serif";

elementNode.style.cssFloat="left";//float 是 js 中的保留字,须要应用 cssFloat 来示意 float 属性

elementNode.style.color="#ff0000";

elementNode.style.width="100px";// 单位不能脱漏

elementNode.style.top=top+"px";// 设置动静属性

2,应用 getPropertyValue()办法, 晚期 IE 不反对,但间接应用 elementNode.style.width 拜访款式属性:

var value=elementNode.style.getPropertyValue(propertyName)

例:

window.onload = function(){var  box = document.getElementById("box");// 获取 <div id="box">
var  width = box.style.getPropertyValue("width");
<=>var width = box.style.width
box.innerHTML = "盒子宽度:"+width;var marginLeft=box.style.getPropertyValue("margin-left"); // 须要连字符的款式属性
box.innerHTML = "盒子外边距:"+marginLeft;}

3,setProperty()办法:为指定元素设定款式; 不兼容晚期的 IE,elementNode.sytle.width=”500px”;

elementNode.setProperty(propertyName,value,priority); // 参数解释:属性名,属性值,是否设置!important 优先级
window.onload = function(){var  box = document.getElementById("box");// 获取 <div id="box">
box.style.setProperty("width","400px","");
box.style.setProperty("height","400px","");

// 兼容晚期的 IE
 box.style.width= "400px";
box.style.height = "400px";
}

4,removeProperty()办法:移除指定 CSS 的款式申明

 elementNode.removeProperty(propertyName)

5,item()办法: 返回 style 对象中指定索引地位的 CSS 属性名称

element.style.item(index);

6,getPropertyPriority()办法:获取指定的 css 属性中是否附加了!important,有则返回 important,无则返回空字符串

<div id="box" style="width: 100px;height: 100px;background-color: red;border: solid 50px blue;"></div>

window.onload = function(){// 不兼容 IE
                var box=document.getElementById("box");
                box.οnmοuseοver=function(){box.style.setProperty("background-color","blue","");
                    box.style.setProperty("border","solid 50px red","");
                }
                box.οnclick=function(){box.innerHTML=(box.style.item(0)+":"+box.style.getPropertyValue("width"));
                    box.innerHTML=box.innerHTML+"<br>"+(box.style.item(1)+":"+box.style.getPropertyValue("height"));
                }
                box.οnmοuseοut=function(){box.style.setProperty("background-color","red","");
                    box.style.setProperty("border","solid 50px blue","");
                }

 window.onload = function(){// 兼容 ie
                var box=document.getElementById("box");
                box.οnmοuseοver=function(){
                    box.style.backgroundColor="blue"
                    box.style.border="solid 50px red";
                }
                box.οnclick=function(){
                    box.innerHTML="width:"+box.style.width;
                    box.innerHTML=box.innerHTML+"<br>"+"height:"+box.style.height;
                }
                box.οnmοuseοut=function(){
                    box.style.backgroundColor="red";
                    box.style.border="solid 50px blue";
                }
            }

拓展:非 IE 浏览器反对 style 拜访,然而它无奈获取 style 对象中指定序号地位的属性名称,能够应用 cssText 属性获取全副的 style 属性值,通过 String 的 split()把字符串转为数组再操作;

应用 getAttribute(“style”) 办法,也是获取 style 的属性值,不过该办法放回值保留 style 属性值的原始模样,比方色彩应用的是 rgb,

而 cssText 的返回值可能会通过浏览器解决,且不同浏览器返回值 略有不同

<div id="box" style="width: 600px;height: 200px;background-color: #81F9A5;border: solid 2px blue;padding:10px;"></div>

 window.οnlοad=function(){var box=document.getElementById("box");
                  var str=box.style.cssText;// 获取全副 style 属性的字符串
               //var str=box.getAttribute("style");
                  var a=str.split(";");
                  var temp="";
                  for(var b in a){var prop=a[b].split(":");
                      if(prop[0]){temp+=b+":"+prop[0]+"="+prop[1]+"<br>";
                      }
                  }
                  box.innerHTML="box.style.cssText="+str;
                  box.innerHTML=box.innerHTML+"<br><br>"+temp;
              }

正文完
 0