Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change html content without using JavaScript?

I've a project where I only use HTML and CSS. Is it possible to change the content of an HTML element on some event with browser, like you do JavaScript '.innerHTML' but only using CSS.

For example using @media screen and(..)

Even removing current classname and appending another class on window re-size would be suitable.

like image 722
RegarBoy Avatar asked Jan 22 '16 02:01

RegarBoy


People also ask

Can I change the HTML content?

Yes, it is possible to change the content of the HTML in javascript. Usually HTML content will be in HTML tags such as <p>, <span>, etc.

How do you change text in HTML?

To change the text font in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-family, font-size, font-style, etc. HTML5 do not support the <font> tag, so the CSS style is used to change font.

Can a website run without JavaScript?

If a site is “client-side rendered” ( CSR ), that means JavaScript is doing the data fetching and creating the DOM and all that. If we're talking about websites “working” or not with or without JavaScript, a site that is client-side rendered will 100% fail without JavaScript.

How do I edit HTML page?

By right-clicking on the HTML in the “Elements” tab and selecting “Edit as HTML,” you can make live edits to the markup of a webpage that Chrome will immediately render once you're done editing.


2 Answers

It completely depends on what action you're wanting to trigger the change on, but in general, yes, this is possible.

Here's an example based on width:

HTML

<div id="something-awesome"></div>

CSS

#something-awesome {
  &:before {
    content: "This is some great text here.";
  }
}

@media (min-width: 500px) {
  #something-awesome {
    &:before {
      content: "This is some other super great text.";
    }
  }
}

Here's a fiddle to show how the example works: https://jsfiddle.net/ttLc7a4t/2/

Change the width of the output box to see it in action.

like image 96
jeffdill2 Avatar answered Sep 27 '22 21:09

jeffdill2


No. CSS can only be used to set the stylying, not the content. Mixing both would be against the separation of concerns.

CSS 2.1 is a style sheet language that allows authors and users to attach style to structured documents. By separating the presentation style of documents from the content of documents, CSS 2.1 simplifies Web authoring and site maintenance.

That said, you can use CSS to look like you changed contents. For example, you can hide an element and show another one, or change the generated content (which is not real content) through the content property.

Additionally, CSS Scoping allows you to replace the contents of an element with a shadow tree, but you need JS to create that shadow tree.

like image 30
Oriol Avatar answered Sep 27 '22 21:09

Oriol