首页 \ 问答 \ 让父div的宽度等于最宽的子元素/ div(Getting Parent div's width to equal widest Child element/div)

让父div的宽度等于最宽的子元素/ div(Getting Parent div's width to equal widest Child element/div)

据我所知,如果未定义div的宽度和高度属性,则父div将等于排列的子元素宽度和高度属性的结果(统称分别)。

我目前的设置是这样的:

<div> //container
  <div> //list of each element
    <div> // element 1
      ... children comprised of on SVG and a few other divs
    </div>
    <div> // element 2
      ... children comprised of on SVG and a few other divs
    </div>
    <div> // element 3
      ... children comprised of on SVG and a few other divs
    </div>
    …etc…
  </div>
</div>

我的问题是元素div的width s没有正确设置(或更新)。 div的宽度应该是子SVG的宽度(因为它是最宽的),div的高度应该是子div的高度(其中2个)和SVG的高度。

这是这个例子的小提琴 。 您会注意到栏示例正在被切断。 这个div的计算宽度是300,尽管它的SVG更大(拖动以查看剩余部分)

JQ-UI正在发挥作用,因此在用户操纵表示的大小后,其他人将重新渲染自己。 如果我们可以找出为什么div渲染到300px,那么它也将修复重新渲染,因为它将基于其子元素。

似乎问题也可能出在SVG本身。 我还没有在SVG上定义宽度和高度属性,而是在CSS上这样做了。 如果我在调整大小时操纵SVG宽度属性,它似乎可能有效,但我仍然不清楚为什么SVG的宽度不计算然后适合div。


To my understanding, if a div's width and height properties are not defined, the Parent div will equal the result of the arranged children elements width and height properties added collectively (and respectively).

My current set up is like so:

<div> //container
  <div> //list of each element
    <div> // element 1
      ... children comprised of on SVG and a few other divs
    </div>
    <div> // element 2
      ... children comprised of on SVG and a few other divs
    </div>
    <div> // element 3
      ... children comprised of on SVG and a few other divs
    </div>
    …etc…
  </div>
</div>

My problem is that the widths of the element divs are not getting set (or updated) correctly. The width of the div should be the width of the child SVG (as it is the widest), and the height of the div should be the height of the child divs (2 of them) and the height of the SVG.

Here is the fiddle of the example. You will notice that the bar example is being cut off. This div's computed width is 300, despite its SVG is larger (drag to see the remaining portion)

JQ-UI is in play, so after a user manipulates the size of on representation, the others will re-render themselves. If we can figure out why the div is rendering to 300px, then it will also fix the re-rendering, since it will be based on its children elements.

It appears that the issue might also be with the SVGs themselves. I have not defined the width and height properties on the SVG, rather I did so on the CSS. If I manipulate the SVG width propertuy on resize, it appears to potentially work, but I am still uynsure of why the SVG's width is not calcualated and then fit into the div.


原文:https://stackoverflow.com/questions/18494367
更新时间:2022-01-15 07:01

最满意答案

最有效的内存方式是将值存储为json数组,然后拆分键,以便使用ziplist编码的哈希值存储它们。

  1. 使用say json数组对数据进行编码,因此您有key = value对,如user:1234567 -> [21,'25-05-2012','14-06-2010']
  2. 将您的键分成两部分,这样第二部分就有大约100种可能性。 例如, user:1234567
  3. 将此组合键存储在像此hset user:12345 67 <json>的哈希中hset user:12345 67 <json>
  4. 要检索用户ID 9876523的用户详细信息,只需执行hget user:98765 23并解析json数组
  5. 确保调整设置hash-max-ziplist-entries和hash-max-ziplist-value

Instagram写了一篇很棒的博客文章来解释这种技术 ,所以我将跳过解释为什么这是内存效率的。

相反,我可以告诉你这种技术的缺点。

  1. 您无法访问或更新用户的单个属性; 你必须重写整个记录。
  2. 即使您只关心某些字段,也必须始终获取整个json对象。
  3. 最后,您必须在拆分键上编写此逻辑,这是添加的维护。

与往常一样,这是一种权衡。 确定您的访问模式,看看这样的结构是否有意义。 如果没有,你必须购买更多的内存。


The most memory efficient way is storing values as a json array, and splitting your keys such that you can store them using a ziplist encoded hash.

  1. Encode your data using say json array, so you have key=value pairs like user:1234567 -> [21,'25-05-2012','14-06-2010'].
  2. Split your keys into two parts, such that the second part has about 100 possibilities. For example, user:12345 and 67
  3. Store this combined key in a hash like this hset user:12345 67 <json>
  4. To retrieve user details for user id 9876523, simply do hget user:98765 23 and parse the json array
  5. Make sure to adjust the settings hash-max-ziplist-entries and hash-max-ziplist-value

Instagram wrote a great blog post explaining this technique, so I will skip explaining why this is memory efficient.

Instead, I can tell you the disadvantages of this technique.

  1. You cannot access or update a single attribute on a user; you have to rewrite the entire record.
  2. You'd have to fetch the entire json object always even if you only care about some fields.
  3. Finally, you have to write this logic on splitting keys, which is added maintenance.

As always, this is a trade-off. Identify your access patterns and see if such a structure makes sense. If not, you'd have to buy more memory.

相关问答

更多
  • 结果是一个非常有趣和令人惊讶的问题! 其真正原因是它们正在使用的分配器中的内存碎片( dlmalloc )。 希望MSFT能够做得更好,但我希望这需要一些时间。 与此同时,解决方法。 正确的解决方法(暂时) 配置maxmemory和maxheap参数。 使maxheap比maxmemory 。 因此,如果你想要maxmemory=100MB那么使maxheap 5倍甚至10倍,例如maxheap=500MB甚至maxheap=1000MB 。 我不认为有一个很好的经验法则需要多大的maxheap ,这就是为 ...
  • 没有区别。 编译器会优化内存使用方面的任何潜在差异。 如果你编译(使用javap -c )这两个例子并比较字节码,你会发现字节码是一样的。 当然,这取决于JVM版本。 但是由于这个例子非常微不足道,所以假设两者都不比另一个更有效率。 例1: 码: public class example1 { public static void main(String[] args) { for (int i=0; i<10; i++) { Object a = new O ...
  • “关键效率”表示您从MySQL内存中保存的索引缓存获得的价值。 如果您的关键效率很高,那么MySQL通常会从内存空间执行关键查找,这比从磁盘检索相关的索引块要快得多。 提高关键效率的方法是将更多的系统内存用于MySQL的索引缓存。 这样做取决于您使用的存储引擎。 对于MyISAM,增加key-buffer-size的值。 对于InnoDB,增加innodb-buffer-pool-size的值。 然而,正如Michael Eakins指出的那样,操作系统还拥有最近访问的磁盘块缓存。 您的操作系统可用的内存越 ...
  • 这取决于您如何访问数据: 选择1: 如果您在大多数访问中使用大多数字段。 如果可能的键有差异 选择方案2: 如果您在大多数访问中只使用单个字段。 如果您总是知道哪些字段可用 PS:作为一个大拇指的规则,去大部分用例需要更少的查询选项。 It depends on how you access the data: Go for Option 1: If you use most of the fields on most of your accesses. If there is variance on po ...
  • Lambdas和Procs有几处不同之处。 兰姆达斯有所谓的“小小回报”。 这意味着Lambda会将流程返回给调用它的函数,而Proc将返回调用它的函数。 def proc_demo Proc.new { return "return value from Proc" }.call "return value from method" end def lambda_demo lambda { return "return value from lambda" }.call "return ...
  • 最有效的内存方式是将值存储为json数组,然后拆分键,以便使用ziplist编码的哈希值存储它们。 使用say json数组对数据进行编码,因此您有key = value对,如user:1234567 -> [21,'25-05-2012','14-06-2010'] 。 将您的键分成两部分,这样第二部分就有大约100种可能性。 例如, user:12345和67 将此组合键存储在像此hset user:12345 67 的哈希中hset user:12345 67 要检索用户ID ...
  • 每个Dictionary都有一些相关的开销:几个int和一些对象引用,即使它是空的,然后一旦你开始添加项目,它就需要两倍的空间。 我假设ConcurrentDictionary几乎是一样的。 因此,不要在每个Point上放置一个新的ConcurrentDictionary,您可能想要尝试使用一个单一的ConcurrentDictionary,这个ConcurrentDictionary不管是使用点数组索引还是正在使用的键。 C#7的新元组语法应该很容易做到。 Each Dictionary tends to ...
  • 无论如何你将是IO绑定,你的代码的性能根本不重要(除非你是一个非常糟糕的程序员..) You will be IO bound anyway, the performance of your code won't matter at all (unless you're a really bad programmer..)
  • hGetContents返回的String与任何其他Haskell字符串没有区别。 通常,除非程序员采取额外步骤来确保它(例如seq , deepseq ,bang模式),否则不会对Haskell数据进行全面评估。 字符串被定义为(基本上) data List a = Nil | Cons a (List a) -- Nil === [], Cons === : type String = List Char 这意味着字符串是空的,或者是单个字符(头部)和另一个字符串(尾部)。 由于懒惰 ,尾巴可能不存在 ...
  • if快或慢的主要问题是可预测性。 现代CPU(2000年以后的任何东西)都使用一种称为分支预测的机制 。 首先阅读上面的链接,然后在下面阅读... 哪个更快? if语句构成一个分支,因为CPU需要决定是否跟随或跳过if部分。 如果它正确猜测分支,则跳转将在0或1个周期(1Ghz计算机上1纳秒)内执行。 如果它没有正确地猜测分支,则跳跃将花费50个周期(给予或接受)(微秒的1/200)。 因此,即使将这些差异视为人类,您也需要执行数百万次的if语句。 上述两个陈述可能会在完全相同的时间内执行,因为: 为变量赋 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)