Can CSS achieve Turing completeness?

As far as my understanding goes, CSS is not considered Turing complete. However, I admit that my knowledge of CSS is quite limited.

Can CSS be classified as Turing complete?

Answer №1

One interesting way to demonstrate the Turing-completeness of CSS3 is by encoding Rule 110 using it. This involves creating an HTML file with appropriate user interactions to simulate the execution of CSS. You can find a well-implemented version here, and below is another implementation:

body {
    -webkit-animation: bugfix infinite 1s;
    margin: 0.5em 1em;
}
@-webkit-keyframes bugfix { from { padding: 0; } to { padding: 0; } }

/*
 * 111 110 101 100 011 010 001 000
 *  0   1   1   0   1   1   1   0
 */

body > input {
    -webkit-appearance: none;
    display: block;
    float: left;
    border-right: 1px solid #ddd;
    border-bottom: 1px solid #ddd;
    padding: 0px 3px;
    margin: 0;
    font-family: Consolas, "Courier New", monospace;
    font-size: 7pt;
}
body > input::before {
    content: "0";
}

p {
    font-family: Verdana, sans-serif;
    font-size: 9pt;
    margin-bottom: 0.5em;
}

... (CSS code continues)

<p><a href="http://en.wikipedia.org/wiki/Rule_110">Rule 110</a> in (webkit) CSS, proving Turing-completeness.</p>

<!-- A total of 900 checkboxes required -->

... (HTML code continues)

Answer №2

One concept related to Turing completeness involves the famous halting problem.

This implies that if CSS were to be considered Turing complete, there would not exist a universal method for determining whether a CSS program will terminate its execution or continue indefinitely.

However, we can actually devise such a method specifically for CSS! Here's how:

  • If the stylesheet does not include any animations, then it will eventually stop running.

  • If animations are present, then:

    • If any animation-iteration-count is set to infinite, and the corresponding selector is matched in the HTML, then it will keep looping endlessly without halting.

    • Otherwise, it will eventually halt.

In conclusion, as we have successfully addressed the halting problem within the context of CSS, it logically follows that CSS is not Turing complete.

(It has been pointed out by Daniel Wagner that considerations besides animations, like selector matching or layout, could potentially contribute to achieving Turing completeness. Nonetheless, formal arguments supporting this remain challenging, but I will attempt to explain why such an occurrence is improbable.)

Firstly: Languages that are Turing complete have mechanisms for recursively feeding data back into themselves. However, the structure of CSS itself inhibits this feedback loop:

  • @media queries only assess properties of the browser environment, such as viewport dimensions or screen resolution. While these properties may vary due to user actions or JavaScript manipulation (e.g., resizing the window), they cannot be altered solely through CSS directives.

  • The pseudo-elements ::before and ::after are considered separate from the DOM structure and thus pose limitations on possible interactions.

  • Selector combinators facilitate inspections of elements above or preceding the current element, hence preventing cyclic dependencies.

  • Although it is feasible to hover over an element and trigger behaviors like moving it, the computed changes occur only with ongoing mouse movement.

This rationale should convince you that solely relying on selector matching is insufficient to achieve Turing completeness. What about layout functionalities?

The intricate nature of modern CSS layouts involving techniques like Flexbox and Grid adds complexity to the scenario. Even supposing infinite loops could somehow be triggered through layout operations, capitalizing on this for practical computations would prove arduous. This difficulty arises from CSS selectors addressing only internal structural aspects of the DOM rather than real-time screen arrangement. Thus, any argument asserting Turing completeness via layout functions must rely exclusively on layout components.

Lastly – and critically important – browser developers intentionally maintain CSS as non-Turing complete. By imposing constraints on the language, developers foster opportunities for efficient optimizations that enhance web performance universally. Moreover, Google allocates significant resources, like an entire server farm, towards identifying bugs in Chrome. If a loophole existed allowing for indefinite loops using CSS, it would likely have been detected already 😉

Answer №3

According to the source I found, it's clear that CSS is not Turing complete. The article makes a strong case against trying to make it one.

In one insightful comment from the article:

Indeed, I don't believe that CSS has the characteristics of being Turing complete. The absence of functions in CSS prevents the possibility of writing an interpreter, an essential component for a system to be considered Turing-complete. With no direct access to variables or the ability to represent program structures in CSS, achieving Turing completeness seems unattainable.

Answer №4

This response is incorrect as it blends descriptions of UTM with the Universal Turing Machine itself.

While we do have a solid response from a different angle, it does not directly highlight the flaws in the current top answer.


Let's start by acknowledging that humans can function as a UTM. This implies that if we were to do:

CSS + Human == UTM

The CSS component becomes redundant since all tasks can be carried out by the Human who will perform the UTM task. The act of clicking could serve as a UTM because clicks are targeted rather than random.

Instead of CSS, I might utilize this text (Rule 110):

000 -> 0
001 -> 1
010 -> 1
011 -> 1
100 -> 0
101 -> 1
110 -> 1
111 -> 0

To direct my actions with the same outcome. Consequently, is this text a UTM? No, it merely serves as input (description) for other UTMs (be it human or computer) to decipher and execute. Clicking alone is sufficient to operate any UTM.


The key deficiency in CSS is its inability to autonomously modify its state arbitrarily. If CSS had the capability to trigger clicks, then it would qualify as a UTM. The argument that your clicks act as a "crank" for CSS is inaccurate since the real "crank" for CSS is the Layout Engine that powers it, serving as ample evidence that CSS is indeed a UTM.

Answer №5

One of the key challenges presented here is that a machine coded in HTML+CSS lacks the ability to process an infinite number of steps (meaning true recursion is not possible) unless the code itself is of infinite length. The inquiry of whether this machine will achieve configuration H within n steps or fewer can always be definitively answered as long as n is a finite value.

Answer №6

Turing completeness goes beyond just "defining functions" or "having ifs/loops/etc". For instance, Haskell lacks "loop", lambda-calculus lacks "ifs", and so on...

One interesting example is showcased on this website: . The author demonstrates creating a "FizzBuzz" program in Ruby using only closures (without utilizing strings, numbers, or any other data types)...

There are instances where individuals have managed to perform arithmetical computations in Scala solely through the type system

Therefore, I believe that CSS3+HTML can be considered turing-complete (even though practical computation may not be feasible without driving oneself insane)

Answer №7

Did you know that CSS is considered Turing-complete? And the most interesting part is, it doesn't need any user interaction to execute programs.

Believe it or not, in CSS you can actually run finite Boolean circuits and even Turing machines, as long as there is a stationary mouse cursor available to activate the :hover rules. It's quite fascinating how CSS can handle such complex operations!

Check out this link for more information on CSS being Turing-complete.

Just so you know, these samples were personally developed by me. To my knowledge, this is one of the first fully functional Turing machines created using pure CSS.

Answer №8

Unlike programming languages, CSS is focused on styling elements and is not considered a true programming language. The concept of turing-completeness does not apply to CSS since it lacks the necessary logic for computation. When additional programming features are introduced to CSS, such as in the case of IE6, it transforms into something entirely different.

CSS remains a tool for visual design, offering a straightforward and linear approach to defining styles for web elements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on making a dynamic table with HTML and CSS featuring square cells

Looking to design an 8x8 table using HTML and CSS, with the first column and row functioning as headers (ideally with header row height equal to header column width), and all table body cells being square. Shown below is the current HTML and CSS code: < ...

Using HTML and CSS to Position Text Within an Image

Is there a way to position an image next to specific points on the screen so that it remains in the same place regardless of screen size? Here is what I am trying to achieve: https://i.stack.imgur.com/A5cop.png Using HTML, this is my desired setup: < ...

When it comes to designing responsive websites, the use of `@

Being new to CSS, I am open to criticism and feedback. @media (max-width: 735px) {... } @media (min-width: 735px) {... } @media (width: 320px) {... } @media (width: 360px) {... } @media (width: 375px) {... } @media (width: 414px) {... } I have tried us ...

Ensure the footer remains fixed while scrolling

For the past day, I've been tackling a challenge with my online shop project. On the specific item's page, I want to include a fixed [add to cart] button as a footer initially, which then becomes sticky when scrolling to a certain point. You can ...

Tweaking the placement of the dropdown menu in the subnavigation area

Just getting back into coding after a long hiatus - I've dabbled in the past but it's been about 6 years since I've worked with any code. Currently, I'm working on replicating a website for practice. I have created a nav bar and a drop ...

Astro Project experiencing issues with loading SRC folder and style tags

After setting up a brand new astro repository with the following commands: npm create astro@latest npm run dev I encountered an issue where the default project template failed to display correctly on my computer. Here is how the page appeared: https://i. ...

Expanding div that adjusts to content size and expands to fit entire page

Appreciate your help. I'm currently facing an issue with the "content" and "footer" sections within the "wrapper" element. Below are the styles for these elements: #content { width: 100%; min-height: 100%; position: relative; background- ...

The appearance of online and local websites varies on a single screen and browser

My current dilemma revolves around the difference in appearance of my website when viewed locally versus online. Both versions are connected to the same git repository and use the same css file. Interestingly, I can view the page on both my local machine a ...

Altering the appearance of a component that is currently selected and in use

Currently, I have incorporated a component with its selector within another component as shown below: <div class="col-xl-4" style="margin-bottom: 30px;"> <app-patient-info-accordion *ngIf="patient" [cardTitle]=&qu ...

Struggling with aligning two divs side by side on an HTML page

Recently, I've been experimenting with Electron and attempting to create 2 divs side by side. Despite trying various alignment options found here, nothing seems to be working for me. Here's the code I have so far: Code body, html { hei ...

Bootstrap 4 Collapse - Ensuring that collapsed elements remain open when expanding other accordions

Can someone help me figure out how to keep an element open when another one is opened? Here is an example: https://getbootstrap.com/docs/4.0/components/collapse/ <div id="exampleAccordion" data-children=".item"> <div class="item"> & ...

Mobile browser not resizing window width in fluid layout on WordPress site

Currently, I am working on developing a basic web layout that can adapt to mobile view. I have made some progress which you can check out here. The layout successfully adjusts when the window is resized in Firefox or Chrome. However, when I tried to acce ...

Just starting out with CSS and coding. Setting up radio buttons and divs for a simple beginner's gallery

One challenge that perplexes me is how to reposition the concealed divs below the radio buttons with labels. Check out my HTML here View my CSS code here /*color palette: abls [lightest to darkest] #eeeeee #eaffc4 #b6c399 ...

What is the best way to align inline-block elements in a straight row?

I am encountering an issue with the code block below: <div class="1"> Foo<br>1 </div> <div class="2"> Bar<br>2 </div> Even though both .1 and .2 have styles of position:relative;display:inline-block, they are displ ...

Tips for defining conditions within the style attribute in JSP

Below is the code that I attempted: <div style="width:85%;"> <input class="formbutt" value="AddNew" title="AddNew" type="button" style="{(${projectEnvironmentBean.divStyle}=='dipslay:none') ? 'display:block' :'display: ...

Tips for replacing default arrow icons with 'Previous' and 'Next' buttons in a Material-UI pagination element

I've been struggling to find a solution with my code provided below. Despite multiple attempts, the issue remains unresolved. import React from "react"; import { gridPageCountSelector, gridPageSelector, gridPageSizeSelector, useGridA ...

Incorporate unique color variables from the tailwind.config.js file

I am working on a project using nextjs and typescript, with tailwind for styling. In my tailwind.config.js file, I have defined some colors and a keyframe object. My issue is how to access these colors to use as background values in the keyframe. Here is ...

When hovering over one item, it causes a hover effect on an item in a SEPARATE container

My goal is to create a unique interaction between links and images, where hovering over a link will transform the corresponding image below it, and vice versa. I have been searching for solutions but have only found methods that work when all items are wit ...

Is it true that event.stopPropagation does not function for pseudoelements?

I am facing an issue with event handling in my ul element. The ul has three li children elements, and the ul itself has a useCapture event handler for click. In the click event handler, I successfully stop the event using event.stopPropagation(), and every ...

Conceal the rating of WordPress products when they have no ratings

I am looking to remove the star ratings under the title for products with empty reviews. I specifically want to hide the stars without allowing users to leave a new review. I found a similar solution for hiding a different element and attempted to customiz ...