How to solve this weird issue in html table and css





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a weird issue regarding my table content



.table {
position:absolute;
top:12px;
left:3px;
width:87px;
height:74px;
}

.table tr {
vertical-align:middle;
}

.table td {
text-align:center;
padding:1px;color:#000000;
font-size:1em;
line-height:1.5em;
width: 50px;
}

.table td span {
padding:3px 5px;
font-size:1em;
background-color:yellow;
}


HTML



<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><span>example 1</span>
</td>
</tr>
</tbody>
</table>


I want the text inside span have a white background, but I don't want the white background extend to only the edge of the texts, so I applied padding.



However, some of the texts are inside the background, but some of them are not.



for example
some texts are like these...
-------
|example|
-------

or

--------
| example|
--------


This is what I want.
---------
| example |
---------


I have give td, span and tr larger width but it still not working well.










share|improve this question

























  • Can you reproduce this on jsfiddle? What doctype do you have in your html?

    – Lucas
    Apr 30 '13 at 22:39











  • I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

    – Rouge
    Apr 30 '13 at 22:41


















0















I have a weird issue regarding my table content



.table {
position:absolute;
top:12px;
left:3px;
width:87px;
height:74px;
}

.table tr {
vertical-align:middle;
}

.table td {
text-align:center;
padding:1px;color:#000000;
font-size:1em;
line-height:1.5em;
width: 50px;
}

.table td span {
padding:3px 5px;
font-size:1em;
background-color:yellow;
}


HTML



<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><span>example 1</span>
</td>
</tr>
</tbody>
</table>


I want the text inside span have a white background, but I don't want the white background extend to only the edge of the texts, so I applied padding.



However, some of the texts are inside the background, but some of them are not.



for example
some texts are like these...
-------
|example|
-------

or

--------
| example|
--------


This is what I want.
---------
| example |
---------


I have give td, span and tr larger width but it still not working well.










share|improve this question

























  • Can you reproduce this on jsfiddle? What doctype do you have in your html?

    – Lucas
    Apr 30 '13 at 22:39











  • I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

    – Rouge
    Apr 30 '13 at 22:41














0












0








0








I have a weird issue regarding my table content



.table {
position:absolute;
top:12px;
left:3px;
width:87px;
height:74px;
}

.table tr {
vertical-align:middle;
}

.table td {
text-align:center;
padding:1px;color:#000000;
font-size:1em;
line-height:1.5em;
width: 50px;
}

.table td span {
padding:3px 5px;
font-size:1em;
background-color:yellow;
}


HTML



<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><span>example 1</span>
</td>
</tr>
</tbody>
</table>


I want the text inside span have a white background, but I don't want the white background extend to only the edge of the texts, so I applied padding.



However, some of the texts are inside the background, but some of them are not.



for example
some texts are like these...
-------
|example|
-------

or

--------
| example|
--------


This is what I want.
---------
| example |
---------


I have give td, span and tr larger width but it still not working well.










share|improve this question
















I have a weird issue regarding my table content



.table {
position:absolute;
top:12px;
left:3px;
width:87px;
height:74px;
}

.table tr {
vertical-align:middle;
}

.table td {
text-align:center;
padding:1px;color:#000000;
font-size:1em;
line-height:1.5em;
width: 50px;
}

.table td span {
padding:3px 5px;
font-size:1em;
background-color:yellow;
}


HTML



<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><span>example 1</span>
</td>
</tr>
</tbody>
</table>


I want the text inside span have a white background, but I don't want the white background extend to only the edge of the texts, so I applied padding.



However, some of the texts are inside the background, but some of them are not.



for example
some texts are like these...
-------
|example|
-------

or

--------
| example|
--------


This is what I want.
---------
| example |
---------


I have give td, span and tr larger width but it still not working well.







html css html-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 8:04









Brian Tompsett - 汤莱恩

4,2671339104




4,2671339104










asked Apr 30 '13 at 22:33









RougeRouge

1,75162235




1,75162235













  • Can you reproduce this on jsfiddle? What doctype do you have in your html?

    – Lucas
    Apr 30 '13 at 22:39











  • I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

    – Rouge
    Apr 30 '13 at 22:41



















  • Can you reproduce this on jsfiddle? What doctype do you have in your html?

    – Lucas
    Apr 30 '13 at 22:39











  • I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

    – Rouge
    Apr 30 '13 at 22:41

















Can you reproduce this on jsfiddle? What doctype do you have in your html?

– Lucas
Apr 30 '13 at 22:39





Can you reproduce this on jsfiddle? What doctype do you have in your html?

– Lucas
Apr 30 '13 at 22:39













I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

– Rouge
Apr 30 '13 at 22:41





I can't but my doctype is <html xmlns="w3.org/1999/xhtml">

– Rouge
Apr 30 '13 at 22:41












2 Answers
2






active

oldest

votes


















3














Setting display: inline-block; on your span should fix this.



As is pointed out in the comments below, span is by default an inline element, but that does not mean that you can't set its padding, setting the padding works just fine on inline elements. Your problem lies with the fact that you have set your table to be 87px wide, and the td to be 50px wide. Now the text inside your span does not fit inside the td, and because it is an inline element, it flows on to the next line. Setting display: inline-block; forces the text to not flow onto the next line. Another way to fix this is by setting your table and your td to be wider, so that it can fit more text on one line.



What you were observing with the "missing" padding on one or on both sides of your span, was not really "missing" padding. The padding is added at the start, at the end, at the top and at the bottom of the span. So in the places where the padding was "missing", it was simply not there because you were looking at a line-break, not at the end or the start of the span. Another way you could have fixed this is by setting white-space: nowrap; on the span. That's another way to ensure that the text inside a span does not wrap onto the next line.






share|improve this answer


























  • it does work. would you care to explain why it works?

    – Rouge
    Apr 30 '13 at 22:42











  • <span> are inline elements. inline-block allows them to have height, width, etc..

    – Biotox
    Apr 30 '13 at 22:44





















2














Your problem is that the span elemnt has display:inline by default. inline elements can't be sized and always wrap their content. By setting display: inline-block on your span you make it behave halfway between inline (can be stacked horizontally, etc.) and block, i.e., it can be sized. The default for a non-positioned block element is to fill horizontally its container and that's why it works.






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%2f16310040%2fhow-to-solve-this-weird-issue-in-html-table-and-css%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Setting display: inline-block; on your span should fix this.



    As is pointed out in the comments below, span is by default an inline element, but that does not mean that you can't set its padding, setting the padding works just fine on inline elements. Your problem lies with the fact that you have set your table to be 87px wide, and the td to be 50px wide. Now the text inside your span does not fit inside the td, and because it is an inline element, it flows on to the next line. Setting display: inline-block; forces the text to not flow onto the next line. Another way to fix this is by setting your table and your td to be wider, so that it can fit more text on one line.



    What you were observing with the "missing" padding on one or on both sides of your span, was not really "missing" padding. The padding is added at the start, at the end, at the top and at the bottom of the span. So in the places where the padding was "missing", it was simply not there because you were looking at a line-break, not at the end or the start of the span. Another way you could have fixed this is by setting white-space: nowrap; on the span. That's another way to ensure that the text inside a span does not wrap onto the next line.






    share|improve this answer


























    • it does work. would you care to explain why it works?

      – Rouge
      Apr 30 '13 at 22:42











    • <span> are inline elements. inline-block allows them to have height, width, etc..

      – Biotox
      Apr 30 '13 at 22:44


















    3














    Setting display: inline-block; on your span should fix this.



    As is pointed out in the comments below, span is by default an inline element, but that does not mean that you can't set its padding, setting the padding works just fine on inline elements. Your problem lies with the fact that you have set your table to be 87px wide, and the td to be 50px wide. Now the text inside your span does not fit inside the td, and because it is an inline element, it flows on to the next line. Setting display: inline-block; forces the text to not flow onto the next line. Another way to fix this is by setting your table and your td to be wider, so that it can fit more text on one line.



    What you were observing with the "missing" padding on one or on both sides of your span, was not really "missing" padding. The padding is added at the start, at the end, at the top and at the bottom of the span. So in the places where the padding was "missing", it was simply not there because you were looking at a line-break, not at the end or the start of the span. Another way you could have fixed this is by setting white-space: nowrap; on the span. That's another way to ensure that the text inside a span does not wrap onto the next line.






    share|improve this answer


























    • it does work. would you care to explain why it works?

      – Rouge
      Apr 30 '13 at 22:42











    • <span> are inline elements. inline-block allows them to have height, width, etc..

      – Biotox
      Apr 30 '13 at 22:44
















    3












    3








    3







    Setting display: inline-block; on your span should fix this.



    As is pointed out in the comments below, span is by default an inline element, but that does not mean that you can't set its padding, setting the padding works just fine on inline elements. Your problem lies with the fact that you have set your table to be 87px wide, and the td to be 50px wide. Now the text inside your span does not fit inside the td, and because it is an inline element, it flows on to the next line. Setting display: inline-block; forces the text to not flow onto the next line. Another way to fix this is by setting your table and your td to be wider, so that it can fit more text on one line.



    What you were observing with the "missing" padding on one or on both sides of your span, was not really "missing" padding. The padding is added at the start, at the end, at the top and at the bottom of the span. So in the places where the padding was "missing", it was simply not there because you were looking at a line-break, not at the end or the start of the span. Another way you could have fixed this is by setting white-space: nowrap; on the span. That's another way to ensure that the text inside a span does not wrap onto the next line.






    share|improve this answer















    Setting display: inline-block; on your span should fix this.



    As is pointed out in the comments below, span is by default an inline element, but that does not mean that you can't set its padding, setting the padding works just fine on inline elements. Your problem lies with the fact that you have set your table to be 87px wide, and the td to be 50px wide. Now the text inside your span does not fit inside the td, and because it is an inline element, it flows on to the next line. Setting display: inline-block; forces the text to not flow onto the next line. Another way to fix this is by setting your table and your td to be wider, so that it can fit more text on one line.



    What you were observing with the "missing" padding on one or on both sides of your span, was not really "missing" padding. The padding is added at the start, at the end, at the top and at the bottom of the span. So in the places where the padding was "missing", it was simply not there because you were looking at a line-break, not at the end or the start of the span. Another way you could have fixed this is by setting white-space: nowrap; on the span. That's another way to ensure that the text inside a span does not wrap onto the next line.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 30 '13 at 23:07

























    answered Apr 30 '13 at 22:41









    Mathijs FlietstraMathijs Flietstra

    11.5k32963




    11.5k32963













    • it does work. would you care to explain why it works?

      – Rouge
      Apr 30 '13 at 22:42











    • <span> are inline elements. inline-block allows them to have height, width, etc..

      – Biotox
      Apr 30 '13 at 22:44





















    • it does work. would you care to explain why it works?

      – Rouge
      Apr 30 '13 at 22:42











    • <span> are inline elements. inline-block allows them to have height, width, etc..

      – Biotox
      Apr 30 '13 at 22:44



















    it does work. would you care to explain why it works?

    – Rouge
    Apr 30 '13 at 22:42





    it does work. would you care to explain why it works?

    – Rouge
    Apr 30 '13 at 22:42













    <span> are inline elements. inline-block allows them to have height, width, etc..

    – Biotox
    Apr 30 '13 at 22:44







    <span> are inline elements. inline-block allows them to have height, width, etc..

    – Biotox
    Apr 30 '13 at 22:44















    2














    Your problem is that the span elemnt has display:inline by default. inline elements can't be sized and always wrap their content. By setting display: inline-block on your span you make it behave halfway between inline (can be stacked horizontally, etc.) and block, i.e., it can be sized. The default for a non-positioned block element is to fill horizontally its container and that's why it works.






    share|improve this answer




























      2














      Your problem is that the span elemnt has display:inline by default. inline elements can't be sized and always wrap their content. By setting display: inline-block on your span you make it behave halfway between inline (can be stacked horizontally, etc.) and block, i.e., it can be sized. The default for a non-positioned block element is to fill horizontally its container and that's why it works.






      share|improve this answer


























        2












        2








        2







        Your problem is that the span elemnt has display:inline by default. inline elements can't be sized and always wrap their content. By setting display: inline-block on your span you make it behave halfway between inline (can be stacked horizontally, etc.) and block, i.e., it can be sized. The default for a non-positioned block element is to fill horizontally its container and that's why it works.






        share|improve this answer













        Your problem is that the span elemnt has display:inline by default. inline elements can't be sized and always wrap their content. By setting display: inline-block on your span you make it behave halfway between inline (can be stacked horizontally, etc.) and block, i.e., it can be sized. The default for a non-positioned block element is to fill horizontally its container and that's why it works.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 30 '13 at 22:46









        fogfog

        2,0831720




        2,0831720






























            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%2f16310040%2fhow-to-solve-this-weird-issue-in-html-table-and-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 to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?