html - How can I center a right-aligned DIV? -


this question has answer here:

i have div has text right aligned. want center div within parent.

the code below applies right-alignment.

css

.outerdiv {     text-align: center;     width: 100%; }  .innerdiv {     text-align: right;     width: auto; } 

html

<div class="outerdiv">     <div class="innerdiv">         10<br/>         <span style="border-bottom: 1px solid black">             + 2         </span><br />         12     </div> </div> 

i know can done using margin-left: auto; margin-right: auto in outer div, requires set inner div's width, don't know ahead of time.

ideas?

since you're setting text-align: center on parent element, set display of inner element inline-block. in doing so, inner element centered horizontally due fact has "shrink-to-fit" width based on width of contents:

.outerdiv {    text-align: center;  }  .innerdiv {    display: inline-block;    text-align: right;  }  .innerdiv span {    display: block;    border-bottom: 1px solid black  }
<div class="outerdiv">    <div class="innerdiv">      10<span>+ 2</span>12    </div>  </div>

i removed <br /> elements, , changed display of span element block. replace p element (since it's block-level default).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -