CSS:保证金仅围绕特定 HTML div?

CSS: margin only around specific HTML div?

我有以下 HTML 文档,在文档中我希望在以下 div:

周围留出空白
<div class="messageBody">

同时保持页眉和页脚的原始宽度。

我该怎么做?

我试过添加

.messageBody{
             margin: 10px
             }

<style> 标签中,但不影响 div?

中的边距

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html data-editor-version="2" class="sg-campaigns" xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
  <!--[if !mso]><!-->
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <!--<![endif]-->
  <!--[if (gte mso 9)|(IE)]>
    <xml>
        <o:OfficeDocumentSettings>
            <o:AllowPNG/>
            <o:PixelsPerInch>96</o:PixelsPerInch>
        </o:OfficeDocumentSettings>
    </xml>
    <![endif]-->
  <style type="text/css">
      body {
      width: 600px;
      margin: 0 auto;
    }
    
    table {
      border-collapse: collapse;
    }
    
    table,
    td {
      mso-table-lspace: 0pt;
      mso-table-rspace: 0pt;
    }
    
    img {
      -ms-interpolation-mode: bicubic;
    }
    
    body,
    p,
    div {
      font-family: arial, helvetica, sans-serif;
      font-size: 14px;
    }
    
    body {
      color: #000000;
    }
    
    body a {
      color: #1188E6;
      text-decoration: none;
    }
    
    p {
      margin: 0;
      padding: 0;
    }
    
    table.wrapper {
      width: 100% !important;
      table-layout: fixed;
      -webkit-font-smoothing: antialiased;
      -webkit-text-size-adjust: 100%;
      -moz-text-size-adjust: 100%;
      -ms-text-size-adjust: 100%;
    }
    
    img.max-width {
      max-width: 100% !important;
    }
    
    .column.of-2 {
      width: 50%;
    }
    
    .column.of-3 {
      width: 33.333%;
    }
    
    .column.of-4 {
      width: 25%;
    }
    
    @media screen and (max-width:480px) {
      .preheader .rightColumnContent,
      .footer .rightColumnContent {
        text-align: left !important;
      }
      .preheader .rightColumnContent div,
      .preheader .rightColumnContent span,
      .footer .rightColumnContent div,
      .footer .rightColumnContent span {
        text-align: left !important;
        .preheader .rightColumnContent,
        .preheader .leftColumnContent {
          font-size: 80% !important;
          padding: 5px 0;
        }
        table.wrapper-mobile {
          width: 100% !important;
          table-layout: fixed;
        }
        img.max-width {
          height: auto !important;
          max-width: 100% !important;
        }
        a.bulletproof-button {
          display: block !important;
          width: auto !important;
          font-size: 80%;
          padding-left: 0 !important;
          padding-right: 0 !important;
        }
        .columns {
          width: 100% !important;
        }
        .column {
          display: block !important;
          width: 100% !important;
          padding-left: 0 !important;
          padding-right: 0 !important;
          margin-left: 0 !important;
          margin-right: 0 !important;
        }
      }
  </style>
  <table class="wrapper" role="module" data-type="image" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;" data-muid="b258ba20-9e45-4cef-96e8-396a2f5fbce8">
    <tbody>
      <tr>
        <td style="font-size:6px; line-height:10px; padding:0px 0px 0px 0px;" valign="top" align="center">
          <img class="max-width" border="0" style="display:block; color:#000000; text-decoration:none; font-family:Helvetica, arial, sans-serif; font-size:16px; max-width:100% !important; width:100%; height:auto !important;" width="600" alt="" data-proportionally-constrained="true"
            data-responsive="true" src="stub src">
        </td>
      </tr>
    </tbody>
  </table>
</head>

<body>
  <table class="module" role="module" data-type="code" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;" data-muid="5dd19a7e-9c00-4720-8d1e-272802ca3964">
    <tbody>
      <tr>
        <td style="font-size:6px; line-height:10px; padding:0px 0px 0px 0px;" valign="top" align="center">
          <img class="max-width" border="0" style="display:block; color:#000000; text-decoration:none; font-family:Helvetica, arial, sans-serif; font-size:16px; max-width:100% !important; width:100%; height:auto !important;" width="600" alt="" data-proportionally-constrained="true"
            data-responsive="true" src="stub src" ">
        </td>
    </tr>
    <div class="messageBody ">
    <tr>
        <td height="100% " valign="top " role="module-content ">
            <div class="introduction ">
                <br>
                <p class="sansserif ">
                    <b>Hi,</b>
                    <br>
                    <br> Click the link in the table below to see more info .
                    <br>
                    <br>
                </p>
            </div>
            <style>

                       .messageBody{
                       margin: 38px
                       }

                        p.sansserif {
                            font-family: Arial, Helvetica, sans-serif;
                        }

                        #carTable {
                            color: #333;
                            font-family: Arial, Helvetica, sans-serif;
                            width: 100%;
                            ;
                            border-collapse: collapse;
                            border-spacing: 0;
                        }

                        #carTable td,
                        th {
                            border: 1px solid transparent;
                            /* No more visible border */
                            height: 30px;
                            transition: all 0.3s;
                            /* Simple transition for hover effect */
                        }

                        #carTable th {
                            background: #DFDFDF;
                            /* Darken header a bit */
                            font-weight: bold;
                            text-align: left
                        }

                        #carTable td {
                            background: #FAFAFA;
                            text-align: left
                        /* Cells in even rows (2,4,6...) are one color */

                        #carTable tr:nth-child(even) td {
                            background: #F1F1F1;
                        }
                        /* Cells in odd rows (1,3,5...) are another (excludes header cells)  */

                        #carTable tr:nth-child(odd) td {
                            background: #FEFEFE;
                        }
                    </style>
            <div class="table ">
                <table id="carTable ">
                    <tbody>
                    <tr>
                        <th>Car Number</th>
                        <th>Group</th>
                    </tr>
                    <!--  -->
                    <tr>
                        <td><a href="https://www.testurl.com ">CAR_1</a></td>
                        <td>Group 1</td>
                    <!-- -->
                    </tbody>
                </table>
            </div>
            <div class="errorInformation ">
                <br>
                <p class="sansserif ">
                    If you think this was sent mistakenly, please contact support at <a href="mailto:mail@test.com ">mail@test.com</a>
                    <br>
                    <br>
                </p>
            </div>
        </td>
    </tr>
    </div>
    </tbody>
</table>
</body>
<footer>
    <table class="module " role="module " data-type="text " border="0 " cellpadding="0 " cellspacing="0 " width="100% " style="table-layout: fixed; " data-muid="20f34b16-a07e-4993-8822-da37a70f37cc " data-mc-module-version="2019-10-22 ">
        <tbody>
        <tr>
            <td style="padding:14px 0px 14px 0px; line-height:10px; text-align:inherit; background-color:#135670; " height="100% " valign="top " bgcolor="#135670 " role="module-content ">
                <div>
                    <div style="font-family: inherit; text-align: center "><span style="color: #f6f2f2 ">Get the latest news at </span><a href="https://testurl.com><span style="color: #20ede7">testurl.com</span></a>
          </div>
          <div>
          </div>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
  </footer>

</html>

我目前在这个 HTML 文档中有 2 个样式表,我想知道一个是否覆盖了另一个?

简介

样式表可以级联,也可以相互覆盖。 您已经为 messagebody 设置了 margin,因此您应该设置那个。

增加现有保证金而不是增加新保证金。

     .messageBody{
                   margin: 38px
                   }

此外,您的代码似乎违反了 HTML 惯例。如评论中所述,您应该 运行 您的代码通过 html 验证器。

使用浏览器检查器玩 CSS

看看下面的文章:Get Started With Viewing And Changing CSS

使用检查器,我看到了您当前代码的结果并添加了一个 dotted border 以使其更加可见。

当我把边距改成200px时,效果更明显: