Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Display Long Dynamic Text Into Adjacent Columns Like Newspaper Layout

DZone's Guide to

Display Long Dynamic Text Into Adjacent Columns Like Newspaper Layout

This article is a tutorial on formatting text with CSS and HTML for readability, usability, and performance.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

If we have quite a long text and we want to show it into three adjacent columns, one way to implement it is via three <div>’s  or <p>’s in HTML and adding CSS properties.

For example, If we have a long text as following :

singlediv

And We want to show it into three separate adjacent columns for better readability, it can be done as follow:

HTML:

threedivs

And furthermore for adjacent view, we have to write this following CSS Code :

.main{
    display:inline-block;
}
.first{
    float: left;
    width: 400px;
    padding: 0 20px;
}

To apply vertical rule between these columns so that it can have better look&feel we have to write another CSS class as follow:

.vertical-rule{
    border-right: 1px solid #cccccc;
}

and add it to first two divs only,

Even though with these CSS and HTML code we can not manage the following scenario:

If the complete text to be displayed in P tag is rendered dynamically through a particular selector, in such a case it can be complicated to measure out how this text should be break to display in 3 divs/columns. 

Unfortunately, this is impossible to do with CSS and HTML without forcing column breaks at fixed positions, or restricting the markup allowed in the text, or using scripting.

CSS3 introduced some new property to make such layouts (if have noticed in newspaper, magazines, etc.)  have block layout mode because people have trouble reading text if lines are too long, if it takes too long for the eyes to move from the end of the one line to the beginning of the next, they lose track of which line they were on.

This limitation is solved by adding new following CSS3 properties to extend the traditional block layout mode.

These properties solve the above-discussed issue, the code becomes concise ,clear  and easy to implement instead of using complicated structure through JS. Here all those extra static divs are removed, using only single CSS class to manipulate display of content.

Multi-column Properties

  • column-width
  • column-count
  • column-gap
  • column-rule
  • column-span

For the above example context We just have write the following :

HTML :

singlediv

CSS:

.main {
    column-count: 3;
    column-gap: 40px;
    column-width: 300px;
}

h2 {
    column-span: all;
}

Its O/p will be:

multiplecolumnproperty

To apply vertical rule between these columns, we just have to add one more CSS property in main class i.e. column-rule, rather than adding an extra class for this.

Let’s explain the multiple column properties

  1. The column-width property specifies a suggested, optimal width for the columns.
  2. The column-count property specifies the number of columns an element should be divided into.
  3. The column-gap property specifies the gap between the columns. (e.g padding between columns)
  4. The column-rule property is a shorthand property for specifying color, width, and style of a vertical rule between all the columns. It displays a vertical rule between all the columns.
  5. The column-span property specifies how many columns an element should span across.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
css ,columns ,html ,web dev

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}