繁体   English   中英

如何在div内部垂直居中放置汉堡图标?

[英]How to center a burger icon vertically inside a div?

我有一个导航菜单,其中包含一个用3 <span>制成的汉堡图标,该图标位于另一个元素内:

 .navbar { width: 100%; color: #fff; background-color: #df0024; padding: 1% 0; } .tog { display: flex; cursor: pointer; float: right; width: 6%; position: absolute; top: 0; right: 0; bottom: 0; align-items: center; justify-content: center; height: auto; } /*This is the div that contain the burger 3 layers*/ #nav-icon { height: -webkit-fill-available; height: -moz-fill-available; height: -o-fill-available; height: fill-available; position: relative; -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); -webkit-transition: .5s ease-in-out; -moz-transition: .5s ease-in-out; -o-transition: .5s ease-in-out; transition: .5s ease-in-out; cursor: pointer; } /*/The style of each of the burger icon 3 layers*/ #nav-icon span { display: block; position: absolute; height: 3.1vh; width: 100%; background: white; border-radius: 9px; opacity: 1; left: 0; -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); -webkit-transition: .25s ease-in-out; -moz-transition: .25s ease-in-out; -o-transition: .25s ease-in-out; transition: .25s ease-in-out; } #nav-icon span:nth-child(1) { top: 0px; } #nav-icon span:nth-child(2) { top: 12px; } #nav-icon span:nth-child(3) { top: 24px; } 
 <nav class="navbar"> <div class="logo"> <a href="#"><img src="" alt='Logo' /></a> </div> <div id='tog' class="tog"> <label for="toggle" id='nav-icon'> <div class='icon-container'> <span></span> <span></span> <span></span> </div> </label> </div> </nav> 

如何居中#nav-icon span里面#nav-icon垂直? 我想要的只是将汉堡图标居中,因此我不需要更改包含汉堡图标的其他元素样式。

为了使这项工作有效,我不得不做很多调整,但是我使用了一个很好的垂直居中技巧,我知道该技巧涉及到top: 50%; transition: translateY(-50%); 如果将其应用于子div,则它将在大小相同的父级中垂直居中(父级也应具有relativeabsolute位置)。

我将这些样式应用于代码中的.icon-container

 .navbar{ width: 100%; position: relative; color: #fff; background-color: #df0024; padding: 1% 0; } .tog { display: flex; cursor: pointer; float: right; width: 6%; position: absolute; top: 0; right: 0; bottom: 0; align-items: center; justify-content: center; height: auto; } /*This is the div that contain the burger 3 layers*/ #nav-icon{ position: absolute; top:0; bottom:0; left:0; right: 0; -webkit-transition: .5s ease-in-out; -moz-transition: .5s ease-in-out; -o-transition: .5s ease-in-out; transition: .5s ease-in-out; cursor: pointer; } .icon-container { padding: 0 5px; box-sizing: border-box; position: relative; top: 50%; -ms-transform: translateY(-50%); /* IE 9 */ -webkit-transform: translateY(-50%); /* Chrome, Safari, Opera */ transform: translateY(-50%); } #nav-icon span{ display: block; width: 100%; height: 5px; margin-bottom: 3px; background: white; border-radius: 9px; opacity: 1; -webkit-transition: .25s ease-in-out; -moz-transition: .25s ease-in-out; -o-transition: .25s ease-in-out; transition: .25s ease-in-out; } 
 <nav class="navbar"> <div class="logo"> <a href="#"><img src="" alt='Logo'/></a> </div> <div id='tog' class="tog"> <label for="toggle" id='nav-icon'> <div class='icon-container'> <span></span> <span></span> <span></span> </div> </label> </div> </nav> 

如果您对flex没有任何要求,也可以放弃绝对定位。

 .navbar { display: flex; align-items: center;/* vertical-centering */ color: #fff; background-color: #df0024; padding: 1% 0; /* DEMO PURPOSE ONLY to show vertical centering */ transition:0.25s; height: 100px; background-image:linear-gradient(to top, transparent 50%, rgba(255,255,255,0.15) 50%); } .navbar:hover {height:200px;} /* end -- DEMO PURPOSE ONLY to show vertical centering */ nav a { /* demo purpose , useless about centering */ margin: 0 0.5em; color: white; } .tog { cursor: pointer; width: 1.5em; margin-left: auto;/* goes all the way to the right side */ } /*This is the div that contain the burger 3 layers*/ #nav-icon { display: block; transform: rotate(0deg); transition: .5s ease-in-out; cursor: pointer; } /*The style of each of the burger icon 3 layers*/ #nav-icon span { display: block; background: white; margin: 0.25em 0; border-radius: 9px; opacity: 1; height: 0.25em; transform: rotate(0deg); transition: .25s ease-in-out; box-shadow: 1px 1px 1px; } 
 <nav class="navbar"> <div class="logo"> <a href="#"><img src="" alt='Logo' /></a> </div> <a href="#">another link ? </a> <div id='tog' class="tog"> <label for="toggle" id='nav-icon'> <div class='icon-container'> <span></span> <span></span> <span></span> </div> </label> </div> </nav> 

如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div>

[英]How to center vertically <kbd> content inside an <div>

暂无
暂无

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

相关问题 如何在导航菜单中将汉堡图标垂直居中? 如何在 flex div 中垂直居中字体真棒图标? 如何在导航栏中使汉堡图标居中响应? 垂直居中文本,带有div按钮内的图标 如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div> 如何在div中垂直居中按钮和文本 如何在div内垂直居中span / svg 如何在div内垂直居中放置列表? 如何在IE中的div中垂直居中放置文本? 如何在div中垂直居中放置跨度和列表?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM