我想在跨度前面创建一个正方形。这样的事情image。
但我没有成功使用span:before
属性创建它。有可能用这个来创造吗?如果是,那么有人可以告诉我我该怎么做?
我用简单的CSS创建了这个。这是我的代码
HTML:
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<div class="r-cl"><span></span>Forecasted Rain Clean</div>
<div class="m-cl"><span></span>Forecasted Manual Clean</div>
<div class="cm-cl"><span></span>Completed Manual Clean</div>
<div class="d-cl"><span></span>Forecasted Dirty Rain</div>
</div>
和 CSS
#five_day_table span {
width: 14px;
height: 14px;
display: block;
float: left;
margin: 1px 3px 0px 0px;
}
.r-cl span
{
background: Blue;
}
.m-cl span
{
background: red;
}
.cm-cl span
{
background: green;
}
.d-cl span
{
background: brown;
}
这是工作链接。但我只想使用这个HTML。
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<span class='one'>Forecasted Rain Clean</span>
<span class='two'>Forecasted Manual Clean</span>
<span class='three'>Completed Manual Clean</span>
<span class='four'>Forecasted Dirty Rain</span>
</div>
怎么可能?
答案 0 :(得分:14)
您需要为content: ""
添加span:before
才能工作
#five_day_table span {
display: block;
margin: 1px 3px 0px 0px;
}
span:before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
margin-right: 5px;
}
.one:before {
background: Blue;
}
.two:before {
background: red;
}
.three:before {
background: green;
}
.four:before {
background: brown;
}
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<span class='one'>Forecasted Rain Clean</span>
<span class='two'>Forecasted Manual Clean</span>
<span class='three'>Completed Manual Clean</span>
<span class='four'>Forecasted Dirty Rain</span>
</div>
答案 1 :(得分:6)
您可以通过添加&#34;内容:&#39;■&#39 ;; &#34;
#five_day_table span {
width: 14px;
height: 14px;
margin: 1px 0 0px 0px;
}
#five_day_table span:before {
content: '■';
margin-right: 2px;
font-size: 25px;
vertical-align: middle;
display: inline-block;
margin-top: -5px;
}
#five_day_table span:after {
content: '';
display: block;
clear:both;
}
span.one:before
{
color: Blue;
}
span.two:before
{
color: red;
}
span.three:before
{
color: green;
}
span.four:before
{
color: brown;
}
&#13;
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<span class='one'>Forecasted Rain Clean</span>
<span class='two'>Forecasted Manual Clean</span>
<span class='three'>Completed Manual Clean</span>
<span class='four'>Forecasted Dirty Rain</span>
</div>
&#13;
答案 2 :(得分:2)
span{
display:block;
}
#five_day_table span:before {
width: 14px;
height: 14px;
display: inline-block;
margin: 1px 3px 0px 0px;
content:"";
}
.one:before
{
background: Blue;
}
.two:before
{
background: red;
}
.three:before
{
background: green;
}
.four:before
{
background: brown;
}
&#13;
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<span class='one'>Forecasted Rain Clean</span>
<span class='two'>Forecasted Manual Clean</span>
<span class='three'>Completed Manual Clean</span>
<span class='four'>Forecasted Dirty Rain</span>
</div>
&#13;
只需在旧CSS中添加
:before
,然后将block
更改为inline-block
以便它符合一行并且block
为{。}} 整个span
和休息将css选择器更改为:before
,这样就可以了 采取各自的颜色。
答案 3 :(得分:1)
试试这个。
您需要做的是从span中删除宽度,然后更改类。请注意,:before
需要content: ''
属性才能显示。
这是HTML代码:
<div id="five_day_table">
<h3>Annual Cleaning Schedule</h3>
<span class='one'>Forecasted Rain Clean</span>
<span class='two'>Forecasted Manual Clean</span>
<span class='three'>Completed Manual Clean</span>
<span class='four'>Forecasted Dirty Rain</span>
</div>
和css:
#five_day_table span {
height: 14px;
display: block;
margin: 1px 3px 3px 0px;
}
#five_day_table span:before{
content: '';
display: inline-block;
width: 14px;
height: 14px;
margin-right: 5px;
}
.one:before{
background: Blue;
}
.two:before{
background: red;
}
.three:before{
background: green;
}
.four:before{
background: brown;
}
答案 4 :(得分:1)
这应该是您正在寻找的
#five_day_table > span {
display:block;
}
#five_day_table > span::before {
content:'';
width: 14px;
height: 14px;
display: block;
float: left;
margin: 1px 3px 0px 0px;
}
.one::before
{
background: Blue;
}
.two::before
{
background: red;
}
.three::before
{
background: green;
}
.four::before
{
background: brown;
}