css - Same width as height on a flex child element
问题描述
I'm working on a navbar where I will have a burger menu for toggling an extended menu.
I want to avoid to explicitly set a width to the burger menu element (.extendedMenu__toggle
) and have it scale based on the font-size set on .header
if possible.
The element gets it height from the parent as it's flex, but not sure how I can translate that into a proper width property so that I get a perfect square.
html {
font-size: 62.5%;
}
html, body {
font-family: helvetica;
line-height: 1.45;
}
.header {
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1000;
font-size: 1.3rem;
font-weight: bold;
}
.nav {
display: flex;
justify-content: flex-start;
margin: 0;
position: relative;
z-index: 5;
margin: 1rem;
}
.extendedMenu__toggle {
/*width: 37px;
height: 37px;*/
background: red;
}
.navLink {
padding: 0.75rem 1.25rem;
text-decoration: none;
border: 2px solid;
border-radius: 30px;
margin-left: 0.75rem;
text-transform: uppercase;
}
.navLink:first-child {
margin-left: 0;
}
<header class="header" role="banner">
<nav class="nav">
<div class="extendedMenu__toggle">
<span></span>
<span></span>
<span></span>
</div>
<a class="navLink" href="#">Home</a>
<a class="navLink" href="#">About</a>
<a class="navLink" href="#">News</a>
</nav>
</header>
解决方案
选项 #1:使用 Javascript。元素渲染后,您将检查高度,然后设置flex-basis
为元素高度。我也设置overflow: hidden
了,因为我的内容是“不方形的”。
SO的代码片段似乎不喜欢window.onload所以这是一个codepen示例
html {
font-size: 62.5%;
}
html, body {
font-family: helvetica;
line-height: 1.45;
}
.header {
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1000;
font-size: 4rem;
font-weight: bold;
}
.nav {
display: flex;
justify-content: flex-start;
margin: 0;
position: relative;
z-index: 5;
margin: 1rem;
}
.extendedMenu__toggle {
/*width: 37px;
height: 37px;*/
background: red;
overflow:hidden;
}
.navLink {
padding: 0.75rem 1.25rem;
text-decoration: none;
border: 2px solid;
border-radius: 30px;
margin-left: 0.75rem;
text-transform: uppercase;
}
.navLink:first-child {
margin-left: 0;
}
<header class="header" role="banner">
<nav class="nav">
<div id="box" class="extendedMenu__toggle">
menu
</div>
<a class="navLink" href="#">Home</a>
<a class="navLink" href="#">About</a>
<a class="navLink" href="#">News</a>
</nav>
</header>
选项 #2:使用图像。这有点骇人听闻,但只要在 HTML 中呈现方形图像,它就会尝试保持其比例。以下是几种可用的方法:
- 在 HTML 中放置具有透明背景的汉堡菜单图像。(变大,这样你就不会扩大,只是缩小)。
- 在 HTML 中放置一个 10px x 10px 的透明图像,并在其上绝对放置其他内容。
- 在 CSS 中创建一个
pseudo class
withcontent: url(image.jpg);
然后在其上方分层内容。
第一个是最容易展示的,所以我正在做那个。我会max-height
在图像中添加一个,这样它就不会失控。
html {
font-size: 62.5%;
}
html, body {
font-family: helvetica;
line-height: 1.45;
}
.header {
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1000;
font-size: 4rem;
font-weight: bold;
}
.nav {
display: flex;
justify-content: flex-start;
margin: 0;
position: relative;
z-index: 5;
margin: 1rem;
}
.extendedMenu__toggle {
/*width: 37px;
height: 37px;*/
background: red;
}
.extendedMenu__toggle img {
display:block;
max-height: 6rem;
}
.navLink {
padding: 0.75rem 1.25rem;
text-decoration: none;
border: 2px solid;
border-radius: 30px;
margin-left: 0.75rem;
text-transform: uppercase;
}
.navLink:first-child {
margin-left: 0;
}
<header class="header" role="banner">
<nav class="nav">
<div class="extendedMenu__toggle">
<img src="https://cdn1.imggmi.com/uploads/2018/10/15/e86f8a312edd60d643c8d80212b64dba-full.png" />
</div>
<a class="navLink" href="#">Home</a>
<a class="navLink" href="#">About</a>
<a class="navLink" href="#">News</a>
</nav>
</header>
推荐阅读
- javascript - 如何在嵌套数组对象数组的条件下使用 While 循环?
- python - PyMySQL - 转义标识符
- c# - 无法通过 AJAX 将值传递给 MVC 控制器
- javascript - 使用 Apps 脚本在 onclick 中调用函数
- android - Android GridView 单元格序列变化
- actionscript-3 - 对于不存在且该文件为 tempInit 的文件在 ActionsScript 中出现错误
- javascript - 如何使用 Ajax 填充 Bootstrap 表?
- java - 在 NetBeans 中取消选择属于 buttonGroup 的单选按钮
- jquery - 使用 jQuery 将值移动到另一个表单
- excel-formula - Excel 公式和多个 IF 条件