【问题标题】:Center Parent Div around child div element (reverse inheritance?)围绕子 div 元素居中父 div(反向继承?)
【发布时间】:2013-09-08 06:13:23
【问题描述】:

我正在努力为以下站点将由不同大小的 div 组成的菜单居中:https://robert-wainblat.squarespace.com/。包装器(父 div - ul.nav.clear)正确居中,包装器内的子 div(菜单按钮)也是如此。我的问题是我不希望按钮在虚拟中心上均匀分布,我希望 HOME 按钮成为中心并将其与其下方徽标的中心对齐,几乎就像一个枢轴,并且有其他按钮围绕它对齐:

 __________________________________________
|      1  2  3  4  HOME  5  6  7  8        |     
 ------------------------------------------

这甚至可能吗?我能想到的唯一其他选择是硬编码前 10 个分辨率宽度并调整 margin-left 以使 HOME 按钮显示居中。

这是菜单的 CSS:

.primary-nav .nav {
  /*this is the parent div;*/
  width: 100%;
  position: relative;
  margin-left: 5.34%; - CURRENTLY USING THIS TO TWEAK POSITION FOR CURRENT RESOLUTION
  padding: 0;
  text-align: center;
  background-color: rgba(19, 19, 19, 0.9);
  z-index: 1001;
  background-attachment: scroll;
  list-style-type: none;
}

我们将不胜感激所有帮助!

谢谢!

【问题讨论】:

  • 我不明白你为什么需要那个margin-left..如果没有它,导航不会以不同的分辨率为中心吗?
  • Heya Robert,我假设您无法控制生成的 HTML,对吧?您只需要使用 CSS 来执行此操作吗?
  • 我可以将 html “注入”到 head 标签或页脚中,但我不确定这是否会有所帮助。
  • 我很尴尬地说解决方案很简单。我意识到使用固定的权利:18.5px; value 可以解决问题,现在 HOME 始终居中! .primary-nav { 位置:固定;背景颜色:rgba(19,19,19,1);文本对齐:居中;空白:nowrap;宽度:100%;右:18.5px; } .primary-nav .nav{ 位置:相对;文本对齐:居中;背景附件:滚动;列表样式类型:无;最小宽度:100px; } 感谢大家的帮助!

标签: css drop-down-menu centering alignment


【解决方案1】:

目前想到的唯一方法是将两侧分成单独的容器,左侧右对齐,右侧左对齐,并为容器添加足够的填充以留出间隙以绝对-定位到 HOME。

#menuLeft {
    width:50%;
    text-align:right;
    padding-right:75px;
    box-sizing:border-box;
}

#menuRight {
    width:50%;
    text-align:left;
    padding-left:75px;
    box-sizing:border-box;
}

#menuHome {
    position:absolute;
    left:50%;
    margin-left:-75px;
    width:150px;
    text-align:center;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多