How to use negative padding in css










0















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.










share|improve this question
























  • 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
















0















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.










share|improve this question
























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













3 Answers
3






active

oldest

votes


















3














You can't.




See the specification:




Unlike margin properties, values for padding values cannot be negative.







share|improve this answer























  • Is there any other solution?

    – Jawad Ajaz
    Nov 11 '18 at 18:55


















0














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)






share|improve this answer























  • 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



















0














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>








share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    3














    You can't.




    See the specification:




    Unlike margin properties, values for padding values cannot be negative.







    share|improve this answer























    • Is there any other solution?

      – Jawad Ajaz
      Nov 11 '18 at 18:55















    3














    You can't.




    See the specification:




    Unlike margin properties, values for padding values cannot be negative.







    share|improve this answer























    • Is there any other solution?

      – Jawad Ajaz
      Nov 11 '18 at 18:55













    3












    3








    3







    You can't.




    See the specification:




    Unlike margin properties, values for padding values cannot be negative.







    share|improve this answer













    You can't.




    See the specification:




    Unlike margin properties, values for padding values cannot be negative.








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 '18 at 18:44









    QuentinQuentin

    646k718761041




    646k718761041












    • 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





    Is there any other solution?

    – Jawad Ajaz
    Nov 11 '18 at 18:55













    0














    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)






    share|improve this answer























    • 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
















    0














    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)






    share|improve this answer























    • 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














    0












    0








    0







    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)






    share|improve this answer













    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>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    0














    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>








    share|improve this answer





























      0














      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>








      share|improve this answer



























        0












        0








        0







        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>








        share|improve this answer















        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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 '18 at 21:53

























        answered Nov 11 '18 at 20:28









        AmessihelAmessihel

        2,1361724




        2,1361724



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ