CSS line height resizing browser text - html

I am no guru at CSS so please excuse what might be a basic question. I have an annoying problem which I can't seem to fix:
Here is my text without CSS line-height:
I would like to move the text up closer to the heading tags so I did this:
<h2>Loren Ipsum Dol Tjovanuu</h2>
<p style="line-height:0px;">
<i>Lorem ipsum dolor sit amet, consectetur adipiscing elit ...</i>
</p>
<h2>Neque porro quisquam est qui dolorem ipsum</h2>
<p style="line-height:0px;">
<i>Lorem ipsum dolor sit amet, consectetur adipiscin.</i>
</p>
The Result
The result is perfect and exactly what I want, but... the problem comes when I resize the browser.
Problem Resizing the browser
My Question
Why is the text condensing on browser resize? What am I doing wrong? Should I not use the line-height property? Any workaround for this?

The line-height property is used to control how much vertical space is allocated for each line. In general, it is used to adjust how much space there is between lines within an element.
line-height: 1 means that lines are exactly big enough to fit the tallest letters and lowest descenders, with no space between. A line-height of more than 1 means there is some extra space between lines, and less than 1 will result in lines overlapping.
line-height: 0 means that a line of text has no vertical space allocated to it, so all lines will overlap each other in one line. That is what you are seeing here: the text is wrapping onto a second line, which is rendered over the top of the first line.
What you are trying to do is adjust the space between elements, not the space between lines in a single element. For this, the recommended approach is to adjust either margin or padding. Consider adjusting the margins of your elements until you have your desired vertical rhythm.
For a really detailed explanation of how all three properties work, see this CSS Tricks article on the box model.
Example
body { font-family: sans-serif; }
.cramped h2 {
margin: 0.4em 0 0.2em;
}
.cramped p {
font-style: italic;
margin: 0;
}
<section class="cramped">
<h2>Loren Ipsum Dol Tjovanuu</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit ...</p>
<h2>Neque porro quisquam est qui dolorem ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscin.</p>
</section>

Add this to your CSS:
h2 {
margin-bottom: -10px;
}
h2 tags have margins by default
Here is the JSFiddle demo

<h2>Loren Ipsum Dol Tjovanuu</h2>
<p style="line-height:23px;">
<i>Lorem ipsum dolor sit amet, consectetur adipiscing elit ...</i>
</p>
<h2>Neque porro quisquam est qui dolorem ipsum</h2>
<p style="line-height:23px;">
<i>Lorem ipsum dolor sit amet, consectetur adipiscin.</i>
</p>
try this it works fine on my browser

try this
p{margin-top:-10px; font-style:italic;}
#media screen and (max-width:768px){
h2{font-size:18px;}
p{font-size:14px}
}

Line height usage is for setting the distance (height) of each line. 0 value gives no distance so you have this problem.
You should let the line-height in the default value and reset default h2 and p element margin.
line-height
On block level elements, the line-height property specifies the
minimum height of line boxes within the element.
On non-replaced inline elements, line-height specifies the height that
is used to calculate line box height. On replaced inline elements such
as buttons or other input element, line-height has no effect. [1]
h2, p {
margin: 0;
}
<h2>Loren Ipsum Dol Tjovanuu</h2>
<p>
<i>Lorem ipsum dolor sit amet, consectetur adipiscing elit ...</i>
</p>
<h2>Neque porro quisquam est qui dolorem ipsum</h2>
<p>
<i>Lorem ipsum dolor sit amet, consectetur adipiscin.</i>
</p>
Reference: MDN - line-height - w3.org - line-height

Related

Why does an HTML child element not seem to inherit the dir attribute when set to auto?

When I set the dir attribute to auto on a parent element, it does not appear that the child elements inherit auto, but it does appear that they inherit ltr or rtl.
According to this:
In HTML the base direction is either (a) set explicitly by the nearest parent element that uses the dir attribute (which could be the html element), or, (b) in the absence of such an attribute, left-to-right (LTR).
And according to the spec:
If the element has a parent element and the dir attribute is not in a defined state (i.e. it is not present or has an invalid value)
The directionality of the element is the same as the element's parent element's directionality.
If I understand that correctly, then every <p> should be inheriting its directionality from the parent <div> (in the last case below, auto), but it does not appear to be doing that when the directionality is set to auto. Why?
It appears this way in Chrome, Firefox, and Safari, so it's not a browser issue.
div {
border: 1px red solid;
margin: 1rem 0;
}
p {
margin: 5px;
border: 1px blue solid;
}
<!-- LTR -->
<div dir="ltr" id="div-ltr">
<p>
This div is set `ltr` so all of its descendants are `ltr` as well, no
matter the characters directionality
</p>
<p id="english-ltr">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Similique,
placeat
</p>
<p id="phoenician-ltr">π€€π€π€Š β€’ π€Šπ€‹π€Œπ€… β€’ 𐀁𐀓 β€’ 𐀇𐀉𐀀</p>
</div>
<!-- RTL -->
<div dir="rtl" id="div-rtl">
<p>
This div is set `rtl` so all of its descendants are `rtl` as well, no
matter the characters directionality
</p>
<p id="english-rtl">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Similique,
placeat
</p>
<p id="phoenician-rtl">π€€π€π€Š β€’ π€Šπ€‹π€Œπ€… β€’ 𐀁𐀓 β€’ 𐀇𐀉𐀀</p>
</div>
<!-- AUTO -->
<div dir="auto" id="div-auto">
<p>
This div is set `auto`, but the children all seem to default to `ltr`
</p>
<p id="english-auto">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Similique,
placeat
</p>
<p id="phoenician-auto">π€€π€π€Š β€’ π€Šπ€‹π€Œπ€… β€’ 𐀁𐀓 β€’ 𐀇𐀉𐀀</p>
</div>
It says the direction is inherited from the parent, not the value of the dir attribute.
Your div has no text nodes in it, so auto is treated as LTR.
Add some RTL text and it changes:
<div dir="auto" id="div-auto"> π€€π€π€Š β€’ π€Šπ€‹π€Œπ€… β€’ 𐀁𐀓 β€’ 𐀇𐀉𐀀
<p>
This div is set `auto`, but the children all seem to default to `ltr`
</p>
<p id="english-auto">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Similique,
placeat
</p>
<p id="phoenician-auto">π€€π€π€Š β€’ π€Šπ€‹π€Œπ€… β€’ 𐀁𐀓 β€’ 𐀇𐀉𐀀</p>
</div>

Semantic HTML: Which is the heading, which is the sub-heading?

I have a situation where I will have a lot of content that has a heading in small text, and directly below it will be a longer heading (sometimes even a sentence) in much larger text, followed by the standard paragraph text. Here is an example of what I'm talking about:
body {
font-family: Arial, sans-serif;
max-width: 400px;
margin: 0 auto;
}
h2 {
font-size: 12px;
text-transform: uppercase;
margin: 20px 0 10px;
}
.tagline {
margin: 0 0 20px 0;
font-size: 24px;
}
<h2>Section Title</h2>
<p class="tagline">A tagline... Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque.</p>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quos dolorem delectus neque est quidem numquam, incidunt corporis temporibus alias fuga.</p>
My initial instinct is to make the first part the heading element via <h2> and make the second section into a paragraph (as you can see in my example). But would this be correct? The larger text is definitely what people will most likely read first, but the smaller text is a better title for the section.
My question is am I good with this or should I swap it and make the small text a paragraph and the large text the heading tag?
In addition, I'm wondering if I should also be wrapping these two "headings" into a <header> tag as well?
Main Rule when dealing with HTML semantics is never work on the presentation part with HTML. That's the reason why we do have CSS.
Present days all websites are modifying there HTML semantics to meet WCAG standards. In simple words, those are some standards which when followed by developers helps people with accessibility problems access the web with ease.
For more details : https://www.w3.org/WAI/standards-guidelines/wcag/
So I suggest keeping the heading and sub-heading in the right way with right semantics. Think of a presentation in terms of CSS.
I'm not sure if this is the answer you are looking for, but I hope this helps to make a decision..
Reading the HTML spec on whatwg.org, this seems to be exactly what the hgroup element is for. In your case, the "correct" markup would be something like:
<hgroup>
<h2>Section Title</h2>
<h3>A tagline... Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque.</h3>
</hgroup>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quos dolorem delectus neque est quidem numquam, incidunt corporis temporibus alias fuga.</p>
The hgroup tag basically lets you write more than one header element while only "counting" the first one for document outline purposes. The subsequent ones are only aesthetic. Example use cases according to the spec are "subtitles", "taglines" etc.
Why not use hierarchy of headings? In your example, if tag line is a heading you should put that behind h3.
Screen readers don’t present CSS to users so ask yourself, β€œdoes the content β€˜structure’ still make sense when css is reduced?” If you use proper semantics then the structure will be preserved but with mocking styles (like heading style applied to a p tag) will lose its meaning when CSS is removed. Meaning screen readers would just present the tag line as paragraph text vs. presenting it as a sub heading. People navigate or jump through headings using screen readers so p tag tag line will not be in their heading navigation.
Does that make sense? Happy to clarify anything confusing.

Can css be used to collapse whitespace around inline elements with varying line heights?

Suppose one is designing a box to frame some content, and wants that box to always have consistent space between its borders and text inside of it, regardless of the text's line-height. Is there a solution aside from custom negative margin on each box?
In theory this should actually be the "responsibility" of the content (in this case, the text), assuming our box is some kind of component allowing transclusion (e.g. web component slots), so I'd be especially interested in any way to style an inline element so that its line-height-generated top and bottom spaces collapse, regardless of line-height value (intentionally not calling them margins to not confuse them with the margin css property).
Here as an runnable example of the issue - the space between the magenta border and the inner text varies due to line height, and if the magenta border wasn't there, it would appear that each box has different padding.
This has probably been answered, but unfortunately since the terms are so generic it's hard to research (though I did try).
.foo {
max-width: 200px;
border: 2px solid blue;
padding: 20px;
line-height: 2;
display: inline-block;
vertical-align: top;
}
.foo>* {
border: 1px solid magenta;
}
.baz {
line-height: 1;
}
.bar {
line-height: 3;
}
<div class="foo">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
<div class="foo">
<div class="baz">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
<div class="foo">
<div class="bar">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
One idea that may solve half the issue is to change the line-height of the text to a smaller value than the container. Doing so, the height of the text will be smaller than the linebox and you can align it to the top. For this you need to consider an extra container.
.foo {
max-width: 200px;
border: 2px solid blue;
padding: 20px;
line-height: 2;
display: inline-block;
vertical-align: top;
}
.foo>* {
border: 1px solid magenta;
}
.baz {
line-height: 1;
}
.bar {
line-height: 3;
}
span {
line-height:1;
vertical-align:top;
}
<div class="foo">
<div><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
<div class="foo">
<div class="baz"><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
<div class="foo">
<div class="bar"><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
As you can see the text will always be aligned on the top whataver the line-height but the issue remains on the bottom. It's like we moved the issue to only one side.
Here is a related answer to better understand the alignment trick : https://stackoverflow.com/a/54190413/8620333

Start paragraphs on same height regardless of headline-height

I've been researching and trying for ages now and it kind of drives me crazy, that I am not able to solve this seemingly simple problem.
I've been trying to fit headings in blogpost-previews to the same height using flexbox. If a heading is "too long" it gets a line-break and has a greater height than the shorter ones, which is okay. However, the paragraph below the heading don't start on the same height anymore, which just looks very odd. Is there a simple way to achieve this, preferably with flexbox?
Here is an image of what I am trying to achieve:
Here is a link to the code on codepen
<div class="blogpost-container">
<div class="blogpost">
<header class="blogpost__header">
<div class="dummy-image"></div>
<h1>Blogpost heading short</h1>
</header>
<div class="blogpost__content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Graecum enim hunc versum nostis omnes: Suavis laborum est praeteritorum memoria.
Ergo instituto veterum, quo etiam Stoici utuntur, hinc capiamus exordium.</p>
</div>
</div>
<div class="blogpost">
<header class="blogpost__header">
<div class="dummy-image"></div>
<h1>Blogpost Heading is longer and thus has a greater height than the short one</h1>
</header>
<div class="blogpost__content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Graecum enim hunc versum nostis omnes: Suavis laborum est praeteritorum memoria.
Ergo instituto veterum, quo etiam Stoici utuntur, hinc capiamus exordium.</p>
</div>
</div>
</div>
Thank you very much in advance for your help.
Get the Fiddle: https://jsfiddle.net/4sew4x2m/
I used <table>. But you can use display:table instead.
From my point of view, if you you flex, you have to give width for that divs. In case of using tables, you don't have to- that's the advantage.
Good luck!

Bullets center with unordered list

Does anyone know the CSS that makes the bullet point sit at the top of a multi-line bulleted list? For some reason with the template that I am using the bullet point centers to the left instead of simply sitting next to the first word if I have more than one line of text.
Set the list style position to inside the list item, see this demo fiddle.
CSS:
ul {
list-style-position: inside;
}
This is not an answer per-se but it may give others an insight to a similar visual situation with a different set of circumstances.
I had the same issue, like so:
My HTML looked like this:
<ul>
<li>
Link with several lines. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, commodi!
<p>Lorem ipsum dolor sit amet.</p>
</li>
<li>
Link with several lines. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, commodi!
<p>Lorem ipsum dolor sit amet.</p>
</li>
</ul>
And the CSS like this:
a {
display: inline-block;
vertical-align: middle;
}
See the offending part? The vertical-align: middle; declaration.
There are two solutions:
Solution 1
Remove the vertical-align: middle; declaration altogether.
Solution 2
Change the value: vertical-align: middle; to vertical-align: top;.
Result (as expected in the beginning):
Hope this helps.
You could do something like this:
(css)
li div:before
{
background-image:url('bullet.png');
}
(html)
<ul>
<li>
<div>
text<br />
more text
</div>
</li>
</ul>