简体   繁体   English

换行时,线元素被下推

[英]Line element gets pushed down when text is wrapped

How do I fix not having line #2 get pushed down when the h3 line is long. 我如何解决在h3行很长的情况下没有将#2行压低的问题。

The numbers and text line up nicely when the titles are short, but when the title wraps onto two lines, the entire section gets pushed down. 当标题较短时,数字和文本会很好地对齐,但是当标题环绕在两行上时,整个部分都将向下推。

Here's my jsfiddle: http://jsfiddle.net/7bzc74qy/29/ 这是我的jsfiddle: http : //jsfiddle.net/7bzc74qy/29/

Here's my code: 这是我的代码:

    <div class="alignright">
    <p>
        <img alt="Sign up Today" height="259" src="" width="300" /></p>
</div>
<div class="circle">
    <span class="number">1</span>
        </div>
            <h3>
                Banana</h3>
            <p>
                A banana is an edible fruit, botanically a berry, produced by several kinds
                of large herbaceous flowering plants in the genus Musa.</p>
            <p>&nbsp;</p>                
<div class="circle">
    <span class="number">2</span>
</div>
    <h3>
        Orange is the colour of saffron, pumpkins and apricots.</h3>
    <p>
        Mobile phones, mobile broadband and home broadband in the Orange Shop. Plus the latest
        news, entertainment, sport and lifestyle content from Orange.</p>
    <p>&nbsp;</p> 
  <div class="circle">
      <span class="number">3</span>
  </div>
      <h3>
          Watermelon</h3>
       <p>
           We here at the National Watermelon Promotion Board have one goal: to increase 
           consumer demand for fresh watermelon through promotion, research</p>
/* Circle Numbers */  
  .circle {
    border-radius: 100%;
    height: 2em;
    width: 2em;
    text-align: center;
    background: #f66511;
    float: left;
}

.circle .number {
    margin-top: 0.10em;
    font-size: 1.5em;
    font-weight: bold;
    font-family: sans-serif;
    line-height: 1.4em;
    color: #ffffff;
}

h1.section-title {
    font-family: SegoeRegular,Helvetica,Arial;
    font-size: 32px;
    font-weight: normal;
    color: #2251a4;
    text-transform: uppercase;
    margin: 10px 0 50px 0;
    border-bottom: 1px dotted #f66511;
    padding: 0 0 5px 0;
    line-height: 38px;
}

h3 {
    font-family:Arial,Helvetica,sans-serif;
    font-size:18px;
    color:#232323;
    margin:0 0 5px 48px;
    text-align:left;
    font-weight:bold;
}

p {
    font-family:Arial,Helvetica,sans-serif;
    font-size:16px;
    color:#616161;
    margin:0 0 0 49px;
    text-align:left;
    font-weight:normal;
    line-height: 24px;
}
.circle + h3,
.circle {
    vertical-align: top;
}
.circle + h3 {
    line-height: 30px;
}

.alignright {
    float: right;
    margin: 0px 0px 20px 30px;
}

Here my attempt, I've made a couple of changes. 在这里,我做了一些更改。

First, instead of using a different div and span, I've used only the ::before pseudo-element. 首先,我只使用:: before伪元素,而不是使用不同的div和span。 This element uses a CSS counter, and is positioned absolutely. 该元素使用CSS计数器,并且位于绝对位置。 Also, the line-height is now 2em, so it fills the height of the circle, and more or less centers the text. 而且,行高现在为2em,因此它填充了圆的高度,并且或多或少地使文本居中。 I've changed some details in the outline of the head, so the lines of a multi-line head are aligned properly underneath each other. 我已经更改了头部轮廓的一些细节,因此多线头部的线条彼此正确对齐。

Html now simply looks like this, without additional markup for the numbers. HTML现在看起来像这样,没有数字的附加标记。

<h3>Heading 1</h3>
<p>Paragraph1</p>
<h3>Heading 2</h3>
<p>Paragraph2</p>
<h3>Heading 3</h3>
<p>Paragraph3</p>

And the CSS: 和CSS:

/* Circle Numbers */  
h3::before {
    display: block;
    content: counter(head-counter);
    counter-increment: head-counter;
    position: absolute;
    left: -49px;
    top: -0.5em;
    ...
    line-height: 2em;
}

H3 needs position relative, otherwise the circle won't stay near it: H3需要相对位置,否则圆将不会停留在其附近:

h3 {
    position: relative;
}

I've wrapped the whole thing in an article, but you could use a div as well, or just the body. 我已经将整个内容包装在一篇文章中,但是您也可以使用div或仅使用正文。 The margin of the div is now the padding of the article, so the header and the text are indented by the same amount (although the heads have their own indenting, which is relative to this one). div的边距现在是文章的空白,因此标题和文本的缩进量相同(尽管头部有自己的缩进,相对于该缩进)。

article {
    counter-reset: head-counter;
    padding-left: 49px;
}

 /* Circle Numbers */ h3::before { display: block; content: counter(head-counter); counter-increment: head-counter; position: absolute; left: -49px; top: -0.3em; border-radius: 100%; height: 1.4em; width: 1.4em; text-align: center; background: #f66511; font-size: 1.5em; font-weight: bold; font-family: sans-serif; line-height: 1.4em; color: #ffffff; } h1.section-title { font-family: SegoeRegular,Helvetica,Arial; font-size: 32px; font-weight: normal; color: #2251a4; text-transform: uppercase; margin: 10px 0 50px 0; border-bottom: 1px dotted #f66511; padding: 0 0 5px 0; line-height: 38px; } h3 { position: relative; font-family:Arial,Helvetica,sans-serif; font-size:18px; color:#232323; padding:0 0 5px 0; text-align:left; font-weight:bold; } article { counter-reset: head-counter; padding-left: 49px; } p { font-family:Arial,Helvetica,sans-serif; font-size:16px; color:#616161; text-align:left; font-weight:normal; line-height: 24px; } .alignright { float: right; margin: 0px 0px 20px 30px; } 
 <div class="alignright"> <p><img alt="Sign up Today" height="259" src="" width="300" /></p> </div> <article> <h3> Banana</h3> <p> A banana is an edible fruit, botanically a berry, produced by several kinds of large herbaceous flowering plants in the genus Musa.</p> <p>&nbsp;</p> <h3> Orange is the colour of saffron, pumpkins and apricots.</h3> <p> Mobile phones, mobile broadband and home broadband in the Orange Shop. Plus the latest news, entertainment, sport and lifestyle content from Orange.</p> <p>&nbsp;</p> <h3> Watermelon</h3> <p> We here at the National Watermelon Promotion Board have one goal: to increase consumer demand for fresh watermelon through promotion, research</p> </article> 

References 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM