简体   繁体   English

CSS中可变的“固定”宽度点?

[英]Variable “fixed” width points in CSS?

 <div class="container"> <div class="content"> Apple </div> </div> 

Let's say 'content' should always have a width either 0, 25, 50, 75 or 100%, depending on its content. 假设“内容”的宽度应始终为0、25、50、75或100%,具体取决于其内容。 If the word 'Apple' takes 20% width of 'container', 'content' should have a size of 25%. 如果“苹果”一词占“容器”宽度的20%,则“内容”的大小应为25%。 If the word 'Lettuce' takes 40% width of 'container', 'content' should have a size of 50%. 如果“莴苣”一词占“容器”宽度的40%,则“内容”的大小应为50%。 I do not know what kind of content will be inside 'content', neither the width of the actual content. 我不知道“内容”中将包含哪种内容,也不知道实际内容的宽度。 But it should always snap to these values. 但是它应该始终保持这些值。

Is this behaviour possible using only css? 仅使用CSS可以做到这一点吗? If no, what would be the easiest solution to achieve this? 如果没有,实现这一目标的最简单解决方案是什么?

Edit: I am aiming to put any number of 'content' divs inside 'container'. 编辑:我的目标是在“容器”中放入任意数量的“内容” div。 All of them should have aa width which is one of these values. 它们所有的宽度都应该是这些值之一。 I also do not know the number of content divs. 我也不知道内容div的数量。

Something to consider is using flex boxes with css. 要考虑的事情是将Flex Box与CSS一起使用。 In this case, using 在这种情况下,使用

.container {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}

Should allow you to dynamically change the width. 应该允许您动态更改宽度。 What you are asking for is not something that seems very responsive or practical. 您所要求的不是响应速度快或实用的内容。 You could also use width: auto so the element will size itself. 您也可以使用width: auto以便元素自行调整大小。

Let's assume there's not a solution in css. 假设CSS中没有解决方案。

In Javascript, 在Javascript中,

var content = document.getElementById('content')
var container = document.getElementById('container')
var contentWidth = content.getBoundingClientRect().width
var containerWidth = container.getBoundingClientRect().width
var percent = contentWidth / containerWidth * 100
if (percent === 0) {
   content.style.width = '0%'
} else if (percent < 25) {
   content.style.width = '25%'
} else if (percent < 50) {
   content.style.width = '50%'
} else if (percent < 75) {
   content.style.width = '75%'
} else {
   content.style.width = '100%'
}

I don't believe this is possible using pure CSS. 我认为使用纯CSS不可能做到这一点。

Here's a javascript method which will do it, though; 不过,这是一个可以执行此操作的javascript方法; comments below explain how it works: 以下评论说明了它的工作原理:

 let list = document.getElementsByClassName('content'); let containerWidth = document.getElementsByClassName('container')[0].offsetWidth; Array.from(list).forEach(function(el) { // by default, divs are 100% width, making this useless. So we need to // change the display style to collapse the element width to its content boundary: el.style.display = "inline-block"; // find the smallest of 25%, 50%, 75%, 100% into which the content fits: let newWidth = Math.ceil(el.offsetWidth / containerWidth * 4) * 25; el.style.width = newWidth + "%"; el.style.display = "block"; // back to the original display. (It would be more elegant to have captured the original style.display value, in case it wasn't "block" in the first place, but I'mma handwave over that) }); 
 .content { border: 1px solid; } 
 <div class="container"> <div class="content"> Apple </div> <div class="content"> Something longer than that </div> <div class="content"> Something even longer than that, plus some more text </div> <div class="content"> Something even longer than even longer than even longer than even longer than that plus some more text even on top of that </div> </div> 

This will set percentage widths on the elements, so it will be somewhat responsive to changes in window size -- but if you need the percentages themselves to update you would need to re-run the above code on a (preferably debounced) window.resize event. 这将设置百分比宽度上的元素,所以这将是有所响应窗口大小的变化-但如果你需要百分比自己去更新你需要一个(最好是去抖)重新运行上面的代码window.resize事件。

This "snapping" behavior isn't possible in pure CSS, however it's possible in JS by measuring the offsetWidth of the element and adjusting its size accordingly. 这种“捕捉”行为在纯CSS中是不可能的,但是在JS中,可以通过测量元素的offsetWidth并相应地调整其大小来实现。

// select your "content" element (you might need a more specific rule)
const content = document.querySelector('.content');
// Calculate the percentage of its container that it fills up when it's unstyled
const proportion = content.offsetWidth / content.parentNode.offsetWidth;
// Adjust width based on that proportion
if (proportion < .25) content.style.width = '25%';
else if (proportion < .5) content.style.width = '50%';
else if (proportion < .75) content.style.width = '75%';
else content.style.width = '100%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM