基本图片与文字布局(浮动,包含块)


正常流中:

1.块级元素(<h> <p> <ul> <li>...):换行,独自占一行显示,就算有足够的空间并排,也会在下一行显示,可以设置width宽度
2.行内元素(<img> <em> <q>...):若有足够的空间并排,就会并排显示
3.定位为relative仍在流中


脱离正常流:

1.设置宽度width并浮动

2.定位为absolute 或 fixed


浮动父级元素:

错误原因:规定若父级包含块的元素都是浮动元素,则浏览器默认父级包含块高度为0,包含块只出现边框,原来所占的空间仍然存在。

解决方法1(推荐)

CSS中父级包含块的属性里添加     

 overflow:auto;     /*   或  overflow:hidden;  */

 

解决方法2:

为父级包含块设置高度height

height:400px;

 
 

解决方法3

在包含块里最后添加 

<br class="clear">

CSS中添加

  .clear{
              clear:both;
          }

例子

1.正常流中显示

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>divDistribution</title>
</head>
<body>
	<img src="pic1.jpg" alt="Picture of the article" width="159" height="203">
	<article>文本略
	</article> 
</body>
</html>
 

2.图片向左浮动
img{
		float: left;
	}
 
3.增加包含块并设置CSS属性
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>divDistribution</title>
	<style type="text/css">
	
	img{
		float: left;
	}
	.sec{
		width:500px;
		background: #F0D8D8;
		border:1px solid #717070;
	}
	</style>
</head>
<body>
	<section class="sec">
		<img src="pic1.jpg" alt="Picture of the article" width="159" height="203">
		<article>文本略
		</article>
	</section>
</body>
 
 
 
  

4.文字向右浮动(必须设置width才能浮动),此时包含块中都是浮动元素,出现问题
article{
		float: right;
		width:320px;
	}
 
 
  5.最后效果
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>divDistribution</title>
	<style type="text/css">
	
	img{
		float: left;
	}

	
	article{
		float: right;
		width:320px;
	}

	.sec{
		width:500px;
		/*height: 400px;*/     /*方法三*/
		background: #F0D8D8;
		border:1px solid #717070;
		overflow: auto;	            /*方法一*/
	}

	/*.clear{               方法二
		clear: both;
	}*/

	</style>
</head>
<body>
	<section class="sec">
		<img src="pic1.jpg" alt="Picture of the article" width="159" height="203">
		<article>文本略
		</article>
		<!-- <br class="clear"> -->   <!-- 方法二 -->
	</section>
</body>
</html>
 
 






  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值