CSS基础

2017-07-12  本文已影响0人  草木不语只深深绿

CSS语法

selector {property: value}
即 选择器{属性:值}
学习css即学些有哪些选择器,哪些属性以及可以使用什么样的值

CSS选择器

元素选择器 ,id选择器 ,类选择器

<style>
p{color:red;} 
</style>
<p>p元素</p>
<p>p元素</p>
<style>
p{color:red;}
#p1{color:blue;}
#p2{color:green;}
</style>
<p>没有id的p</p>
<p id="p1">id=p1的p</p>
<p id="p2">id=p2的p</p>
<style>
.pre{color:blue;}
.after{color:green;}
</style> 
<p class="pre">前3个</p>
<p class="pre">前3个</p>
<p class="pre">前3个</p> 
<p class="after">后3个</p>
<p class="after">后3个</p>
<p class="after">后3个</p>

CSS注释

注释 以/* 开始 以*/结束
被注释掉的文字会自动隐藏

CSS尺寸大小

属性:width
值:可以是百分比或者象素

<style>
p#percentage{
  width:50%;
  height:50%;
  background-color:pink;
}
p#pix{
  width:180px;
  height:50px;
  background-color:green;
}
</style>
<p id="percentage"> 按比例设置尺寸50%宽 50%高</p>
<p id="pix"> 按象素设置尺寸  180px宽 50 px高</p>

背景

<style>
p.gray {background-color: gray;}
h1 {background-color: transparent}
h2 {background-color: rgb(250,0,255)}
h3 {background-color: #00ff00}
</style>
<p class="gray">灰色</p>
<h1>透明背景,默认即透明背景</h1>
<h2>紫色</h2>
<h3>绿色背景</h3>
<style>
div#test
  {background-image:url(/study/background.jpg); /*图片路径*/
    width:200px;
    height:100px;}
</style> 
<div id="test">
  这是一个有背景图的DIV
</div>
<style>
div#norepeat
  {
    background-image:url(/study/background_small.jpg);
    width:200px;
    height:100px;
    background-repeat: no-repeat;
  } 
div#repeat-x
  {
    background-image:url(/study/background_small.jpg);
    width:200px;
    height:100px;
    background-repeat: repeat-x;
  }
</style> 
<div id="norepeat">
  背景不重复
</div>
<div id="repeat-x">
  背景水平重复
</div>
<style>
div#contain
  {
    background-image:url(/study/background_small.jpg);
    width:200px;
    height:100px;
    background-size: contain;
  }
</style>
<div id="contain">
   背景平铺,通过拉伸实现,会有失真
</div>
上一篇 下一篇

猜你喜欢

热点阅读