Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Columns with corresponding content

Tags:

html

css

Let's say I have some structured content in HTML – for example text in paragraphs grouped into some sections. And let's say I have another instance of the same structure and I want to display both contents as two columns side by side using HTML and CSS. How to do that? The point is that I want that corresponding elements (paragraphs, sections) inside the columns are aligned so they start at the same height.

Examples of such structures may be a bilingual page, or a source code together with numbers of lines or with some side comments to individual lines.

The only idea I have is to use a table, but I'm not sure it is the best solution. I want to be able to select the content as if the column was an ordinary web page, but selecting in a table works in a way that cells in a row are selected first.

An example follows. Recall that I want the corresponding elements to start at the same height.

<!doctype html>
<html>
    <head>
        <title>Corresponding columns</title>
        <meta charset="utf-8">
        <style type="text/css">
            .main {
                margin: auto;
                width: 500px;
            }
            .column {
                float: left;
                width: 50%;
            }
            .corresponding {
                background-color: #FFFF99;
            }
        </style>
    </head>
    <body>
        <div class="main">
            <div class="column">
                <h1>Section</h1>
                <p>Some text</p>
                <h2 class="corresponding">Subsection</h2>
                <p>Some other text</p>
            </div>
            <div class="column">
                <h1>Section</h1>
                <p>The text translated to another language, which may be longer.</p>
                <h2 class="corresponding">Subsection</h2>
                <p>Some other text</p>
            </div>
        </div>
    </body>
</html>
like image 786
user87690 Avatar asked Sep 20 '15 14:09

user87690


People also ask

How do I distribute Data in a column in Excel?

Select the cell, range, or entire column that contains the text values that you want to split. On the Data tab, in the Data Tools group, click Text to Columns. Follow the instructions in the Convert Text to Columns Wizard to specify how you want to divide the text into separate columns.

How do you check find if value exists in another column?

You can use the MATCH() function to check if the values in column A also exist in column B. MATCH() returns the position of a cell in a row or column. The syntax for MATCH() is =MATCH(lookup_value, lookup_array, [match_type]) . Using MATCH, you can look up a value both horizontally and vertically.


2 Answers

If you want each section/subsection to start at the same height I would suggest to do like this:

<div class="main">
    <div class="row">
        <div class="column">
            <h1>Section</h1>
            <p>Some text</p>
        </div>
        <div class="column">
            <h1>Section</h1>
            <p>The text translated to another language, which may be longer.</p>
        </div>
    </div>
    <div class="row">
        <div class="column">
            <h2 class="corresponding">Subsection</h2>
            <p>Some other text</p>
        </div>
        <div class="column">
            <h2 class="corresponding">Subsection</h2>
            <p>This text might also be longer so you need to push the next section's as well to start at the same height</p>
        </div>
    </div>
</div>

Same (almost) as a table but with div's.

I'm no "flex-box" expert so that might be a way, though with less broad browser support.

If you can't/don't want to use the "row" elements (and no/can't/don't want flex option) you will need a javascript snippet that iterate through your elements and compute margins to be set.

UPDATE

Check these 2 links, they will help you set this up as you like using flex:
- https://chriswrightdesign.com/experiments/using-flexbox-today/#card-layout
- http://codepen.io/imohkay/pen/PwPwWd/

A future proof way without using javascript.

UPDATE 2

And this one has some really cool grid solutions:
- https://philipwalton.github.io/solved-by-flexbox/demos/grids/

like image 130
Asons Avatar answered Oct 19 '22 00:10

Asons


Well, I dont know what exactly You want... I thing that You might want two sections side-by-side, where You can place anything... Thats what I found:

make two div's <div id="first"> and <div id="second">

and place what You want in them. Now css:

#first {float:left;width:50%;}

#second {float:right;width:50%;}

Make sure You have body {padding:0; margin:0;}

like image 2
David Stančík Avatar answered Oct 18 '22 22:10

David Stančík