Software & Web Engineering

What Is CSS? The Styling Language That Makes a Website Look Good on Every Screen

Bảng màu sơn, mẫu vải và bản in thử giao diện website trên bàn gỗ sáng dưới ánh sáng tự nhiên
In this article

    Open the same website on a laptop and then on a phone, and you can see two completely different scenes. On the laptop, everything sits neatly in place. On the phone, text spills off the screen, buttons overlap, and the whole page jumps down a notch once an image finishes loading. The content is identical on both sides. What differs is the presentation. What is CSS, and how does it decide that? CSS is the styling language of the web: it tells the browser how each part of a page should look, from text color and size to how the layout adapts to each screen size.

    This article explains CSS from the start, using one running example: the homepage of a small bakery. You will see what a CSS rule is made of, how the browser applies it to a page, why the CSS you write sometimes has no effect, and why a few careless lines of CSS can make a page jump around and lose visitors. At the end, you will open the browser’s own tools and edit CSS live on the very page you are reading, with nothing to install.

    Quick summary

    • CSS is the styling language: HTML says what a page contains, CSS says how each part looks.
    • A CSS rule has a selector and a declaration block, for example h1 { color: red; }.
    • When two rules conflict, the browser decides by source order, specificity, and !important.
    • CSS also shapes experience and search rankings: a page must fit every screen and must not shift while loading.

    What is CSS?

    CSS stands for Cascading Style Sheets. According to MDN, CSS is a rule-based language: you write rules that say which group of styles applies to which elements on a page. HTML says what a page contains: a heading, a paragraph, a button. CSS says how each of those parts looks: what color the heading is, how big it is, where the button sits.

    Where did CSS come from?

    Separating content from presentation is the original idea behind CSS. Håkon Wium Lie, then working at CERN, published the first CSS draft in October 1994, then developed it further with Bert Bos. On 17 December 1996, W3C published CSS Level 1 as an official Recommendation. Before that, changing a text color or size meant stuffing formatting tags straight into the HTML, which made the source messy and hard to edit. With CSS, one styling file serves a whole website: change one place and hundreds of pages change with it.

    CSS is so widespread that almost no website goes without it. According to W3Techs, as of 4 December 2026, CSS appears on 99.7% of all surveyed websites. That number is no surprise: even if you never write a line of CSS yourself, the browser still applies its own default styles. According to MDN, headings show up larger than body text on their own, paragraphs break onto new lines with space between them, and links get a color and an underline by themselves. Those are the browser’s default CSS, not HTML. The CSS you write is only a layer on top of that default layer.

    What is a CSS rule made of?

    A CSS rule has two parts: a selector and a declaration block. The selector tells the browser which elements to style. The declaration block sits inside curly braces and holds property-value pairs, separated by a colon and finished with a semicolon.

    Take the bakery page. Its main heading is the bakery’s name, written in an h1 tag. The rule below tells the browser to paint that heading red and set its size to 2.5 times the normal text size:

    h1 {
      color: red;
      font-size: 2.5em;
    }

    h1 is the selector. color: red; and font-size: 2.5em; are two declarations. The color property takes a color value, and the font-size property takes a size value. A CSS file is hundreds of rules like this written one after another. When you add a new paragraph to the page, the rule for p applies to it without any extra writing, because the p selector already covers every paragraph on the page.

    There are three ways to attach CSS to a web page: write it straight into the style attribute of a tag (inline CSS, which applies to that tag alone), put CSS inside a style tag at the top of the page (internal CSS, which applies to the whole page), or write CSS in a separate file ending in .css and link to it from the page with a link tag (external CSS). The third way is the one used in practice: one CSS file shared across a whole website, where one edit changes every page.

    How does the browser apply CSS to a page?

    Knowing what CSS is is only half the story; the other half is how the browser handles it. According to MDN, when you open a web page, the browser takes the HTML document and turns it into a DOM tree, the tree structure of the elements nested inside each other on the page. Then the browser collects every CSS rule it can find and sorts them into groups by the elements their selectors target, applies them to the DOM tree to build a render tree, then paints that tree on screen: HTML builds the frame, CSS paints it and arranges it, and the browser puts the two together and draws the result. Anything that looks wrong on screen sits in one of three places, and the next section covers the third: the rule is right but applied to the wrong place.

    Why does your CSS have no effect?

    Why is that? The word “cascading” in the name of CSS means styles fall in layers. When two rules set different values for the same property of the same element, the browser has to pick one. According to MDN, the browser decides by three factors, in increasing order of importance: source order, selector specificity, and importance.

    The later rule wins

    Source order is the easiest factor to grasp. When two rules are equally specific, the one written later wins. The example below paints the heading blue, even though the red rule comes first:

    h1 {
      color: red;
    }
    
    h1 {
      color: blue;
    }

    The more specific selector wins

    Specificity is the factor that surprises people most. A type selector picks every element of one kind, so it carries the lowest weight. A class selector, written with a dot, picks only elements carrying that class, so it weighs more. An ID selector, written with a hash, picks exactly one element, so it weighs the most. In the example below, the heading carrying the class main-heading shows up red, even though the blue rule comes later, because a class selector outweighs a type selector:

    .main-heading {
      color: red;
    }
    
    h1 {
      color: blue;
    }

    Importance: !important

    Importance is !important attached after a value. It beats both source order and specificity. Using it once as a quick fix is fine, but leaning on it leaves you with no way to override anything later, because every rule carries !important and the browser falls back to comparing source order. The usual advice when building websites is to treat !important as an emergency exit, not an everyday tool.

    Inheritance: children take after their parents

    Some properties, such as color or font-family, pass from a parent element down to its children when set on the parent, unless a child has its own rule. Properties such as width, margin, padding, or border do not inherit. That is why setting the text color once on body recolors the whole page, while setting a width on body does not narrow every child element along with it.

    Everything on a page is a box

    If you want to understand what is CSS in full, start with the box. According to MDN, every element in CSS has a box around it, and understanding those boxes is the key to building layouts. Each box has four layers, from the inside out: the content, padding as the cushion around the content, border as the outline, and margin as the empty space outside that separates one box from another.

    The easiest place to trip up is how the browser works out a box’s width. By default, the width property counts only the content, then adds padding and border on top. A box set to width: 400px with 20px of padding on each side and a 2px border takes up 444px on screen, not 400px. Switch to box-sizing: border-box and the padding and border are subtracted inside that 400px, so the box takes up exactly 400px as you set it. Many websites set border-box on every element from the start so the declared size is also the real size on screen.

    Laying things out: flexbox and grid

    People used to lay out pages by floating boxes to the left or right, a patched-together method that was hard to control. Flexbox and grid answer the practical half of the question “what is CSS”: how do you arrange boxes into a usable page? Modern CSS has two tools built for this job. Flexbox arranges elements in a row or a column, which fits things like navigation bars or rows of buttons. Grid splits a page into rows and columns, which fits the overall layout of a page with a main content column and a side column.

    The two tools do not exclude each other: grid usually frames the large layout of the page, while flexbox arranges the small elements inside each cell of that frame.

    One page, every screen: media queries and responsive design

    On the same bakery page, a visitor on a laptop sees three product columns side by side, while a visitor on a phone needs to see a single column stacked vertically. CSS does this with a media query: a block of rules that only applies when the screen meets a condition, usually its width. The example below switches a three-column layout to one column when the screen is narrower than 600px:

    .products {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
    }
    
    @media (max-width: 600px) {
      .products {
        grid-template-columns: 1fr;
      }
    }

    This approach is called responsive design: the page adapts itself to the screen size. Google recommends responsive design because it is the easiest pattern to implement and maintain, and Google uses the mobile version of a page for indexing and ranking, an approach called mobile-first indexing. That is why the question of what CSS is does not stop at making things pretty: the same HTML with careless CSS on small screens frustrates visitors and also shapes how Google sees your page.

    Careless CSS makes the page jump

    There is a very common CSS mistake that visitors feel at once: the page loads and its content jumps up and down. You are reading a paragraph when an image above it finishes loading and pushes the whole paragraph down. You are about to tap a button when it gets pushed somewhere else just before your finger lands. Google measures this with the CLS metric, Cumulative Layout Shift. According to web.dev, a CLS of 0.1 or less is good, and above 0.25 is poor.

    The cause usually sits in one of three places. First, an image or video with no declared size, so the browser does not know how much room to leave and pushes the surrounding content once the file loads. The fix is to write width and height on the image so the browser reserves exactly the right space from the start. Second, a font that loads later and renders larger or smaller than the fallback font, making the whole page stretch. Third, an ad or third-party widget that resizes itself once it loads.

    Motion can cause shifts too

    Changing top, left, width, or height to animate something forces the browser to recalculate the layout of the whole page, which easily causes shifts. Web.dev recommends using transform to move and scale elements, because transform does not change the layout around them. The question of what is CSS also covers what happens when it is written carelessly: a few wrong properties are enough to make a reader tap the wrong thing and leave.

    Try it right now on this page with DevTools

    You do not need to create a file or install software to see CSS at work. Every modern browser ships with developer tools, called DevTools. According to MDN, you only need to right-click any element on the page and choose Inspect. DevTools opens with two panels: the HTML tree on the left and the CSS rules applying to your selected element on the right.

    Try three things, right here on this article:

    1. Select the article heading and change the color value in the rules panel. The text color changes at once on the page, with no reload.
    2. Uncheck a rule to switch it off temporarily, then switch it back on, and watch how the page changes.
    3. Select a paragraph and add a new property, for example font-size: 20px, at the end of a rule. DevTools suggests property names as you type.

    If a rule is crossed out in the DevTools panel, it is losing to another rule under exactly the three cascade factors from earlier: source order, specificity, or !important. This is also the fastest way to find out why CSS you wrote has no effect on a real website, because you can see which rule is winning and which one is crossed out instead of guessing.

    Every change you make in DevTools lives only in your browser and disappears when you reload the page. To keep it, you have to copy the value you tried into the website’s real CSS file. DevTools is a place to experiment, not a place to save.

    Common misunderstandings

    Wrong Right
    CSS is a programming language. CSS is a declarative styling language: you describe the look you want, and the browser works out how to apply it. CSS has no loops or conditions like a programming language.
    Learning CSS means memorizing properties. Learning CSS means learning how the browser decides which style wins: the cascade, specificity, inheritance, and the box model. Properties can be looked up when needed.
    Once CSS is written, the page looks good on every device. CSS only looks good on devices you have checked. You have to test on narrow screens and use media queries for each size range.
    !important is a quick fix for every CSS conflict. !important should only be used in emergencies. Overusing it leaves no way to override later, and debugging gets much harder.
    CSS is only about looks, not search. CSS decides whether a page fits small screens and whether it shifts while loading, two things Google uses to judge the page experience.

    Frequently asked questions

    How is CSS different from HTML?

    HTML says what a page contains and how the parts relate: this is a heading, this is a paragraph, this is a button. CSS says how each part looks: what color, what size, where it sits. HTML without CSS is still readable thanks to the browser’s default styles, but every page would look the same. You can read more in What is HTML.

    Is CSS a programming language?

    No. CSS is a declarative language: you describe the result you want, for example a red heading, and the browser works out how to apply it. CSS has no variables, loops, or branches like a programming language, although newer versions have added CSS variables and some calculation features.

    How is CSS different from JavaScript?

    CSS handles the look: colors, layout, effects. JavaScript handles behavior: what happens when you click a button, how data loads and shows up. A page can use only HTML and CSS and still carry full content, but without JavaScript it does not respond to what the user does. You can read more in What is JavaScript and What is HTML.

    What to do next

    CSS looks simple in its syntax, but its weight sits in how the browser decides which style wins and how that style affects the reader. Start by opening DevTools on this very page and changing the color of a heading: you have just seen the whole life cycle of a CSS rule, from writing it to applying it, checking it, and fixing it. Once that feels familiar, write a separate CSS file for a simple HTML page, then open that page on a phone to watch the media query at work. If the page holds up on the phone as well, you have grasped the hardest part of CSS.


    Sources

    • MDN, “What is CSS?”, https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/What_is_CSS
    • MDN, “Handling conflicts”, https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_conflicts
    • MDN, “Debugging CSS”, https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Debugging_CSS
    • W3C, “A brief history of CSS until 2016”, https://www.w3.org/Style/CSS20/history.html
    • W3Techs, “Usage Statistics of Site Elements for Websites, December 2026”, https://w3techs.com/technologies/overview/site_element
    • web.dev, “Cumulative Layout Shift (CLS)”, https://web.dev/articles/cls
    • Google Search Central, “Mobile-first Indexing Best Practices”, https://developers.google.com/search/docs/crawling-indexing/mobile/mobile-sites-mobile-first-indexing
    Assistant Gnurt

    Full-stack developer

    Discuss your project

    Comments

    No comments yet. Be the first!