2014-12-31 7 views

답변

7

:after이 이용 가능하다 : 가상 소자.


당신이 응답에 대한 백분율 단위로 divwidth을 설정하려면

div { 
 
    font-size: 15px; 
 
    width: 300px; 
 
    line-height: 30px; 
 
} 
 
span { 
 
    position: relative; 
 
} 
 
span:after { 
 
    position: absolute; 
 
    content: '2'; 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 100%; 
 
    left: 0; 
 
    border-top: 1px solid black; 
 
    text-align: center; 
 
    font-size: 9px; 
 
    line-height: 15px; 
 
}
<div>over the <span>Triangle, a few months later,</span> another plane disappeared. A ship named the Sandra.</div>
, 당신은 white-space: pre을 설정하여 span에 줄 바꿈을 피할 수 있습니다. 아래 예에서 Fiddle

상기 width는이를 보여 25%로 설정되어있다. 또한


div { 
 
    font-size: 15px; 
 
    width: 25%; 
 
    line-height: 30px; 
 
} 
 
span { 
 
    position: relative; 
 
    white-space: pre; 
 
} 
 
span:after { 
 
    position: absolute; 
 
    content: '2'; 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 100%; 
 
    left: 0; 
 
    border-top: 1px solid black; 
 
    text-align: center; 
 
    font-size: 9px; 
 
    line-height: 15px; 
 
}
<div>over the <span>Triangle, a few months later</span>, another plane disappeared. A ship named the Sandra.</div>
, 당신은 HTML 요소에서 속성 값을 가져 CSS의 attr() 기능을 사용할 수 있습니다 : 의사 요소가 추가됩니다. @chipChocolates 당신이 CSS 스타일 블록에 첨자를 넣어하지 않으려면, 대답, 오히려 당신이 그들을 필요로 몸에서 사용할 사용

div { 
 
    font-size: 15px; 
 
    width: 50%; 
 
    line-height: 30px; 
 
} 
 
span { 
 
    position: relative; 
 
    white-space: pre; 
 
} 
 
span:after { 
 
    position: absolute; 
 
    content: attr(data-num); 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 100%; 
 
    left: 0; 
 
    border-top: 1px solid black; 
 
    text-align: center; 
 
    font-size: 9px; 
 
    line-height: 15px; 
 
}
<div>over the <span data-num="2">Triangle, a few months later</span>, another plane disappeared. A ship named the Sandra.</div>

+0

'span : after'에'top : 100 %'을 추가하는 것을 잊었습니까? – 0x2D9A3

+0

흠, Safari/Mac에서는 좋지 않습니다. Chrome에서도 완벽 해 보입니다. http://i.imgur.com/V2WbdtU.png – 0x2D9A3

+0

궁금한 점이 있다면, 'width : auto; 높이 : 15px,'span '? – 0x2D9A3

2

...

div { 
 
    font-size: 15px; 
 
    width: 300px; 
 
    line-height: 30px; 
 
} 
 
span { 
 
    position: relative; 
 
    width: auto; 
 
    height: 15px; 
 
} 
 
span:after { 
 
    position: absolute; 
 
    content: attr(subscript-line); 
 
    width: 100%; 
 
    height: 100%; 
 
    top: 100%; 
 
    left: 0; 
 
    border-top: 1px solid black; 
 
    text-align: center; 
 
    font-size: 10px; 
 
    line-height: 10px; 
 
}
<div>over the <span subscript-line="1">Triangle, a few months later</span>, another plane disappeared. A ship named the Sandra.</div> 
 
<div>over the <span subscript-line="2">Triangle, a few months later</span>, another plane disappeared. A ship named the Sandra.</div> 
 
<div>over the <span subscript-line="3">Triangle, a few months later</span>, another plane disappeared. A ship named the Sandra.</div>

+1

확실히 정답, CSS의 하드 코딩 컨텐트는 피할 수 있다면 좋지 않습니다. –

+1

비즈니스 규칙이 허용하는 경우 추상화를 추가로 수행하고 CSS 카운터를 사용하여 첨자 값을 가져올 수 있습니다. –