css怎么用margin和paddingcss中的margin属性

如题所述

在css中使用边距和填充

举个例子。

比如两幅画。

从绘画到框架的距离是填充。

框架带有边框。

两幅画之间的距离是空白的

css设置列表项之间的距离?

列表项的间距可以通过margin或padding值设置

css怎么设置表格单元格的边距与填充?

直接设置表格的tabletd

padding就是填充

margin就是边距

display:block;加上这个,让td获得p的特性,使其支持margin

不过设置的margin是直接在单元格设置出来的两倍

你理解下,不明白我再解释

css样式中如何设置所有标签属性?

第一种方法:body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}第二种方法:*{margin:0;padding:0}

css浏览器区域设置?

css设置自适应浏览器

做页面,经常遇到,在调整浏览器大小的时候,想要实现内容自适应浏览器宽度或者高度,可以使用js处理:

jQuery(window).resize(function(){

jQuery('#contextDiv').width(jQuery(document).width());

});

jQuery(window).resize();

整理获得浏览器宽度和高度的方法:

$j(window).height();//浏览器窗口可视区域高度

$j(window).width());//浏览器窗口可视区域宽度

$j(document).height());//浏览器窗口文档的高度

$j(document).width());//浏览器窗口文档对于象宽度

$j(document.body).width());//浏览器窗口文档body的高度

$j(document.body).height());//浏览器窗口文档body的高度

$j(document.body).outerHeight(true));//浏览器窗口文档body的总高度包括borderpaddingmargin

$j(document.body).outerWidth(true));//浏览器窗口文档body的总宽度包括borderpaddingmargin

$j(document).scrollTop());//获取滚动条到顶部的垂直高度

$j(document).scrollLeft());//获取滚动条到左边的垂直宽度

如何使用html5与css3完成google涂鸦动画?

知道如何使用CSS3动画比知道如何使用canvas>元素更重要:因为浏览器能够优化那些元素的性能(通常是他们的样式,比如CSS),而我们使用canvas自定义画出来的效果却不能被优化。原因又在于,浏览器使用的硬件主要取决于显卡的能力。目前,浏览器没有给予我们直接访问显卡的权力,比如,每一个绘画操作都不得不在浏览器中先调用某些函数。1.canvashtml代码:

代码如下:

html>head>metacharset="UTF-8"/>title>AnimationinHTML5usingthecanvaselement/title>/head>bodyonload="init();">canvasid="canvas"width="1000"height="600">Yourbrowserdoesnotsupportthecode>canvas>/code>-element.Pleasethinkaboutupdatingyourbrower!/canvas>pid="controls">buttontype="button"onclick="speed(-0.1);">Slower/button>buttontype="button"onclick="play(this);">Play/button>buttontype="button"onclick="speed(+0.1)">Faster/button>/p>/body>/html>

js代码:定义一些变量:

代码如下:

vardx=5,//当前速率rate=1,//当前播放速度ani,//当前动画循环c,//画图(CanvasContext)w,//汽车(CanvasContext)grassHeight=130,//背景高度carAlpha=0,//轮胎的旋转角度carX=-400,//x轴方向上汽车的位置(将被改变)carY=300,//y轴方向上汽车的位置(将保持为常量)carWidth=400,//汽车的宽度carHeight=130,//汽车的高度tiresDelta=15,//从一个轮胎到最接近的汽车底盘的距离axisDelta=20,//汽车底部底盘的轴与轮胎的距离radius=60;//轮胎的半径

为了实例化汽车canvas(初始时被隐藏),我们使用下面的自执行的匿名函数

代码如下:

(function(){varcar=document.createElement('canvas');//创建元素car.height=carHeight+axisDelta+radius;//设置高度car.width=carWidth;//设置宽度w=car.getContext('2d');})();

点击“Play”按钮,通过定时重复执行“画汽车”操作,来模拟“帧播放”功能:

代码如下:

functionplay(s){//参数s是一个buttonif(ani){//如果ani不为null,则代表我们当前已经有了一个动画clearInterval(ani);//所以我们需要清除它(停止动画)ani=null;s.innerHTML='Play';//重命名该按钮为“播放”}else{ani=setInterval(drawCanvas,40);//我们将设置动画为25fps,40/1000,即为二十五分之一s.innerHTML='Pause';//重命名该按钮为“暂停”}}

加速,减速,通过以下方法,改变移动距离的大小来实现:

代码如下:

functionspeed(delta){varnewRate=Math.max(rate+delta,0.1);dx=newRate/rate*dx;rate=newRate;}页面加载的初始化方法://initfunctioninit(){c=document.getElementById('canvas').getContext('2d');drawCanvas();}

主调方法:

代码如下:

functiondrawCanvas(){c.clearRect(0,0,c.canvas.width,c.canvas.height);//清除Canvas(已显示的),避免产生错误c.save();//保存当前坐标值以及状态,对应的类似“push”操作drawGrass();//画背景c.translate(carX,0);//移动起点坐标drawCar();//画汽车(隐藏的canvas)c.drawImage(w.canvas,0,carY);//画最终显示的汽车c.restore();//恢复Canvas的状态,对应的是类似“pop”操作carX+=dx;//重置汽车在X轴方向的位置,以模拟向前走carAlpha+=dx/radius;//按比例增加轮胎角度if(carX>c.canvas.width){//设置某些定期的边界条件carX=-carWidth-10;//也可以将速度反向为dx*=-1;}}

画背景:

代码如下:

functiondrawGrass(){//创建线性渐变,前两个参数为渐变开始点坐标,后两个为渐变结束点坐标vargrad=c.createLinearGradient(0,c.canvas.height-grassHeight,0,c.canvas.height);//为线性渐变指定渐变色,0表示渐变起始色,1表示渐变终止色grad.addColorStop(0,'#33CC00');grad.addColorStop(1,'#66FF22');c.fillStyle=grad;c.lineWidth=0;c.fillRect(0,c.canvas.height-grassHeight,c.canvas.width,grassHeight);}

画车身:

代码如下:

functiondrawCar(){w.clearRect(0,0,w.canvas.width,w.canvas.height);//清空隐藏的画板w.strokeStyle='#FF6600';//设置边框色w.lineWidth=2;//设置边框的宽度,单位为像素w.fillStyle='#FF9900';//设置填充色w.beginPath();//开始绘制新路径w.rect(0,0,carWidth,carHeight);//绘制一个矩形w.stroke();//画边框w.fill();//填充背景w.closePath();//关闭绘制的新路径drawTire(tiresDelta+radius,carHeight+axisDelta);//我们开始画第一个轮子drawTire(carWidth-tiresDelta-radius,carHeight+axisDelta);//同样的,第二个}

画轮胎:

代码如下:

functiondrawTire(x,y){w.save();w.translate(x,y);w.rotate(carAlpha);w.strokeStyle='#3300FF';w.lineWidth=1;w.fillStyle='#0099FF';w.beginPath();w.arc(0,0,radius,0,2*Math.PI,false);w.fill();w.closePath();w.beginPath();w.moveTo(radius,0);w.lineTo(-radius,0);w.stroke();w.closePath();w.beginPath();w.moveTo(0,radius);w.lineTo(0,-radius);w.stroke();w.closePath();w.restore();}

由于原理简单,并且代码中作了详细注释,这里就不一一讲解!2.CSS3你将看到我们未通过一句JS代码就完全实现了和上面一样的动画效果:HTML代码:

代码如下:

html>head>metacharset="UTF-8"/>title>AnimationsinHTML5usingCSS3animations/title>/head>body>pid="container">pid="car">pid="chassis">/p>pid="backtire">p>/p>p>/p>/p>pid="fronttire">p>/p>p>/p>/p>/p>pid="grass">/p>/p>footer>/footer>/body>/html>CSS代码:body{padding:0;margin:0;}

定义车身与轮胎转到的动画(你会看到基本每一个动画都有四个版本的定义:原生版本/webkit【Chrome|Safari】/ms【为了向后兼容IE10】/moz【FireFox】)

代码如下:

/*定义动画:从-400px的位置移动到1600px的位置*/@keyframescarAnimation{0%{left:-400px;}/*指定初始位置,0%等同于from*/100%{left:1600px;}/*指定最终位置,100%等同于to*/}/*SafariandChrome*/@-webkit-keyframescarAnimation{0%{left:-400px;}100%{left:1600px;}}/*Firefox*/@-moz-keyframescarAnimation{0%{left:-400;}100%{left:1600px;}}/*IE暂不支持,此处定义是为了向后兼容IE10*/@-ms-keyframescarAnimation{0%{left:-400px;}100%{left:1600px;}}@keyframestyreAnimation{0%{transform:rotate(0);}100%{transform:rotate(1800deg);}}@-webkit-keyframestyreAnimation{0%{-webkit-transform:rotate(0);}100%{-webkit-transform:rotate(1800deg);}}@-moz-keyframestyreAnimation{0%{-moz-transform:rotate(0);}100%{-moz-transform:rotate(1800deg);}}@-ms-keyframestyreAnimation{0%{-ms-transform:rotate(0);}100%{-ms-transform:rotate(1800deg);}}#container{position:relative;width:100%;height:600px;overflow:hidden;/*这个很重要*/}#car{position:absolute;/*汽车在容器中采用绝对定位*/width:400px;height:210px;/*汽车的总高度,包括轮胎和底盘*/z-index:1;/*让汽车在背景的上方*/top:300px;/*距顶端的距离(y轴)*/left:50px;/*距左侧的距离(x轴)*//*以下内容赋予该元素预先定义的动画及相关属性*/-webkit-animation-name:carAnimation;/*名称*/-webkit-animation-duration:10s;/*持续时间*/-webkit-animation-iteration-count:infinite;/*迭代次数-无限次*/-webkit-animation-timing-function:linear;/*播放动画时从头到尾都以相同的速度*/-moz-animation-name:carAnimation;/*名称*/-moz-animation-duration:10s;/*持续时间*/-moz-animation-iteration-count:infinite;/*迭代次数-无限次*/-moz-animation-timing-function:linear;/*播放动画时从头到尾都以相同的速度*/-ms-animation-name:carAnimation;/*名称*/-ms-animation-duration:10s;/*持续时间*/-ms-animation-iteration-count:infinite;/*迭代次数-无限次*/-ms-animation-timing-function:linear;/*播放动画时从头到尾都以相同的速度*/animation-name:carAnimation;/*名称*/animation-duration:10s;/*持续时间*/animation-iteration-count:infinite;/*迭代次数-无限次*/animation-timing-function:linear;/*播放动画时从头到尾都以相同的速度*/}/*车身*/#chassis{position:absolute;width:400px;height:130px;background:#FF9900;border:2pxsolid#FF6600;}/*轮胎*/.tire{z-index:1;/*同上,轮胎也应置于背景的上方*/position:absolute;bottom:0;border-radius:60px;/*圆半径*/height:120px;/*2*radius=height*/width:120px;/*2*radius=width*/background:#0099FF;/*填充色*/border:1pxsolid#3300FF;-webkit-animation-name:tyreAnimation;-webkit-animation-duration:10s;-webkit-animation-iteration-count:infinite;-webkit-animation-timing-function:linear;-moz-animation-name:tyreAnimation;-moz-animation-duration:10s;-moz-animation-iteration-count:infinite;-moz-animation-timing-function:linear;-ms-animation-name:tyreAnimation;-ms-animation-duration:10s;-ms-animation-iteration-count:infinite;-ms-animation-timing-function:linear;animation-name:tyreAnimation;animation-duration:10s;animation-iteration-count:infinite;animation-timing-function:linear;}#fronttire{right:20px;/*设置右边的轮胎距离边缘的距离为20*/}#backtire{left:20px;/*设置左边的轮胎距离边缘的距离为20*/}#grass{position:absolute;/*背景绝对定位在容器中*/width:100%;height:130px;bottom:0;/*让背景色线性渐变,bottom,表示渐变的起始处,第一个颜色值是渐变的起始值,第二个颜色值是终止值*/background:linear-grdaient(bottom,#33CC00,#66FF22);background:-webkit-linear-gradient(bottom,#33CC00,#66FF22);background:-moz-linear-gradient(bottom,#33CC00,#66FF22);background:-ms-linear-gradient(bottom,#33CC00,#66FF22);}.hr,.vr{position:absolute;background:#3300FF;}.hr{height:1px;width:100%;/*轮胎的水平线*/left:0;top:60px;}.vr{width:1px;height:100%;/*轮胎的垂直线*/left:60px;top:0;}

3.JQuery与CSS3这是一个效果与兼容性俱佳的方式(特别对于IE9暂不支持CSS3而言)HTML代码(可以看到与CSS3中的HTML代码并无不同):

代码如下:

html>head>metacharset="UTF-8"/>title>AnimationsinHTML5usingCSS3animations/title>/head>body>pid="container">pid="car">pid="chassis">/p>pid="backtire">p>/p>p>/p>/p>pid="fronttire">p>/p>p>/p>/p>/p>pid="grass">/p>/p>footer>/footer>/body>/html>CSS:style>body{padding:0;margin:0;}#container{position:relative;width:100%;height:600px;overflow:hidden;/*这个很重要*/}#car{position:absolute;/*汽车在容器中采用绝对定位*/width:400px;height:210px;/*汽车的总高度,包括轮胎和底盘*/z-index:1;/*让汽车在背景的上方*/top:300px;/*距顶端的距离(y轴)*/left:50px;/*距左侧的距离(x轴)*/}/*车身*/#chassis{position:absolute;width:400px;height:130px;background:#FF9900;border:2pxsolid#FF6600;}/*轮胎*/.tire{z-index:1;/*同上,轮胎也应置于背景的上方*/position:absolute;bottom:0;border-radius:60px;/*圆半径*/height:120px;/*2*radius=height*/width:120px;/*2*radius=width*/background:#0099FF;/*填充色*/border:1pxsolid#3300FF;-o-transform:rotate(0deg);/*旋转(单位:度)*/-ms-transform:rotate(0deg);-webkit-transform:rotate(0deg);-moz-transform:rotate(0deg);}#fronttire{right:20px;/*设置右边的轮胎距离边缘的距离为20*/}#backtire{left:20px;/*设置左边的轮胎距离边缘的距离为20*/}#grass{position:absolute;/*背景绝对定位在容器中*/width:100%;height:130px;bottom:0;/*让背景色线性渐变,bottom,表示渐变的起始处,第一个颜色值是渐变的起始值,第二个颜色值是终止值*/background:linear-grdaient(bottom,#33CC00,#66FF22);background:-webkit-linear-gradient(bottom,#33CC00,#66FF22);background:-moz-linear-gradient(bottom,#33CC00,#66FF22);background:-ms-linear-gradient(bottom,#33CC00,#66FF22);}.hr,.vr{position:absolute;background:#3300FF;}.hr{

温馨提示:答案为网友推荐,仅供参考