2015-01-04 21 views
1

我使用伪元素为编号列表(由div制成)创建样式编号。是否有可能在divs左边的数字?是否可以使用css将可变宽度的伪元素内容添加到div的/ left /中?

使用position: absoulute; left: 1em将不起作用,因为它没有考虑数字的实际宽度。

+0

不完全重复,但你可能有兴趣在此http:// ST ackoverflow.com/questions/26997532/using-css-to-style-a-numbered-list/27009218#27009218 - 这是相关/相似的东西。 – Harry

+0

在伪元素上设置宽度? – sodawillow

回答

0

使用right: 100%把伪元素开始在的左侧div

.box { 
 
    border: 1px solid #c66; 
 
    background: #f99; 
 
    position: relative; 
 
    width: 100px; 
 
    height: 20px; 
 
    margin: 20px; 
 
} 
 

 
.box:before { 
 
    position: absolute; 
 
    right: 100%; 
 
    margin-right: 5px; 
 
} 
 

 
.box--one:before { 
 
    content: '1' 
 
} 
 
.box--two:before { 
 
    content: '2' 
 
} 
 
.box--ten:before { 
 
    content: '10' 
 
}
<div class="box box--one"> 
 
</div> 
 
<div class="box box--two"> 
 
</div> 
 
<div class="box box--ten"> 
 
</div>

+0

谢谢!那很完美。现在我想知道是否有方法在容器div中自动添加足够的填充,以便所有数字都适合它。但我认为这可能是太多的要求从伪元素... – avv

+0

如果你想要框对齐,我不认为你可以 – ckuijjer

1

简单的例子

小提琴http://jsfiddle.net/63pc4y57/1/

HTML

<div>one</div> 
<div>two</div> 
<div>three</div> 

CSS

body 
{ 
    counter-reset: divs; 
} 

div 
{ 
    background: grey; 
    color: black; 
    height: 5em; 
    line-height: 5em; 
    margin-left: 2em; 
    width: 5em; 
    counter-increment: divs; 
    position: relative; 
} 

div:before 
{ 
    background: lightgrey; 
    content: counter(divs); 
    left: -1em; 
    position: absolute; 
    width: 1em; 
} 

如果你可以使用数字div的然后删除position: absolute并添加div:beforedisplay: inline-block其宽度将考虑

相关问题