Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XHTML/CSS Padding on inline element with linebreak

Tags:

css

xhtml

inline

I have an inline element with a line break in it. It has padding on all sides. However, the side padding on where the line break cuts the element is not there.

This is what i mean:

http://jsfiddle.net/4Gs2E/

There should be 20px padding on the right of tag and left of with but there isnt. The only other way I can see this working is if i create a new element for every line but this content will be dynamically generated and will not be in a fixed width container so i dont see that working out. Is there any other way I can do this in css without any javascript?

I want the final result to look like this : http://jsfiddle.net/GNsw3/

but without any extra elements

i also need this to work with display inline only as I want the background to wrap around the text as inline block doesnt do this

Is this possible?

edit, altered the examples to make what i want more visible:

current http://jsfiddle.net/4Gs2E/2/

what i want it to look like http://jsfiddle.net/GNsw3/1/

like image 382
Ozzy Avatar asked Oct 28 '11 19:10

Ozzy


People also ask

Can you use padding on inline elements?

When it comes to margins and padding, browsers treat inline elements differently. You can add space to the left and right on an inline element, but you cannot add height to the top or bottom padding or margin of an inline element. Inline elements can actually appear within block elements, as shown below.

Does padding-inline element ignore?

Padding does not have this feature. Padding ignores inline elements: When your web design includes inline elements, which do not flow to a new line and only take up as much width as necessary, you can use margin and padding to add space to either side of the inline element but not above or below it.

How do you write inline padding in HTML?

Syntax. The padding-inline property may be specified with one or two values. If one value is given, it is used as the value for both padding-inline-start and padding-inline-end . If two values are given, the first is used for padding-inline-start and the second for padding-inline-end .

Does inline block start new line?

An inline element does not start on a new line. An inline element only takes up as much width as necessary. This is a <span> element inside a paragraph.


1 Answers

In some cases you can use box-shadow for a workaround.

Move the right and left padding of the element to its parent and add two box-shadows.

The result: http://jsfiddle.net/FpLCt/1/

Browser support for box-shadow: http://caniuse.com/css-boxshadow

Update:

There is also a new css property for this issue called box-decoration-break. It is currently only supported by opera, but hopefully more browsers will implement this soon.

Hope this helps

like image 76
ausi Avatar answered Sep 17 '22 14:09

ausi