{"id":1058,"date":"2010-06-09T17:06:49","date_gmt":"2010-06-09T17:06:49","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1058"},"modified":"2012-01-18T11:20:50","modified_gmt":"2012-01-18T11:20:50","slug":"using-inline-block-to-centre-text-and-other-items-e-g-on-a-primefaces-command-button","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1058","title":{"rendered":"Centre text and other items e.g. on a Primefaces command button"},"content":{"rendered":"<p><strong><span style=\"text-decoration: underline;\">Update<\/span><\/strong><\/p>\n<p>This post originally used <em>display:inline-block<\/em> and <em>vertical-align:middle<\/em> thinking mistakenly that these were part of the fix for the problem.<br \/>\nSee <a title=\"http:\/\/salientsoft.co.uk\/?p=1084\" href=\"http:\/\/salientsoft.co.uk\/?p=1084\">here <\/a>for a more complete understanding of it.<br \/>\nThe modified CSS below works fine just by removing the padding and setting the font.<\/p>\n<p>Taking the Primefaces commandButton as an example, the label is embedded in a span:-<\/p>\n<p style=\"padding-left: 30px;\">&lt;button attr1=value attr2=value\u2026&gt;<br \/>\n&lt;span&gt;Submit&lt;\/span<br \/>\n&lt;\/button&gt;<\/p>\n<p>The button has many attributes which are not relevant to this discussion.<br \/>\nHowever, as the span is an inline element, it does not have a top\/bottom margin, width or height and using <em>text-align:center;<\/em> in css has no effect.<\/p>\n<p>The default Primefaces button has fixed padding for the label, which works fine by default but which is a pain when you change the size of either the button or the text.<\/p>\n<p>The following rules remove the padding and set the desired font correctly to centre the text for the Primefaces <em>commnadButton:-<\/em><\/p>\n<p style=\"padding-left: 30px;\">.ss-textbutton {<br \/>\nheight:20px;<br \/>\nwidth:52px;<br \/>\n}<br \/>\n.ss-textbutton &gt; .ui-button-text {<br \/>\nfont-size:90%;<br \/>\nfont-weight: bold;<br \/>\npadding:0px;<br \/>\n}<\/p>\n<p>This CSS is activated when the class <em>ss-textbutton<\/em> is passed to the <em>styleClass<\/em> attribute of the <em>commandButton<\/em> tag.<br \/>\nThis example declares a 52 x 20 button. It then targets the &lt;span&gt; within the button using <em>ss-textbutton &gt; ui-button-text<\/em>, as the &lt;span&gt; in the commandButton has the <em>ui-button-text<\/em> class. It does the following to the &lt;span&gt;:-<\/p>\n<ul>\n<li>sets a font size and weight<\/li>\n<li>removes all the padding, so that the centring works correctly<\/li>\n<\/ul>\n<p>In this example, I have deliberately not changed all buttons globally. The Primefaces documentation explains how to use the classes\u00a0 ui-button, <em>ui-button-text <\/em>and <em>ui-button-text-only<\/em> to skin all buttons globally if this is desired.<\/p>\n<p>This now allows us to resize our button, change the font size and the text, and still have the label nicely centred. We don\u2019t have to mess around with tweaking the padding and other attributes in every case, which is a whole lot simpler and more modular.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update This post originally used display:inline-block and vertical-align:middle thinking mistakenly that these were part of the fix for the problem. See here for a more complete understanding of it. The modified CSS below works fine just by removing the padding and setting the font. Taking the Primefaces commandButton as an example, the label is embedded [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[82],"tags":[189,40,78,171,112,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1058"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1058"}],"version-history":[{"count":8,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":1701,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1058\/revisions\/1701"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}