How to highlight long sentences with padding in a paragraph? - html

I would like to highlight sentences in some text. The normal way is to wrap the sentence in a span element and set a background color:
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. <span class="highlight">Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
And:
.highlight{
background-color: yellow;
}
If I now want to use some padding and other styles with this highlight, I have a problem:
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px
}
With small highlights, this problem is adequately solved by display: inline-block;. But here I now have the problem of my sentence being, well, a block, breaking the text in three paragraphs:
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px;
display: inline-block;
}
Is there a way to use inline-block without all the trouble?
Working JS fiddle

Use line-height property. Do little bit math, if your font-size is 16px and you are adding 5px padding then line-height will be 26px
p{
width: 300px;
}
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px;
line-height: 26px;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <span class="highlight">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

It sounds to me like you're not entirely clear what you want. Observer has given you a solution that might work for you and might not, since the line height persists throughout your entire paragraph, making all of the lines wider. If that is indeed what you want, then you have your answer.
From a UX perspective, though, it seems to me that you need to take a couple of steps back. Do you really want to set highlighted text apart in such a way as it interrupts the flow of reading? That's what you will do, if you add extra padding to it. The idea that you don't like it to be set apart as a paragraph suggests that you don't want padding to be added to it, because after all that's how a paragraph is set apart.
If you want to keep the sentence in with the rest of the paragraph and pad it as well, you can probably go to the trouble of figuring out exactly how to do it, but you'll notice that a line that has a combination of both highlighted and normal text will have the entire line padded. So you'll have some normal text that will be set apart farther than other normal text, as in Observer's sample. That draws the reader's attention to the text in such a way as to be distracting.
If you wanted it to stick out more than it would by just highlighting it, you could put a border around it, as in my example. But I think that looks a bit off, too, because the border gets broken on line breaks. You might consider putting a border on just the top and bottom as well.
In the end, though, I would stick to what everyone else does, and just change the color. That's what readers are used to, and if you change it, they'll have to take a little time to figure out what you're up to.
.highlight{
background-color: yellow;
border: 1px solid green;
padding: 0 5px;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <span class="highlight">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

Related

Optionally wrapping text

I got some slogans that can either be displayed on a single line or multiple lines if wrapped at specific places. Is there a CSS option to respect a <br> tag only if necessary to fit the container width and leave it unwrapped otherwise?
For example
<div>
They told me that<br> aesthetics matter.
</div>
You can use CSS code to break work
#content::after {
content: "\a";
white-space: pre;
}
<div id="content">They told me that</div>
<div id="break">aesthetics matter.</div>
and also you can define padding instead of height because padding expands the container according to its children and also prevent children to overflow the example is the following
#container {
width: 300px;
padding: 5px;
background-color: grey;
}
<div id="container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
You can CSS property word-wrap: break-word; in div to solve your issue. And if it doesn't solve your issue, let me know in comments I will try my best to help you.

Css Add 3 dots on the end

I have problem with adding 3 dots on the end of div. I my text is too long and don;'t have enough space to display, on the of div I want to display 3 dots.
I know for text-overflow ellipsis but now workig correct on IE and Firefox.
Is it possible to do on another way.
https://jsfiddle.net/kq1Lp6og/
I want to create ease in CSS, but don't know how without ellipsis.
HTML
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
CSS
div{
height:38px !important;
overflow: hidden;
}
div {
max-width:100%;
height:38px;
overflow: hidden;
position: relative;
line-height: 1em;
max-height: 3em;
text-align: justify;
padding-right: 1em;
font-size: 20px;
}
div:before {
content: '...';
position: absolute;
right: 2px;
bottom: 0px;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
I know you asked for CSS, but you may (or future readers may) not want CSS: it doesn't update when window size changes and it's not a very good nor flexible nor easy method to have "...", I had the same problem that I tried to fix with CSS and I finally use a JS library called dotdotdot.
PS : please do not dislike because it doesn't match all the criteria, I had exactly the same problem and I understood that CSS isn't necessarily the best option (for user experience and developer) so it's important that you know all the option you have.

Floating text around a non-rectangular object

I need to float some text around a spangle on my page as you can see on the attached screenshot. I'd like to not to use JavaScript. Is that possible to do in pure CSS?
Use CSS shapes if you're not too worried about compatability with older browsers.
.element{
shape-outside: url(image.png);
shape-image-threshold: 0.5;
float: left;
}
http://www.html5rocks.com/en/tutorials/shapes/getting-started/
Might the padding attribute be what you mean?
.container {
background: url('someimage.png') no-repeat left bottom;
border: 1px solid #111;
color: #666;
font: 9pt/14pt 'consolas';
padding: 40px 12px 12px 40px;
}
<div class="container">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
Oooh... I didn't know that you meant the text bending over the image! sorry, See evilunix' answer, as I don't want to copy someone else's solution.

How to remove space at top of <p> tag / align contained text to top of container?

This is undoubtedly a stupid question but i'm having a bad day and it's confusing me!
If you view http://jsfiddle.net/E6kGP/1/ then you can see 2 simple divs next to each other each of which contains a p tag each with different font sizes and matching line-heights.
There is a small gap between the top of the p container and the top of the contained text which is different depending on the font sizes (and line-heights). This means that the top of the text in each p is not vertically aligned. If the line-heights didn't match the font-sizes then I could understand this but surely if they are the same then the line-heights should match the tallest character and hence the highest point of the first line should be the top of the p container? Obviously this can be hacked using padding/margins or absolute positioning but i would like to understand why this doesn't work by default and what the correct way is to fix it?
As requested by SO the code from jsfiddle is also below:
div {float: left; width: 50%;}
p {margin:0 0 1em;padding:0;}
#left p {line-height:36px;font-size:36px;}
#right p {line-height:16px;font-size:16px;}
<div id="left">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<div id="right">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
Thanks very much as ever everyone!
It might be to accommodate accented characters, try putting Ä into the first <p>, the extra space helps accommodate the accent. That said, I'm not 100% convinced that is the definitive reason.
You can always specifically target the first line of a <p> element to reduce it using:
p::first-line {
line-height: 0.8em;
}
Though granted, that doesnt solve the 'why' issue.
Like ExtPro has said, it's to accommodate accented characters. A simple work around is to have margin-top:-<number>px; so that you manually align it.

Drop-caps using CSS

How can I make the first character of each paragraph look like this:
I'd prefer using CSS only.
p:first-letter {
float: left;
font-size: 5em;
line-height: 0.5em;
padding-bottom: 0.05em;
padding-top: 0.2em;
}
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
Tweak the font, padding, line-height as needed.
Example: http://jsfiddle.net/RLdw2/
add this p:first-letter{font-size:50px}
DEMO
Here is the exact solution for your requirement shown in the image
DEMO 2
WIKIPEDIA EXPLANATION
see DEMO here...
CSS///
p{ width:300px; border:1px solid #000;}
p:first-letter
{font-size:52px;color:#8A2BE2;font-weight:bold;float: left;margin-top:4px;}
HTML///
<p>The first character of this paragraph will be 52px big
as defined in the CSS rule above. Rest of the
characters in this paragraph will remain normal. This example
shows how to use :first-letter pseduo element to give effect to
the first characters of any HTML element.</p>
<p>The first character of this paragraph will be 52px big
as defined in the CSS rule above. Rest of the
characters in this paragraph will remain normal. This example
shows how to use :first-letter pseduo element to give effect to
the first characters of any HTML element.</p>