Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting width from percentage to pixels

I have a table inside a <div>:

<div id="fixeddiv">
    <table id="fixedtable">
        <tr class="firstrow">
            <td class="td11"></td>
            <td class="td12"></td>
            <td class="td13"></td>
        </tr>

        <tr class="secondrow">
            <td class="td21" style="width:10%"></td>
            <td class="td22" style="width:20%"></td> 
            <td class="td23" style="width:70%"></td>
        </tr>
    </table>
</div>

CSS:

#fixeddiv
{
    overflow:auto;
    margin:0px;
    padding:0px;
    position: relative;
    text-align:left;
    width: 48%;
}

#fixedtable
{
    background-color: White;
    border-spacing:0px;
    cursor: pointer;
    width: 100%;
    height: 100%;
    font-family: Calibri !important;
    color: Black;
    font-size: 14px;
}

.firstrow
{
    position: absolute;
    margin: 0px;
    left: 0;
    top: 0;
    background: url(../Content/Images/header.jpg) repeat-x center top;
    color: White;
    font-weight: bold;
    text-align: center;

}
#fixedtable tr td
{
    padding: 5px !important;
    border: 1px solid #FFFFFF;
    text-align: center;
}

I am calculating the width of td21 with $('.td21').width() and assigning the width to td11 like $('td11').width($('.td21').width()).

The problem is that the widths applying are not same, they vary by 1px and I could not find how this 1px difference occur. The .td21 width is 1px greater than .td11.

Can any one please help me to find the solution?

like image 499
Swetha Bindu Avatar asked Jul 06 '12 07:07

Swetha Bindu


People also ask

How do you convert percentage to pixels?

Pixel (Px) = (Percent * Base)/100 for example, if the Percent value is 3% and the text size or font size (Base) is 16, the pixels = (3*16)/100 = 0.48 .

What is difference between percentage and pixel?

Pixel is a static measurement, while percent and EM are relative measurements. Percent depends on its parent font size. EM is relative to the current font size of the element (2em means 2 times the size of the current font).

How wide is 1 px?

A pixel is a unit of measurement commonly used in graphic and web design. We can abbreviate it using "px". You can convert mm to pixels – the length of a pixel is 0.26 mm or 0.0104 inches (provided that the resolution equals 96).

What is 100px in percentage?

(A / B) * 100 (760/1024) * 100 = 74.22% ..


1 Answers

<div id="div-1" style="width: 1000px;">
    <div id="div-2" style="width: 10%;"></div>
</div>

<script language="javascript">                
    var percents = parseInt(document.getElementById("div-2").style.width);
    var parentWidth = parseInt(document.getElementById("div-1").style.width);
    var pixels = parentWidth*(percents/100);
    alert(pixels); // will print "100"
</script>
like image 130
Nikola K. Avatar answered Oct 04 '22 00:10

Nikola K.