负margin技巧

负margin特点

设置margin-leftmargin-top时,当前元素往设置方向收缩;
设置margin-rightmargin-bottom时,后续元素往设置方向靠拢;

<!-- css -->
<style>
    .margin-character {
        float: left;
        padding: 0 5px;
    }  
    .margin-character>div {
        height: 30px; 
        width: 50px; 
        border: 2px solid;
    }
</style>
<!-- html -->
<div class="margin-character">
   <div style="border-color:#FF0000;"></div>
   <div style="border-color:#00FFFF; margin-top: -10px; margin-bottom: -10px;"></div>
   <div style="border-color:#FF00FF;"></div>
</div>
<div class="margin-character">
   <div style="border-color:#FF0000; float: left;"></div>
   <div style="border-color:#00FFFF; float: left; margin-left: -10px; margin-right: -10px;"></div>
   <div style="border-color:#FF00FF; float: left;"></div>
</div>

左侧可看出,元素2向上移动覆盖了元素1的一部分; 元素3向上靠拢覆盖了元素2的一部分。 右侧同理
在这里插入图片描述

图片和文本对齐

在img标签上添加margin: 0 3px -3px 0样式,可以使得文本与图片底部对齐

<div>
    <img alt="" src="./hello.jpg" style="margin: 0 3px -3px 0;" />
    <span>向你问好</span>
</div>

图片与文本对齐

自适应布局

一列固定宽度,一列自适应

语法
主元素
	width: 100%;
	float: left;
	margin-right: -侧边栏宽度;
侧边元素
	width: 侧边栏宽度;
	float: left;
例子
<section>
    <div style="width: 100%; height: 200px; float: left; margin-right: -200px; background: pink;">
    	主内容
    </div>
    <aside style="width: 200px; height: 200px; float: left; background: #fff566;">
    	侧边内容
    </aside>
</section>

在这里插入图片描述

水平垂直居中

<!-- css -->
<style>
	.box {
		position: relative;
		width: 200px; 
		height: 100px; 
		background: pink; 
	}
	.box>div {
		position: absolute; 
		width: 50px; 
		height: 50px; 
		background: gray; 
		top: 50%;
		left: 50%; 
		margin-top: -25px;
		margin-left: -25px;
	}
</style>

<!-- html -->
<div class="box">
    <div>/div>
</div>

在这里插入图片描述

tab选项卡

更多 …

学习资料:《Web前端开发精品课:HTML与CSS进阶教程》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值