How to have background around text, but white space in between lines? - html

I'm trying to get text to have a background color, but have white space in between lines. I've managed to make it mostly work in Firefox and Explorer, but Safari and Chrome don't have space between lines. Here's how it looks in
Firefox:
But here's how it looks in Chrome:
To make it work in Firefox and IE, I had to add a span around the text inside the H2 tag, apply a background to that span, and add a second span inside that, which I positioned relative so that I could drop the text down to center it vertically inside the background color.
HTML:
<h2><span class="h2inner"><span class="h2inner2">Conflict of Interest Policy of A Better City, Inc.</span></span></h2>
CSS:
.h2inner {
padding: 4px 0 3px 0;
background-color: $h2color; }
.h2inner2 {
position: relative;
top:5px;}
Ideally, I'd also like padding at the beginning and end of each line's block of color, but I can only getting padding at the start and end of the entire head, not in the middle where it breaks onto a new line.
Any ideas?

You can adjust line-height for line spacing and for left and right padding on each line you can use white-space: pre-wrap and one inner span with left position
h2 {
width: 400px;
font-size: 30px;
font-weight: bold;
text-transform: uppercase;
color: white;
line-height: 1.3em;
}
span {
background-color: #F07317;
white-space: pre-wrap;
padding-right: 10px;
}
span span {
position: relative;
left: 10px;
}
<h2><span><span>Conflict of Interest Policy of A Better City, Inc.</span></span></h2>

Related

List of blocks and inline-blocks show incorrect vertical spacings

I have a simple list of divs, with the exception that one div is an inline-block
<div>xxxxxxxxxxxxxxxxxxxx</div>
<div>xxxxxxxxxxxxxxxxxxxx</div>
<div>xxxxxxxxxxxxxxxxxxxx</div>
<div>xxxxxxxxxxxxxxxxxxxx</div>
...
div {
background-color: #000;
color: #fff;
line-height: 20px;
font-size: 20px;
}
div:nth-child(5) {
display: inline-block;
color: #bada55;
}
DEMO
all looks just fine (font-size :20px). However, when I change the font-size to 10px things are getting weird
DEMO
Although I can fix it by adding
body { font-size: 0 }
DEMO
I still don't understand why it did work with a line-height and font-size of 20px ? Any suggestions ?
Because the inline one has to be positioned inside the line height of its container.
If you set the container's line-height to 10px (the body in your examples) it will work fine.

CSS: inline-block changes to block

I have been struggling to find answers as to why this does this so I'd figured I'd post it here to see if anyone else knows this happens and if there is an explaination. Or maybe it is just a CSS/HTML bug I am unaware of.
I created the following jsfiddle for an example.
#import url('https://fonts.googleapis.com/css?family=Playfair+Display:400,700,900');
body {
background-color: #20262e;
height: 100%;
margin: 0px;
padding: 0px;
}
.title {
margin-bottom: 0 !important;
padding-right: 100px;
color: #ffffff;
font-size: 50px;
font-family: 'Playfair Display', serif;
font-style: normal;
font-weight: 700;
text-transform: uppercase;
line-height: 1;
position: relative;
display: inline-block;
}
.title::after {
background-color: #f5a800;
width: 80px;
height: 4px;
content: '';
position: absolute;
top: 50%;
right: 0px;
}
<div class="container">
<h1 class="title">
This Is An Awesome Title
</h1>
</div>
Simple effect and works great as long as the H1 is one line. However, once you resize the screen and the H1 breaks to 2 lines, it seems that the behavior of the H1 changes? inline-block->block? If you inspect the H1 in devtools, it goes from having its "space" go from wrapping the text to full width when it breaks and the pseudo element is then pushed way out to the right of the screen instead of the right side of the text like it is when it is on one line.
I am all out of ideas on hacks around this, I've tried floats and flex to no avail.
Open to any suggestions on how to accomplish this, or maybe its a lost cause and I am stuck with changing font sizes and padding using media queries.
Thanks.
EDIT
Here are a few screenshots to further clarify what I am asking:
Text on one line: https://screencast.com/t/W83PxIck
Text when it breaks to two lines: https://screencast.com/t/Lx8xjHkrWx
Nope, that's the expected behavior. It is still inline-block. The right side of the block is the rightmost side of the text, not the right side of the final line.
If you're looking to place it immediately after the final line, you need to use display: inline, not inline-block.
You can view the boxes by using dev tools, which will clearly show you where they begin and end.

Why does the text inside an <input> tag get cut off even if there's already a padding?

Ok so I found out that the text inside an <input> tag still gets cut off even though the <input> tag already has a padding. You'll notice it more when you set your font style to anything cursive.
Take a look at this image:
The first text box in the screenshot is an input of type=text and the second text box is just a div. The input text box cuts off the tail of character 'j', while the div text box does not.
HTML:
<input type="text" value="juvenescent" />
<div>juvenescent</div>
CSS:
input, div {
font-family: cursive;
font-size: 2em;
padding: 15px 20px;
margin-bottom: 10px;
width: 300px;
border: 1px solid black;
}
div {
background-color: white;
}
Here is a link to the jsfiddle:
https://jsfiddle.net/9eLzqszx
What would be the workaround here? Obviously, I want the padding of the input text box to NOT cut the text inside it.
It looks like the curve of the J goes past the left-hand side of what the browser considers to be the edge of the letter. Instead of using padding for both sides, use padding for top/right/bottom and instead use text-indent for the left, it should do the trick!
input {
font-family: cursive;
font-size: 2em;
padding: 15px 20px 15px 0;
font-style:italic;
margin-bottom: 10px;
width: 300px;
border: 1px solid black;
text-indent: 20px;
}
https://jsfiddle.net/will0220/pxrs321f/3/
An input element is a special element as it needs to cut and allow the user to navigate through its text. Its active text zone isn't increased by the padding, and that's why you're seeing this behavior. Firefox seems to be more clever than the bunch, as it doesn't vertically cut the text if the text's width is smaller than the input's active text zone.
A workaround would be to add text-indent and decrease padding-left:
text-indent: 5px;
padding-left: 15px; /* Originally 20px */
You can see it in a fiddle over here.
You could try increasing your line height property. That would be restricting the viewable area for the letters causing them to be cut off. However, that's probably a crappy hack if you want it to match the same size as your div.
Add height: auto; to your input type=text to keep flexibility, and change the padding to get the original effect, like this padding: 14px 20px;

Why does my hyperlinked div stretch across the entire page?

This div is inside a 'page-container' div with a 'content div inside it, but the issue can be reproduced without those (as seen in the Fiddle below).
HTML:
<div class="download_link">Download PDF</div>
CSS:
.download_link {
margin: 0 auto;
width: 200px;
border-radius: 5px;
transition: 0.5s;
background-color: lightblue;
text-align: center;
font-family: 'Source Serif Pro';
font-weight: 600;
font-size: 25px;
}
.download_link:hover {
transition: 0.5s;
background-color: limegreen;
}
The div links properly and even changes color on hover. But the link stretches across the entire container. I have tried changing the width of all sorts of things.
>>> Convenient JSFiddle <<<
Generic division (div), by default, is a block element. Blocks, regardless of their width, take an entire line to themselves within their parent. In your case, the parent of the div is an anchor tag, which, by default, is inline. Inlines, likes absolute elements, inline-blocks, and floats, shrink-wrap around their children. The block within an inline inherently wants to "have" a line to itself, which is why it makes its parent stretch to the right and left edges of its body parent.
Franky, placing a div inside an anchor makes little sense. All you really need is just an anchor tag that serves its purpose. And, interestingly, if you display an anchor as a block, then the clickable link area will only be the width of the anchor. You have less markup and the functionality that you want.
Here's the jsfiddle: http://jsfiddle.net/hhm46/2/.
Here's HTML:
Download PDF
<p>Sample paragraph</p>
Here's CSS:
a[href $= ".pdf"] {
display: block;
margin: 0 auto;
width: 200px;
border-radius: 5px;
transition: background-color 0.5s;
background-color: lightblue;
text-align: center;
font-family: 'Source Serif Pro';
font-weight: 600;
font-size: 25px;
}
a[href $= ".pdf"]:hover {
transition: 0.5s;
background-color: limegreen;
}
You should wrap the anchor tags inside the div, not outside. Fiddle.
<div class="download_link">Download PDF</div>
Simple, div is a block level element by default. Change it to display:inline-block or display:inline.
Inline Block Demo: http://jsfiddle.net/3Ld8U/2/
Though as #josh mentioned you may be better off putting the a inside the div
Two ways to do this:
I found a workaround for you:
<div id=wrapit style="width:200px; margin:0 auto 0 auto;">
<a href="http://www.example.com">
<div class="download_link">Download PDF</div></a></div>
If you would like for the anchor tag to go in the div like the others have recommended, I recommend you increase the padding of the anchor tag so that it extends to the edge of the borders.
.download_link a {
padding: 0 20px 0 20px;
}

Responsive Text block on top of image

I would like to place a responsive text block on top of an image that I have set up based on this dated tutorial and amended based on this previous question.
Unfortunately there appears to be a couple of bugs. the span.spacer used to create padding either side of the line break appears taller than the rest of the text block, and I also think it is causing the text to not align left correctly. The development page can be viewed here. You can see a taller black block at the end of the first line of text, and a taller black block at the beginning of the second line.
The CSS i'm using is
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
.image h2 {
position: absolute;
bottom: 20px;
left: 0;
width: 100%;
text-shadow: none;
}
h2 span {
color: #fff;
font-size: 110%;
width: 40%;
line-height: 110%;
padding: 0 20px;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.7);
}
h2 span.spacer {
padding:0 5px;
}
The HTML is
<div class="image">
<img alt="Trekking" src="http://davidkneale.com/wc/wp-content/uploads/borneo_trek_mock.jpg" />
<h2><span>Trekking:<span class='spacer'></span><br />
<span class='spacer'></span>It's a Jungle Out There</span></h2>
</div>
Any advice on a fix for this or a better way to do it much appreciated!
It is becase you have span element in another span element (they are overlaid) and CSStyle is applied to both.
I think you can modify selector to: h2>span {...},
You can use one span element for each line (each with diferent look):
<h2>
<span class="big">Trekking:</span>
<br>
<span>It's a Jungle Out There</span>
</h2>
h2 span {
color: #fff;
font-size: 110%;
line-height: normal;
padding: 0 20px;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.7);
display: inline-block;;
}
h2 span.big {
font-size:130%;
}
Width 40% was too short.
The reason for the increased height is the span within a span causing the font-size 110% to be applied twice. Set font-size 100% on the spacer.
You also probably want an increased line height (more like 140% than 110% with the font you're using), and a spacer padding of 10px to match the 20px of the start/end. It does feel like there should be a simpler way to do this!
You are right, this tutorial is quite outdated – I would not bother with that “spacer-span” mumbo-jumbo at all.
And while it is not possible to have a horizontal padding applied to each line of an inline element (it’ll only be applied before the first and after the last line) – it is possible to use box-shadow to achieve a similar effect (as long as only a background color is required, and not f.e. an image).
<div>
<img src="http://davidkneale.com/wc/wp-content/uploads/borneo_trek_mock.jpg">
<h2><span>Trekking:
It’s a Jungle Out There</span></h2>
</div>
div { position:relative; }
img { display:block; max-width:100%; }
h2 { position:absolute; bottom:0; left:.5em; white-space:pre; line-height:1.333; }
h2 span { padding:.125em 0 .125em .25em; background:rgba(0,0,0,.75); color:#fff;
box-shadow:-.5em 0 0 rgba(0,0,0,.75), .5em 0 0 rgba(0,0,0,.75); }
See it here in this fiddle: http://jsfiddle.net/FXJEL/
I gave the span element a padding-left here to have the first line of text be slightly moved to the right, as in your example – assuming that is a desired effect; if not, simply remove it.
And instead of using a <br> to break the text into two lines, I used
for a line break character, and white-space:pre to have it displayed as such. But feel free to change that back to using a br element if that seems more convenient.
The span element inside the h2 is necessary here to have an inline element, because only that will behave like this regarding the element dimensions; under normal conditions, one could of course make the h2 display as inline, but that does not work here, because the h2 is positioned absolutely, and that “overwrites” display:inline, and one would end up with a box that is as wide as the whole text.