1

I wanted to develop a calculator using Javascript. I am stuck in the design. The text box display is going beyond the border of the table cell. Following is the

 /*The Style file is the following*/
 
 table{
    	border:1px solid silver;
    	border-radius:5px;
    }
    
    .btn{
    	width:75px;
    	height:45px;
    }
    
    #equalToBtn{
    	background-color:blue;
    	color:white;
    	border-color:blue;
    }
    
    #resultText{
    	width:100%;
    	height:40px;
    	margin-right:2px;
    }
<!--HTML code-->

    <!DOCTYPE html>
    <html>
    	<head>
    		<link rel="stylesheet" href="calculator.css">
    	</head>
    	<body>
    		<table>
    			<tr>
    			<td colspan="4"><input type="text" id="resultText"></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">7</button></td>
    				<td><button class="btn" type="button">8</button></td>
    				<td><button class="btn" type="button">9</button></td>
    				<td><button class="btn" type="button">/</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">4</button></td>
    				<td><button class="btn" type="button">5</button></td>
    				<td><button class="btn" type="button">6</button></td>
    				<td><button class="btn" type="button">*</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">1</button></td>
    				<td><button class="btn" type="button">2</button></td>
    				<td><button class="btn" type="button">3</button></td>
    				<td><button class="btn" type="button">-</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">0</button></td>
    				<td><button class="btn" type="button">.</button></td>
    				<td><button class="btn" id="equalToBtn" type="button">=</button></td>
    				<td><button class="btn" type="button">+</button></td>
    			</tr>
    		</table>
    	</body>
    </html>



   

I have put width of the text 100% and it overflows. Any help would be appreciated.

Thanks in advance!!!

1 Answer 1

1

added

*{
box-sizing: border-box;
}

it was going out because of padding given to the input. hope this helps.

/*The Style file is the following*/

*{
box-sizing: border-box;
}
 
 table{
    	border:1px solid silver;
    	border-radius:5px;
    }
    
    .btn{
    	width:75px;
    	height:45px;
    }
    
    #equalToBtn{
    	background-color:blue;
    	color:white;
    	border-color:blue;
    }
    
    #resultText{
    	width:100%;
    	height:40px;
    	margin-right:2px;
    }
<!--HTML code-->

    <!DOCTYPE html>
    <html>
    	<head>
    		<link rel="stylesheet" href="calculator.css">
    	</head>
    	<body>
    		<table>
    			<tr>
    			<td colspan="4"><input type="text" id="resultText"></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">7</button></td>
    				<td><button class="btn" type="button">8</button></td>
    				<td><button class="btn" type="button">9</button></td>
    				<td><button class="btn" type="button">/</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">4</button></td>
    				<td><button class="btn" type="button">5</button></td>
    				<td><button class="btn" type="button">6</button></td>
    				<td><button class="btn" type="button">*</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">1</button></td>
    				<td><button class="btn" type="button">2</button></td>
    				<td><button class="btn" type="button">3</button></td>
    				<td><button class="btn" type="button">-</button></td>
    			</tr>
    			<tr>
    				<td><button class="btn" type="button">0</button></td>
    				<td><button class="btn" type="button">.</button></td>
    				<td><button class="btn" id="equalToBtn" type="button">=</button></td>
    				<td><button class="btn" type="button">+</button></td>
    			</tr>
    		</table>
    	</body>
    </html>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the answer. Is it that making padding 0px to the input does the same thing as box-sizing:border-box
no box-sizing:border-box will adjust the width with padding it will go out of the div but it will make the input smaller. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.