Wrap background color around text aligned right












3















What I'm trying to do:



I'm trying to achieve the following result:






div {
text-align: right;
}

span {
display: inline-block;
background: #000;
color: #fff;
}

<div>
<span>abc</span><br/>
<span>Hello world this is some text</span><br />
<span>hello world</span>
</div>





However, in the above example, I have split my lines up individually, as I have contained each line in a separate span.



I want to achieve the same (or similar) result as above by using a width to define when a new line should start, rather than containing each new line in its own span.



What I've tried:



I've tried doing the following:






div {
text-align: center;
}

span {
display: inline-block;
background: #000;
color: #fff;
width: 200px; /* Width to define where line break should appear */
}

<div style="text-align: right;">
<span>abc</span><br/>
<span>Hello world this is some text hello world</span>
</div>





However, in the above example, the black background covers a "block" rather than wrapping around each line of text.



So, how do I get my second snippet to act in the same way as the first?










share|improve this question





























    3















    What I'm trying to do:



    I'm trying to achieve the following result:






    div {
    text-align: right;
    }

    span {
    display: inline-block;
    background: #000;
    color: #fff;
    }

    <div>
    <span>abc</span><br/>
    <span>Hello world this is some text</span><br />
    <span>hello world</span>
    </div>





    However, in the above example, I have split my lines up individually, as I have contained each line in a separate span.



    I want to achieve the same (or similar) result as above by using a width to define when a new line should start, rather than containing each new line in its own span.



    What I've tried:



    I've tried doing the following:






    div {
    text-align: center;
    }

    span {
    display: inline-block;
    background: #000;
    color: #fff;
    width: 200px; /* Width to define where line break should appear */
    }

    <div style="text-align: right;">
    <span>abc</span><br/>
    <span>Hello world this is some text hello world</span>
    </div>





    However, in the above example, the black background covers a "block" rather than wrapping around each line of text.



    So, how do I get my second snippet to act in the same way as the first?










    share|improve this question



























      3












      3








      3








      What I'm trying to do:



      I'm trying to achieve the following result:






      div {
      text-align: right;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      }

      <div>
      <span>abc</span><br/>
      <span>Hello world this is some text</span><br />
      <span>hello world</span>
      </div>





      However, in the above example, I have split my lines up individually, as I have contained each line in a separate span.



      I want to achieve the same (or similar) result as above by using a width to define when a new line should start, rather than containing each new line in its own span.



      What I've tried:



      I've tried doing the following:






      div {
      text-align: center;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      width: 200px; /* Width to define where line break should appear */
      }

      <div style="text-align: right;">
      <span>abc</span><br/>
      <span>Hello world this is some text hello world</span>
      </div>





      However, in the above example, the black background covers a "block" rather than wrapping around each line of text.



      So, how do I get my second snippet to act in the same way as the first?










      share|improve this question
















      What I'm trying to do:



      I'm trying to achieve the following result:






      div {
      text-align: right;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      }

      <div>
      <span>abc</span><br/>
      <span>Hello world this is some text</span><br />
      <span>hello world</span>
      </div>





      However, in the above example, I have split my lines up individually, as I have contained each line in a separate span.



      I want to achieve the same (or similar) result as above by using a width to define when a new line should start, rather than containing each new line in its own span.



      What I've tried:



      I've tried doing the following:






      div {
      text-align: center;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      width: 200px; /* Width to define where line break should appear */
      }

      <div style="text-align: right;">
      <span>abc</span><br/>
      <span>Hello world this is some text hello world</span>
      </div>





      However, in the above example, the black background covers a "block" rather than wrapping around each line of text.



      So, how do I get my second snippet to act in the same way as the first?






      div {
      text-align: right;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      }

      <div>
      <span>abc</span><br/>
      <span>Hello world this is some text</span><br />
      <span>hello world</span>
      </div>





      div {
      text-align: right;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      }

      <div>
      <span>abc</span><br/>
      <span>Hello world this is some text</span><br />
      <span>hello world</span>
      </div>





      div {
      text-align: center;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      width: 200px; /* Width to define where line break should appear */
      }

      <div style="text-align: right;">
      <span>abc</span><br/>
      <span>Hello world this is some text hello world</span>
      </div>





      div {
      text-align: center;
      }

      span {
      display: inline-block;
      background: #000;
      color: #fff;
      width: 200px; /* Width to define where line break should appear */
      }

      <div style="text-align: right;">
      <span>abc</span><br/>
      <span>Hello world this is some text hello world</span>
      </div>






      html css






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 9:18









      Paolo Forgia

      4,51262849




      4,51262849










      asked Nov 22 '18 at 0:59









      Nick ParsonsNick Parsons

      4,8622721




      4,8622721
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Add another wrapper that you keep inline:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          You can also adjust padding and keep the default line-height:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>








          share|improve this answer


























          • Ah, I see. Is there any way to remove the line gaps between each line though?

            – Nick Parsons
            Nov 22 '18 at 1:18








          • 1





            @NickParsons yes by adjusting line-height, check the update

            – Temani Afif
            Nov 22 '18 at 1:19






          • 1





            @NickParsons added another way to control the gap

            – Temani Afif
            Nov 22 '18 at 1:23











          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%2f53422511%2fwrap-background-color-around-text-aligned-right%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Add another wrapper that you keep inline:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          You can also adjust padding and keep the default line-height:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>








          share|improve this answer


























          • Ah, I see. Is there any way to remove the line gaps between each line though?

            – Nick Parsons
            Nov 22 '18 at 1:18








          • 1





            @NickParsons yes by adjusting line-height, check the update

            – Temani Afif
            Nov 22 '18 at 1:19






          • 1





            @NickParsons added another way to control the gap

            – Temani Afif
            Nov 22 '18 at 1:23
















          3














          Add another wrapper that you keep inline:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          You can also adjust padding and keep the default line-height:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>








          share|improve this answer


























          • Ah, I see. Is there any way to remove the line gaps between each line though?

            – Nick Parsons
            Nov 22 '18 at 1:18








          • 1





            @NickParsons yes by adjusting line-height, check the update

            – Temani Afif
            Nov 22 '18 at 1:19






          • 1





            @NickParsons added another way to control the gap

            – Temani Afif
            Nov 22 '18 at 1:23














          3












          3








          3







          Add another wrapper that you keep inline:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          You can also adjust padding and keep the default line-height:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>








          share|improve this answer















          Add another wrapper that you keep inline:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          You can also adjust padding and keep the default line-height:






          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>








          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          div {
          text-align: center;
          }

          span {
          display: inline-block;
          line-height:1; /*remove the gap*/
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>





          div {
          text-align: center;
          }

          span {
          display: inline-block;
          color: #fff;
          width: 200px; /* Width to define where line break should appear */
          }
          span > span {
          display:inline;
          background: #000;
          padding:2px 0;
          }

          <div style="text-align: right;">
          <span><span>abc</span></span><br>
          <span><span>Hello world this is some text hello world</span></span>
          </div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 1:22

























          answered Nov 22 '18 at 1:17









          Temani AfifTemani Afif

          66.8k93776




          66.8k93776













          • Ah, I see. Is there any way to remove the line gaps between each line though?

            – Nick Parsons
            Nov 22 '18 at 1:18








          • 1





            @NickParsons yes by adjusting line-height, check the update

            – Temani Afif
            Nov 22 '18 at 1:19






          • 1





            @NickParsons added another way to control the gap

            – Temani Afif
            Nov 22 '18 at 1:23



















          • Ah, I see. Is there any way to remove the line gaps between each line though?

            – Nick Parsons
            Nov 22 '18 at 1:18








          • 1





            @NickParsons yes by adjusting line-height, check the update

            – Temani Afif
            Nov 22 '18 at 1:19






          • 1





            @NickParsons added another way to control the gap

            – Temani Afif
            Nov 22 '18 at 1:23

















          Ah, I see. Is there any way to remove the line gaps between each line though?

          – Nick Parsons
          Nov 22 '18 at 1:18







          Ah, I see. Is there any way to remove the line gaps between each line though?

          – Nick Parsons
          Nov 22 '18 at 1:18






          1




          1





          @NickParsons yes by adjusting line-height, check the update

          – Temani Afif
          Nov 22 '18 at 1:19





          @NickParsons yes by adjusting line-height, check the update

          – Temani Afif
          Nov 22 '18 at 1:19




          1




          1





          @NickParsons added another way to control the gap

          – Temani Afif
          Nov 22 '18 at 1:23





          @NickParsons added another way to control the gap

          – Temani Afif
          Nov 22 '18 at 1:23


















          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%2f53422511%2fwrap-background-color-around-text-aligned-right%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'