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

The functionality of 'ngbPopover' in Ng-bootstrap may be affected if the parent container contains a 'transform' css property

Struggling to implement Ng-bootstrap's 'ngbPopover' functionality, I encountered a frustrating issue where the popover would not display after clicking the button. After numerous hours of troubleshooting, I was relieved to discover the root ...

What could be preventing my logo from properly resizing with html/css?

* { box-sizing: border-box; padding: 0; margin: 0; } ul { list-style-type: none; } .nav-links, .logo { text-decoration: none; color: #000; } .logo { max-width: 80%; width: 20%; height: 20%; } .navbar { padding: 20px; height: 50vh; ...

Achieving overlapping div layouts with CSS styling

Displayed above are 4 different divs: red, green, and blue. I am seeking to have the blue div positioned directly below the green div, which contains the text "JACKSON". Additionally, I do not want the green div to expand beyond its content, nor do I want ...

`<a> The value does not appear upon page load`

As a newcomer to development, I am currently experimenting and creating a sample dashboard. However, I am facing an issue where the sidebar value does not display upon loading the page. I have to manually click on a list in my sidebar for it to appear. Th ...

Activate the jQuery function multiple times

I am currently working on the menu structure for my website <div class="header"> <ul> <li id="menu__lnk_one"><a href="#">Home</a></li> <li><a href="#">Our Story</a></ ...

Update the scrollspy navigation in a div with overflow by toggling the active class

Struggling to implement a navigation menu within a self-scrolling div using html/css/jquery. Looking for the menu items to toggle the active class when in view or at the top of the scrolling div. I'm essentially trying to achieve something like this ...

Update the font URL in Bootstrap 5

I am looking to update the URL that Bootstrap uses to access fonts, as I want to host them on my server instead of relying on users to fetch them from Google with each page load. However, despite my efforts, I cannot locate the specific URLs mentioned when ...

Modifying the font style within an ePub document can affect the page count displayed in a UIWebView

Currently in the development phase of my epubReader app. Utilizing CSS to customize the font style within UIWebView, however encountering a challenge with the fixed font size causing fluctuations in the number of pages when changing the font style. Seeki ...

Stable navigation bar implemented with a grid design

I'm struggling with creating Navbars in Grid Layout. https://codepen.io/Aeshtray/pen/BdqeZL For mobile view, I want the Navbar to be fixed horizontally (as currently coded), but once reaching a width of 500px, I need it to become vertically fixed on ...

Within an HTML document, select one checkbox out of two checkboxes without utilizing JQuery

Is there a way to select only one checkbox out of two? I know it can be done easily using Jquery, but is there a default option in HTML for this as well? I have exactly 2 checkboxes. Thank you. code: <input type="checkbox" value="1" name="foo" /> ...

Ways to define a variable based on a CSS class attribute?

I need help figuring out how to create a variable in my script that is defined by the width attribute of a CSS class. The snippet I have currently doesn't seem to work as expected. var dist = $('.nav').css(width()); The goal of my script i ...

Troubleshooting multiple element visibility problems with jQuery

Please take a look at the code snippet below. $(document).ready(function(){ $(".like-btn").hover(function() { var rowid = $(this).attr("data-rowid"); $(".reaction-box[data-rowid='" + rowid + "']").fadeIn(100, function() { $(".reaction ...

What could be causing my navbar to not be responsive on mobile devices?

I'm in need of assistance. When viewing my website in mobile mode, the collapse hamburger menu is hiding too far to the right. This issue is making the site look unresponsive, and I've been unable to find a solution. It seems like the problem sta ...

Move from right to left on the slide?

Is there a way to toggle a div's visibility between collapsed and expanded, transitioning smoothly from right to left? I've noticed that most examples I come across demonstrate the transition from left to right. ...

Converting dynamic content within a div into an interactive link

I am currently working with Longtail's JW Player and facing some difficulties with a basic function. Since I am not familiar with the programming language terminologies, I will describe the issue step by step: There is a JavaScript code that displays ...

Looking for a solution to organize the dynamically generated list items in an HTML page

I am currently working on a movie listing website where all the movies are displayed in sequence based on their #TITLE#. The webpage is generated automatically by the software using a template file. Here is the section of code in the template file that sho ...

Anchoring links on a webpage that provide users with a clear indication of their current position within the page

In the scenario of a single-page website with various sections (divs) and a header containing links to different anchors on the page, there is a desire to have an indicator highlight which anchor the user is currently viewing. An example of this can be s ...

Is there a way to apply toggling and styles to only the card that was clicked in Vue.js?

DisplayBooks.vue consists of a single page responsible for showcasing books fetched from a backend API. Each card on the UI features two buttons - ADD TO BAG and ADDED TO BAG. When a user clicks on the ADD TO BAG button of a specific card, it should toggle ...

SVG Mask not functioning properly with SVGs created in Illustrator (in all web browsers)

Alright, let's get to it - I've got a couple of SVGs here. The first one is a blue circle and the second one is a map of the United States. (Both of these were created in Illustrator) Here's what I want to do: I want the map to serve as a ...

Adjust the dimensions of the dropdown menus

I've set up 2 dropdown menus, but I'm having trouble adjusting their size. I need them to fill the screen width. Here's my code: <select id="repeatSelect" ng-model="selectedArea"> <option ng-repeat="(key,prop) in result" value=" ...