Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display block without 100% width

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; } 
like image 409
Staffan Estberg Avatar asked Oct 03 '12 12:10

Staffan Estberg


People also ask

How do inline-block elements add up to 100% width?

If you have a border or padding set for your divs, then you've created additional pixels that will prevent your divs from adding up to a 100% width. To fix this, make sure you've added box-sizing: border-box to the div's styles.

How do I increase the width of an inline-block?

Inline elements are built for text and thus they should flow inline with the text, and only be as wide as the text they contain. Thus, you can't set the width of an inline element. Block elements are made to fill all the available width by default and are thus on their own line rather than flowing inline.

What is the difference between display block and inline-block?

Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not. Compared to display: block , the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements.

What is display block display flex?

Block: Displays an element as a block element. It starts on a new line and takes up take up as much horizontal space as they can. Block-level elements do not appear in the same line, but breaks the existing line and appear in the next line. Flex: Flex displays an element as a flexible structure.


2 Answers

Use display: table.

This answer must be at least 30 characters; I entered 20, so here's a few more.

like image 167
ha404 Avatar answered Sep 21 '22 16:09

ha404


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;  } 
like image 33
PJ McCormick Avatar answered Sep 20 '22 16:09

PJ McCormick