Place two <pre> elements of different size next to each other

67 Views Asked by At

I have two <pre> elements, and I would like them to be on the same row with each other, so that I will be able to add more of those in the future as rows. I am not a professional in HTML, so I have found a solution which uses display: table-row and display: table-cell. It works almost perfectly, but the smaller <pre> increases in size and adds a lot of empty space to itself like this: Here is what happens I would like the element on the left to be just enough height to show the text, and the right element to have a fixed height.

Here is the code which I used for this screenshot:

CSS

<style>
pre {
    background: #f4f4f4;
    border: 1px solid #ddd;
    border-left: 3px solid #00eeff;
    color: #666;
    page-break-inside: avoid;
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 1.6em;
    overflow: auto;
    padding: 1em 1.5em;
    display: inline;
    word-wrap: break-word;
    border-radius: 1em;
    width: 67vw;
    height: 10px;
}

#two {
    background: #f4f4f4;
    border: 1px solid #ddd;
    border-left: 3px solid #000efa;
    color: #666;
    page-break-inside: avoid;
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 1.6em;
    margin-left: 3vw;
    max-height: 30vh;
    overflow: auto;
    padding: 1em 1.5em;
    display: block;
    word-wrap: break-word;
    border-radius: 1em;
    width: 30vw;
    height: 100px;
}

#row {
    display: table-row;
}

pre, sam
{
    display:table-cell;
}

#table {
  table-layout: fixed;
}

</style>

HTML

<div id='table'>
<div id='row'>
<pre>
this is a small text
</pre>
<pre id='two'>
this text takes a lot of
space 
so it needs to be scrolled
ok, some more text
and even more just to make sure
</pre>
</div>
</div>
2

There are 2 best solutions below

0
G-Cyrillus On BEST ANSWER

display:flex seems more appropriate here ;)

.row {display:flex;}
pre {
/*flex:1; optional */
  background: #f4f4f4;
  border: 1px solid #ddd;
  border-left: 3px solid #00eeff;
  color: #666;
  page-break-inside: avoid;
  font-family: monospace;
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 1.6em;
  overflow: auto;
  padding: 1em 1.5em;
  display: inline;
  word-wrap: break-word;
  border-radius: 1em;
  width: 67vw;
  height: 10px;
}

#two {
  background: #f4f4f4;
  border: 1px solid #ddd;
  border-left: 3px solid #000efa;
  color: #666;
  page-break-inside: avoid;
  font-family: monospace;
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 1.6em;
  margin-left: 3vw;
  max-height: 30vh;
  overflow: auto;
  padding: 1em 1.5em;
  display: block;
  word-wrap: break-word;
  border-radius: 1em;
  width: 30vw;
  height: 100px;
}
<div class='row'>
  <pre>
this is a small text
</pre>
  <pre id='two'>
this text takes a lot of
space 
so it needs to be scrolled
ok, some more text
and even more just to make sure
</pre>
</div>

0
Simp4Code On

Here is a CSS Grid alternative. Also cleaned up a lot of the CSS, you had repeated a lot of styles

* { box-sizing: border-box }

.row {
  display: grid;
  gap: 3vw;
  grid-template-columns: repeat(3, 1fr); /* Create 3. even columns */
}

pre {
  background: #f4f4f4;
  border: 1px solid #ddd;
  border-left-width: 3px;
  border-radius: 1em;
  color: #666;
  font-family: monospace;
  font-size: 15px;
  flex: 1 1 auto;
  height: fit-content;
  line-height: 1.6;
  margin-bottom: 1.6em;
  overflow: auto;
  padding: 1em 1.5em;
  page-break-inside: avoid;
  word-wrap: break-word;
}

pre:first-child {
  border-left-color: #00eeff;
  grid-column: 1 / span 2; /* start at colum 1 and span 2 columns */
  max-height: calc( 15px * 1.6 + 2em ); /* maximum of 1 line */
}

pre:last-child {
  border-left-color: #000efa;
  grid-column: 3 / span 1; /* start at column 3 and span 1 column */
  max-height: calc( ( 15px * 1.6 ) * 3 + 2em ); /* maximum of 3 lines */
}
<div class='row'>
  <pre>this is a small text</pre>
  <pre>this text takes a lot of
space 
so it needs to be scrolled
ok, some more text
and even more just to make sure</pre>
</div>