代码之家  ›  专栏  ›  技术社区  ›  Sherwood Botsford

CSS样式:在位置之前?

  •  0
  • Sherwood Botsford  · 技术社区  · 7 年前

    在这里拉小提琴: https://jsfiddle.net/sgbotsford/d52zyp0t/72/

    额外的第一行缩进是从哪里来的 <li> 使用=>:在满足之前?

    simple case to illustrate extra space after pseudo-element.

    问题是。我无法控制的情况将段落标记放在列表元素中。当我使用a:before时,它是一个负数左边,它在页边空白处,就像我想要的那样。但是文本缩进在我之前使用的地方增加了:before。这个额外的缩进是从哪里来的?

    期望的行为是段落和列表项的样式相同,除了=>在左边空白处浮动。

    我相信我最终可以做足够多的实验来破解一个解决方案,但现在我正试图从盒子模型的角度来理解这种行为。

    一个答案建议从中删除文本缩进标记 <p> 这会导致一次冲水 < 还有一个较小的缩进 <李>

    HTML

    <html>
    <head>
    
    </head>
    <body>
    
    <div class=content>
    
    <p>This is a paragraph. It has several sentences.  It goes on and on and on and on. It has several sentences.  It goes on and on and on and on. </p>
    
    <ul class="c">
      <li><p>This is a list element that is long enough to wrap, I think. But it needs to be longer to check justification. It looks a lot like a paragraph.</p></li>
     <li><p>This is a list element that is long enough to wrap, I think. But it needs to be longer to check justification.</p></li>
     </ul>
    
    <ul class="b">
      <li><p>This is a list element that is styled using ::before and outdenting the content. Where does the extra space come from? </p></li>
       <li><p>This is a list element that is long enough to wrap, I think.</p></li>
    </ul>
    
    
    </div>
    </body>
    </html>
    

    CSS

    body {
      text-align: justify;
      hyphens: auto;
      margin-left: 3rem;
    }
    
    .content {
      width: 40rem;
      background-color: yellow;
    }
    
    p   {
      width: 15rem;
      margin: auto;
      margin-top: 0.6rem;
      background-color: lightgreen;
      padding: 0;
      font-style: normal;
      font-size: 1.25rem;
      line-height: 1.4rem;
      text-indent: 1rem;
      text-align: justify;
      hyphens: auto;
      word-break: break-word;
        }
    
    
    ul {
      list-style-type: none;
      margin: 1rem;
      padding: 0;
    }
    
    ul.b li p::before {
        content: "=>";
        font-weight: bold;
        position: relative;
        left: -2.5rem;
        }
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Kuldeep Prajapati    7 年前

    嗨,你能检查一下吗?这对你真的很有帮助。我只是删除了p 标记文本缩进并更改ul。b li p::左前位置添加 ul中的文本缩进。b li p元素

    body {
        text-align: justify;
        hyphens: auto;
        margin-left: 3rem;
    }  
    .content {
        width: 40rem;
        background-color: yellow;
    }  
    p {
        width: 15rem;
        margin: auto;
        margin-top: 0.6rem;
        background-color: lightgreen;
        padding: 0;
        font-style: normal;
        font-size: 1.25rem;
        line-height: 1.4rem;
        text-align: justify;
        hyphens: auto;
        word-break: break-word;
        padding: 5px;
    }
    
    ul {
        list-style-type: none;
        margin: 1rem;
        padding: 0;
    }  
    ul.b li p::before {
        content: "=>";
        font-weight: bold;
        position: relative;
        left: -10px;
    }  
    ul.b li p {
        text-indent: -22px;
    }  
    .bluetable {
        background-color: #9FF;
        border: 2px solid black;
    }  
    td {
        margin: 14rem;
        border: 1px dashed green;
    }