{"id":1351,"date":"2020-08-14T13:57:57","date_gmt":"2020-08-14T13:57:57","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=1351"},"modified":"2020-08-14T13:57:57","modified_gmt":"2020-08-14T13:57:57","slug":"uncommon-css-properties","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=1351","title":{"rendered":"Uncommon CSS Properties"},"content":{"rendered":"\n<p>There are a lot of CSS properties that some don\u2019t know about, or they know about them, but forget to use them when they\u2019re needed. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Some of those can save you using JavaScript to achieve a specific result, or some can save your time by writing less CSS. As a front-end developer, I came across such things every now and then, and I asked myself, why not list all those less-used and interesting CSS properties in an article?<\/p>\n\n\n\n<p>In this article, I will go through some different CSS properties that I hope you find them interesting. For some properties, I will try to show the browser support for them, and apply the progressive enhancement approach, so you can be encouraged to use it without the fear of saying \u201cThis is not supported in browser X, what should I do?\u201d.<\/p>\n\n\n\n<p>Are you ready? Let\u2019s dive in the less-used so you can know more. \ud83d\ude42<\/p>\n\n\n\n<h2 id=\"using-place-items-with-css-grid\">Using&nbsp;<code>Place-Items<\/code>&nbsp;With CSS Grid<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/place-center.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>I learned about this trick from&nbsp;<a href=\"https:\/\/twitter.com\/bdc\/status\/901530956624015361\">Benjamin De Cock<\/a>. You can center an element horizontally and vertically with two lines of CSS.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"hero\">\n    &lt;div class=\"hero-wrapper\">\n        &lt;h2>CSS is awesome&lt;\/h2>\n        &lt;p>Yes, this is a hero section made for fun.&lt;\/p>\n        &lt;a href=\"#\">See more&lt;\/a>\n    &lt;\/div>\n&lt;\/div>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.hero {\n    display: grid;\n    place-items: center;\n}\n<\/code><\/pre>\n\n\n\n<p>Before going into details, it\u2019s worth mentioning that&nbsp;<code>place-items<\/code>&nbsp;is a shorthand property that combines&nbsp;<code>justify-items<\/code>&nbsp;and&nbsp;<code>align-items<\/code>. Here is how the code above could be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.hero {\n    display: grid;\n    justify-items: center;\n    align-items: center;\n}\n<\/code><\/pre>\n\n\n\n<p>You may wonder, how this works? Well, let me explain that. When the&nbsp;<code>place-items<\/code>&nbsp;is used, it\u2019s applied on&nbsp;<strong>each cell<\/strong>&nbsp;in the grid. That means it will center the cell\u2019s content. That means, this technique can work with multiple cells. If we increase the number of columns, that will be more clear.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.hero {\n    display: grid;\n    grid-template-columns: 1fr 1fr;\n    place-items: center;\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/place-center-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 id=\"the-good-old-margin-auto-with-flexbox\">The Good Old&nbsp;<code>Margin: Auto<\/code>&nbsp;With Flexbox<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/margin-auto.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Combined with flexbox,&nbsp;<code>margin: auto<\/code>&nbsp;can center a flex item horizontally and vertically very easily.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"parent\">\n    &lt;div class=\"child\">&lt;\/div>\n&lt;\/div>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.parent {\n    width: 300px;\n    height: 200px;\n    background: #ccc;\n    display: flex;\n}\n\n.child {\n    width: 50px;\n    height: 50px;\n    background: #000;\n    margin: auto;\n}\n<\/code><\/pre>\n\n\n\n<p>Isn\u2019t that cool?<\/p>\n\n\n\n<h2 id=\"styling-a-lists-marker\">Styling A List\u2019s Marker<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/li-marker.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>First, let me be clear that I wasn\u2019t aware that the little default circle next to each list item is called a&nbsp;<strong>marker<\/strong>. Before I know about the&nbsp;<code>::marker<\/code>&nbsp;pseudo-element, the process was to reset the list style, and then to add the circle as a&nbsp;<code>::before<\/code>&nbsp;or&nbsp;<code>::after<\/code>&nbsp;pseudo-elements. That\u2019s isn\u2019t practical. Here is the bad way of doing this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ul {\n    list-style: none;\n    padding: 0;\n}\n\nli {\n    color: #222;\n}\n\nli::before {\n    content: \"\u2022\";\n    color: #ccc;\n    margin-right: 0.5em;\n}\n<\/code><\/pre>\n\n\n\n<p>As you see, the&nbsp;<code>&lt;li&gt;<\/code>&nbsp;color is&nbsp;<code>#222<\/code>, while the&nbsp;<code>::before<\/code>&nbsp;pseudo-element is&nbsp;<code>#ccc<\/code>. If the&nbsp;<code>&lt;li&gt;<\/code>&nbsp;and&nbsp;<code>::before<\/code>&nbsp;have the same color, then the circle will inherit by default, and thus the pseudo-element is not needed at all.<\/p>\n\n\n\n<p>Let\u2019s see a better way of doing this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>li {\n    color: #222;\n}\n\nli::marker {\n    color: #ccc;\n}\n<\/code><\/pre>\n\n\n\n<p>And we\u2019re done! Isn\u2019t that much, much easier?<\/p>\n\n\n\n<p>The&nbsp;<code>::marker<\/code>&nbsp;is&nbsp;<a href=\"https:\/\/caniuse.com\/#search=%3A%3Amarker\">supported<\/a>&nbsp;in Firefox 68+ and Safari 11.1+. It\u2019s supported behind a flag in Chrome and Edge 80+.<\/p>\n\n\n\n<h2 id=\"the-text-align-property\">The&nbsp;<code>Text-Align<\/code>&nbsp;Property<\/h2>\n\n\n\n<p>With the rising popularity of CSS flexbox and grid, it\u2019s common for someone who has just started with CSS to use the modern methods for centering and alignment instead of&nbsp;<code>text-align<\/code>. However, the old methods still work.<\/p>\n\n\n\n<p>Using the&nbsp;<code>text-align: center<\/code>&nbsp;can solve an issue quickly. Consider the following example.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/text-align-center.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The content needs to be centered. Is it worth to use flexbox or grid? With&nbsp;<code>text-align<\/code>, this can be easily achieved.<\/p>\n\n\n\n<p>I don\u2019t have to explain about the browser support for&nbsp;<code>text-align<\/code>, you should guess that yourself (Sorry!).<\/p>\n\n\n\n<h2 id=\"the-display-inline-flex-property\">The&nbsp;<code>Display: Inline-Flex<\/code>&nbsp;Property<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/inline-flex.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Do you remember if you needed to display a list of badges inline, and each one of them should be a flexbox element? That\u2019s what&nbsp;<code>inline-flex<\/code>&nbsp;is for.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;span class=\"badge\">&lt;svg>&lt;\/svg>&lt;\/span>\n&lt;span class=\"badge\">&lt;svg>&lt;\/svg>&lt;\/span>\n&lt;span class=\"badge\">&lt;svg>&lt;\/svg>&lt;\/span>\n&lt;span class=\"badge\">&lt;svg>&lt;\/svg>&lt;\/span>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.badge {\n    display: inline-flex; \/* where the magic happens *\/\n    justify-content: center;\n    align-items: center;\n}\n<\/code><\/pre>\n\n\n\n<p>Next time you need an inline element with a flex functionality, remember to use&nbsp;<code>inline-flex<\/code>. Simple and easy.<\/p>\n\n\n\n<h2 id=\"the-column-rule-property\">The&nbsp;<code>Column-Rule<\/code>&nbsp;Property<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/column-rule.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>CSS columns is a layout method which can divide an element into columns. A common use-case for it is to divide a paragraph text content into two lines. However, the less common thing about it is that we can add borders between the columns. I learned about this tip from&nbsp;<a href=\"https:\/\/www.matuzo.at\/blog\/heres-what-i-didnt-know-about-color\/\">Manuel Matuzovic\u2019s<\/a>&nbsp;article.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p {\n    columns: 3;\n    column-rule: solid 2px #222;\n}\n<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>column-rule<\/code>&nbsp;property name might not reflect its purpose, but imagine it as&nbsp;<code>border-right<\/code>. The property is well supported in all browsers (IE 10+, Firefox 3.5+, Chrome 4+, Safari 3.1+, Edge 12+).<\/p>\n\n\n\n<h2 id=\"background-repeat-round\">Background Repeat Round<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/repeat-round.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>I recently learned about this value from a&nbsp;<a href=\"https:\/\/twitter.com\/addyosmani\/status\/1275322697933881344\">tweet<\/a>&nbsp;by Addy Osmani. There is a value for&nbsp;<code>background-repeat<\/code>&nbsp;which prevent the clipping of a background.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.element {\n\tbackground-size: contain;\n\tbackground-repeat: round;\n}\n<\/code><\/pre>\n\n\n\n<p>According to&nbsp;<a href=\"https:\/\/css-tricks.com\/almanac\/properties\/b\/background-repeat\/\">CSS Tricks<\/a>, here is how&nbsp;<code>round<\/code>&nbsp;works:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>tile the image in both directions. Never crop the image unless a single image is too large to fit. If multiple images can fit with leftover space, squish them or stretch them to fill the space. If it\u2019s less than half one image width left, stretch, if it\u2019s more, stretch.<\/p><\/blockquote>\n\n\n\n<p>It\u2019s Magical. Isn\u2019t it?<\/p>\n\n\n\n<h2 id=\"object-fit\">Object Fit<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/ishadeed.com\/assets\/uncommon-css\/object-fit.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>object-fit<\/code>&nbsp;CSS property is magical. When I first knew about it, it changed a lot of things and made my life easier as a front-end developer. Recently, I was working on a section that displays a grid of logos. That kind of thing is sometimes hard due to the inconsistent sizes of the logos. Some of them have a horizontal shape, some have a vertical one.<\/p>\n\n\n\n<p>By using&nbsp;<code>object-fit: contain<\/code>, I was able to control the&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>&nbsp;of the logos and force the image to be contained in the defined width and height.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ul class=\"brands\">\n    &lt;li class=\"brands__item\">\n        &lt;a href=\"#\">\n            &lt;img src=\"img\/logo.png\" alt=\"\">\n        &lt;\/a>\n    &lt;\/li>\n    &lt;li> &lt;!-- other logos --> &lt;\/li>\n&lt;\/ul>\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>img {\n    width: 130px;\n    height: 75px;\n    object-fit: contain;\n}\n<\/code><\/pre>\n\n\n\n<p>By defining a&nbsp;<code>width<\/code>&nbsp;and&nbsp;<code>height<\/code>, we will force the image to be contained. This is a huge benefit. Even better, we can wrap the above in a&nbsp;<code>@supports<\/code>&nbsp;to avoid stretching the logo image in browsers that don\u2019t support&nbsp;<code>object-fit<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@supports (object-fit: contain) {\n  img {\n    object-fit: contain;\n    height: 75px;\n  }\n}\n<\/code><\/pre>\n\n\n\n<h2 id=\"related-articles\">Related Articles<\/h2>\n\n\n\n<ul><li><a href=\"https:\/\/ishadeed.com\/article\/aligning-logos-css\/\">Aligning Logo Images in CSS<\/a><\/li><li><a href=\"https:\/\/ishadeed.com\/article\/how-i-used-inline-flex-first-time\/\">I Used CSS Inline Flex For The First Time<\/a><\/li><li><a href=\"https:\/\/ishadeed.com\/article\/the-hidden-power-text-align\/\">The Hidden Power of CSS Text Align<\/a><\/li><\/ul>\n\n\n\n<h2 id=\"the-end\">The End<\/h2>\n\n\n\n<p>That\u2019s a wrap. Do you have a comment or a suggestion? Please feel free to ping me on&nbsp;<a href=\"https:\/\/twitter.com\/shadeed9\">@shadeed9<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of CSS properties that some don\u2019t know about, or they know about them, but forget to use them when they\u2019re needed.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[105],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1351"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1351"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions\/1352"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}