Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does :before element break layout?

Tags:

html

css

I want to display some background images along with some text. Everything should be divided by a | (pipe) to separate the elements.

The pipe is included with an :before selector with the pipe as content. However it seems that this breaks the layout as the background images are now not longer on the same line as the text. If I remove the content completely it works as expected.

What is the reason for this and how can I fix it?

I've created a small Fiddle as example.

like image 457
flash Avatar asked Apr 10 '26 04:04

flash


1 Answers

Your layout uses float: left so :before should follow the same rule. For example:

.list-piped:before {
    display: block; /* fix */
    float: left; /* fix */
    content: "|"; /* This breaks the layout */
}

https://jsfiddle.net/infous/1cbeyn84/4/

BTW, Manoj Kumar below has described the real problem. My answer is a possible solution because float: left as well as position: absolute has its own flow.

like image 125
starikovs Avatar answered Apr 12 '26 19:04

starikovs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!