Vertically and horizontally centering text in circle in CSS (like iphone notification badge)

后端 未结 5 653
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 06:50

I\'m looking for a way of to do a cross-browser iphone-like badge in CSS3. I\'d obviously like to use one div for this, but alternative solutions would be fine. The importan

5条回答
  •  青春惊慌失措
    2020-12-04 07:24

    If you have content with height unknown but you know the height the of container. The following solution works extremely well.

    HTML

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt obcaecati maiores nulla praesentium amet explicabo ex iste asperiores nisi porro sequi eaque rerum necessitatibus molestias architecto eum velit recusandae ratione.

    CSS

    .center-test { 
       width: 300px; 
       height: 300px; 
       text-align: 
       center; 
       background-color: #333; 
    }
    .center-test span { 
       height: 300px; 
       display: inline-block; 
       zoom: 1; 
       *display: inline; 
       vertical-align: middle; 
     }
    .center-test p { 
       display: inline-block; 
       zoom: 1; 
       *display: inline; 
       vertical-align: middle; 
       color: #fff; 
     }
    

    EXAMPLE http://jsfiddle.net/thenewconfection/eYtVN/

    One gotcha for newby's to display: inline-block; [span] and [p] have no html white space so that the span then doesn't take up any space. Also I've added in the CSS hack for display inline-block for IE. Hope this helps someone!

提交回复
热议问题