How to use negative padding in css
I want to add negative padding in css, I have written a small code of battery charging cell. What I want is if I enter value in negative like -1px than the cell color should move to the left side and div should stay in center.
.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>Please help me.
html css
add a comment |
I want to add negative padding in css, I have written a small code of battery charging cell. What I want is if I enter value in negative like -1px than the cell color should move to the left side and div should stay in center.
.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>Please help me.
html css
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09
add a comment |
I want to add negative padding in css, I have written a small code of battery charging cell. What I want is if I enter value in negative like -1px than the cell color should move to the left side and div should stay in center.
.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>Please help me.
html css
I want to add negative padding in css, I have written a small code of battery charging cell. What I want is if I enter value in negative like -1px than the cell color should move to the left side and div should stay in center.
.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>Please help me.
.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>.cell
width: 100px;
height: 30px;
border: 1px solid black;
.padding
background-color: #3D9970;
width: 10px;
float: left;
height: 30px;
position: absolute;
left: 55px;
padding-right: 1px;
<div class="cell">
<div class="cell1"></div>
<div class="padding"></div><span style="display: inline;">
</div>html css
html css
edited Nov 11 '18 at 18:55
Gerard
11.1k41938
11.1k41938
asked Nov 11 '18 at 18:36
Jawad AjazJawad Ajaz
63
63
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09
add a comment |
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09
add a comment |
3 Answers
3
active
oldest
votes
You can't.
See the specification:
Unlike margin properties, values for padding values cannot be negative.
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
add a comment |
I think you can achieve the same effect with pseudo elements:
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>"Left" property could be negative, so if you change it you can move the position of the green rectangle in the middle (.cell:before) of the block and border itself (.after)
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
add a comment |
The easiest way is to use an absolute positioning relatively to a parent node. Here the parent node would be the battery "housing".
So you can set the position CSS value of the rot div to relative, and then the charge one to absolute. Indeed, according to MDN Webdocs:
absolute: [...] It is positioned relative to its closest positioned ancestor, if any.
Then, you just have to play with the left and width CSS properties. For the "middle" case, I chose to display one border.
Below a working snippet. Just click the "Begin the charge variation" button to start the show.
var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251901%2fhow-to-use-negative-padding-in-css%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't.
See the specification:
Unlike margin properties, values for padding values cannot be negative.
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
add a comment |
You can't.
See the specification:
Unlike margin properties, values for padding values cannot be negative.
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
add a comment |
You can't.
See the specification:
Unlike margin properties, values for padding values cannot be negative.
You can't.
See the specification:
Unlike margin properties, values for padding values cannot be negative.
answered Nov 11 '18 at 18:44
QuentinQuentin
646k718761041
646k718761041
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
add a comment |
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
Is there any other solution?
– Jawad Ajaz
Nov 11 '18 at 18:55
add a comment |
I think you can achieve the same effect with pseudo elements:
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>"Left" property could be negative, so if you change it you can move the position of the green rectangle in the middle (.cell:before) of the block and border itself (.after)
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
add a comment |
I think you can achieve the same effect with pseudo elements:
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>"Left" property could be negative, so if you change it you can move the position of the green rectangle in the middle (.cell:before) of the block and border itself (.after)
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
add a comment |
I think you can achieve the same effect with pseudo elements:
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>"Left" property could be negative, so if you change it you can move the position of the green rectangle in the middle (.cell:before) of the block and border itself (.after)
I think you can achieve the same effect with pseudo elements:
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>"Left" property could be negative, so if you change it you can move the position of the green rectangle in the middle (.cell:before) of the block and border itself (.after)
.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>.cell
display:block;
width: 100px;
height: 30px;
position:relative;
.cell:before
content:'';
background-color: #3D9970;
width: 10px;
top:0;
left:calc(50% - 5px);
height: 100%;
position: absolute;
.cell:after
content:'';
border: 1px solid black;
width:100%;
height:100%;
display:block;
top:0;
left: 0px;
position: absolute;
<div class="cell">
</div>answered Nov 11 '18 at 19:09
TelaryTelary
112111
112111
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
add a comment |
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
no bro it's not what i want. i want to add padding on left side and div should stay where it is
– Jawad Ajaz
Nov 11 '18 at 19:18
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
what i want is if i'll add padding left than div should stay in center and padding should be added in left side and vice versa
– Jawad Ajaz
Nov 11 '18 at 19:49
add a comment |
The easiest way is to use an absolute positioning relatively to a parent node. Here the parent node would be the battery "housing".
So you can set the position CSS value of the rot div to relative, and then the charge one to absolute. Indeed, according to MDN Webdocs:
absolute: [...] It is positioned relative to its closest positioned ancestor, if any.
Then, you just have to play with the left and width CSS properties. For the "middle" case, I chose to display one border.
Below a working snippet. Just click the "Begin the charge variation" button to start the show.
var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>add a comment |
The easiest way is to use an absolute positioning relatively to a parent node. Here the parent node would be the battery "housing".
So you can set the position CSS value of the rot div to relative, and then the charge one to absolute. Indeed, according to MDN Webdocs:
absolute: [...] It is positioned relative to its closest positioned ancestor, if any.
Then, you just have to play with the left and width CSS properties. For the "middle" case, I chose to display one border.
Below a working snippet. Just click the "Begin the charge variation" button to start the show.
var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>add a comment |
The easiest way is to use an absolute positioning relatively to a parent node. Here the parent node would be the battery "housing".
So you can set the position CSS value of the rot div to relative, and then the charge one to absolute. Indeed, according to MDN Webdocs:
absolute: [...] It is positioned relative to its closest positioned ancestor, if any.
Then, you just have to play with the left and width CSS properties. For the "middle" case, I chose to display one border.
Below a working snippet. Just click the "Begin the charge variation" button to start the show.
var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>The easiest way is to use an absolute positioning relatively to a parent node. Here the parent node would be the battery "housing".
So you can set the position CSS value of the rot div to relative, and then the charge one to absolute. Indeed, according to MDN Webdocs:
absolute: [...] It is positioned relative to its closest positioned ancestor, if any.
Then, you just have to play with the left and width CSS properties. For the "middle" case, I chose to display one border.
Below a working snippet. Just click the "Begin the charge variation" button to start the show.
var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>var chargeElement = document.getElementById("charge");
// To set a charge to the battery, simply call: setCharge(percentage)
function setCharge(percentage)
var left;
var width;
if (percentage > 100) percentage = 100;
if (percentage < 0) percentage = 0;
chargeElement.setAttribute("data-value", percentage);
// If the charge is 50%, simply draw a line
if (percentage == 50)
chargeElement.className = "middle";
else
chargeElement.className = "";
// Otherwise, adjust left and width values
if (percentage >= 50)
left = 50;
width = percentage - left;
else
left = percentage;
width = 50 - left;
// Then update the charge style.
chargeElement.style.left = left + "%";
chargeElement.style.width = width + "%";
// A simple function to add / remove some charge
function addCharge(percentage)
var value = parseInt(chargeElement.getAttribute("data-value"));
value += percentage;
setCharge(value);
// Here just some stuff for illustration.
// You don't need those functions to set the charge.
function letsBeginTheShow(buttonElement)
buttonElement.disabled = true;
setNextCharge(10);
function setNextCharge(increment)
var percentage = parseInt(chargeElement.getAttribute("data-value"))
percentage += increment;
if (percentage > 100)
percentage = 100;
increment = -5;
if (percentage < 0)
percentage = 0;
increment = 5;
setCharge(percentage);
setTimeout(function()
setNextCharge(increment);
, 50);
setCharge(50);.battery
position: relative;
width: 100px;
height: 30px;
border: 1px solid black;
/* Below : only for aestethic reasons */
float: left;
margin-right: 30px;
/* End of aesthethic stuff */
#charge
height: 100%;
position: absolute;
background-color: #3D9970;
border-color: #3D9970;
.middle
border-left: 1px solid;
<div class="battery">
<div id="charge" data-value="50" class="middle"></div>
</div>
<button onclick="letsBeginTheShow(this)">Begin the charge variation</button>edited Nov 11 '18 at 21:53
answered Nov 11 '18 at 20:28
AmessihelAmessihel
2,1361724
2,1361724
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251901%2fhow-to-use-negative-padding-in-css%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can elaborate more what you want to achieve [forget how you already did]
– Temani Afif
Nov 11 '18 at 19:06
assume that one side of cell's value is - center is 0 and other side is + if someone enter a value of -0.5 than padding should add on left side from center, and if value is entered 1 than 1% padding should added from right side on padding div
– Jawad Ajaz
Nov 11 '18 at 19:09