盒子模型

本文详细介绍了CSS盒子模型,包括内容(content)、内边距(padding)、边框(border)和外边距(margin)的概念,并通过实例展示了如何设置边框样式。同时,文章还探讨了网页布局中的浮动(float)和定位(position)技术,解释了如何使用absolute和relative定位实现元素在页面上的精确放置。
摘要由CSDN通过智能技术生成

CSS盒子模型

网页设计中常听的属性名:内容(content)、内边距(padding)、边框(border)、外边距(margin), CSS盒子模式都具备这些属性。

这些属性我们可以用日常生活中的常见事物——盒子作一个比喻来理解,所以叫它盒子模式。

CSS盒子模型就是在网页设计中经常用到的CSS技术所使用的一种思维模型。

 

border:设置边框     border:2px solid red;

(border-left:左边框   border-right:右边框   border-top:上边框   

border-bottom:下边框)

(none无边框,solid边框为实线,dotted边框为点状即点线,dashed边框为虚线,double边框为双线)

padding :边框内边距   padding:20pax

padding-left:左内边距

padding-right:右内边距

padding-top:上内边距

padding-bottom:下内边距

 

margin:边框外边距       margin:20px

margin-left:左外边距

margin-right右外边距

margin-top上外边距

margin-bottom下外边距

 

对数据进行操作,需要把数据放到一个区域里面(div)

 

布局的漂浮

     float

float 属性定义元素在哪个方向浮动。以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动。浮动元素会生成一个块级框,而不论它本身是何种元素。

如果浮动非替换元素,则要指定一个明确的宽度;否则,它们会尽可能地窄。

实例

把图像向右浮动:


   
   
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. img
  5. {
  6. float:right
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <p>在下面的段落中,我们添加了一个样式为 <b>float:right </b> 的图像。结果是这个图像会浮动到段落的右侧。 </p>
  12. <p>
  13. <img src="/i/eg_cute.gif" />
  14. This is some text. This is some text. This is some text.
  15. This is some text. This is some text. This is some text.
  16. This is some text. This is some text. This is some text.
  17. This is some text. This is some text. This is some text.
  18. This is some text. This is some text. This is some text.
  19. This is some text. This is some text. This is some text.
  20. This is some text. This is some text. This is some text.
  21. This is some text. This is some text. This is some text.
  22. This is some text. This is some text. This is some text.
  23. This is some text. This is some text. This is some text.
  24. </p>
  25. </body>
  26. </html>

效果:

布局的定位

position 属性规定元素的定位类型。

absolute

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed

生成绝对定位的元素,相对于浏览器窗口进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative

生成相对定位的元素,相对于其正常位置进行定位。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static

默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。

 

 

 

 

 

 

 

 

 

实例 绝对定位


   
   
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. h2 .pos_abs
  5. {
  6. position:absolute;
  7. left: 100px;
  8. top: 150px
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <h2 class="pos_abs">这是带有绝对定位的标题 </h2>
  14. <p>通过绝对定位,元素可以放置到页面上的任何位置。下面的标题距离页面左侧 100px,距离页面顶部 150px。 </p>
  15. </body>
  16. </html>

效果:

实例 相对定位

 


   
   
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. h2 .pos_left
  5. {
  6. position:relative;
  7. left:- 20px
  8. }
  9. h2 .pos_right
  10. {
  11. position:relative;
  12. left: 20px
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h2>这是位于正常位置的标题 </h2>
  18. <h2 class="pos_left">这个标题相对于其正常位置向左移动 </h2>
  19. <h2 class="pos_right">这个标题相对于其正常位置向右移动 </h2>
  20. <p>相对定位会按照元素的原始位置对该元素进行移动。 </p>
  21. <p>样式 "left:-20px" 从元素的原始左侧位置减去 20 像素。 </p>
  22. <p>样式 "left:20px" 向元素的原始左侧位置增加 20 像素。 </p>
  23. </body>
  24. </html>

效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值