Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set width of spans without text using php?

How to set width of spans without text using php?

This is what I have tried:


for($i = 0; $i < 256; ++$i)
{
    echo '<span id="red_' . $i . '" style="width:10px;height:10px;background-color:red;"><span>';
}

The spans are not displaying. If I add text to spans then they are displaying but it’s width is equal to the width of text. I want the width to exactly 10px.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The element <span> is not a block element but an inline one. Therefore it does not have a width. The simplest way to set it as a block or at least an inline-block element.

for($i = 0; $i < 256; ++$i)
{
    //                                     ******* here *******
    echo '<span id="red_' . $i . '" style="display:inline-block;width:10px;height:10px;background-color:red;"></span>';
}

or

for($i = 0; $i < 256; ++$i)
{
    //                                     **** here ****
    echo '<span id="red_' . $i . '" style="display:block;width:10px;height:10px;background-color:red;"></span>';
}

The difference is inline-block does not change to a new line, but block changes to a new line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading