0

I am able to vertically align text in span(which is inside div) but i need my page to be independent of screen resolution. have used line height feature to curb the my text from going out of the div when I change the resolution..but I see its not vertically aligned as middle in all resolution. what feature in css i should look into?

<div>
 <span style="width:49%; height:50%; top:25%; left:0%; line-height:50%;font-size:x-small;font-family:Verdana;font-weight:bold; position:absolute; display:block;">
      Random Text
    </span>
</div>

The above span tag is inside a div. Also What should be the recommended value of line-height.Should it be same as height of the span tag? I saw this link for understanding the vertical aligning of single line text.

> Some other CSS doubts>> 1. a textbox inside a div containing single line of text also goes out of the control while changing screen resolution. what could I possibly do to solve this?I am about to control it in a button

2 Answers2

3

Remove those position unless extremely needed and use, display:table-cell

Fiddle Demo

div  span {
    border:1px solid red;
    width:49%;
    height:50%;
    display:table-cell; /*check this */
    vertical-align:middle; /**aligns where you want it */
    font-size:x-small;
    font-family:Verdana;
    font-weight:bold;
}

You might wanna check out this thread too : Align multiple divs in one line and center text veticaly and horizontally

Community
  • 1
  • 1
NoobEditor
  • 14,239
  • 13
  • 66
  • 102
2

You could position the span element vertically at the middle by using top: 50% and adding a negative top margin which is equal to half of the height of the line.

Here you go:

EXAMPLE HERE

span {
  position:absolute;
  top: 50%; left:0;

  font-size: 0.7em; 
  line-height: 100%;  /* 100% of font-size */

  margin-top: -0.35em;
}

However if you're not eager to use absolute positioning, you could follow this approach.

Note: I've nothing against table-cells, except the effect relative positioning is undefined for such types of elements. If you don't mind, you could follow @NoobEditor's solution as well.

Community
  • 1
  • 1
Hashem Qolami
  • 88,138
  • 25
  • 132
  • 151