Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use :before property to create a square before a span

I want to create a square in front of a span. Something like this image.

enter image description here

But I am not successful in creating this with span:before property. Is it possible to create with this? If yes then can someone please tell me how can I do this?

I have created this with simple CSS . Here is my code

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>

and 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; 
}

Here is the working link. But I want to use this HTML only.

<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>

How is it possible?

like image 701
Manoj Dhiman Avatar asked May 04 '15 05:05

Manoj Dhiman


People also ask

How do you add before Span?

Just Add :before in your old CSS and change the block to inline-block so that it fits in a line and have a block for the whole span and rest change the css selectors to :before so that is takes its respective color.

How do you make an element square in CSS?

But it's possible to make an element square if you use units like px or em to set both dimensions. Show activity on this post. A CSS only solution can be found here on the last "Resize with content" update. Although it applies for circles, you can remove the border-radius: 50% to make it work for squares.

How do you make a square responsive in CSS?

By making use of the :after pseudo-element and 'padding-bottom' , we can create our responsive square using only CSS. The solution relies on the somewhat counterintuitive fact that padding is calculated as a percentage of its parent element's width, not height.

How do I make text a square box in HTML?

First, we create a <style> tag in the <head> section. Then define the CSS classes . CLASS-NAME { STYLES-TO-APPLY } in the <style> tag. Finally, simply attach the CSS classes to the HTML elements – <ELEMENT class="CLASS-NAME"> .


1 Answers

You need to add content: "" for span:before to work

#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>
like image 152
anpsmn Avatar answered Sep 20 '22 08:09

anpsmn