0

i'm looking for a solution to replace a line break <br> with a character, let's say a comma.

i'm having a website that displays credits to images. in landscape mode credits are displayed like this:

title
artist
year

in portrait mode it should be displayed like this:

title, artist, year

so far i found this solution, works in safari, safari for ios and chrome (haven't tested chrome on android but i guess it should work too):

first set an empty content to remove the line break

br {content: '';}

then set the string you want the line break to be replaced with

br:after {content: ', ';)

if you need the line break to work normally again put

br {content:none;}

works great actually but is it the right way to do this? how would you replace a line break with a character in css? i'm looking for a solution in which the end user of the cms doesn't have to add too much html code when entering the credits.

6
  • This shouldn't work as content should only work with the :before and :after pseudo-classes Commented Jul 1, 2013 at 18:20
  • 1
    Hi and welcome on SO. I don't see any question here so I guess you want to answer one of your own question? This is perfectly accepted but you must pretend to ask a question (How would somebody...?) and then answer it below. Commented Jul 1, 2013 at 18:21
  • @IanClark First stackoverflow.com/a/11175158/137626 and it at least has some effect somewhere (Chrome?) on replaced elements (input? img?) that triggers a "oh now I've content and I'll behave differently"... Sorry for being so vague ;) Commented Jul 1, 2013 at 18:24
  • But a br is an empty/void element, so it can't contain any content. Commented Jul 1, 2013 at 18:25
  • @IanClark Found the link CSS generated content on replaced elements while kalley had already posted it :) Google is my extension memory... Commented Jul 1, 2013 at 18:30

2 Answers 2

2

Self-closing tags (eg. <br>, <img>, <input>, etc) can't have generated content. These are replaced elements.

For more information on replaced elements, see this article:http://www.red-team-design.com/css-generated-content-replaced-elements

Sign up to request clarification or add additional context in comments.

1 Comment

Good link, and I think it confirms that this behaviour should be treated more as a bug than a solution to this issue.
1

/* cross browser inline-block */
p br {
  display: -moz-inline-stack;
  display: inline-block;
  vertical-align: top;
  zoom: 1;
  *display: inline;
}

/* replace <br> with comma */

p br {
  content: '';
  width: 9px;
  height: 18px;
}

p br:before {
  content: ', '
}
<p>some<br />text with &lt;br&gt;</p>

works in mobile Chrome 40.0.2214.89, Chrome 40.0.2214.95, Opera 12.16, Safari 7.0.4

not work in Firefox 35.0.1 and Internet Explorer 11

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.