{"id":15939,"date":"2012-01-10T11:07:20","date_gmt":"2012-01-10T18:07:20","guid":{"rendered":"http:\/\/css-tricks.com\/?p=15939"},"modified":"2015-03-28T05:55:20","modified_gmt":"2015-03-28T12:55:20","slug":"better-tabs-with-round-out-borders","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/better-tabs-with-round-out-borders\/","title":{"rendered":"(Better) Tabs with Round Out Borders"},"content":{"rendered":"
<\/p>\n
A good-looking tab control usually has one feature that I’ve always found impossible to reproduce without images: borders that bend to the outside<\/em> at the bottom of each tab. In this article I would like to show how you can use the CSS This would be about as basic as you can get. Not a lot of elements to work with here, but it will do.<\/p>\n To get started, let’s get rid of the default The selected tab should of course be clearly visible.<\/p>\n Every tab control needs a well-defined bottom edge. It won’t do much now, but later it will help emphasize the effect of the selected tab being on top.<\/p>\n Here we introduced our first In my opinion, the terms In this case, we use the pseudo element to create a bottom border that doesn’t influence the tabs’ positioning. We could have just put a bottom border on the Now, an essential part of a convincing looking tab control, is that the selected tab sits in front of<\/em> the edge while the rest fall behind the edge. To do this, we change its bottom border and do some It is now time to add the elusive border that bends to the outside and we’ll use You can probably see where this is going. Let’s remove the borders we don’t want and add some rounded corners.<\/p>\n <\/p>\n There’s something not quite right about this result. Let’s look at it up close. As you can see both the original straight corner as well as the rounded corner are visible. We need to somehow get rid of the straight corner. To do that, we will cover it up with a shadow. To illustrate what’s going on, let’s make the shadow stand out a little bit.<\/p>\n <\/p>\n As you can see, the red shadows completely cover up the square corners we would like to hide. If we give the shadow the correct colors the illusion is complete.<\/p>\n All that’s left to do now is adding a sprinkling of gradients and shadows to spice it up just a little bit.<\/p>\n If you’re wondering about browser compatibility, it’s exactly as you’d expect: everything but IE. It’s very possible that it’ll work in IE10, but I haven’t had the chance to test with a preview release. Since IE8 and IE9 do support View Demo<\/a> Download Files<\/a><\/p>\n Editor’s note:<\/strong> I added anchor links inside the tabs in the demo since I think it’s the most likely case that tabbed navigation like this have them. Most likely, they would have an Update:<\/strong> Allan Watkins<\/a> sent me a variation of these that point downwards:<\/p>\n See the Pen Round Out Tabs that Point Down<\/a> by Chris Coyier (@chriscoyier<\/a>) on CodePen<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" A good-looking tab control usually has one feature that I’ve always found impossible to reproduce without images: borders that bend to the outside<\/em> at the bottom of each tab. In this article I would like to show how you can use the CSS :before<\/code> and
:after<\/code> pseudo elements to create this effect without using images. First, let’s start with some basic markup.<\/p>\n
The markup<\/h3>\n
<ul class=\"tabrow\">\r\n <li>Lorem<\/li>\r\n <li>Ipsum<\/li>\r\n <li class=\"selected\">Sit amet<\/li>\r\n <li>Consectetur adipisicing<\/li>\r\n<\/ul><\/code><\/pre>\n
Getting started<\/h3>\n
<ul><\/code> and
<li><\/code> styling and get these babies in line.<\/p>\n
\n
.tabrow {\r\n text-align: center;\r\n list-style: none;\r\n margin: 0;\r\n padding: 0;\r\n line-height: 24px;\r\n}\r\n.tabrow li {\r\n margin: 0 10px;\r\n padding: 0 10px;\r\n border: 1px solid #AAA;\r\n background: #ECECEC;\r\n display: inline-block;\r\n}<\/code><\/pre>\n
Selecting a tab<\/h3>\n
\n
.tabrow li.selected {\r\n background: #FFF;\r\n color: #000;\r\n}<\/code><\/pre>\n
Getting to the bottom of things<\/h3>\n
\n
.tabrow {\r\n position: relative;\r\n}\r\n.tabrow:after {\r\n position: absolute;\r\n content: \"\";\r\n width: 100%;\r\n bottom: 0;\r\n left: 0;\r\n border-bottom: 1px solid #AAA;\r\n z-index: 1;\r\n}<\/code><\/pre>\n
:after<\/code> pseudo-element. Basically,
:after<\/code> appends another child to an element. It’s not in the DOM (which is why it’s called a pseudo<\/em> element), so it is not a real child but it is completely stylable, as long as you add some
content<\/code>, in this case a single space character.<\/p>\n
:before<\/code> and
:after<\/code> are slightly confusing since the pseudo’s aren’t actually added before or after the element they apply to, but are inserted as children. This is also why you can’t apply
:before<\/code> and
:after<\/code> to elements that can’t contain children (“no content” elements), like
<input><\/code>.<\/p>\n
<ul><\/code> but that would’ve made the next step a little trickier.<\/p>\n
Above and beyond<\/h3>\n
z-index<\/code> magic.<\/p>\n
\n
.tabrow:before {\r\n z-index: 1;\r\n}\r\n.tabrow li {\r\n position: relative;\r\n z-index: 0;\r\n}\r\n.tabrow li.selected {\r\n z-index: 2;\r\n border-bottom-color: #FFF;\r\n}<\/code><\/pre>\n
Around the bends<\/h3>\n
:before<\/code> and
:after<\/code> for this. Let’s take this step by step and first just put everything in position.<\/p>\n
\n
.tabrow li:before,\r\n.tabrow li:after {\r\n position: absolute;\r\n bottom: -1px;\r\n width: 6px;\r\n height: 6px;\r\n content: \" \";\r\n}\r\n.tabrow li:before {\r\n left: -6px;\r\n}\r\n.tabrow li:after {\r\n right: -6px;\r\n}\r\n.tabrow li:after, .tabrow li:before {\r\n border: 1px solid #AAA;\r\n}<\/code><\/pre>\n
Don’t be such a square<\/h3>\n
\n
.tabrow li {\r\n border-top-left-radius: 6px;\r\n border-top-right-radius: 6px;\r\n}\r\n.tabrow li:before {\r\n border-bottom-right-radius: 6px;\r\n border-width: 0 1px 1px 0;\r\n}\r\n.tabrow li:after {\r\n border-bottom-left-radius: 6px;\r\n border-width: 0 0 1px 1px;\r\n}<\/code><\/pre>\n
Cutting corners<\/h3>\n
\n
.tabrow li:before {\r\n box-shadow: 2px 2px 0 red;\r\n}\r\n.tabrow li:after {\r\n box-shadow: -2px 2px 0 red;\r\n}<\/code><\/pre>\n
Almost there<\/h3>\n
\n
.tabrow li:before {\r\n box-shadow: 2px 2px 0 #ECECEC;\r\n}\r\n.tabrow li:after {\r\n box-shadow: -2px 2px 0 #ECECEC;\r\n}\r\n.tabrow li.selected:before {\r\n box-shadow: 2px 2px 0 #FFF;\r\n}\r\n.tabrow li.selected:after {\r\n box-shadow: -2px 2px 0 #FFF;\r\n}<\/code><\/pre>\n
Pieces of flair<\/h3>\n
\n
.tabrow li {\r\n background: linear-gradient(to bottom, #ECECEC 50%, #D1D1D1 100%);\r\n box-shadow: 0 3px 3px rgba(0, 0, 0, 0.4), inset 0 1px 0 #FFF;\r\n text-shadow: 0 1px #FFF;\r\n margin: 0 -5px;\r\n padding: 0 20px;\r\n}<\/code><\/pre>\n
:before<\/code> and
:after<\/code> but not
border-radius<\/code> you’ll have to create a separate stylesheet<\/a> for them if you want to give their users a nice visual experience.<\/p>\n
href<\/code> attribute that would link to the content they go with by
id<\/code>, and that behavior would be controlled by JavaScript. The fact that this tutorial doesn’t need the anchor links for the extra pseudo elements is further testament to it being better than my original.<\/p>\n
:before<\/code> and
:after<\/code> pseudo elements to create this effect without using images.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4,20],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":14001,"url":"https:\/\/css-tricks.com\/tabs-with-round-out-borders\/","url_meta":{"origin":15939,"position":0},"title":"Tabs with Round Out Borders","date":"September 4, 2011","format":false,"excerpt":"A technique for a rounded tabs where the top corners are rounded, but also the bottom corners are rounded where they attach to the content area. \"Round out\" or \"flared\" borders, if you will.","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2011\/09\/Pseudos.071.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":366252,"url":"https:\/\/css-tricks.com\/conditionally-styling-selected-elements-in-a-grid-container\/","url_meta":{"origin":15939,"position":1},"title":"Conditionally Styling Selected Elements in a Grid Container","date":"June 15, 2022","format":false,"excerpt":"Calendars, shopping carts, galleries, file explorers, and online libraries are some situations where selectable items are shown in grids (i.e. square lattices). You know, even those security checks that ask you to select all images with crosswalks or whatever. \ud83e\uddd0 I found a neat way to display selectable options in\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/06\/s_C6C1FBE60A3624B876B34697BF3BE9266CAA9E9CFB20B11CDBDE5D32FDF0FD18_1654436553993_diagram-1.png?fit=1200%2C877&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":8183,"url":"https:\/\/css-tricks.com\/body-borders-without-markup\/","url_meta":{"origin":15939,"position":2},"title":"Body Borders without Markup","date":"December 21, 2010","format":false,"excerpt":"Harry Roberts\u200a with a nice technique on using pseudo elements to create the body border effect without extraneous markup (same idea with extra markup gets you older browser support). Read the comments for some other good suggestions.","rel":"","context":"In "Link"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":326412,"url":"https:\/\/css-tricks.com\/how-to-add-text-in-borders-using-basic-html-elements\/","url_meta":{"origin":15939,"position":3},"title":"How to Add Text in Borders Using Basic HTML Elements","date":"December 1, 2020","format":false,"excerpt":"Some HTML elements come with preset designs, like the inconveniently small squares of elements, the limited-color bars of