I’m trying to create a text box in the style of a source code editor, but I’m using the <p>
tag for the number side bar thing, and the <textarea>
tag for the source code, so they do
I’m pretty sure the problem occurs for all cases, but here’s the code:
<link href="https://www.fonts.googleapis.com/css?family=Source%20Code%20Pro" rel="stylesheet">
<p style=" font-family: 'Source Code Pro', monospace; color: #a9a9a9; background-color: #4f4f4f; width: 12px; height: 360px; text-align: center; word-wrap: break-word; float: left;">1</p>
<textarea style=" font-family: 'Source Code Pro', monospace; resize: none; width: 472px; height: 360px; color: white; resize: none; background-color: black; border-radius: 0; float: left;"></textarea>
3
Answers
You’re using a
<p>
but the P element comes with lots of built-in style properties to make text look "good" (that even will vary between browsers). This default styling is what is causing the improper alignment.It’s much better to use a
<div>
instead, which has no default styling.If we do that, and also set
padding
andborder
to 0 for the textarea, then it will align:I believe you need to dive into flex css, this is go-to approach to align children objects in parent object
CSS Flexbox
In your code you need to apply
margin: 0; padding: 0;
top
tag.The extra margin on
p
tag is the default css.To remove default css styles from all element you need to add
which I have added in following example.
Apart from this you are encouraged to go through different layout techniques like flexbox and grid.