I'm wanting to implement a multiline selection section in html that looks like:
But with <span> tags and css, the closest I've been able to come up with is:
Code pen
.textblock {
width:200px;
/* I don't want justified text, but this straightens the ratty edge */
text-align: justify;
}
.highlighted {
border-radius: 10px 0 10px 0;
background-color: rgb(61, 107, 39);
border: 1px solid rgb(61, 107, 39); /* bit of a cheat*/
/* the following almost works */
/* box-shadow: 10px 10px 10px black; */
}
This isn't so great because:
Any tips on how to make this better?
Right now I'm wondering if I may have to craft the shape I want in javascript and draw it on a lower layer. Maybe I could use something like offsetLeft and friends to get the span's dimensions...?
Of course, if there's a simpler way in css, I'd love to do that instead!
you can use box-decoration-break: clone; to change how lines get styled and for the radius you can use box-shadow to hide the lines that get extra border-radius that you don't want. In the end, your code will end up like this:
.textblock {
width:200px;
text-align: justify;
line-height: calc(1rem + 6px);
}
.highlighted {
display: inline;
background-color: rgb(61, 107, 39);
box-shadow: 5px 0 0 rgb(61, 107, 39), -5px 0 0 rgb(61, 107, 39), 5px 5px 0 rgb(61, 107, 39), -5px 5px rgb(61, 107, 39), 2px 6px 6px #000000, -2px 6px 6px #000000;
box-decoration-break: clone;
padding: 4px;
border-radius: 5px;
}
<div class="textblock">
Four score and seven years ago our fathers brought forth upon this continent,<span class="highlighted">a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war</span>. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate—we can not consecrate—we can not hallow—this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth.
—Abraham Lincoln
</div>
I also set line-height to manage the space between lines and make them fit perfectly for some fonts it is different.
You can remove the text-align: justify; on the textblock class but you need to add float: left; in order to have a full-width background.
Be aware this technique will move the highlighted section to a completely new block.
.textblock {
width:200px;
line-height: calc(1rem + 6px);
}
.highlighted {
display: inline;
float: left;
background-color: rgb(61, 107, 39);
box-shadow: 5px 0 0 rgb(61, 107, 39), -5px 0 0 rgb(61, 107, 39), 5px 5px 0 rgb(61, 107, 39), -5px 5px rgb(61, 107, 39), 2px 6px 6px #000000, -2px 6px 6px #000000;
box-decoration-break: clone;
padding: 4px;
border-radius: 5px;
}
<div class="textblock">
Four score and seven years ago our fathers brought forth upon this continent,<span class="highlighted">a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war</span>. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate—we can not consecrate—we can not hallow—this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth.
—Abraham Lincoln
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With