By simply adding border: 1px solid black
(for example) to your existing code, everything functions perfectly. The h1
element adjusts its size based on the content, including the border:
const result = document.getElementById('result');
const sentence = "HELLO! IT LOOKS LIKE THIS IS WORKING FINE...";
let index = 0;
setInterval(() => {
index = (index % sentence.length) + 1;
result.innerHTML = sentence.slice(0, index);
}, 250);
#result {
position:absolute;
top: 10%;
left: 10%;
padding: 0 .5rem;
font-family: Sans-Serif;
font-size: 2rem;
line-height: 3rem;
color: black;
border: 3px solid black;
border-radius: 3px;
min-height: 3rem;
}
<h1 id="result"></h1>
It appears that you might be concerned about the border impacting the dimensions of your element:
#bar1 {
width: 50%;
height: 1rem;
background: red;
margin: .25rem;
}
#bar2 {
width: 50%;
height: 1rem;
background: cyan;
margin: .25rem;
border: 3px solid black;
}
<div id="bar1"></div>
<div id="bar2"></div>
By default, the width and height of your element account for the specified width and height properties, as well as padding and border, as illustrated in the example above.
To maintain the exact dimensions specified with width and height, you have two options:
Utilize box-sizing: border-box
. This includes padding and border in the total width and height of the element.
Opt for box-shadow
instead of border
. The inset
property allows you to place the shadow inside the element rather than outside.
#bar1 {
width: 50%;
height: 1rem;
background: red;
margin: .25rem;
}
#bar2 {
width: 50%;
height: 1rem;
background: cyan;
margin: .25rem;
border: 3px solid black;
box-sizing: border-box;
}
#bar3 {
width: 50%;
height: 1rem;
background: yellow;
margin: .25rem;
box-shadow: inset 0 0 0 3px black;
}
#bar4 {
width: 50%;
height: 1rem;
background: lime;
margin: .25rem;
box-shadow: 0 0 0 3px black;
}
<div id="bar1"></div>
<div id="bar2"></div>
<div id="bar3"></div>
<div id="bar4"></div>
It's worth noting that the 4th bar, with the outer box-shadow
, may appear larger, but upon inspection, its dimensions are identical to the other 3 bars.