html+css制作任意大小边框一致的面板



html+css制作任意大小边框一致的面板

我想制作一个通用的面板框用html+css,由于需要圆角效果,但是不是所有浏览器都支持,想支持的话需要牵连出别的东西,

指导老师的给出方案:

面板四个角分别是独立的 div用来存放四个方向不同的角背景,上下左右的边框是由一个背景图通过background-repeat来制造出任意长度的横线和竖线。

面板中央是内容主体,这个时候只要设定整个box的宽高即可得到任意大小的边框一致的面板。

优势:只需要精确的切一组图片,切浏览器加载这一组图片就可以应付所有面板,

所有的同一风格面板只需要copy一下代码,设置一下宽高即可

劣势:冗余代码稍稍多了点(应该可以优化到可以忽略不记)

大部分网站的方案:

每一个面板都有独立的背景图,背景图宽高固定了,这个没什么可说的,做N张图就行了。

优势:省代码


劣势:切N张图耗费时间,浏览器加载N张图耗费时间

上代码

html

  1. <div class=”jdbox” style=”width:490px;height:320px;margin-top:10px;margin-right:10px;”>
  2.     <div class=”boxtop”>
  3.         <div class=”ltcorner”></div>
  4.         <div class=”lrline”></div>
  5.         <div class=”rtcorner”></div>
  6.     </div>
  7.     <div class=”boxcenter”>
  8.         <div class=”ltbline”></div>
  9.         <div class=”content”>
  10.         </div>
  11.         <div class=”rtbline”></div>
  12.     </div>
  13.     <div class=”boxbottom”>
  14.         <div class=”lbcorner”></div>
  15.         <div class=”lrline”></div>
  16.         <div class=”rbcorner”></div>
  17.     </div>
  18. </div>
<div style="width:490px;height:320px;margin-top:10px;margin-right:10px;">
	<div>
		<div></div>
		<div></div>
		<div></div>
	</div>
	<div>
		<div></div>
		<div>
		</div>
		<div></div>
	</div>
	<div>
		<div></div>
		<div></div>
		<div></div>
	</div>
</div>

css 

 

  1. .jdbox{
  2.     display: block;
  3.     background-color: #eee;
  4.     position: relative;
  5.     float: left;
  6. }
  7. .jdbox .boxtop{
  8.     width: 100%;
  9.     height: 10px;
  10.     background-color: #aaa;
  11.     position: absolute;
  12.     top:0px;
  13. }
  14. .jdbox .boxtop .ltcorner,
  15. .jdbox .boxbottom .ltcorner
  16. {
  17.     width: 10px;
  18.     height: 10px;
  19.     background-color: #bbb;
  20.     position: absolute;
  21.     top: 0px;
  22.     left: 0px;
  23. }
  24. .jdbox .boxtop .lrline,
  25. .jdbox .boxbottom .lrline
  26. {
  27.     width: auto;
  28.     height: 10px;
  29.     background-color: #ccc;
  30.     position: absolute;
  31.     top: 0px;
  32.     left: 10px;
  33.     right: 10px;
  34. }
  35. .jdbox .boxtop .rtcorner,
  36. .jdbox .boxbottom .rtcorner
  37. {
  38.     width: 10px;
  39.     height: 10px;
  40.     background-color: #bbb;
  41.     position: absolute;
  42.     top: 0px;
  43.     right: 0px;
  44. }
  45. .jdbox .boxcenter{
  46.     width: 100%;
  47.     height: auto;
  48.     position: absolute;
  49.     top: 10px;
  50.     bottom: 10px;
  51. }
  52. .jdbox .boxcenter .ltbline{
  53.     width: 10px;
  54.     height: auto;
  55.     background-color: #ccc;
  56.     position: absolute;
  57.     top: 0px;
  58.     left: 0px;
  59.     bottom: 0px;
  60. }
  61. .jdbox .boxcenter .content{
  62.     width: auto;
  63.     height: auto;
  64.     background-color: rgba(255, 255, 255, 0);;
  65.     position: absolute;
  66.     top: 0px;
  67.     left: 10px;
  68.     right: 10px;
  69.     bottom: 0px;
  70.     word-wrap:break-word;
  71. }
  72. .jdbox .boxcenter .rtbline{
  73.     width: 10px;
  74.     height: auto;
  75.     background-color: #ccc;
  76.     position: absolute;
  77.     top: 0px;
  78.     right: 0px;
  79.     bottom: 0px;
  80. }
  81. .jdbox .boxbottom{
  82.     width: 100%;
  83.     height: 10px;
  84.     background-color: #aaa;
  85.     position: absolute;
  86.     bottom: 0px;
  87. }
.jdbox{
	display: block;
	background-color: #eee;
	position: relative;
	float: left;
}

.jdbox .boxtop{
	width: 100%;
	height: 10px;
	background-color: #aaa;
	position: absolute;
	top:0px;
}
.jdbox .boxtop .ltcorner,
.jdbox .boxbottom .ltcorner
{
	width: 10px;
	height: 10px;
	background-color: #bbb;
	position: absolute;
	top: 0px;
	left: 0px;
}
.jdbox .boxtop .lrline,
.jdbox .boxbottom .lrline
{
	width: auto;
	height: 10px;
	background-color: #ccc;
	position: absolute;
	top: 0px;
	left: 10px;
	right: 10px;
}
.jdbox .boxtop .rtcorner,
.jdbox .boxbottom .rtcorner
{
	width: 10px;
	height: 10px;
	background-color: #bbb;
	position: absolute;
	top: 0px;
	right: 0px;
}

.jdbox .boxcenter{
	width: 100%;
	height: auto;
	position: absolute;
	top: 10px;
	bottom: 10px;
}
.jdbox .boxcenter .ltbline{
	width: 10px;
	height: auto;
	background-color: #ccc;
	position: absolute;
	top: 0px;
	left: 0px;
	bottom: 0px;
}
.jdbox .boxcenter .content{
	width: auto;
	height: auto;
	background-color: rgba(255, 255, 255, 0);;
	position: absolute;
	top: 0px;
	left: 10px;
	right: 10px;
	bottom: 0px;
	word-wrap:break-word;
}
.jdbox .boxcenter .rtbline{
	width: 10px;
	height: auto;
	background-color: #ccc;
	position: absolute;
	top: 0px;
	right: 0px;
	bottom: 0px;
}

.jdbox .boxbottom{
	width: 100%;
	height: 10px;
	background-color: #aaa;
	position: absolute;
	bottom: 0px;
}

下面是几个宽高不同的面板,四个角是独立的div,效果就是这样,

 

接下来就是需要切一组图片出来。

ie6和ie7兼容性存在问题,正在解决中…