{"id":3083,"date":"2020-09-16T16:13:17","date_gmt":"2020-09-16T21:13:17","guid":{"rendered":"https:\/\/cssnewbie.com\/?p=2564"},"modified":"2020-09-16T16:13:17","modified_gmt":"2020-09-16T21:13:17","slug":"how-to-center-text-in-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-center-text-in-css\/","title":{"rendered":"How To Center Text In CSS?"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2568 size-full\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/centered-paragraph-text-lines-symbol-for-interface-1.jpg\" alt=\"How to center text in CSS\" width=\"700\" height=\"500\" \/><\/p>\n<p>Formatting and styling is an important step towards making a\u00a0<a href=\"https:\/\/www.impactbnd.com\/blog\/best-site-structure-google\">well-structured website<\/a>. While\u00a0<a href=\"https:\/\/www.smashingmagazine.com\/2019\/06\/styling-modern-web-apps\/\">CSS is generally used to style a web page<\/a>, HTML also provides a lot of options for formatting.<\/p>\n<p>Aligning the text is a vital feature of representation on a modern web page. It is generally done for aesthetics, decoration, titles, sub-titles or sometimes for emphasizing a quote. One may have their own reason to align the given text, but we shall see in this article how can we achieve it using CSS code alongside HTML.<\/p>\n<p>The text to be displayed on the web page can be wrapped inside many tags like &lt;p&gt;, &lt;body&gt;, &lt;div&gt;, &lt;anchor&gt;, &amp; many more. Wherever we wish to center a text inside the tag, we can code in CSS inside \u201ctag{}\u201d as we have done with p{} in the example. Inside this braces we shall use the property <em>text-align <\/em>with the value as <strong>center. <\/strong><\/p>\n<p>In the following example we have used some more properties as well,.I would encourage you to try removing those and check the output to understand it\u2019s usage.<\/p>\n<p>Here\u2019s the code:<\/p>\n<pre>&lt;html&gt;\n  &lt;head&gt;\n   &lt;title&gt; How to center text using css &lt;\/title&gt;\n   &lt;link rel= \u201cstylesheet\u201d href= \u201ccenter.css\u201d &gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n  &lt;p&gt; Hi,I am centered beacauses of css file. &lt;\/p&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>The following shall be the CSS code for the above HTML code. Remember to save it in the same folder as HTML code.<\/p>\n<p>CSS file name: center.css<\/p>\n<pre>p {\n  text-align:center;\n  color:red;\n  font-size:35px;\n}\nbody {\n  background-color:powderblue;\n}<\/pre>\n<p>Let us take a look at the output:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2566 size-large\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/Centertext-1024x316.jpg\" alt=\"How to center text in CSS\" width=\"1024\" height=\"316\" \/><\/p>\n<h3><\/h3>\n<h3>How To Center Table In CSS<\/h3>\n<p>Now that we have seen how to center text in CSS, let us also take a look at how to center a table in CSS. Center table can be done directly in HTML with a &lt;center&gt; tag, but it restricts the changes required in alignment at a later stage. Hence, we prefer styling it separately in a CSS file.<\/p>\n<p>In order to control the alignment of a section, as we have done in &lt;div&gt; for the following example, we will also use properties <em>margin-left<\/em> &amp; <em>margin-right <\/em>with the values as auto. At a later stage, if you want to change the alignment from center you can simply change the value.<\/p>\n<p>We shall use an example to add a table and then center it. In this example we have also used <em>center-align<\/em> method for centering the text inside the cell.<\/p>\n<p>The first one is HTML code. The &lt;link&gt; tag shall be used to call the .css file, which is our second code.<\/p>\n<p>Below is the HTML Code for our example:<\/p>\n<pre>&lt;html&gt;\n  &lt;head&gt;\n \u00a0\u00a0 &lt;title&gt; how to center a table in html &lt;\/title&gt;\n \u00a0\u00a0 &lt;link rel= \u201cstylesheet\u201d href= \u201cctable.css\u201d &gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n \u00a0\u00a0 &lt;div&gt;\n \u00a0\u00a0 &lt;table id=\"check\"&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;tr id=\"y1\"&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;th&gt; First Name &lt;\/th&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;th&gt; Last Name &lt;\/th&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;\/tr&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;tr id=\"r1\"&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; tom &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; hanks &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;\/tr&gt;\n \u00a0\u00a0\u00a0\u00a0 \u00a0&lt;tr id=\"g1\"&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; virat &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; kohli &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;\/tr&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;tr id=\"pb1\"&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; nelson &lt;\/t&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;td&gt; mandela &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;\/tr&gt;\n \u00a0\u00a0 &lt;tr id=\"o1\"&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;td&gt; M.K. &lt;\/td&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;td&gt; Gandhi &lt;\/td&gt;\n \u00a0\u00a0 &lt;\/tr&gt;\n \u00a0\u00a0 &lt;\/table&gt;\n \u00a0\u00a0 &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre>\n<p>CSS file name: ctable.css<\/p>\n<pre>table,th,td {\n  border:5px solid black;\n  border-collapse:collapse;\n  padding:10px;\n  border-color:blue;\n}\ntr{\n\u00a0 text-align:center;\n}\n#y1{\n  background-color:yellow;\n}\n#r1{\n  background-color:red;\n}\n#g1{\n  background-color:green;\n}\n#pb1{\n  background-color:powderblue;\n}\n#o1{\n  background-color:orange;\n}\n#check{\n  margin-left:auto;\n  margin-right:auto;\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>By now you would know what the output will look like. You can explore more with your own codes at <a href=\"https:\/\/codepen.io\/pen\/\">Codepen<\/a>.<\/p>\n<p>Output:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2567 size-large\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/Ctable-1024x316.jpg\" alt=\"How to center table in CSS\" width=\"1024\" height=\"316\" \/><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this tutorial, we learned how to center text in CSS and how to center table. Here\u2019s an article for you to learn similar codes to <a href=\"https:\/\/www.inserthtml.com\/2020\/09\/how-to-center-text-in-html\/\">center text<\/a> and <a href=\"https:\/\/www.inserthtml.com\/2020\/09\/how-to-center-an-image-in-html\/\">image<\/a> in HTML. We have seen two different methods for aligning namely, <em>margin-left &amp; right <\/em>and <em>text-align. <\/em>One of them is exclusively used when we want to align the text i.e., center-align.<\/p>\n<p>To know more about CSS and its uses, visit our <a href=\"https:\/\/cssnewbie.com\/what-does-css-stand-for\/\">previous post<\/a>! To keep learning CSS, subscribe to our <a href=\"http:\/\/www.cssnewbie.com\/\">blog<\/a>!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>Formatting and styling is an important step towards making a\u00a0well-structured website. While\u00a0CSS is generally used to style a web page, HTML also provides a lot of options for formatting.<\/p>\n<p>Aligning the text is a vital feature of representation on a modern [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-center-text-in-css\/\" title=\"Click to read 'How To Center Text In CSS?'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3083"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=3083"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3083\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=3083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=3083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=3083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}