学习HTML与CSS的第七天————内边距、外边距

<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8"/>
      <title></title>
      <style type="text/css"> 
      .box1{
        width: 200px;
        height: 200px;
        background-color: green;
        border: red solid 10px;
        /* 
          内边距(padding),指的是盒子的内容区与盒子边框之间的距离

           一共有四个方向的内边距,可以通过:
             padding-top
             padding-right
             padding-bottom
             padding-left
              来设置四个方向的内边距
            

            内边距会影响盒子的可见框的大小,元素的背景会延伸到内边距
            盒子的大小由内容区、内边距和边框共同决定
            
         */
        /*  padding-top: 100px;
         padding-right: 100px;
         padding-bottom: 100px;
         padding-left: 100px; */
         /* 
            使用padding可以同时设置四个边框的样式,用法和border-width一样
          */
      }
      .box2{
        width: 200px;
        height: 200px;
        background-color: yellow;
      }
      </style>
    </head>
    <body>
      <div class="box1">
        <div class="box2"></div>
      </div>
    </body>
</html>
 
<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8"/>
      <title></title>
      <style type="text/css"> 
      .box{
        width: 200px;
        height: 200px;
        background-color:#bfa;
        border: red solid 10px;
        /* 
           外边距指的是当前盒子与其他盒子之间的距离
           不会影响可见框的大小,而是会影响到盒子的位置
           盒子有四个方向的外边距:
              margin-top
              margin-right
              margin-left
              margin-bottom
              由于页面中的元素都是靠左排放
              所以设置左外边框或上外边框时,会导致原来盒子的位置改变
              但是设置右外边距或下外边距时,会导致其他盒子的位置改变
         */
         /* 设置box的上外边距,盒子上边框和其他盒子的距离 */
         margin-top: 100px;
         /*  设置box的左外边距,盒子左边框和其他盒子的距离 */
       /*   margin-left: 100px; */
         margin-bottom: 100px;
         /* 
           外边距也可以指定为一个负值,
            如果外边距设置的是负值,则元素会向反方向移动
          */
          margin-left: -100px;
      }
      .box2{
        width: 200px;
        height: 200px;
        background-color: yellow;
        /* 
        margin还可以设置为auto,auto一般只设置给水平方向的margin
         如果只指定,左外边距或者右外边距的margin为auto则会讲外边距设置为最大值
         垂直方向外边距如果设置为auto,则外边距默认就是0


         如果left和right同时设置为auto,则会将两侧的外边距设置为相同的值
          就可以使元素自动在父元素中居中,所以我们经常将左右外边距设置为auto
          以使子元素在父元素中水平居中

         */
         /* margin-left: auto; */
         margin-top: auto;

         /* 外边距同样可以使用简写属性margin,规则和padding一样 */
      }
      </style>
    </head>
    <body>
      <div class="box"></div>
      <div class="box2"></div>
    </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答您的问题。这里给出一个简单的HTML+CSS+JavaScript代码示例,用于设计和实现一个绿色古典旅游景区的网站。 HTML代码: ```html <!DOCTYPE html> <html> <head> <title>绿色古典旅游景区</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <h1>绿色古典旅游景区</h1> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">景点介绍</a></li> <li><a href="#">预定门票</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <main> <section> <h2>欢迎来到绿色古典旅游景区</h2> <p>这里是一座绿色、古典的旅游景区,拥有众多自然景观和历史文化遗址。我们提供最佳的旅游体验,让您留下美好的回忆。</p> <button>了解更多</button> </section> <section> <h2>景点介绍</h2> <ul> <li> <img src="scenery1.jpg" alt="景点1"> <h3>景点1</h3> <p>景点1的介绍。</p> </li> <li> <img src="scenery2.jpg" alt="景点2"> <h3>景点2</h3> <p>景点2的介绍。</p> </li> <li> <img src="scenery3.jpg" alt="景点3"> <h3>景点3</h3> <p>景点3的介绍。</p> </li> </ul> </section> <section> <h2>预定门票</h2> <form> <label>姓名:<input type="text" name="name"></label> <label>电话:<input type="text" name="phone"></label> <label>邮箱:<input type="email" name="email"></label> <label>门票数量:<input type="number" name="quantity"></label> <button type="submit">提交</button> </form> </section> </main> <footer> <p>版权所有 © 绿色古典旅游景区</p> </footer> <script type="text/javascript" src="script.js"></script> </body> </html> ``` CSS代码: ```css body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #003300; color: #fff; padding: 20px; } header h1 { margin: 0; } nav ul { list-style: none; margin: 0; padding: 0; } nav li { display: inline-block; margin-right: 20px; } nav a { color: #fff; text-decoration: none; } main { margin: 20px; } section { margin-bottom: 30px; } section h2 { margin-top: 0; } section ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; } section li { flex-basis: calc(33.33% - 20px); margin-right: 20px; margin-bottom: 20px; } section li:last-child { margin-right: 0; } section li img { width: 100%; height: auto; } form label { display: block; margin-bottom: 10px; } form input { padding: 5px; margin-bottom: 10px; border-radius: 5px; border: none; } form button { background-color: #003300; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; } form button:hover { background-color: #009900; } footer { background-color: #ccc; color: #333; text-align: center; padding: 10px; } ``` JavaScript代码: ```javascript window.onload = function() { var button = document.querySelector('button'); button.addEventListener('click', function() { alert('感谢您的关注!'); }); }; ``` 这些代码可以帮助您创建一个简单的绿色古典旅游景区网站。当然,具体实现还需要根据您的需求和技能水平进行调整。希望这些信息对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值