118

I want to set a span element to appear below another element using the display property. I tried applying inline-block but without success, and figured I could use block if I somehow managed to avoid giving the element a width of 100% (I don't want the element to "stretch out"). Can this be done, or if not, what's good praxis for solving this kind of issue?

Example: a news list where I want to set a "read more" link at the end of each post (note: <a> instead of <span>)

<li>
<span class="date">11/15/2012</span>
<span class="title">Lorem ipsum dolor</span>
<a class="read-more">Read more</a> 
</li>


Update: Solved. In CSS, apply

li {
    clear: both;
}
li a {
    display: block;
    float: left;
    clear: both;
}
Staffan Estberg
  • 6,795
  • 16
  • 71
  • 107
  • 1
    Have you got any markup (HTML or CSS) to show us? It's difficult to work on the problem without anything to actually work on. Also a demo on [jsfiddle](http://jsfiddle.net) would be good. – Tom Oakley Oct 03 '12 at 12:28
  • Thought I didn't need any sample code since it's just about positioning a span element. See updated post. – Staffan Estberg Oct 03 '12 at 12:29
  • You can seriously cut down on all that markup by just using just one line of code as I mentioned below. – ha404 Apr 17 '15 at 22:57

9 Answers9

179

Use

display: table

in your CSS code.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
ha404
  • 1,989
  • 2
  • 13
  • 10
79

If I'm understanding your question properly, the following CSS will float your a below the spans and keep it from having a 100% width:

a {
    display: block; 
    float: left; 
    clear: left; 
}
PJ McCormick
  • 1,903
  • 14
  • 12
  • 1
    I tried applying this but since the span elements (title and date, in the example) doesn't have float the link gets positioned before the last span. Guess one solution would be to make all child elements in the li float. – Staffan Estberg Oct 03 '12 at 17:01
  • This works if I apply a clearfix for the li parent. Will go with this solution, thanks PJ. – Staffan Estberg Oct 03 '12 at 18:44
62

you can use:

width: max-content;

Note: support is limited, check here for a full breakdown of supporting browsers

Mustafa J
  • 955
  • 8
  • 9
  • 1
    I'd never heard of this! Interesting. – Ryan Mar 30 '18 at 19:13
  • Was good since Chrome 46 and FF 66. Should be accepted answer. `inline-block` et al was breaking my layout. – i336_ Sep 28 '19 at 02:32
  • 1
    @i336_ As of today it's still not supported by the Edge browser. – ChrisW Feb 05 '20 at 13:39
  • Edge, or Chromium Edge? (To be clear; the former is going to be transitioning out for quite a while...) – i336_ Feb 07 '20 at 10:49
  • According to caniuse.com it is supported in Edge as of Edge 79 released Jan 14th, 2020. In total has a 92% support rating. https://caniuse.com/?search=max-content This is a nice welcome to me. When I first heard about it almost 2 years ago it did not have wide enough support for me to use in production but now it does. – Xandor Sep 11 '20 at 21:53
  • I have never heard of this rule and it is amazing – labago Nov 18 '20 at 16:00
  • Thanks, (not only semantically) much better than obsolescent solutions like table-mimicing or floats. Or in the words of the populace: *“This should be the accepted answer”*. – dakab Mar 25 '22 at 11:00
9

I would keep each row to its own div, so...

<div class="row">
    <div class="cell">Content</div>
</div>
<div class="row">
    <div class="cell">Content</div>
</div>

And then for the CSS:

.cell{display:inline-block}

It's hard to give you a solution without seeing your original code.

George
  • 36,413
  • 9
  • 66
  • 103
6

Again: an answer that might be a little bit too late (but for those who find this page for the answer anyways).

Instead of display:block; use display:inline-block;

Renske
  • 226
  • 3
  • 3
2

Try this:

li a {
    width: 0px;
    white-space:nowrap;
}
Roberto
  • 31
  • 1
2

I had this issue, I solved it like so:

.parent {
  white-space: nowrap;
  display: table;
}

.child {
  display: block;
}

the "white-space: nowrap" makes sure that the children of the child(if it has any) don't wrap to new line if there is not enough space.

without "white-space: nowrap" :

enter image description here

with "white-space: nowrap" :

enter image description here


edit: it seems that it also just works without the child block part for me, so just this seems to work fine.

.parent {
  white-space: nowrap;
  display: table;
}
Noob
  • 710
  • 11
  • 15
1

You can use the following:

display: inline-block;

Works well on links and other elements.

Bruno Leveque
  • 2,647
  • 2
  • 23
  • 33
0

i use this:

vertical-align: top; //do the trick

a {
display: inline-block;
vertical-align: top;
padding: 10px 30px;
border-radius: 5px;
background-color: #372a20;
border: 1px solid var(--blanco); 
color: var(--blanco);
margin: 0 auto -25px;
text-decoration: none;

}