Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline elements, and markup white space rendering issues

Way back in the day, we had to deal with browsers adding white space between elements if in the source markup we also had white space.

I thought that issue had all but disappeared but I rant into a situation today where the problem still exists. What's odd is that the problem isn't confined to a particular browser. It shows up the same problematic way in Firefox, Safari, Chrome and Opera and only slightly differently in IE.

Sample CSS:

<style type="text/css">
li {
    display: inline;
    background: pink;
    margin: 0px;
    padding: 10px 0px;
}

li a {
    background: green;
    margin: 0px;
    padding: 0px;
}

</style>    

Sample markup:

<ul>
<li>
    <a href="#">hello</a>
</li>
<li>
    <a href="#">world</a>
</li>
</ul>

<ul>
<li><a href="#">hello</a></li>
<li><a href="#">world</a></li>
</ul>

<ul>
<li><a href="#">hello</a></li><li><a href="#">world</a></li>
</ul>

Only that last UL appears the way I'd like it to appear...with the A tags spanning the full width of the container LI tag.

Given the consistency across browsers, this maybe is actually rendering as it should? Short of reverting to old comment hacks (starting a comment on the end of one line and expanding to the beginning of the next) anyone know of a workaround for this or why this is doing what it does?

Ideally, I'd float my LI's instead, but due to some other issues keeping then inline would be preferred.

like image 382
DA. Avatar asked Dec 05 '25 10:12

DA.


2 Answers

Yup, whitespace is whitespace when it comes to inline elements. That's almost always exactly what you want. Take, for example, the following:

<p>I <em>really</em> <strong>really</strong> want that whitespace.</p>

Would suck pretty hard if that ended up rendered as:

I *really***really** want that whitespace.

If all the browsers render it a certain way, it's pretty likely that they're right and you're wrong. That goes double if all browsers except for IE render it a certain way...

like image 116
Bobby Jack Avatar answered Dec 07 '25 10:12

Bobby Jack


I believe this is by design.
You have included white space (even if it is carriage returns) in your source so the browser is rendering this faithfully. I doubt there is a way around this apart from ensuring there is no white space between your elements.

like image 21
AUSteve Avatar answered Dec 07 '25 12:12

AUSteve



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!