We are going to create a container for the code block that has two columns: one for line numbers and one for the code.
We'll use a flexbox or grid layout to keep them aligned.
However, note that when code wraps, the line number should only appear once per line of code, and the wrapped lines should not have a line number.
We can structure the HTML so that each line of code is in its own element (like a or with line breaks) and then have a separate element for line numbers.
But a common issue is that if we use two separate columns (like two s) and the code wraps, the line numbers might not align because the code column might have more lines (due to wrapping) than the line number column.
Alternatively, we can use a single container for each line of code that includes both the line number and the code for that line. Then we can use a grid or flex to put the line number and code side by side.
However, note that the requirement is for line numbers to stay aligned even when the code wraps. This means that if a line of code wraps, it should still be considered as one line and have one line number, and the wrapped part should be indented to align with the code (but without a line number).
Let's consider two approaches:
Approach 1: Two separate columns (line numbers and code) but using a monospace font and ensuring that the line numbers and code lines are in separate elements that are the same height. But if the code wraps, the code element will have multiple lines and the line number element will have only one line, so they won't align.
Approach 2: Each line of code (including its line number) is in a single container. Then we use a grid with two columns: one for the line number and one for the code. The code part can wrap within its column, and the line number will be on the left and the code will be on the right. The height of the container will be determined by the code (if it wraps, the container becomes taller). But then the next line of code will be in the next container, so the line numbers will be aligned because each container has its own line number and code.
However, note that the requirement says "line numbers on the left", and we want the line numbers to be aligned with the code lines. In this approach, each code line (even if it wraps) is in one container, so the line number for that container will be on the left and the entire code for that line (including wrapped parts) will be in the same container. Then the next line number will be for the next container.
This seems to work. But note: the code block might be inside a or we might be using a code editor style. We want the code to wrap on small screens.
Let's design the HTML structure:
We can have a container for the entire code block, then for each line of code we have a div with two children: one for the line number and one for the code.
Example:
Then we style:
.code-block {
display: flex;
flex-direction: column;
font-family: monospace;
}
.code-line {
display: grid;
grid-template-columns: auto 1fr; /* line number takes its width, code takes the rest /
align-items: start; / so that if the code wraps, the line number stays at the top */
}
.line-number {
text-align: right;
padding-right: 10px;
user-select: none; /* so that when selecting code, the line numbers are not included */
color: #888;
}
.code-content {
white-space: pre-wrap; /* This allows wrapping and preserves whitespace /
word-break: break-all; / or break-word, depending on your needs */
}
However, note that using white-space: pre-wrap will preserve the original whitespace (like tabs) but also allow wrapping. But if the code has indentation, we might want to preserve that. Alternatively, we can use white-space: pre and then set word-break: break-all to break long words (like a long string without spaces). But note that pre does not wrap by default. So we need to allow wrapping for long lines.
Alternatively, we can use:
.code-content {
white-space: pre-wrap; /* CSS3 /
white-space: -moz-pre-wrap; / Firefox /
white-space: -pre-wrap; / Opera /
white-space: -o-pre-wrap; / Opera /
word-wrap: break-word; / IE */
}
But note that using pre-wrap will preserve the whitespace and also wrap. However, the wrapped part will be aligned with the beginning of the code content, not with the original indentation. Actually, pre-wrap preserves the whitespace at the beginning of the line, so if you have indentation (spaces or tabs) they will be preserved and the wrapped line will start at the same indentation level.
But note: if you have a long line that wraps, the second line will start at the same column as the first line (because of the preserved whitespace). That is usually what we want.
However, there is a catch: if the code has a long string without spaces, pre-wrap might not break it because it only breaks at whitespace by default. So we might need word-break: break-all to break anywhere.
But break-all will break even in the middle of words, which might be acceptable for code because we don't want horizontal scrolling.
Alternatively, we can use:
.code-content {
white-space: pre;
overflow-wrap: break-word;
word-break: break-word;
}
But note: overflow-wrap: break-word and word-break: break-word are not exactly the same. overflow-wrap (formerly word-wrap) will break at arbitrary points if needed to prevent overflow, but it's a soft break. word-break: break-all is more aggressive and breaks between any two characters.
For code, we might want to break at any character to avoid horizontal scrolling, so word-break: break-all might be better.
However, note that some code might have very long strings (like a long string of 1000 'a's) and we want to break that. So let's use:
.code-content {
white-space: pre-wrap; /* as above, for cross-browser */
word-break: break-all;
}
But wait, pre-wrap already preserves whitespace and allows wrapping at normal word break points. Then word-break: break-all will allow breaking within words. So combined, we get: preserve whitespace, wrap at normal break points (spaces, hyphens, etc.) and also break within words if necessary.
This should work for code.
However, there is another issue: if the code line has a tab character, the width of a tab is not fixed and might be rendered as multiple spaces (usually 4 or 8). But in monospace fonts, each character is the same width, so a tab is typically rendered as 4 or 8 spaces. But note that the line number column might not align with the code if the code uses tabs because the tab might be more than one character width in the code column but the line number column uses a fixed width? Actually, we are using auto for the line number column, so it will adjust to the width of the line number (which is just a number). The code column is 1fr, so it takes the rest. But if the code has a tab, it will take more space in the code column, but that's okay because the line number is in its own column and we want the code to start after the line number.
But note: the line number column width is set to auto, so it will be as wide as the widest line number in the block. That might be, say, 3 digits. Then the code column starts after that. But if the code has a tab at the beginning, the tab will be rendered in the code column and will be indented. That's okay.
However, if we want the code to be aligned with the line numbers (meaning the code of each line starts at the same horizontal position, regardless of the line number width), then we need to set a fixed width for the line number column. But note that the line numbers might be 1, 2, ... up to 100, so we need to set a width that can accommodate the maximum line number we expect. Alternatively, we can use a fixed width in ch units (the width of the '0' character in the current font). Since we are using monospace, we can set:
.code-line {
grid-template-columns: 4ch 1fr; /* for example, 4 characters wide for line numbers */
}
But then if we have more than 9999 lines, it will break. Alternatively, we can use:
.code-line {
grid-template-columns: min-content 1fr;
}
But min-content for the line number will make it as small as possible, which is the width of the largest line number (in terms of number of digits). However, note that we want the line numbers to be right-aligned, so we might need to adjust the padding.
Alternatively, we can set:
.line-number {
width: 4ch; /* or whatever width you want */
text-align: right;
}
But then if the line number is 1 digit, it will be right-aligned and have extra space on the left. That's okay.
However, note that if we set a fixed width for the line number column, then the code column will start at the same position for every line, which is what we want.
But what if the line numbers are 1, 10, 100? With fixed width of 4ch, they will all be right-aligned in a 4ch wide column, so they will be:
1
10
100
That looks good.
So let's adjust:
.code-line {
display: grid;
grid-template-columns: 4ch 1fr; /* adjust 4ch to the maximum expected line number digits */
}
.line-number {
text-align: right;
padding-right: 10px; /* space between line number and code */
}
But wait, if we have a very long line number (like 10000), 4ch might not be enough. We can use:
.code-line {
grid-template-columns: max-content 1fr;
}
And then set a min-width on the line number? Actually, max-content will make the column as wide as the widest line number (without wrapping). Then the code column will start after that. But then the code column's starting position will vary if the line numbers have different widths? No, because the grid column is set to max-content for the first column, so the first column will be as wide as the widest line number in the entire grid (because grid tracks are sized based on the grid items). So that's good.
Alternatively, we can use:
.code-line {
grid-template-columns: auto 1fr;
}
and then set a min-width on the line-number:
.line-number {
min-width: 4ch;
text-align: right;
}
But note: if the line number is 10000, it will be 5 characters, so 4ch is not enough. We can set:
.line-number {
min-width: 5ch; /* for up to 99999 */
}
But we don't know the maximum. Alternatively, we can use:
.code-line {
grid-template-columns: max-content 1fr;
}
and then the first column will be as wide as the widest line number (which is the maximum line number in the block). This is dynamic and will adjust.
However, note that if we have a code block with 10 lines, the first column will be as wide as the number 10 (2 characters). Then if we add a 100th line, the first column will become 3 characters wide and the code column will shift to the right for all lines. That might be acceptable, but it might cause re-layout.
Alternatively, we can set a fixed width that is large enough for the maximum expected lines. But if we don't know, we can use max-content and accept the re-layout when the number of lines changes.
But note: the code block might be static (we know the number of lines). So we can set the width accordingly.
However, for a general solution, let's use:
.code-line {
display: grid;
grid-template-columns: max-content 1fr;
}
.line-number {
text-align: right;
padding-right: 10px;
}
This will make the first column as wide as the widest line number (in the entire block) and the code column takes the rest.
But wait, what if the code has a very long line that wraps? The code column will be 1fr, which means it will take the available space in the container. The container's width is the width of the code block. If the code block is not wide enough, the code will wrap. The line number column will be fixed to the width of the widest line number, and the code column will adjust to the remaining space.
This should work.
However, note that the max-content for the first column might be too wide if we have a very long line number (like 1000000). But that's unlikely.
Alternatively, we can set a max-width on the line number column? But then it might truncate.
Let's stick with max-content for the line number column.
But note: the line numbers are in a separate element and we are using grid. The grid will lay out each row independently? Actually, the grid tracks (columns) are defined for the entire grid (the .code-block). Since we are using display: grid on each .code-line? No, we are using display: grid on each .code-line. That means each .code-line is its own grid. So the grid-template-columns is defined per .code-line, and the first column in each .code-line will be max-content for that line's line number. That means if one line has a 1-digit number and another has 3-digit, the first column of each row will be as wide as its own line number. So the code columns will not align vertically.
We want the code columns to start at the same horizontal position for every line. Therefore, we must have the same column widths for every row. So we should set the grid on the parent (.code-block) and then each .code-line is a grid item? No, we want each line to be a row with two columns.
Alternatively, we can set:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
Then each .code-line is not a grid, but we want each line to have two children (line number and code). But then the .code-block would only have two columns and we want multiple rows.
Actually, we can set:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
and then each .code-line would be a grid item? But then we have two columns and we want each .code-line to span both columns? That doesn't make sense.
Let me re-think:
We want each line of code to be a row with two cells: one for line number, one for code.
So we can structure:
And then set:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
But then the grid will automatically place the children in the grid cells in order. The first child goes to (1,1), second to (1,2), third to (2,1), fourth to (2,2), etc.
This will work, but then we lose the ability to have each line as a separate container (which might be useful for hover effects, etc.). Also, the HTML structure is less semantic.
Alternatively, we can keep the structure with each line as a container and use:
.code-block {
display: flex;
flex-direction: column;
}
.code-line {
display: grid;
grid-template-columns: max-content 1fr;
}
But then each .code-line is its own grid, and the max-content for the first column will be based on that line's line number. So if one line has a 1-digit number and the next has 3-digit, the first column of the first line will be 1ch wide and the first column of the second line will be 3ch wide. Then the code columns will not align.
Therefore, we need the grid to be on the parent and the children to be the line numbers and code contents as separate grid items.
Let's change the HTML to:
And then:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.line-number {
text-align: right;
padding-right: 10px;
user-select: none;
}
.code-content {
white-space: pre-wrap;
word-break: break-all;
}
But note: the .code-block will have many children, and the grid will automatically place them in rows. Each row will have two items: the line number and the code content.
However, what if we want to have a hover effect on a whole line? We can't because the line is split into two grid items.
Alternatively, we can keep the structure with each line as a container and use:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.code-line {
display: contents; /* so that the children of .code-line are treated as direct children of the grid */
}
Then the HTML:
And CSS:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.code-line {
display: contents;
}
.line-number {
grid-column: 1;
text-align: right;
padding-right: 10px;
user-select: none;
}
.code-content {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
But wait, with display: contents, the .code-line elements are not rendered as boxes, so their children are directly in the grid. Then the grid will place the children in the order they appear. The first .code-line's children are the line number and code content, so they go to (1,1) and (1,2). The next .code-line's children go to (2,1) and (2,2). This works.
However, display: contents is not supported in some old browsers (like IE). But if we don't need to support IE, it's okay.
Alternatively, we can avoid display: contents by making the .code-block have two columns and then each .code-line is a grid item that spans two columns? But then we would have to put the line number and code inside the same grid item? That doesn't help.
Another alternative: use a table. But we want to avoid tables for layout.
Given the complexity, let's consider a simpler approach:
We can have two separate divs: one for line numbers and one for the code, and we try to sync their heights and line breaks. But that's tricky because when the code wraps, the code div will have more lines than the line numbers div.
So the grid approach with display: contents seems the most semantically correct and maintains alignment.
But note: the .code-line elements are not needed if we use display: contents because they don't generate a box. So we can remove them and just have the line numbers and code contents as direct children of the grid container? But then we lose the grouping. However, the grouping is not necessary for the grid layout because the grid will place every two children in a row. But if we want to style a whole line (for example, on hover), we can't because the two children are separate.
So if we want to style a whole line (like background color on hover), we need a wrapper for each line.
Therefore, we can do:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.code-line {
display: contents; /* so the children are in the grid */
}
.code-line:hover .line-number,
.code-line:hover .code-content {
background-color: #f0f0f0;
}
But note: with display: contents, the .code-line element is not a box, so we cannot hover on it. The hover event will be on the children. So we can do:
.code-line:hover .line-number,
.code-line:hover .code-content {
...
}
But the hover is on the .code-line, which is not a box, so it won't trigger. We need to set the hover on the children? But then we have to set it on both.
Alternatively, we can use:
.code-line:hover > * {
background-color: #f0f0f0;
}
But again, the .code-line is not a box, so the hover won't trigger on it.
So we have two options:
-
Don't use display: contents and instead make each .code-line a grid with two columns, but then we have the alignment issue because the first column width is per row.
-
Use a different approach: use a single container for each line and use flexbox to put the line number and code side by side, and set a fixed width for the line number column? But then we have the same problem: the code column might have different starting positions if the line numbers have different widths.
Wait, if we set a fixed width for the line number in the flex container, then every line will have the same width for the line number, so the code will start at the same position.
Example:
.code-line {
display: flex;
}
.line-number {
width: 4ch; /* or whatever fixed width /
text-align: right;
margin-right: 10px;
flex-shrink: 0; / so it doesn't shrink */
}
.code-content {
flex-grow: 1;
white-space: pre-wrap;
word-break: break-all;
}
Then, every .code-line will have the same width for the line number (4ch) and the code will start after that. But note: if the line number is 10000, it will be 5 characters, so it will overflow the 4ch width. We can set:
.line-number {
min-width: 4ch;
width: auto;
text-align: right;
margin-right: 10px;
}
But then the width of the line number column will vary per line if the line numbers have different lengths. We want it to be the same for every line, so we need to set a fixed width that is wide enough for the maximum line number.
So we can set:
.code-block {
--line-number-width: 4ch; /* set this to the maximum expected digits */
}
.code-line {
display: flex;
}
.line-number {
width: var(--line-number-width);
text-align: right;
margin-right: 10px;
flex-shrink: 0;
}
This way, every line number will have the same width, and the code will start at the same position.
But what if we don't know the maximum line number? We can use JavaScript to calculate the maximum line number and set the width accordingly. But that's not pure CSS.
Alternatively, we can use grid on the parent and set the first column to max-content and then have each line as a grid item that spans two columns? That doesn't work because then each line would be one grid item and we want two columns per line.
Let's go back to the grid approach with display: grid on the parent and two columns, and then each line is two grid items (line number and code). But then we lose the wrapper for the line.
We can use:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.line-number, .code-content {
/* every odd child is line number, every even is code */
}
But then we cannot style a whole line easily.
However, we can use:
.code-block > :nth-child(odd) {
/* line number */
}
.code-block > :nth-child(even) {
/* code */
}
But then we cannot have a hover on the whole line.
Alternatively, we can use a pseudo-element on the code-content to cover the entire row? But that's messy.
Given the requirement for line numbers and code alignment, and the ability to wrap, I think the flex approach with a fixed width for line numbers is the simplest and works in all browsers, including when we don't know the maximum line number (we can set a large enough fixed width, like 5ch for up to 99999 lines).
But note: if we have 100000 lines, 5ch is not enough. We can set 6ch for up to 999999. But if we have more, we need more.
Alternatively, we can use a fixed width in em or rem that is large enough for the maximum line number we expect. But if we don't know, we can set a very large fixed width, say 10ch, but then if we only have 10 lines, we waste space.
So, if we want a dynamic width, we need to use JavaScript to calculate the maximum line number and set the width of the line number column accordingly.
But the question asks for HTML/CSS.
Let me propose a solution that uses CSS variables and sets the width of the line number column based on the number of digits of the maximum line number. But we cannot do that in pure CSS without knowing the maximum line number in advance.
Alternatively, we can use a monospace font and set the line number column to be at least as wide as the maximum line number. We can set:
.line-number {
width: 3ch; /* for up to 999 lines */
}
and then if we have more, we can increase it.
But if we are generating the code block dynamically, we can count the lines and set a class on the code-block:
and then:
.lines-1000 .line-number {
width: 4ch;
}
.lines-10000 .line-number {
width: 5ch;
}
But that's not elegant.
Given the constraints, I think the best pure CSS solution without knowing the maximum line number is to use display: grid on the parent with grid-template-columns: max-content 1fr; and then have each line as two separate children (line number and code). Then the first column will be as wide as the widest line number.
And for hover on the whole line, we can use:
.code-block > div:hover {
background: #f0f0f0;
}
but note that each line is two children, so we can't hover on a single element that covers both.
We can use:
.code-block > .line-number:hover,
.code-block > .code-content:hover {
background: #f0f0f0;
}
but then we have to set the background on both when hovering on either.
Alternatively, we can use a wrapper for each line and set display: contents so that the wrapper doesn't generate a box, but then we can't hover on the wrapper.
So, if we want hover on the whole line, we might have to accept that the line number column and code column are separate and we have to set the hover on both.
We can do:
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content,
.code-block > .code-content:hover,
.code-block > .code-content:hover + .line-number {
background: #f0f0f0;
}
But note: the structure is:
So when we hover on a line-number, the next sibling is the code-content for the same line. And when we hover on a code-content, the previous sibling is the line-number for the same line? Actually, the code-content is after the line-number, so the previous sibling of a code-content is the line-number for that line. But note: the code-content for line 2 is preceded by the line-number for line 2? Actually, the order is:
line-number for line1
code-content for line1
line-number for line2
code-content for line2
So for a given code-content, the previous sibling is the line-number for the same line. And for a given line-number, the next sibling is the code-content for the same line.
So we can do:
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content {
background: #f0f0f0;
}
.code-block > .code-content:hover,
.code-block > .code-content:hover + .line-number {
background: #f0f0f0;
}
But wait, the second rule for code-content:hover will also select the next sibling, which is the line-number for the next line? Because the next sibling of the code-content for line1 is the line-number for line2.
That's not what we want.
So we need to be careful.
Alternatively, we can use:
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content {
background: #f0f0f0;
}
and for code-content:
.code-block > .code-content:hover,
.code-block > .code-content:hover ~ .line-number? No, that would select all following line numbers.
We want the previous sibling, but CSS doesn't have a previous sibling selector.
So we can do:
.code-block > .code-content:hover {
background: #f0f0f0;
}
and then also set the previous sibling (the line-number for the same line) when the code-content is hovered? We can't.
Alternatively, we can set the hover on the code-content and set the background for the code-content and then use a negative margin to cover the line-number? That's hacky.
Given the complexity, if we want hover on the whole line, we might need to use the wrapper with display: contents and then set the hover on the wrapper? But with display: contents, the wrapper is not a box, so hover doesn't work.
So, without display: contents, we can have a wrapper that is a flex container and set the hover on the wrapper. Then the line number and code are inside the wrapper and will both get the background when the wrapper is hovered.
But then we have the alignment issue because the wrapper is a flex container and we set a fixed width for the line number? We can set the wrapper to display: flex and set the line number to have a fixed width (based on the maximum line number). But then we need to know the maximum line number.
Alternatively, we can set the wrapper to display: grid with two columns: max-content and 1fr. But then each wrapper will have its own grid and the max-content for the first column will be based on that wrapper's line number. So if one wrapper has a 1-digit line number and the next has 3-digit, the first column of the first wrapper will be 1ch and the second wrapper will be 3ch, so the code columns won't align.
So we are back to the same problem.
Therefore, the only way to have the code columns align and also have a hover effect on the whole line is to have a common grid for all lines (so the column widths are consistent) and then use a wrapper for each line that spans both columns? But then we have to put the line number and code inside the same grid item? That doesn't work because we want two columns.
Alternatively, we can use a single grid with two columns and then each line is two grid items, but then we cannot style the whole line as one box.
So, if we want hover on the whole line, we might have to accept that the line number and code are in the same grid row and we can set the hover on both by using:
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content {
background: #f0f0f0;
}
and for the code-content, we can set:
.code-block > .code-content:hover {
background: #f0f0f0;
}
but then when we hover on the code-content, the line-number for the same line does not get the background because it's the previous sibling and we can't select previous siblings.
So we can do:
.code-block > .code-content:hover {
background: #f0f0f0;
}
and then also set the previous sibling? We can't.
Alternatively, we can set the background on the code-content and then use a negative margin on the line-number to cover it? That's not feasible.
Given the trade-offs, I think the best approach is to use the grid on the parent and have two columns, and then accept that we cannot have a hover on the whole line (unless we hover on both the line number and the code content separately). But note that when you hover over the code content, the line number for that line will not be highlighted. That might be acceptable.
Alternatively, we can use JavaScript to add a class to the line when either the line number or the code content is hovered.
But the question is about HTML/CSS.
Let me summarize:
Option 1: Two separate columns (line numbers and code) with the same grid for all lines.
HTML:
CSS:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
}
.line-number {
text-align: right;
padding-right: 10px;
user-select: none;
}
.code-content {
white-space: pre-wrap;
word-break: break-all;
}
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content {
background: #f0f0f0;
}
.code-block > .code-content:hover {
background: #f0f0f0;
}
But note: the second rule for .code-content:hover will only highlight the code content, not the line number.
So we have to hover on the line number to get both, or hover on the code content to get only the code content.
Option 2: Use a fixed width for the line number and flex for each line.
HTML:
CSS:
.code-block {
font-family: monospace;
}
.code-line {
display: flex;
}
.line-number {
width: 4ch; /* or whatever fixed width */
text-align: right;
margin-right: 10px;
flex-shrink: 0;
user-select: none;
}
.code-content {
flex-grow: 1;
white-space: pre-wrap;
word-break: break-all;
}
.code-line:hover {
background: #f0f0f0;
}
This works for hover, but the line number width is fixed. If we have more than 9999 lines, we need to adjust.
Given the requirement for alignment and hover, and if we can set a fixed width for the line numbers (or use a class to set the width based on the number of lines), then Option 2 is better because we can hover the whole line.
But if we don't want to set a fixed width and want the line number column to be exactly as wide as the widest line number, then we have to use Option 1 and give up on the whole-line hover (or use JavaScript).
Considering the original question: "How do I structure the HTML/CSS so that the line numbers stay aligned with the code lines even if the code wraps to a new line on smaller screens?"
Both options keep the line numbers aligned with the code lines when the code wraps, because in Option 1 the grid will have two columns and the code content will wrap within its column, and the line number will be in the first column and aligned with the first line of the code content. In Option 2, each line is a flex container and the code content will wrap within its flex item, and the line number will be at the top left of that line.
So both work for alignment.
I think Option 2 is simpler and more robust for hover effects, provided we can set a fixed width for the line numbers that is large enough for the maximum line number.
But if we don't want to set a fixed width, we can use grid-template-columns: max-content 1fr on the parent and then have each line as two grid items, and then use JavaScript to add a class to the line number and code content of the same line for hover.
Given the complexity, I will provide two solutions:
Solution 1 (with fixed line number width and hover on whole line):
HTML:
CSS:
.code-block {
font-family: monospace;
}
.code-line {
display: flex;
}
.line-number {
width: 4ch; /* adjust based on maximum line number */
text-align: right;
margin-right: 10px;
flex-shrink: 0;
user-select: none;
color: #888;
}
.code-content {
flex-grow: 1;
white-space: pre-wrap;
word-break: break-all;
}
.code-line:hover {
background-color: #f0f0f0;
}
Solution 2 (dynamic line number width, no whole-line hover without JS):
HTML:
CSS:
.code-block {
display: grid;
grid-template-columns: max-content 1fr;
font-family: monospace;
}
.line-number {
text-align: right;
padding-right: 10px;
user-select: none;
color: #888;
}
.code-content {
white-space: pre-wrap;
word-break: break-all;
}
/* Hover effect for the line when hovering on the line number */
.code-block > .line-number:hover,
.code-block > .line-number:hover + .code-content {
background-color: #f0f0f0;
}
/* Hover effect for the line when hovering on the code content */
.code-block > .code-content:hover {
background-color: #f0f0f0;
}
/* Note: the second rule will not highlight the line number when hovering on the code content. */
Given the requirement for alignment and the fact that we want the line numbers to be aligned even when the code wraps, both solutions work. The second solution adjusts the line number column width to the widest line number, but we lose the whole-line hover when hovering on the code content.
Choose based on your needs.
If you need whole-line hover and can set a fixed width for the line numbers (or set a class for different widths), use Solution 1.
If you want the line number column to be exactly as wide as the widest line number and don't mind the incomplete hover, use Solution 2.
Alternatively, you can use JavaScript to add a class to the parent of the line number and code content for the same line when either is hovered, but that's beyond the scope of HTML/CSS.
Let me adjust Solution 1 to use ch units that are relative to the font, so if you change the font size, the width will adjust. But note: ch is the width of the '0' character. In a monospace font, all characters have the same width, so 1ch is the width of one character. So if you have 4 digits, that's 4ch. But note: the line number might have more than 4 digits, so set the width accordingly.
Example: if you expect up to 9999 lines, set width: 4ch; for .line-number.
But if you have 10000 lines, that's 5 digits, so set width: 5ch;.
So you might need to adjust the width based on the maximum number of lines.
Alternatively, you can set:
.line-number {
width: 5ch; /* for up to 99999 lines */
}
and if you have more, increase accordingly.
I hope this helps.
To create a code block with aligned line numbers that handle text wrapping correctly, you need a structure where line numbers and code lines share row containers. The key is to prevent line numbers from wrapping while allowing code to wrap naturally. Here's the optimal solution:
HTML Structure
<div class="code-block">
<div class="code-line">
<span class="line-number">1</span>
<pre class="code-content"><code>const example = "This is a very long line of code that will wrap on smaller screens but keep the line number aligned to the first line only";</code></pre>
</div>
<div class="code-line">
<span class="line-number">2</span>
<pre class="code-content"><code>function test() { return true; }</code></pre>
</div>
<!-- More lines... -->
</div>
CSS Solution
.code-block {
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
background: #1e1e1e;
color: #d4d4d4;
padding: 16px;
border-radius: 6px;
overflow-x: auto; /* Horizontal scroll for very long lines */
}
.code-line {
display: flex; /* Aligns line number and code horizontally */
align-items: flex-start; /* Keeps line number at top when code wraps */
min-height: 1.5em; /* Minimum height for empty lines */
}
.line-number {
flex: 0 0 auto; /* Prevents shrinking/growing */
width: 2.5em; /* Fixed width for numbers (adjust for your max digits) */
text-align: right;
padding-right: 1em;
color: #858585;
user-select: none; /* Prevents selecting line numbers */
border-right: 1px solid #333; /* Optional separator */
}
.code-content {
flex: 1 1 auto; /* Takes remaining space and wraps */
margin: 0;
padding: 0;
white-space: pre-wrap; /* Preserves spaces but allows wrapping */
word-break: break-word; /* Breaks long words/strings */
word-wrap: break-word; /* Legacy support */
overflow-wrap: break-word;
}
/* Optional: Highlight a specific line */
.code-line:hover .code-content {
background-color: rgba(255, 255, 255, 0.05);
}
Key Points
display: flex on .code-line - Creates horizontal alignment between line number and code
- Fixed width
.line-number - Ensures all line numbers occupy the same horizontal space
white-space: pre-wrap - Preserves code formatting while allowing line breaks
align-items: flex-start - Keeps line numbers aligned to the top of wrapped code
user-select: none - Makes line numbers unselectable (better UX)
Alternative: CSS Grid Approach
If you prefer CSS Grid (especially useful if you want the line number column to dynamically size to the widest number):
.code-block {
display: grid;
grid-template-columns: max-content 1fr; /* Second column takes remaining space */
gap: 0;
}
.code-line {
display: contents; /* Children become direct grid items */
}
.line-number {
grid-column: 1;
text-align: right;
padding: 0 1em 0 0;
}
.code-content {
grid-column: 2;
white-space: pre-wrap;
word-break: break-word;
}
Browser Support Note
display: flex has excellent browser support
display: contents has limited support in older browsers (IE doesn't support it)
- For maximum compatibility, stick with the flexbox solution
Important Considerations
- Monospace Font: Ensure your code uses a monospace font; otherwise, character widths will vary and break alignment.
- Tab Characters: If your code contains tabs, set
tab-size property:
.code-content {
tab-size: 4; /* or 2, depending on your preference */
}
- Empty Lines: The
min-height on .code-line ensures empty lines still show line numbers.
This structure keeps line numbers strictly left-aligned and prevents them from wrapping, while code wraps naturally within its column. The line number always corresponds to the first visual line of that code statement.