Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html/css alignment issue because of an empty <span>. What is moving my content down?

Tags:

html

css

web

I have an alignment problem in html/css. I simplified my problem to this:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <style>
        span { display: inline-block; height: 50px; }
        span.text { background-color: Yellow;}
        span.left, span.right { background-color: Red;  width: 20px;}
    </style>
</head>
<body>
    <span class="left">x</span>
    <span class="text">Text comes here</span>
    <span class="right">x</span>
</body>
</html>

The output in the browser is as expected:

enter image description here

However, span.left and span.right should not contain any content. They are there for layout purposes only. But when I remove the content ("x") of both spans like this:

<span class="left"></span>
<span class="text">Text comes here</span>
<span class="right"></span>

The output changes to this:

enter image description here

Why is it doing this? What can I do to solve this?

like image 674
NetWave Avatar asked Feb 03 '12 08:02

NetWave


People also ask

How do I fix the alignment problem in CSS?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

How do I keep content inside a div?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

Why is text-align center not working?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

How do I center align content in CSS?

To just center the text inside an element, use text-align: center; This text is centered.


1 Answers

Vertical align is problematic, since it is set to the default baseline. Just change it to top:

span { display: inline-block; height: 50px;vertical-align:top; }
like image 186
easwee Avatar answered Sep 30 '22 18:09

easwee