8

Here's what happening:

For my computer science class we're working on a project where we can only modify the CSS. I think it'd be super cool to change the content of the elements when you plug in my CSS file. Since I can't add a tag, I dont know how else can I do this. Here's what I have:

<div>

  <h1>Is the makerspace door open?</h1>

</div>

<div>


  <p id="switch_txt">Checking...</p>

</div>


</body>


I've tried using ::before and ::after pseudo selectors but these selectors can't place elements in the content property. Any advice would be much appreciated.

Will Pearson
  • 95
  • 1
  • 1
  • 4

4 Answers4

10

The pseudo element is a good approach to visually replace text or insert an image.

text-indent can be useful added to a float context for the pseudo element to hide any dom inline or inline-block content(such as text or image).

Example:

HTML test

<h1>show me something else</h1>

CSS test

h1 {
  text-indent:-9999px;
}
h1:before {
  text-indent:0;
  content:'Here is something else !';
  float:left;
} 
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
2

It's possible to change your content, but it's actually not the common way to do it through CSS. I think the goal was just to style up your HTML with some CSS. But here is a Snippet which adds Question: before your text in <h1> tag.

.container h1:before {
  content: "Question: ";
}
<div class="container">
  <h1>Is the makerspace door open?</h1>
</div>
Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33
1

Did i get it right, that you want to replace the text in the h1?

Try this approach.

HTML:

<h1>Is the makerspace door open?</h1>

CSS:

h1:after {
  content: "test";
  left: -95px;
  position: absolute;
  top: 5px;
}
h1 {
  height: 20px;
  line-height: 25px;
  margin: 0;
  overflow: hidden;
  padding: 0;
  text-indent: 105px;
  width: 100px;
}
Der Vampyr
  • 941
  • 1
  • 7
  • 13
1

How about this

<div>
     <h1>Is the makerspace door open?</h1>

</div>
<div>
    <p id="switch_txt">Checking...</p>
</div>

CSS

* {
    margin: 0;
    padding: 0;
}
h1 {
    content:"";
    position: absolute;
    visibility: hidden;
    left: 0;
    top:0;
}
h1:after {
    float: left;
    content:"Ultra Cool";
    visibility: visible;
}
#switch_txt {
    position: relative;
    top: 50px;
}

FIDDLE

Benjamin
  • 2,612
  • 2
  • 20
  • 32