I'm adding row throught a <table>
with a foreach
.
At some point I want a cell to have a grey background based on the percentage calcultated in PHP.
Ex: 50% means half the background of the cell with be grey the rest will stay blank | 33,33% = 1/3 of the background etc..
The problems I've met is either the text in the <td>
got streched by any other div, if I apply the color to the <td>
I'll also override the text later on etc..
Here is the code :
$percent = 1/3; // For example
$percent_friendly = number_format( $percent * 100, 2 ); //This will return 33.33
echo '<td>'.$percent_friendly.' %
<div style="background-color: grey"> // So I want the grey to fill 33.33% of the space
</div>
<div style="background-color: white">
</div>
</td>';
and the style applied so far :
table {
margin-left:auto;
margin-right:auto;
font-size:18px;
}
table, th, td {
text-align: center;
border: 1px solid black;
position:relative;
}
I must be missing something but CSS really isn't my thing, any explanation or help would be greatly appreciated.
To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.
If that class has a background-color of blue, and you want your <div> to have a red background instead, try to change the color from blue to red in the class itself. You could also create a new CSS class that defined a background-color property with a value of red and let your <div> reference that class.
Margin is completely transparent, and it does not have any background color. It clears the area around the element.
Add div, ul, li { background-color: inherit; } to the end of your stylesheet and the background will be inherited all the way down and the border will not show up.
Here is a simple solution using 2 divs with a fixed width. The percentage is inserted directly in the markup. With setting the <p>
to an absolute position, you shouldn't have problems with stretching the td.
table {
border: 1px solid;
}
td {
height: 50px;
}
.progress {
padding:0;
width: 200px;
border: 1px solid #555;
background: #ddd;
position: relative;
}
.bar {
height: 100%;
background: #57a;
}
.bar p {
position: absolute;
text-align: center;
width: 100%;
margin: 0;
line-height: 30px;
}
<table>
<tr>
<td class="progress">
<div class="bar" style="width: 33.33%"><p>33.33% complete</p></div>
</td>
</tr>
<tr>
<td class="progress">
<div class="bar" style="width: 16.66%"><p>16.66% complete</p></div>
</td>
</tr>
<tr>
<td class="progress">
<div class="bar" style="width: 66.66%"><p>66.66% complete</p></div>
</td>
</tr>
</table>
Should work in all browsers, because it only uses elements width.
In the PHP:
echo "
<td>
<div class='progress-bar' style='width: $percent_friendly%'></div>
<span>$percent_friendly%</span>
</td>";
In the CSS:
td { position: relative; }
td .progress-bar { position: absolute; background: grey; top: 0; bottom: 0; left: 0; }
td span { position: relative; }
This lets you apply any structural/font styles to the td
element, and the progress bar will adjust correctly on its own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With