css之padding,marging

本文详细介绍了CSS中内边距(padding)和外边距(margin)的概念及用法。包括它们的属性值类型如auto、length、%、inherit等,并通过具体的HTML代码示例展示了如何应用这些属性来改变元素的布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

padding:内边距,所有浏览器都支持,不允许使用负值

    继承内部格式生成了10px的边距。

属性:

  auto:浏览器计算机内边距。

  length:规定以具体单位计的内边距值,比如像素、厘米等。默认值是 0px。

  %:规定基于父元素的宽度的百分比的内边距。

  inherit:规定应该从父元素继承内边距。

 

html代码

<body>
    <div style="width: 500px;height: 300px;border: solid 1px;background-color: #303a40">
        <div style="width: 200px;height: 200px;background-color: #4cae4c;padding: 10px;"></div>
    </div>
</body>

  效果图:

内边距生效前:

       

 

marging:外边距

  继承外部格式生成了10px边距

css代码

.cs1{
    width: 400px;
    height: 400px;
    border: solid 1px;
    background-color: yellow;
    position: relative;
    left:20px;
    top:20px;
}
.cs2{
    width: 100px;
    height: 100px;
    background-color: black;
    position: relative;
    left:20px;
    top:40px;
    margin:20px;
}

  html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="ctest.css">
</head>
<body>
    <div class="cs1">
        <div class="cs2"></div>
    </div>
</body>
</html>

  效果图原来样式:                                                                      效果图:设置margin后

 

转载于:https://www.cnblogs.com/kongzhagen/p/6124922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值