Display long dynamic text into adjacent columns like newspaper layout

Reading Time: 3 minutes

Folks, If we have quite a long text, and we want to show it into three adjacent columns, One way to implement it 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 following scenario i.e

if the complete text to be displayed in P tag is rendered dynamically through a particular selectorin such 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, In case you 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 column, 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.

KNOLDUS-advt-sticker

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading