7

I want to add bottom padding to the ul (or to the last li) that is exactly equal to the computed value of the line-height property at ul and li (they both inherit their line-height). In other words, I want white space under "Item 3" of the same size as if an "Item 4" li was added. Is that possible?

Note that em and rem units don't achieve this if line-height is not equal to 1. Also, consider that the "environment" in which the ul lives is unknown. For example, I don't know if body has line-height: 1.1, line-height: 2, etc.

I want to have the blue space in this image as padding.

enter image description here

This achieves the desired look, but it just feels wrong to have to use pseudo-elements for such a simple use-case.

ul {
  list-style: none;
  background: #ccc;
}

ul::after {
  content: "\00a0";
  display: list-item;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
bernie
  • 9,820
  • 5
  • 62
  • 92
  • you mean the value of the computed value of line-height if this one is set to `1`? – Temani Afif Feb 08 '19 at 09:37
  • @TemaniAfif I mean the computed `line-height` value for the `li` element. – bernie Feb 08 '19 at 09:59
  • 1
    If you are using SASS/LESS, then you can define a `variable` and read the value of `line-height` and `padding` from that variable – pixlboy Feb 08 '19 at 10:00
  • 1
    and by the way, CSS has variables as well. – pixlboy Feb 08 '19 at 10:02
  • @rach8garg This is true, but then the styling doesn't adjust itself automatically based on its computed `line-height` which can come from any parent HTML element. – bernie Feb 08 '19 at 10:09
  • @rach8garg even CSS variable won't help here, because it's about a computed value that we don't set explicitely .. if you set `1.3` to line height you don't have the computed value since it's relative to font – Temani Afif Feb 08 '19 at 10:09
  • 2
    _“but it just feels wrong to have to use pseudo-elements for such a simple use-case”_ - that is something one could get over though, I feel :-) If you have ever used `float` in the past to achieve any kind of column layout, then you are familiar already with the “abuse” of CSS features for stuff they were never actually intended for in the first place, so … I’d probably accept an already found working solution, and not try to find a different one just for the heck of it. – 04FS Feb 08 '19 at 10:11
  • So why can't we set value of padding and line-height relative to parent/root in em/rem ? They both will be computed as same if I am not mistaken. Similar to variable example I gave below. – pixlboy Feb 08 '19 at 10:13
  • 1
    @rach8garg and what if we want to keep the default line-height and don't set it? – Temani Afif Feb 08 '19 at 10:16
  • 2
    @04FS yes of course I can get over it :) I'm asking this to know if I'm lacking something useful in my knowledge of CSS. The answer could very well be that there is no other way. – bernie Feb 08 '19 at 10:19
  • I guess you could say knowing when to apply such “tricks”/workarounds to achieve a certain desired effect, _is_ a large part of useful CSS knowledge :-) Introducing a fake “line” of text via the line-break in a pseudo element lets you reap the benefits of an already built-in mechanism, anything else that did not make use of that automatism in some form or other would mean you’d have to do some sort of calculation or specify some magic number somewhere … so this is probably as good as it gets. – 04FS Feb 08 '19 at 10:35
  • Well, its easy .. is not possible using CSS to dynamically set the `padding-bottom`. Either use what you already did with a `::psuedo element` or use `javascript`. – Red Feb 08 '19 at 10:38

3 Answers3

0

I hope this is what you are looking for.

Total height of <ul> is 80px and each item has line-height of 20px.

:root {
  --custom-height: 20px; 
}
ul {
  list-style: none;
  padding-bottom: var(--custom-height);
}
li{
   line-height: var(--custom-height);
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
pixlboy
  • 1,452
  • 13
  • 30
  • This can work if the `line-height` is fixed. In my case, I'm looking for something that adjusts to its computed property value though. – bernie Feb 08 '19 at 10:11
0

You can't get the computed line-height just using CSS. So with that said, its not possible to dynamicaly change the padding-bottom based on the line-height with CSS only.

So either do what you already did using the ::pseudo element, which can take its parent height. Acting as a new <li> element.

Or use JavaScript, that could be done like this

var ul = document.querySelectorAll('ul');
var li = document.querySelectorAll('ul li');

for(var i = 0;i < ul.length;i++) {
  ul[i].style.paddingBottom = li[0].clientHeight + 'px';
}
ul {
  list-style: none;
  background: #ccc;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

JavaScript: Find DIV's line-height, not CSS property but actual line-height

https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight

This achieves the desired look, but it just feels wrong to have to use pseudo-elements for such a simple use-case.

There is nothing wrong to use ::pseudo elements for such a use-case.

Red
  • 6,599
  • 9
  • 43
  • 85
-1

You can do it using jquery, But the approach you've applied is better than jquery approach. see below example.

$(document).ready(function(){
  var get_padding = $('ul>li').css('font-size');
  $('ul>li:last-child').css('padding-bottom',get_padding);
});
ul {
  background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
ElusiveCoder
  • 1,593
  • 1
  • 8
  • 23
  • 1
    the pseudo element would be a lot better than using jQuery I guess – Temani Afif Feb 08 '19 at 09:58
  • I've already told that bro..., Couldn't understand negative votes...I just gave other option. – ElusiveCoder Feb 08 '19 at 09:58
  • If `line-height` is not 1, (e.g. `line-height: 2;`) then the padding will not match the line height of an item. From a quick search, getting the computed `line-height` value using jquery is not a straightforward task. This might help others searching for this though. – bernie Feb 08 '19 at 10:06
  • because your answer is wrong, this will only work in one particular case when line-height is equal to `1` – Temani Afif Feb 08 '19 at 10:07