Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css] 第56天 margin和padding使用的场景有哪些? #220

Open
haizhilin2013 opened this issue Jun 10, 2019 · 4 comments
Open

[css] 第56天 margin和padding使用的场景有哪些? #220

haizhilin2013 opened this issue Jun 10, 2019 · 4 comments
Labels
css css

Comments

@haizhilin2013
Copy link
Collaborator

第56天 margin和padding使用的场景有哪些?

@haizhilin2013 haizhilin2013 added the css css label Jun 10, 2019
@encountermm
Copy link

margin:

     需要在border外侧添加空白时;

     空白处不需要背景(色)时;

    上下相连的两个盒子之间的空白,需要相互抵消时。

padding:

    需要在border内测添加空白时;

    空白处需要背景(色)时;

    上下相连的两个盒子之间的空白,希望等于两者之和时。

@LeiDeMing
Copy link

实现自适应的等比例矩形效果:
div { padding: 50%; }
div { padding: 25% 50%; } //宽高比为 2:1 的矩形效果

实现头图高度天然等比例缩小效果
.box {
padding: 10% 50%;
position: relative;
}
.box > img {
position: absolute;
width: 100%; height: 100%;
left: 0; top: 0;
}

padding 属性和 background-clip 属性配合,可以在有限的标签下实现一些 CSS 图形绘制效果

margin 可以实现很多流体布局
margin 外部尺寸实现等高布局
实现垂直居中布局

from《css世界》

@MrZ2019
Copy link

MrZ2019 commented Dec 1, 2020

margin:

     需要在border外侧添加空白时;

     空白处不需要背景(色)时;

    上下相连的两个盒子之间的空白,需要相互抵消时。

padding:

    需要在border内测添加空白时;

    空白处需要背景(色)时;

    上下相连的两个盒子之间的空白,希望等于两者之和时。

@zxcdsaqwe123
Copy link

margin用于给元素定位,padding用于改变元素的大小(除非使用了box-sizing:border-box)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css css
Projects
None yet
Development

No branches or pull requests

5 participants