Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add line breaks between inline-block elements using CSS?

Tags:

html

css

I have list of images. I want to put a line break after the second image using CSS. I have used a CSS trick listed below but it doesn't work for the inline-block elements. It only works for display inline elements

Code that working for inline Elements.

HTML

<div>
    <a href="#">Dummy Link</a>
    <a href="#">Dummy Link</a>
    <a href="#">Dummy Link</a>
    <a href="#">Dummy Link</a>
</div>
<div>
    <img src="http://placehold.it/50X50" />
    <img src="http://placehold.it/50X50" />
    <img src="http://placehold.it/50X50" />
    <img src="http://placehold.it/50X50" />
</div>

CSS

a:nth-of-type(2):after
{
    white-space: pre;content:'\A';
}
img:nth-of-type(2):after
{
    white-space: pre;content:'\A';
} 

If I am using img element which is display:inine-block by default. The above CSS does not working for it.

Check below fiddle for example

http://jsfiddle.net/murli2308/dD52z/1/

Please let me know if there any way of doing it

Note - I cannot change the HTML structure as it is coming through database.

like image 972
murli2308 Avatar asked Feb 26 '14 12:02

murli2308


People also ask

How do I add a line break in CSS?

A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).

How do I put a space between inline elements in CSS?

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. I've added white padding on the left and right side of each inline element.

How do I add a solid line break in HTML?

The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems.


1 Answers

You cannot use :after pseudo elements with <img> tag. This is because pseudo elements work only with double tags. For achieving the desired result you can give <br> tag after each <img> and style the <br> instead.

<div>
  <a href="#">Dummy Link</a>
  <a href="#">Dummy Link</a>
  <a href="#">Dummy Link</a>
  <a href="#">Dummy Link</a>
</div>
<div>
  <img src="http://placehold.it/50X50" />
  <br>
  <img src="http://placehold.it/50X50" />
  <br>
  <img src="http://placehold.it/50X50" />
  <br>
  <img src="http://placehold.it/50X50" />
  <br>
</div>

And in the css you can style the <br> tag like this:

a:nth-of-type(2):after
{
    white-space: pre;content:'\A';
}
br{
  display: none;
}
br:nth-of-type(2){
  display:block;
}
like image 171
Nishant Kumar Avatar answered Oct 06 '22 01:10

Nishant Kumar