<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Archives des css | dash-resources.com</title>
	<atom:link href="https://dash-resources.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>https://dash-resources.com/tag/css/</link>
	<description>Learn to build interactive web applications with Python and Dash plotly</description>
	<lastBuildDate>Sun, 09 Mar 2025 10:54:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://dash-resources.com/wp-content/uploads/2024/12/cropped-dash-logo-favicon-512-32x32.png</url>
	<title>Archives des css | dash-resources.com</title>
	<link>https://dash-resources.com/tag/css/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A guide to beautiful Dashboards (basic design principles)</title>
		<link>https://dash-resources.com/a-guide-to-beautiful-dashboards-basic-design-principles/</link>
		
		<dc:creator><![CDATA[Fran]]></dc:creator>
		<pubDate>Sun, 09 Feb 2025 15:26:47 +0000</pubDate>
				<category><![CDATA[Beginner level]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">https://dash-resources.com/?p=707</guid>

					<description><![CDATA[<p>So you’ve started building dashboards with Dash Plotly. Bravo! But you soon realize that even if it is easy to build dashboards, it is somehow [...]</p>
<p>L’article <a href="https://dash-resources.com/a-guide-to-beautiful-dashboards-basic-design-principles/">A guide to beautiful Dashboards (basic design principles)</a> est apparu en premier sur <a href="https://dash-resources.com">dash-resources.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>So you’ve started building dashboards with Dash Plotly. Bravo! But you soon realize that even if it is easy to build dashboards, it is somehow <strong>not easy to make them look good</strong>. So how do you design a beautiful dashboard with Dash?</p>


        
            
            <div class="fit_content">
                <div class="bd_toc_container" data-fixedWidth="">
                    <div class="bd_toc_wrapper" data-wrapperPadding="48px">
                        <div class="bd_toc_wrapper_item">
                            <div class="bd_toc_header active" data-headerPadding="2px">
                                <div class="bd_toc_header_title">
                                    Table of Contents                                </div>
                                <div class="bd_toc_switcher_hide_show_icon">
                                    <span class="bd_toc_arrow"></span>                                </div>
                            </div>
                            <div class="bd_toc_content list-type-disc">
                                <div class="bd_toc_content_list ">
                                    <div class='bd_toc_content_list_item'>    <ul>
      <li class="first">
        <a href="#introduction">Introduction</a>
      </li>
      <li>
        <a href="#pick-a-font">Pick a font</a>
      </li>
      <li>
        <a href="#pick-colors-correctly">Pick colors correctly</a>
      </li>
      <li>
        <a href="#beware-of-the-hierarchy">Beware of the hierarchy</a>
      </li>
      <li>
        <a href="#space-things-out">Space things out!</a>
      </li>
      <li>
        <a href="#keep-things-homogeneous">Keep things homogeneous</a>
      </li>
      <li>
        <a href="#remember-to-style-charts-too">Remember to style charts too</a>
      </li>
      <li>
        <a href="#use-pre-made-components">Use pre-made components</a>
        <ul class="menu_level_2">
          <li class="first last">
            <a href="#see-also">See also:</a>
          </li>
        </ul>
      </li>
      <li class="last">
        <a href="#conclusion">Conclusion</a>
      </li>
    </ul>
</div>                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layout_toggle_button">
                        <span class="bd_toc_arrow"></span>
                    </div>
                </div>
            </div>




<p>In this tutorial, I’ll give you <strong>tips</strong> and <strong>advices</strong> to keep in mind to create visually appealing dashboards and Dash apps, to increase the usability and aesthetics of your project.</p>



<h2 id='introduction'  id="boomdevs_1" class="wp-block-heading" >Introduction</h2>



<p>Let’s be honest: most dashboards are functional but completely fail in terms of UI/UX (user interface/user experience). And that’s normal—most of us are data analysts, data scientists, or Python developers, and UI/UX is not our job.</p>



<p>If you are building a dashboard for your own use or designed for technical people, you might not care about aesthetics. But if you want people outside your team to engage with and use the amazing dashboard that you made, you should invest time in making your dashboard visually appealing.</p>



<figure class="wp-block-image size-full is-style-default"><img fetchpriority="high" decoding="async" width="1378" height="1076" src="https://dash-resources.com/wp-content/uploads/2025/02/image.png" alt="Illustration: a dashboard with all possible visual problems." class="wp-image-708" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image.png 1378w, https://dash-resources.com/wp-content/uploads/2025/02/image-300x234.png 300w, https://dash-resources.com/wp-content/uploads/2025/02/image-1024x800.png 1024w, https://dash-resources.com/wp-content/uploads/2025/02/image-768x600.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-1320x1031.png 1320w" sizes="(max-width: 1378px) 100vw, 1378px" /><figcaption class="wp-element-caption">Can you even look at this dashboard ? It’s really hurting the eyes.</figcaption></figure>



<p>This is even more true if you are creating an app used by non-technical people (not in your field) or if you are developing a SaaS product for a broader audience with Dash.</p>



<p>Fortunately, by following a few rules, you can significantly improve your skills in building beautiful dashboards and apps. Let’s dive in. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 id='pick-a-font'  id="boomdevs_2" class="wp-block-heading" >Pick a font</h2>



<p>The harsh truth: the <strong>lack of a personalized font</strong> is one of the common traits of ugly dashboards. That’s why I put it at the top of this list. You’ll never see a professional-looking app that sticks to &#8220;<em>Times New Roman</em>&#8221; (i.e., the default font in most web browsers).</p>



<p>This doesn’t mean this font should never be used, but it means that default fonts quickly show that no effort was put into aesthetics. So, setting a proper font is a real quick win.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1382" height="1063" src="https://dash-resources.com/wp-content/uploads/2025/02/image-1.png" alt="Illustration: the same dashboard with fonts fixed." class="wp-image-709" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-1.png 1382w, https://dash-resources.com/wp-content/uploads/2025/02/image-1-300x231.png 300w, https://dash-resources.com/wp-content/uploads/2025/02/image-1-1024x788.png 1024w, https://dash-resources.com/wp-content/uploads/2025/02/image-1-768x591.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-1-1320x1015.png 1320w" sizes="(max-width: 1382px) 100vw, 1382px" /><figcaption class="wp-element-caption">The same dashboard, but we changed font: it looks instantly different.</figcaption></figure>



<p>Serif fonts often imply tradition and formality (<em>Times New Roman</em> is widely used in newspapers and books, and there’s nothing wrong with that if your app is focused on reports, articles, or anything content-heavy). But usually, dashboards benefit from sans-serif fonts for a cleaner, more modern look.</p>



<p>Recommended fonts are the basic ones:</p>



<ul class="wp-block-list">
<li><em>Arial, Open Sans, Verdana, Helvetica Neue</em>.</li>
</ul>



<p>If you need more stylish and trendy fonts, here are some great options:</p>



<ul class="wp-block-list">
<li><em>Roboto</em> (modern and widely used in web design)</li>



<li><em>Lato</em> (clean and friendly, great for dashboards)</li>



<li><em>Montserrat</em> (sleek and professional)</li>



<li><em>Inter</em> (highly readable and trendy for UI design)</li>
</ul>



<p>If you need more inspiration, you can explore <a href="https://fonts.google.com/">Google Fonts</a> or <a href="https://fonts.adobe.com/">Adobe Fonts</a> to find stylish options that fit your dashboard’s aesthetic. But being too exotic is not a good idea either.</p>



<p class="callout"><strong>Note:</strong> You should always set a font because otherwise, your app might look different on various devices (Windows, Mac, Linux, etc.) and browsers, which may pick different default fonts.</p>



<h2 id='pick-colors-correctly'  id="boomdevs_3" class="wp-block-heading" >Pick colors correctly</h2>



<p><strong>No colors is terrible</strong></p>



<p>Colors help with contrast, and contrast is absolutely necessary if you want to ease the eye. A dashboard with no contrast is hard to read, making users tire quickly. If everything looks the same, users won’t know where to focus.</p>



<p>Using just black and white or a single muted color can make your app look dull and unstructured. Instead, introduce some contrast using different shades and tones, but in a subtle way. Use colors to differentiate sections, highlight key metrics, and guide user attention effortlessly.</p>



<p><strong>Too many colors is terrible</strong></p>



<p>On the flip side, throwing in too many colors makes everything look chaotic. Too much contrast can make your dashboard overwhelming and confusing. A rainbow-colored dashboard is not visually appealing—it’s distracting.</p>



<p>Go easy on gradients. Too many gradients will make your app look like outdated WordArt! Use soft gradients sparingly—to highlight a few buttons or text, but not everywhere. Stick to a defined color palette and ensure your colors complement each other.</p>



<p><strong>Importance of color choice</strong></p>



<p>Colors enhance the appearance of your app but also improve user experience: you can emphasize the importance of a button by its color alone, meaning the user instantly understands its significance.</p>



<p>People also associate certain colors with specific actions—red buttons usually indicate critical actions, while blue or black buttons tend to be more neutral. Green often signals success, and yellow can be used for warnings.</p>



<p>Just like fonts, using default colors (100% red, 100% green, or 100% blue) will make your dashboard look unpolished. Instead, pick variants of these colors—softer shades, deeper tones, or slight variations.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1376" height="1083" src="https://dash-resources.com/wp-content/uploads/2025/02/image-2.png" alt="Illustration: the same dashboard with better colors." class="wp-image-710" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-2.png 1376w, https://dash-resources.com/wp-content/uploads/2025/02/image-2-300x236.png 300w, https://dash-resources.com/wp-content/uploads/2025/02/image-2-1024x806.png 1024w, https://dash-resources.com/wp-content/uploads/2025/02/image-2-768x604.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-2-1320x1039.png 1320w" sizes="(max-width: 1376px) 100vw, 1376px" /><figcaption class="wp-element-caption">Our dashboard after changing the colors for softer colors. We also got rid of contrasts problems like a dark text on a dark button and removed old-looking gradients.</figcaption></figure>



<p class="callout"><strong>Pro tip:</strong> If you don’t know what colors to use, you can rely on predefined color palettes from <a href="https://mantine.dev/theming/colors/">Mantine</a>, <a href="https://getbootstrap.com/docs/4.0/utilities/colors/">Bootstrap</a>, or websites like <a href="https://flatuicolors.com/">Flat UI Colors</a>.</p>



<p>Also remember that users have different visual impairments (e.g., color blindness). Learn more about it here: <a href="https://davidmathlogic.com/colorblind/">coloring for colorblindness</a>.</p>



<h2 id='beware-of-the-hierarchy'  id="boomdevs_4" class="wp-block-heading" >Beware of the hierarchy</h2>



<p>Hierarchy isn&#8217;t just about making titles bigger —it&#8217;s about creating a <strong>natural flow for the eyes</strong>. When someone looks at your dashboard, they should naturally understand what&#8217;s important and what&#8217;s secondary.</p>



<p>A simple but effective approach is to stick to 3-4 text sizes for your entire app. Your main title should be the biggest (around 32px), followed by section titles (24px), and then your regular text (14-16px). Don&#8217;t go smaller than 12px &#8211; nobody likes squinting at tiny text!</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1384" height="1118" src="https://dash-resources.com/wp-content/uploads/2025/02/image-3.png" alt="Illustration: the same dashboard with better hierarchy." class="wp-image-712" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-3.png 1384w, https://dash-resources.com/wp-content/uploads/2025/02/image-3-300x242.png 300w, https://dash-resources.com/wp-content/uploads/2025/02/image-3-1024x827.png 1024w, https://dash-resources.com/wp-content/uploads/2025/02/image-3-768x620.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-3-1320x1066.png 1320w" sizes="auto, (max-width: 1384px) 100vw, 1384px" /><figcaption class="wp-element-caption">Now titles are bigger, as well as the most important data (sales, revenue, profit). The “Recent activity” section is larger and the text below is smaller.</figcaption></figure>



<p>But size isn&#8217;t everything. You can also create hierarchy through weight (bold vs regular) and color intensity. Just like in a newspaper, the most important stuff should catch your eye first.</p>



<p>Think about it: when you look at a well-designed website, you instantly know where to focus, right? That&#8217;s good hierarchy at work.</p>



<h2 id='space-things-out'  id="boomdevs_5" class="wp-block-heading" >Space things out!</h2>



<p>Let your components breathe:</p>



<ul class="wp-block-list">
<li>Add margins around text and padding inside buttons.</li>



<li>Text should not be stuck to the border of its container—use proper padding and margins.</li>
</ul>



<p>The easiest way to handle spacing is to pick a basic unit (like 8 pixels) and use multiples of it. Put a little space between related items (8px), more space between different elements (16px), and even more space between major sections (24px or 32px).</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1385" height="1637" src="https://dash-resources.com/wp-content/uploads/2025/02/image-4.png" alt="Illustration: the same dashboard with better spacing." class="wp-image-713" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-4.png 1385w, https://dash-resources.com/wp-content/uploads/2025/02/image-4-254x300.png 254w, https://dash-resources.com/wp-content/uploads/2025/02/image-4-866x1024.png 866w, https://dash-resources.com/wp-content/uploads/2025/02/image-4-768x908.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-4-1300x1536.png 1300w, https://dash-resources.com/wp-content/uploads/2025/02/image-4-1320x1560.png 1320w" sizes="auto, (max-width: 1385px) 100vw, 1385px" /><figcaption class="wp-element-caption">Now we can breathe! We added space between each section along with borders. We also fix alignment of the info icon, of the logout button and of the activities. The main buttons are spaced gracefully too, as well as the main data.</figcaption></figure>



<p>Remember: <strong>white space isn&#8217;t wasted space</strong>. It&#8217;s like the margins in a book &#8211; without them, everything would be a mess and hard to read. Give your components some breathing room. Your users&#8217; eyes will thank you.</p>



<h2 id='keep-things-homogeneous'  id="boomdevs_6" class="wp-block-heading" >Keep things homogeneous</h2>



<p>In art, <strong>proportions</strong> are an integral part of the works of the greatest masters. Many sculptures and paintings, for example, use the <a href="https://en.wikipedia.org/wiki/Golden_ratio">golden ratio</a>.</p>



<p>The same applies to interfaces: consistency is key to making dashboards look polished and professional.</p>



<p>But what does this mean in practice?</p>



<ul class="wp-block-list">
<li>Apply the same margins and padding throughout, ensuring a balanced layout while respecting hierarchy.</li>



<li>Stick to 2-3 main colors with one accent color for emphasis. Example: if you use blue buttons, don’t randomly switch to green unless it serves a purpose.</li>



<li>Keep shapes and styles uniform—rounded buttons should stay rounded, and shadows should be consistent across cards.</li>
</ul>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1385" height="1637" src="https://dash-resources.com/wp-content/uploads/2025/02/image-5.png" alt="Illustration: the same dashboard with better proportions." class="wp-image-714" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-5.png 1385w, https://dash-resources.com/wp-content/uploads/2025/02/image-5-254x300.png 254w, https://dash-resources.com/wp-content/uploads/2025/02/image-5-866x1024.png 866w, https://dash-resources.com/wp-content/uploads/2025/02/image-5-768x908.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-5-1300x1536.png 1300w, https://dash-resources.com/wp-content/uploads/2025/02/image-5-1320x1560.png 1320w" sizes="auto, (max-width: 1385px) 100vw, 1385px" /><figcaption class="wp-element-caption">We homogenized the buttons, so that only the most important (export data) stands out. By their size and color, the user now guesses instantly what are the most importants buttons.</figcaption></figure>



<p>Small details make a big difference!</p>



<h2 id='remember-to-style-charts-too'  id="boomdevs_7" class="wp-block-heading" >Remember to style charts too</h2>



<p>By default, Plotly charts do not inherit the styles of your app. So, be sure to at least set the font style and colors to match your dashboard.</p>



<p>Also, tweak chart elements like gridlines, legend placement, and axis labels to keep the same spacing you used before (to keep things homogeneous!).</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1391" height="1755" src="https://dash-resources.com/wp-content/uploads/2025/02/image-7.png" alt="Illustration: the same dashboard with better chart styling." class="wp-image-716" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-7.png 1391w, https://dash-resources.com/wp-content/uploads/2025/02/image-7-238x300.png 238w, https://dash-resources.com/wp-content/uploads/2025/02/image-7-812x1024.png 812w, https://dash-resources.com/wp-content/uploads/2025/02/image-7-768x969.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-7-1217x1536.png 1217w, https://dash-resources.com/wp-content/uploads/2025/02/image-7-1320x1665.png 1320w" sizes="auto, (max-width: 1391px) 100vw, 1391px" /><figcaption class="wp-element-caption">We changed the color and the font familiy of the plotly chart to use the same as the rest of the dashboard. You will often need to hardcode the values in Python, in the figure configuration / layout parameters.</figcaption></figure>



<p class="callout">Another <strong>pro tip</strong>: If your dashboard has multiple charts, keep them consistent in terms of styling. Same font, same color scheme, same layout rules—it all helps with clarity and makes your dashboard look like a professionally designed tool.</p>



<h2 id='use-pre-made-components'  id="boomdevs_8" class="wp-block-heading" >Use pre-made components</h2>



<p>Last but not least: you should use component libraries like <a href="https://dash-bootstrap-components.opensource.faculty.ai/">Dash Bootstrap Components</a> (DBC) or <a href="https://www.dash-mantine-components.com/">Dash Mantine Components</a> (DMC). These libraries come with a lot of default CSS rules and choices, a technic known as a <a href="https://meyerweb.com/eric/tools/css/reset/">CSS reset</a>.</p>



<p>These rules ensure that HTML elements are correctly placed, well-spaced, have the right font sizes, and maintain a consistent, homogeneous style. Exactly everything we did before, so it will make you gain time.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1377" height="2113" src="https://dash-resources.com/wp-content/uploads/2025/02/image-8.png" alt="Illustration: the same dashboard with pre made component library." class="wp-image-717" srcset="https://dash-resources.com/wp-content/uploads/2025/02/image-8.png 1377w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-196x300.png 196w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-667x1024.png 667w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-768x1178.png 768w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-1001x1536.png 1001w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-1335x2048.png 1335w, https://dash-resources.com/wp-content/uploads/2025/02/image-8-1320x2026.png 1320w" sizes="auto, (max-width: 1377px) 100vw, 1377px" /><figcaption class="wp-element-caption">We applied the Mantine components styles to our dashboard: buttons are different, and containers have some remarkable shadow. The good thing is that we don’t need all the previous steps to get to this one.</figcaption></figure>



<p>However, these component libraries are popular which is a good and a bad thing:</p>



<ul class="wp-block-list">
<li>The good: <strong>people like them</strong>. Remember, users don’t want anything too crazy—they prefer familiar, intuitive designs. They will like your design because they are used to it.</li>



<li>The bad: <strong>all websites look the same</strong>. However, with the choices you have (layout, colors, fonts, etc.), you can still make your dashboard unique while keeping it aesthetically pleasant.</li>



<li></li>
</ul>



<p class="callout"><strong>My 2 cents:</strong> I always use at least DBC or Mantine in my Dash projects, whether I create custom components or not, because they bring a CSS reset and a lot of CSS utils.</p>



<h3 id='see-also'  id="boomdevs_9" class="wp-block-heading" >See also:</h3>



<ul class="wp-block-list">
<li><a href="https://dash-resources.com/writing-in-process/">How to use Dash Bootstrap Components (soon)</a></li>



<li><a href="https://dash-resources.com/writing-in-process/?article=dash-mantine-components-tutorial">How to use Dash Mantine Components (soon)</a></li>
</ul>



<h2 id='conclusion'  id="boomdevs_10" class="wp-block-heading" >Conclusion</h2>



<p>Let&#8217;s wrap this up with an important reminder: <strong>these aren&#8217;t strict commandments set in stone</strong>. Think of them more as helpful guidelines that, when mixed and matched thoughtfully, can lead to visually appealing dashboards. You don&#8217;t need to follow every single rule.</p>



<p>The best way to improve your design skills is to <strong>experiment and stay observant</strong>. Pay attention to the apps and tools you enjoy using. What makes them pleasant to work with? How do they handle fonts, colors, and spacing? <strong>Take inspiration</strong> from what works well in your favorite applications.</p>



<p>And here&#8217;s a final thought: people tend to find &#8220;normal&#8221; things beautiful.</p>



<ul class="wp-block-list">
<li><strong>You don&#8217;t need to reinvent the wheel </strong>or create the most unique dashboard ever seen. Aim for clean, familiar, and professional rather than wildly original.</li>



<li>A “dash” <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f643.png" alt="🙃" class="wp-smiley" style="height: 1em; max-height: 1em;" /> of creativity is great, but remember that if your dashboard <strong>looks too exotic</strong>, it might end up being harder to use!</li>
</ul>



<p>The goal is simply to create something that looks professional and is pleasant to use.</p>



<p>Follow these guidelines, trust your eye, and you&#8217;ll be well on your way to creating dashboards that both you and your users will enjoy.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>I hope you liked this guide and learn. This is part of my <a href="https://dash-resources.com/dash-plotly-course/">Dash plotly Course</a>, be sure to check it out if you want to learn more about building dashboards. </p>



<p>If you have any question, join us on the dedicated topic on Plotly’s forum: <a href="https://community.plotly.com/t/a-guide-to-beautiful-dashboards-basic-design-principles-dash-resources-com/90495/4">here</a>. </p>
<p>L’article <a href="https://dash-resources.com/a-guide-to-beautiful-dashboards-basic-design-principles/">A guide to beautiful Dashboards (basic design principles)</a> est apparu en premier sur <a href="https://dash-resources.com">dash-resources.com</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Improving Dash DataTables: Simple CSS Tweaks</title>
		<link>https://dash-resources.com/improving-dash-datatables-simple-css-tweaks/</link>
		
		<dc:creator><![CDATA[Fran]]></dc:creator>
		<pubDate>Sun, 19 Jan 2025 12:06:50 +0000</pubDate>
				<category><![CDATA[Intermediate level]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[datatable]]></category>
		<guid isPermaLink="false">https://dash-resources.com/?p=527</guid>

					<description><![CDATA[<p>The Dash DataTable is a powerful component to display tabular data. However, I’ve always felt it didn’t seamlessly integrate with the applications I was developing. [...]</p>
<p>L’article <a href="https://dash-resources.com/improving-dash-datatables-simple-css-tweaks/">Improving Dash DataTables: Simple CSS Tweaks</a> est apparu en premier sur <a href="https://dash-resources.com">dash-resources.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The <a href="https://dash.plotly.com/datatable">Dash DataTable</a> is a powerful component to display tabular data. However, I’ve always felt it didn’t seamlessly integrate with the applications I was developing.</p>



<p>In this article, I’ll show you how you can improve the user interface and user experience (UI/UX) of a DataTable by tweaking some parts using CSS (<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b07.png" alt="⬇" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong><a href="#interactive-app">interactive app below</a></strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b07.png" alt="⬇" class="wp-smiley" style="height: 1em; max-height: 1em;" />)</p>


        
            
            <div class="fit_content">
                <div class="bd_toc_container" data-fixedWidth="">
                    <div class="bd_toc_wrapper" data-wrapperPadding="48px">
                        <div class="bd_toc_wrapper_item">
                            <div class="bd_toc_header active" data-headerPadding="2px">
                                <div class="bd_toc_header_title">
                                    Table of Contents                                </div>
                                <div class="bd_toc_switcher_hide_show_icon">
                                    <span class="bd_toc_arrow"></span>                                </div>
                            </div>
                            <div class="bd_toc_content list-type-disc">
                                <div class="bd_toc_content_list ">
                                    <div class='bd_toc_content_list_item'>    <ul>
      <li class="first">
        <a href="#1-add-bootstrap-styles">1 &#8211; Add Bootstrap styles</a>
      </li>
      <li>
        <a href="#2-improve-the-header">2 &#8211; Improve the header</a>
      </li>
      <li>
        <a href="#3-striped-rows">3 &#8211; Striped rows</a>
      </li>
      <li>
        <a href="#4-hover-effect">4 &#8211; Hover effect</a>
      </li>
      <li>
        <a href="#5-row-selection">5 &#8211; Row selection</a>
      </li>
      <li>
        <a href="#how-to-modify-styles-yourself">How to modify styles yourself</a>
      </li>
      <li>
        <a href="#how-to-reuse-this-code">How to reuse this code</a>
      </li>
      <li class="last">
        <a href="#conclusion">Conclusion</a>
      </li>
    </ul>
</div>                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layout_toggle_button">
                        <span class="bd_toc_arrow"></span>
                    </div>
                </div>
            </div>




<p>Let’s dive in!</p>



<h2 id='1-add-bootstrap-styles'  id="boomdevs_1" class="wp-block-heading" >1 &#8211; Add Bootstrap styles</h2>



<p>First things first: the DataTable’s basic styles don’t integrate well with an app. The main reason is that many styles, such as the font-family, are hardcoded.</p>



<p>If you work with Dash Bootstrap Components, this becomes obvious. Let’s take a simple app as an example:</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import pandas as pd
import numpy as np
import dash_bootstrap_components as dbc
from dash import Dash, html, Input, Output, dash_table

# Create sample data
np.random.seed(42)
size = 20
df = pd.DataFrame({
    "id": range(size),
    "x": np.random.normal(0, 1, size),
    "y": np.random.normal(0, 1, size),
    "category": np.random.choice(["A", "B", "C"], size)
})

# Initialize the Dash app
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

# Define the layout
app.layout = html.Div(
    [
        # Data table
        html.Div(
            dash_table.DataTable(
                id="table",
                columns=[
                    {"name": i, "id": i, "deletable": True, "selectable": True} for i in df.columns
                ],
                data=df.to_dict("records"),

                # Enable multi-row selection and paging
                filter_action="native",
                sort_action="native",
                sort_mode="multi",
                column_selectable="multi",
                row_selectable="multi",  
                page_action="native",
                page_size=10,
            ),
            id="table_container",
        )
    ],
    className="container"
)

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)
</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="654" src="https://dash-resources.com/wp-content/uploads/2025/01/image-8.png" alt="Illustration: the default DataTable is not looking like a dash boostrap table at all." class="wp-image-528" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-8.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-8-300x96.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-8-1024x327.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-8-768x245.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-8-1536x491.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-8-1320x422.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: the default DataTable is looking a bit &#8220;raw&#8221; compared to modern themes like Bootstrap.</figcaption></figure>



<p>The solution is to overwrite the DataTable’s styles with Bootstrap styles. Fortunately, <strong>Ann Marie Ward</strong>, a major Dash contributor, has already created a solution for this <a href="https://hellodash.pythonanywhere.com/adding-themes/DataTable">here</a> ( thank you Ann if you see this <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f64f.png" alt="🙏" class="wp-smiley" style="height: 1em; max-height: 1em;" />).</p>



<p>Her CSS stylesheet includes styles to apply Bootstrap themes to Dash components, such as tables and buttons, for a consistent appearance. We’ll download it <a href="https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.css">(link)</a>, and save into into the assets folder at <code>assets/dbc.css</code>.</p>



<p>Then, we just need to add the <code>dbc</code> CSS class to a container <code>div</code> wrapping our DataTable:</p>



<pre class="wp-block-code"><code lang="python" class="language-python"># Data table
html.Div(
    dash_table.DataTable(
        # ...
    ),
    id="table_container",
    className="dbc"  # we added the dbc class
)</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="633" src="https://dash-resources.com/wp-content/uploads/2025/01/image-9.png" alt="Illustration: the DataTable now has a Bootstrap theme, which integrates better in apps. But the pagination style is incorrect." class="wp-image-529" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-9.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-9-300x93.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-9-1024x317.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-9-768x237.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-9-1536x475.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-9-1320x408.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: the DataTable now has a Bootstrap theme, which integrates better in apps. But the pagination style is incorrect.</figcaption></figure>



<p>As you can see, the table now inherits the Bootstrap styles. However, the pagination remains untouched.</p>



<p>Unfortunately, there is no option to dynamically set the className of the buttons to Bootstrap’s <code>btn</code>, <code>btn-outline-primary</code>, and <code>btn-sm</code>. But we can copy their styles (e.g., from the Bootstrap documentation) and apply them to the four pagination buttons (<code>.first-page</code>, <code>.next-page</code>, <code>.previous-page</code>, <code>.last-page</code>):</p>



<pre class="wp-block-code"><code lang="css" class="language-css">
/* reset font styles on pagination buttons */
.dbc .dash-table-container .previous-next-container .page-number,
.dbc .dash-table-container .previous-next-container .page-number .current-page-container input.current-page {
    font-family: var(--bs-body-font-family);
    font-size: var(--bs-body-font-size);
    font-weight: var(--bs-body-font-weight);
    line-height: var(--bs-body-line-height);
    min-width: auto !important;
}

/* apply the same styles as .btn, .btn-sm, .btn-outline-primary */
.dbc .dash-table-container .previous-next-container {
    button {
        &amp;.first-page,
        &amp;.last-page,
        &amp;.previous-page,
        &amp;.next-page {
            display: inline-block;
            font-weight: 400;
            text-align: center;
            vertical-align: middle;
            user-select: none;
            padding: 0.25rem 0.5rem;
            margin: 0 0.25rem;
            font-size: 0.875rem;
            line-height: 1.5;
            border-radius: 0.2rem;
            transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
            color: var(--bs-primary);
            border: 1px solid var(--bs-primary);
            background-color: transparent;

            &amp;:hover {
                color: var(--bs-white) !important; /* override dbc.css */
                background-color: var(--bs-primary);
                border-color: var(--bs-primary);
            }

            &amp;:focus {
                box-shadow: 0 0 0 0.25rem var(--bs-primary-rgb, rgba(13, 110, 253, 0.25));
            }

            &amp;:active,
            &amp;.active {
                color: var(--bs-white);
                background-color: var(--bs-primary);
                border-color: var(--bs-primary);
            }

            &amp;:disabled,
            &amp;.disabled {
                color: var(--bs-primary);
                background-color: transparent;
                border-color: var(--bs-primary);
                opacity: 0.65;
                pointer-events: none;
            }
        }
    }
}</code></pre>



<p>CSS rules are applied by order of importance (priority) to an element. The more you add precision to the selection, the more weight the rule has:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* a rule targetting a button */
.dash-table-container button { ... }
/* equivalent, but more precise (more weight): */
body .dash-table-container .previous-next-container button {...} </code></pre>



<p>That’s why sometimes we see a long selector definition, and sometimes not —because in some cases, it’s too complex to increase the weight of a rule. Then <code>!important</code> keyword enable us to override some CSS properties without having to make a stronger rules.</p>



<p class="callout"><strong>Good to know:</strong> It’s better to avoid <code>!important</code> whenever possible because each use makes future overrides more complex. Imagine if the creators of Bootstrap had used <code>!important</code> everywhere—overriding their styles would have been a nightmare!</p>



<p>Now let’s visualize the result:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="630" src="https://dash-resources.com/wp-content/uploads/2025/01/image-10.png" alt="Illustration: we now have proper Bootstrap buttons." class="wp-image-530" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-10.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-10-300x92.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-10-1024x315.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-10-768x236.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-10-1536x473.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-10-1320x406.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: we now have proper Bootstrap buttons.</figcaption></figure>



<p>We now have a Bootstrap-styled DataTable. Much better!</p>



<h2 id='2-improve-the-header'  id="boomdevs_2" class="wp-block-heading" >2 &#8211; Improve the header</h2>



<p>There are a few things I don’t like about the default DataTable header, but thankfully, everything is fixable with CSS.</p>



<p>First, the header row should be highlighted:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* make the heading bold and with a gray background */
.header-style .dash-table-container th {
    background-color: var(--bs-gray-200) !important;
}

.header-style .dash-table-container th .column-header-name {
    font-weight: bold;
}

.header-style .dash-table-container th input::placeholder {
    /* overrides back a style set in dbc.css */
    background-color: transparent !important;
}</code></pre>



<p>If you are not familiar with CSS variables, take a look at <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties">this documentation</a>. They allow using the Bootstrap without hardcoding them. It is useful for themes like dark/light themes that will only require a change of variable value.</p>



<p>Second, the icons in the header are not uniformly sized, vertically aligned, or evenly spaced. Let’s fix that:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* better homogeneous styling of buttons */
.header-style .column-actions {
    /* let us breath! */
    gap: 0.4em; 
    margin-right: 0.5em;
}

.header-style .column-actions &gt; span {
    /* align components vertically */
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.header-style .dash-table-container th input[type="radio"],
.header-style .dash-table-container th input[type="checkbox"] {
		/* fix inconsistent color and size */
    width: 1rem;
    height: 1rem;
    opacity: 0.6;
}

.header-style .dash-table-container th input[type="radio"]:checked,
.header-style .dash-table-container th input[type="checkbox"]:checked {
		/* when checked, reset opacity */
    opacity: 1;
}</code></pre>



<p>Finally, clicking on the case-sensitive filter (<code>aA</code>) causes the column width to change. Adding a transparent border of the same size resolves this issue:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">.header-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner th.dash-filter input.dash-filter--case--insensitive {
    /* avoid column width size change when clicking */
    border-style: solid;
    border-width: 2px;
    border-color: transparent;
}</code></pre>



<p>Remember the CSS weights for rules? The definition above is long on purpose: thanks to this, I don’t need to add <code>!important</code>.</p>



<p>Here is the result:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="638" src="https://dash-resources.com/wp-content/uploads/2025/01/image-11.png" alt="Illustration: a DataTable with a better looking header." class="wp-image-531" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-11.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-11-300x93.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-11-1024x319.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-11-768x239.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-11-1536x479.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-11-1320x411.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: a DataTable with a better looking header.</figcaption></figure>



<p>At this point, there are two types of people: those who notice the differences and those who don’t. If you don’t, I don’t blame you—you live in a much simpler world than I do!</p>



<p>If you do notice the differences, you may experience the satisfaction of seeing these buttons properly arranged. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 id='3-striped-rows'  id="boomdevs_3" class="wp-block-heading" >3 &#8211; Striped rows</h2>



<p>Displaying striped rows can make the table easier on the eyes. It’s a tiny detail, but combined with other enhancements (like mouseover color), it improves the overall user experience of your app.</p>



<p>Striped rows for the DataTable <a href="https://dash.plotly.com/datatable/style#striped-rows">can be achieved in Python</a>, but a simple CSS snippet will also do the job:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* Alternating row background */
.striped-style .dash-table-container td:not(.cell--selected) {
    background-color: transparent !important;
}

.striped-style .dash-table-container tr:nth-child(odd) {
    background-color: var(--bs-gray-100) !important;
}

.striped-style .dash-table-container tr:nth-child(even) {
    background-color: var(--bs-body-bg) !important;
}</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="638" src="https://dash-resources.com/wp-content/uploads/2025/01/image-12.png" alt="Illustration: striped lines help visualizing many rows." class="wp-image-532" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-12.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-12-300x93.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-12-1024x319.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-12-768x239.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-12-1536x479.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-12-1320x411.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: striped lines help visualizing many rows.</figcaption></figure>



<p>The CSS property <code>:nth-child</code> is the key here, as it allow selecting either odd or even rows <code>tr</code>. The cells <code>td</code> are made transparent as the background is changed at the row level.</p>



<h2 id='4-hover-effect'  id="boomdevs_4" class="wp-block-heading" >4 &#8211; Hover effect</h2>



<p>Adding a hover effect is a simple trick to enhance interactivity, even for static tables. It implicitly signals to the user that the table is interactive.</p>



<p>Here’s the CSS to add a hover effect:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* Hover row background */
.hover-style .dash-table-container tr:hover td:not(.cell--selected)  {
    background-color: var(--bs-gray-200) !important;
}</code></pre>



<p>The background is applied to all cells expect selected cells (i.e., active cells), using <code>td:not(.cell—selected)</code>.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1186" height="366" src="https://dash-resources.com/wp-content/uploads/2025/01/datatable_hover.gif" alt="Illustration: row hover is a simple trick to make a table look like “interactive”." class="wp-image-533"/><figcaption class="wp-element-caption">Illustration: row hover is a simple trick to make a table look like “interactive”.</figcaption></figure>



<p>The hover color is slightly darker (<code>—bs-gray-200</code> instead of <code>—bs-gray-100</code>) to remain visible even on striped rows.</p>



<h2 id='5-row-selection'  id="boomdevs_5" class="wp-block-heading" >5 &#8211; Row selection</h2>



<p>By default, clicking on a cell highlights it. However, in most cases, it’s more intuitive to highlight the entire row. It is also helpful to emphasize rows selected via the left checkboxes. Let’s address this.</p>



<p>This code is slightly more complex:</p>



<ul class="wp-block-list">
<li>We style rows that have the <code>.cell--selected</code> class. This includes rows selected via checkboxes or mouse clicks.</li>



<li>To avoid overlapping borders, we use the adjacent sibling selector (<code>+</code>) to remove duplicate borders. For example, instead of <code>|X||X|</code>, we get <code>|X|X|</code>.</li>
</ul>



<pre class="wp-block-code"><code lang="css" class="language-css">/* apply style to selected cell */
.selection-style .dash-table-container tr:has(td.cell--selected) td.cell--selected {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border: 1px solid var(--bs-primary) !important;
    outline: none !important;
}

/* apply styles to the other cells of selected rows */
.selection-style .dash-table-container tr:has(td.cell--selected) td {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border: 1px solid #d0d0d0 !important;
}

/* remove border between selected cells (vertically) */
.selection-style .dash-table-container tr:has(td.cell--selected) + tr:has(td.cell--selected) td.cell--selected {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border-top: none !important;
}

/* remove border between selected cells (horizontally) */
.selection-style .dash-table-container tr:has(td.cell--selected) td.cell--selected + td.cell--selected {
    border-left: none !important;
}

/* apply style to checked rows */
.selection-style .dash-table-container tr:has(input:checked) td {
    background-color: rgba(var(--bs-primary-rgb), 0.1) !important;
    border: 1px solid #d0d0d0 !important;
}
</code></pre>



<p>Most styles here use <code>!important</code> to remain compatible with the <code>dbc.css</code> styles. If we don’t keep the <code>dbc.css</code> styles, additional lines would be required for proper functionality:</p>



<pre class="wp-block-code"><code lang="css" class="language-css">/* add dbc styles to selection-style */
body .selection-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner td {
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    border: var(--bs-border-width) solid var(--bs-border-color) !important;
}
.selection-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner table {
    border-collapse: inherit;
    border-spacing: unset;
}</code></pre>



<p>The selection works for rows selected via checkboxes, single mouse clicks, and multiple selections (holding <code>Shift</code>).</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="648" src="https://dash-resources.com/wp-content/uploads/2025/01/image-13.png" alt="Illustration: selected rows are not more visible." class="wp-image-534" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-13.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-13-300x95.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-13-1024x324.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-13-768x243.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-13-1536x486.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-13-1320x418.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: selected rows are not more visible.</figcaption></figure>



<p>Notice that row selection do not work well visually with striped rows, so I removed it from the above screenshot. Feel free to try it yourself with the <strong>interactive app</strong>!</p>



<h2 id='how-to-modify-styles-yourself'  id="boomdevs_6" class="wp-block-heading" >How to modify styles yourself</h2>



<p>You might wonder how I came up with these precise CSS rules while reading this article.</p>



<p>Overriding CSS styles involves trial and error. Essentially, you need to use developer tools to inspect the applied rules on the target element and figure out how to “override” those rules.</p>



<p>Here’s how you can do it :</p>



<ol class="wp-block-list">
<li><em>Right click</em> in the browser on the element &gt; <em>Inspect</em></li>



<li>Select the element in the browser in the list if necessary</li>



<li>Look at the CSS styles applied to this element.</li>
</ol>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="1098" src="https://dash-resources.com/wp-content/uploads/2025/01/image-14.png" alt="Illustration: how you can find and debug CSS styles in the browser." class="wp-image-535" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-14.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-14-300x161.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-14-1024x549.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-14-768x412.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-14-1536x824.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-14-1320x708.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">Illustration: how you can find and debug CSS styles in the browser.</figcaption></figure>



<p>CSS rules are applied in order of priority. Inline styles (applied directly to the element) usually take precedence. Overridden styles are shown as struck-through in developer tools, indicating which rule has the highest priority.</p>



<p>You can also use developer tools to copy styles from another source (as I did for Bootstrap):</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2048" height="1092" src="https://dash-resources.com/wp-content/uploads/2025/01/image-15.png" alt="lllustration: the same principle applies to copy styles from another app or source." class="wp-image-536" srcset="https://dash-resources.com/wp-content/uploads/2025/01/image-15.png 2048w, https://dash-resources.com/wp-content/uploads/2025/01/image-15-300x160.png 300w, https://dash-resources.com/wp-content/uploads/2025/01/image-15-1024x546.png 1024w, https://dash-resources.com/wp-content/uploads/2025/01/image-15-768x410.png 768w, https://dash-resources.com/wp-content/uploads/2025/01/image-15-1536x819.png 1536w, https://dash-resources.com/wp-content/uploads/2025/01/image-15-1320x704.png 1320w" sizes="auto, (max-width: 2048px) 100vw, 2048px" /><figcaption class="wp-element-caption">lllustration: the same principle applies to copy styles from another app or source.</figcaption></figure>



<p>If you want to know more about this debugging process, please <a href="https://dash-resources.com/request-an-article/">send an article request</a>.</p>



<h2 id='how-to-reuse-this-code'  id="boomdevs_7" class="wp-block-heading" >How to reuse this code</h2>



<p>Each part of the styling corresponds to a specific class. This means you can copy and paste the code and simply add the relevant class name: <code>dbc</code>(for the Bootstrap style), <code>header-style</code>, <code>striped-style</code>, <code>hover-style</code> and <code>selection-style</code>.</p>



<p>Example with all styles except striped rows:</p>



<pre class="wp-block-code"><code lang="python" class="language-python"># Data table
html.Div(
    dash_table.DataTable(
        # ...
    ),
    id="table_container",
    className="dbc header-style hover-style selection-style" # all styles except striped rows
)
</code></pre>



<p id="interactive-app">You can also play with the interactive app below to see the differences (or <a href="https://scripts.dash-resources.com/datatable_styling/app.py/">click here</a>):</p>



<iframe loading="lazy" src="https://scripts.dash-resources.com/datatable_styling/app.py/" width="100%" height="550" frameBorder="0"></iframe>



<details class="wp-block-details is-layout-flow wp-block-details-is-layout-flow"><summary>Click here to see the final CSS code <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b07.png" alt="⬇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></summary>
<pre class="wp-block-code"><code lang="css" class="language-css">/* Author: Fran from dash-resources.com 
 * Originally posted in: https://dash-resources.com/improving-dash-datatables-simple-css-tweaks/
 */

/* ------------------------------------------------*/
/* Bootstrap style */

/* reset font styles on pagination buttons */
.dbc .dash-table-container .previous-next-container .page-number,
.dbc .dash-table-container .previous-next-container .page-number .current-page-container input.current-page {
    font-family: var(--bs-body-font-family);
    font-size: var(--bs-body-font-size);
    font-weight: var(--bs-body-font-weight);
    line-height: var(--bs-body-line-height);
    min-width: auto !important;
}

/* apply the same styles as .btn, .btn-sm, .btn-outline-primary */
.dbc .dash-table-container .previous-next-container {
    button {
        &amp;.first-page,
        &amp;.last-page,
        &amp;.previous-page,
        &amp;.next-page {
            display: inline-block;
            font-weight: 400;
            text-align: center;
            vertical-align: middle;
            user-select: none;
            padding: 0.25rem 0.5rem;
            margin: 0 0.25rem;
            font-size: 0.875rem;
            line-height: 1.5;
            border-radius: 0.2rem;
            transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
            color: var(--bs-primary);
            border: 1px solid var(--bs-primary);
            background-color: transparent;

            &amp;:hover {
                color: var(--bs-white) !important; /* override dbc.css */
                background-color: var(--bs-primary);
                border-color: var(--bs-primary);
            }

            &amp;:focus {
                box-shadow: 0 0 0 0.25rem var(--bs-primary-rgb, rgba(13, 110, 253, 0.25));
            }

            &amp;:active,
            &amp;.active {
                color: var(--bs-white);
                background-color: var(--bs-primary);
                border-color: var(--bs-primary);
            }

            &amp;:disabled,
            &amp;.disabled {
                color: var(--bs-primary);
                background-color: transparent;
                border-color: var(--bs-primary);
                opacity: 0.65;
                pointer-events: none;
            }
        }
    }
}

/* ------------------------------------------------*/
/* Header style 
 * Notice we use !important to override the default style and those written in the dbc.css file
 */

/* make the heading bold and with a gray background */
.header-style .dash-table-container th {
    background-color: var(--bs-gray-200) !important;
}

.header-style .dash-table-container th .column-header-name {
    font-weight: bold;
}

.header-style .dash-table-container th input::placeholder {
    /* overrides back a style set in dbc.css */
    background-color: transparent !important;
}

/* better homogeneous styling of buttons */
.header-style .column-actions {
    /* let us breath! */
    gap: 0.4em; 
    margin-right: 0.5em;
}

.header-style .column-actions &gt; span {
    /* align components vertically */
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.header-style .dash-table-container th input[type="radio"],
.header-style .dash-table-container th input[type="checkbox"] {
    /* fix inconsistent color and size */
    width: 1rem;
    height: 1rem;
    opacity: 0.6;
}

.header-style .dash-table-container th input[type="radio"]:checked,
.header-style .dash-table-container th input[type="checkbox"]:checked {
        /* when checked, reset opacity */
    opacity: 1;
}

.header-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner th.dash-filter input.dash-filter--case--insensitive {
    /* avoid column width size change when clicking */
    border-style: solid;
    border-width: 2px;
    border-color: transparent;
}

/* ------------------------------------------------*/
/* Alternating row background */
.striped-style .dash-table-container td:not(.cell--selected) {
    background-color: transparent !important;
}

.striped-style .dash-table-container tr:nth-child(odd) {
    background-color: var(--bs-gray-100) !important;
}

.striped-style .dash-table-container tr:nth-child(even) {
    background-color: var(--bs-body-bg) !important;
}

/* ------------------------------------------------*/
/* Hover row background */
.hover-style .dash-table-container tr:hover td:not(.cell--selected)  {
    background-color: var(--bs-gray-200) !important;
}

/* ------------------------------------------------*/
/* Selected row style */

/* apply style to selected cell */
.selection-style .dash-table-container tr:has(td.cell--selected) td.cell--selected {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border: 1px solid var(--bs-primary) !important;
    outline: none !important;
}

/* apply styles to the other cells of selected rows */
.selection-style .dash-table-container tr:has(td.cell--selected) td {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border: 1px solid #d0d0d0 !important;
}

/* remove border between selected cells (vertically) */
.selection-style .dash-table-container tr:has(td.cell--selected) + tr:has(td.cell--selected) td.cell--selected {
    background-color: rgba(var(--bs-primary-rgb), 0.2) !important;
    border-top: none !important;
}

/* remove border between selected cells (horizontally) */
.selection-style .dash-table-container tr:has(td.cell--selected) td.cell--selected + td.cell--selected {
    border-left: none !important;
}

/* apply style to checked rows */
.selection-style .dash-table-container tr:has(input:checked) td {
    background-color: rgba(var(--bs-primary-rgb), 0.1) !important;
    border: 1px solid #d0d0d0 !important;
}

/* add dbc styles to selection-style */
body .selection-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner td {
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    border: var(--bs-border-width) solid var(--bs-border-color) !important;
}
.selection-style .dash-table-container .dash-spreadsheet-container .dash-spreadsheet-inner table {
    border-collapse: inherit;
    border-spacing: unset;
}</code></pre>
</details>



<h2 id='conclusion'  id="boomdevs_8" class="wp-block-heading" >Conclusion</h2>



<p>I hope this article showed how far you can go with CSS to override predefined styles. Even though it’s not perfect, small enhancements like these can significantly improve the user interface and user experience of your dashboard or app.</p>



<p>Feel free to contact me if you have suggestions for this code. <strong>Don’t forget to subscribe to get notified about new articles!</strong></p>



<p>Happy Dash coding <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>L’article <a href="https://dash-resources.com/improving-dash-datatables-simple-css-tweaks/">Improving Dash DataTables: Simple CSS Tweaks</a> est apparu en premier sur <a href="https://dash-resources.com">dash-resources.com</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
