简体   繁体   English

仅使用CSS使相邻的兄弟元素具有相同的宽度

[英]Make adjacent sibling elements same width using only CSS

I'm sorry in advance because I cannot show the code/images of what I'm working on due to confidentiality reasons, however I think I can explain it quite simply. 我很抱歉,因为保密原因我无法显示我正在处理的代码/图像,但我想我可以很简单地解释一下。

I have an <h1> element that acts as the title to my webpage - this title can change in length based on the title of the specific page the user is on, so it could say "Homepage" or it could say "Saved Projects", etc. The length varies. 我有一个<h1>元素作为我的网页的标题 - 这个标题可以根据用户所在的特定页面的标题来改变长度,所以它可以说“主页”或它可以说“已保存的项目”等长度各不相同。

The <h1> has a sibling element, a <ul> , that acts as a dropdown to navigate to these other pages. <h1>有一个兄弟元素,一个<ul> ,作为下拉列表导航到这些其他页面。

My goal is to make the size of the dropdown be the same size as the <h1> at all times. 我的目标是使下拉列表的大小始终与<h1>相同。 Currently the width is explicit, I've tried 'auto' which did not work, naturally and am looking for other workarounds now but I figured I could post here in the meantime. 目前宽度是显而易见的,我已经尝试过“自动”,但是我现在正在寻找其他解决方法,但我想我可以在此期间发布。

I am using LESS, so I know variables are big and it could be a matter of passing a variable as the value for the dropdown width, however I don't know if these variables can be dynamic based on the length of the <h1> . 我使用LESS,所以我知道变量很大,可能是将变量作为下拉宽度的值传递,但是我不知道这些变量是否可以基于<h1>的长度动态。 I am new to LESS. 我是LESS的新手。

I've browsed a couple other posts asking a similar questions, but none have gotten a very solid solution. 我浏览了几个其他帖子,询问类似的问题,但没有一个得到一个非常可靠的解决方案。 Google also wasn't too helpful, most articles talked about parent-child sizes or keeping width/height relative. 谷歌也没有太大的帮助,大多数文章都谈到了父子尺寸或保持宽度/高度相对。 Not comparing widths of siblings. 不比较兄弟姐妹的宽度。

Thanks for any assistance! 谢谢你的帮助!

I tried to make a demo based on your description (without revealing the cure to cancer). 我试着根据你的描述做一个演示(没有透露治愈癌症的方法)。 Not sure if I understood you correctly, but is this what you are after? 不确定我是否理解正确,但这就是你所追求的吗? http://jsbin.com/hekimije/1/edit (jsFiddle is down :-( ) http://jsbin.com/hekimije/1/edit(jsFiddle向下:-()

If so, allow me to explain, as it is in fact quite simple: 如果是这样,请允许我解释,因为它实际上非常简单:

  • add a wrapper around your h1 and ul h1ul周围添加一个包装器
    • float it left to make it take the width of its content 将其向左浮动以使其占用其内容的宽度
    • give it a position relative so you can use it to position your ul against 给它一个相对位置,这样你就可以用它来定位你的ul
  • position the ul absolute ul绝对定位
    • give it a left and right of 0 to make it take the same width as its parent 给它左右为0,使其宽度与其父级相同
    • give it a top of 100% to start at the bottom edge of its parent 给它一个100%的顶部从它的父母的底部边缘开始

This method does lift your ul out of the document flow, but for a dropdown that is usually desirable, so it shouldn't be an issue. 这种方法确实可以提升文档流的ul,但是对于通常需要的下拉列表,所以它不应该是一个问题。

And the full code: 和完整的代码:

HTML HTML

  <div id='title-wrapper'>
    <h1>Some title</h1>
    <ul>
      <li>item 1</li>
      <li>item 2</li>
    </ul> 
  </div>

Less

#title-wrapper {
  float: left; 
  position: relative;

  ul {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0; 
  }
}

All the rest of the code in the jsBin is just to make things a bit better visible. jsBin中的所有其余代码只是为了让事情更好看。 You can change the length of the title and you'll notice the ul follows. 您可以更改标题的长度,然后您会注意到ul。 Obviously you have to hover the title to see the ul , it is a dropdown after all... 显然你必须悬停标题才能看到ul ,毕竟它是一个下拉列表...

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

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