{"id":3035,"date":"2022-05-21T11:50:50","date_gmt":"2022-05-21T11:50:50","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3035"},"modified":"2023-06-12T14:31:03","modified_gmt":"2023-06-12T14:31:03","slug":"css-tutorial-how-to-add-text-in-borders-using-basic-html-elements","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/css-tutorial-how-to-add-text-in-borders-using-basic-html-elements\/","title":{"rendered":"CSS Tutorial &#8211; How to Add Text in Borders Using Basic HTML Elements"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; Some HTML elements come with preset designs, like the inconveniently small squares of\u00a0<code>&lt;input type=\"checkbox\"&gt;<\/code>\u00a0elements, the limited-color bars of\u00a0<code>&lt;meter&gt;<\/code>\u00a0elements, and the \u201csomething about them bothers me\u201d arrows of the\u00a0<code>&lt;details&gt;<\/code>\u00a0elements. We can style them to match the modern aesthetics of our websites while making use of their functionalities. There are also many elements that rarely get used as\u00a0<em>both<\/em>\u00a0their default appearance and functionality are less needed in modern web designs.<\/p>\n<p>One such HTML element is\u00a0<code>&lt;fieldset&gt;<\/code>, along with its child element\u00a0<code>&lt;legend&gt;<\/code>.<\/p>\n<p>A\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0element is traditionally used to group and access form controls. We can visually notice the grouping by the presence of a border around the grouped content on the screen. The caption for this group is given inside the\u00a0<code>&lt;legend&gt;<\/code>\u00a0element that\u2019s added as the first child of the\u00a0<code>&lt;fieldset&gt;<\/code>.<br \/>\n<iframe style=\"width: 100%;\" title=\"How to Add Text in Borders 01\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/VwXEMQe?default-tab=result&amp;theme-id=dark\" height=\"300\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/VwXEMQe\"><br \/>\nHow to Add Text in Borders 01<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><br \/>\nThis combination of\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0and\u00a0<code>&lt;legend&gt;<\/code>\u00a0creates a unique ready-made \u201ctext in border\u201d design where the caption is placed right where the border is and the line of the border doesn\u2019t go through the text. The border line \u201cbreaks\u201d when it encounters the beginning of the caption text and resumes after the text ends.<\/p>\n<p>In this post, we\u2019ll make use of the\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0and\u00a0<code>&lt;legend&gt;<\/code>\u00a0combo to create a more modern border text design that\u2019s quick and easy to code and update.<br \/>\n<iframe style=\"width: 100%;\" title=\"Untitled\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/poLxWaZ?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/poLxWaZ\"><br \/>\nUntitled<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><br \/>\nFor the four borders, we need four\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0elements, each containing a\u00a0<code>&lt;legend&gt;<\/code>\u00a0element inside. We add the text that will appear at the borders inside the\u00a0<code>&lt;legend&gt;<\/code>\u00a0elements.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;fieldset&gt;&lt;legend&gt;Wash Your Hands&lt;\/legend&gt;&lt;\/fieldset&gt;\r\n&lt;fieldset&gt;&lt;legend&gt;Stay Apart&lt;\/legend&gt;&lt;\/fieldset&gt;\r\n&lt;fieldset&gt;&lt;legend&gt;Wear A Mask&lt;\/legend&gt;&lt;\/fieldset&gt;\r\n&lt;fieldset&gt;&lt;legend&gt;Stay Home&lt;\/legend&gt;&lt;\/fieldset&gt;<\/pre>\n<p>To begin, we stack the\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0elements on top of each other in a grid cell and give them borders. You can stack them using any way you want \u2014 it doesn\u2019t necessarily have to be a grid.<\/p>\n<p>Only the top border of each\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0element is kept visible while the remaining edges are transparent since the text of the\u00a0<code>&lt;legend&gt;<\/code>\u00a0element appears at the top border of the\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0by default.<\/p>\n<p>Also, we give all the\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0elements a\u00a0<code>box-sizing<\/code>\u00a0property with a value of\u00a0<code>border-box<\/code>\u00a0so the width and height of the\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0elements include their border and padding sizes too. Doing this later creates a leveled design, when we style the\u00a0<code>&lt;legend&gt;<\/code>\u00a0elements.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">body {\r\n  display: grid; \r\n  margin: auto; \/* to center *\/\r\n  margin-top: calc(50vh - 170px); \/* to center *\/\r\n  width: 300px; height: 300px; \r\n}\r\n\r\nfieldset {\r\n  border: 10px solid transparent; \r\n  border-top-color: black; \r\n  box-sizing: border-box; \r\n  grid-area: 1 \/ 1; \/* first row, first column *\/\r\n  padding: 20px; \r\n  width: inherit; \r\n}<\/pre>\n<p>After this, we rotate the last three\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0elements in order to use their top borders as the side and bottom borders of our design.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* rotate to right *\/\r\nfieldset:nth-of-type(2){ transform: rotate(90deg); }\r\n\/* rotate to bottom *\/\r\nfieldset:nth-of-type(3){ transform: rotate(180deg); }\r\n\/* rotate to left *\/\r\nfieldset:nth-of-type(4){ transform: rotate(-90deg); }<\/pre>\n<p>Next up is styling the\u00a0<code>&lt;legend&gt;<\/code>\u00a0elements. The key to create smooth border text using a\u00a0<code>&lt;legend&gt;<\/code>\u00a0element is to give it a zero (or small enough)\u00a0<code>line-height<\/code>. If it has a large line height, that will displace the position of the border it\u2019s in, pushing the border down. And when the border moves with the line height, we won\u2019t be able to connect all the four sides of our design and will need to readjust the borders.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">legend {\r\n  font: 15pt\/0 'Averia Serif Libre'; \r\n  margin: auto; \/* to center *\/\r\n  padding: 0 4px; \r\n}\r\n\r\nfieldset:nth-of-type(3) &gt; legend { \r\n  transform: rotate(180deg);\r\n}<\/pre>\n<p>I used the\u00a0<code>font<\/code>\u00a0shorthand property to give the values for the\u00a0<code>font-size<\/code>,\u00a0<code>line-height<\/code>\u00a0and\u00a0<code>font-family<\/code>\u00a0properties of the\u00a0<code>&lt;legend&gt;<\/code>\u00a0elements.<\/p>\n<p>The\u00a0<code>&lt;legend&gt;<\/code>\u00a0element that adds the text at the bottom border of our design,\u00a0<code>fieldset:nth-of-type(3)&gt;legend<\/code>, is upside-down because of its rotated\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0parent element. Flip that\u00a0<code>&lt;legend&gt;<\/code>\u00a0element vertically to show its text right-side-up.<\/p>\n<p>Add an image to the first\u00a0<code>&lt;fieldset&gt;<\/code>\u00a0element and you get something like this:<br \/>\n<iframe style=\"width: 100%;\" title=\"Untitled\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/poLxWaZ?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/poLxWaZ\"><br \/>\nUntitled<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><br \/>\nLateral margins can move the text along the border. Left and right margins with\u00a0<code>auto<\/code>\u00a0values will center the text, as seen in the above Pen. Only the left margin with an\u00a0<code>auto<\/code>\u00a0value will flush the text to the right, and vice versa, for the right margin.<br \/>\n<iframe style=\"width: 100%;\" title=\"Border Text Design using HTML &quot;fieldset&quot; and &quot;legend&quot; 02\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/rNdqGvR?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/rNdqGvR\"><br \/>\nBorder Text Design using HTML &#8220;fieldset&#8221; and &#8220;legend&#8221; 02<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><br \/>\n<strong>Bonus:<\/strong>\u00a0After a brief geometrical detour, here\u2019s an octagonal design I made using the same technique:<br \/>\n<iframe style=\"width: 100%;\" title=\"Border Text Design using HTML &quot;fieldset&quot; and &quot;legend&quot; 03\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/mdxzBKR?default-tab=result&amp;theme-id=dark\" height=\"500\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/mdxzBKR\"><br \/>\nBorder Text Design using HTML &#8220;fieldset&#8221; and &#8220;legend&#8221; 03<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; Some HTML elements come with preset designs, like the <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/css-tutorial-how-to-add-text-in-borders-using-basic-html-elements\/\" title=\"CSS Tutorial &#8211; How to Add Text in Borders Using Basic HTML Elements\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,2],"tags":[272,268,197,103,227],"newstopic":[],"class_list":{"0":"post-3035","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-css","8":"category-web-design","9":"tag-add-text-in-border","10":"tag-css-grid","11":"tag-css-tutorial","12":"tag-web-design","13":"tag-web-designer"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3035"}],"version-history":[{"count":3,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3035\/revisions"}],"predecessor-version":[{"id":3039,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3035\/revisions\/3039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3744"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3035"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}