简体   繁体   English

将stretch上的项目与父div的中心对齐

[英]align items on stretch to the center of the parent div

I tried to create a menu that stretches to full height on mobile screens. 我尝试在移动屏幕上创建一个延伸到全高的菜单。 For the parent container I use a flexbox with flex-direction column and stretch the items on full screen height. 对于父容器,我使用带有flex-direction列的flexbox,并在全屏高度上拉伸项目。

 $(document).ready(() => { $("#btnMenu").click(() => { toggleMenu(); }); $(".navbarLink").click(() => { if ($("#navbarItems").hasClass("activeNavbar")) { toggleMenu(); } }); }); function toggleMenu() { $("#navbarItems").toggleClass("activeNavbar"); toggleMenuBtn(); } function toggleMenuBtn() { $("#btnMenu").toggleClass("activeMenuBtn"); } 
 body { margin: 0; } .link { text-decoration: none; } #navbar { height: 60px; top: 0; padding-left: 200px; padding-right: 200px; position: sticky; background: #1e222a; } #navbarItems { height: 100%; display: flex; align-items: center; } #logoLink { display: flex; align-items: center; } #navbarItems .navbarItem:not(:first-child) { margin-left: 30px; } .navbarItem { background: #1e222a; } .navbarLink { color: #ffffff; } .navbarLink:hover { color: #3abcf3; } #btnMenuContainer { height: 100%; display: none; } #btnMenu { cursor: pointer; } .menuBtnBar { width: 35px; height: 5px; margin: 6px 0; background-color: #ffffff; transition: 0.4s; } .activeMenuBtn #barTop { transform: rotate(-45deg) translate(-9px, 6px); } .activeMenuBtn #barCenter { opacity: 0; } .activeMenuBtn #barBottom { transform: rotate(45deg) translate(-8px, -8px); } @media(max-width: 1200px) { #navbar { padding-left: 150px; padding-right: 150px; } } @media(max-width: 1100px) { #navbar { padding-left: 0; padding-right: 0; } #btnMenuContainer { display: flex; align-items: center; } #btnMenu { margin-left: 20px; } #navbarItems { margin-left: 0; display: none; } #navbarItems.activeNavbar { display: flex; flex-direction: column; align-items: stretch; height: 100vh; } #logoLink { display: inline-block; } .navbarItem { flex: 1 1 100%; align-items: center; justify-content: center; text-align: center; } #navbarItems .navbarItem:not(:first-child) { margin-left: 0; } #navbarItems .navbarItem:not(:last-child) { border-bottom: 1px solid #676767; } .navbarLink { width: 100%; height: 100%; display: inline-block; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="navbar"> <div id="btnMenuContainer"> <div id="btnMenu"> <div id="barTop" class="menuBtnBar"></div> <div id="barCenter" class="menuBtnBar"></div> <div id="barBottom" class="menuBtnBar"></div> </div> </div> <div id="navbarItems"> <div class="navbarItem"> <a id="logoLink" class="link navbarLink" href="#"> <img class="img" src="https://cdn4.iconfinder.com/data/icons/32x32-free-design-icons/32/Ok.png"> </a> </div> <div class="navbarItem"> <a class="link navbarLink" href="#"> Link 2 </a> </div> <div class="navbarItem"> <a class="link navbarLink" href="#"> Link 3 </a> </div> </div> </div> 

When using align-items: stretch; 使用align-items: stretch; how can I place the links (and the logo) back to center? 如何将链接(和徽标)放回中心?

My second question is what can I use for height: 100vh; 我的第二个问题是我可以用于height: 100vh; instead? 代替? I would like to keep it dynamic so maybe there is a better way than using a constant number? 我想保持它的动态,所以也许有比使用常数更好的方法?

It works fine. 它工作正常。 Check the code snippets and fiddle for further clarification. 检查代码片段和小提琴以获得进一步说明。

Noe all links are center aligned and menu height takes the full height of the space available and there is no scroll any more. Noe所有链接都是中心对齐的,菜单高度占用了可用空间的全部高度,并且不再有滚动。

My suggestion is to use 100vh for the height because it better than applying static value since 100vh is dynamic value. 我的建议是使用100vh作为高度因为它比应用静态值更好,因为100vh是动态值。

https://jsfiddle.net/Sampath_Madhuranga/4uLb6rsw/7/ https://jsfiddle.net/Sampath_Madhuranga/4uLb6rsw/7/

 $(document).ready(() => { $("#btnMenu").click(() => { toggleMenu(); }); $(".navbarLink").click(() => { if ($("#navbarItems").hasClass("activeNavbar")) { toggleMenu(); } }); }); function toggleMenu() { $("#navbarItems").toggleClass("activeNavbar"); toggleMenuBtn(); } function toggleMenuBtn() { $("#btnMenu").toggleClass("activeMenuBtn"); } 
 body { margin: 0; } .link { text-decoration: none; } #navbar { height: 60px; top: 0; padding-left: 200px; padding-right: 200px; position: sticky; background: #1e222a; } #navbarItems { height: 100%; display: flex; align-items: center; } #logoLink { display: flex; align-items: center; } #navbarItems .navbarItem:not(:first-child) { margin-left: 30px; } .navbarItem { background: #1e222a; } .navbarLink { color: #ffffff; } .navbarLink:hover { color: #3abcf3; } #btnMenuContainer { height: 100%; display: none; } #btnMenu { cursor: pointer; } .menuBtnBar { width: 35px; height: 5px; margin: 6px 0; background-color: #ffffff; transition: 0.4s; } .activeMenuBtn #barTop { transform: rotate(-45deg) translate(-9px, 6px); } .activeMenuBtn #barCenter { opacity: 0; } .activeMenuBtn #barBottom { transform: rotate(45deg) translate(-8px, -8px); } @media(max-width: 1200px) { #navbar { padding-left: 150px; padding-right: 150px; } } @media(max-width: 1100px) { #navbar { padding-left: 0; padding-right: 0; } #btnMenuContainer { display: flex; align-items: center; } #btnMenu { margin-left: 20px; } #navbarItems { margin-left: 0; display: none; } #navbarItems.activeNavbar { display: flex; flex-direction: column; align-items: stretch; height: calc( 100vh - 60px); } #logoLink { display: flex; justify-content: center; } .navbarItem { flex: 1 1 100%; align-items: center; justify-content: center; text-align: center; } #navbarItems .navbarItem:not(:first-child) { margin-left: 0; } #navbarItems .navbarItem:not(:last-child) { border-bottom: 1px solid #676767; } .navbarLink { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="navbar"> <div id="btnMenuContainer"> <div id="btnMenu"> <div id="barTop" class="menuBtnBar"></div> <div id="barCenter" class="menuBtnBar"></div> <div id="barBottom" class="menuBtnBar"></div> </div> </div> <div id="navbarItems"> <div class="navbarItem"> <a id="logoLink" class="link navbarLink" href="#"> <img class="img" src="https://cdn4.iconfinder.com/data/icons/32x32-free-design-icons/32/Ok.png"> </a> </div> <div class="navbarItem"> <a class="link navbarLink" href="#"> Link 2 </a> </div> <div class="navbarItem"> <a class="link navbarLink" href="#"> Link 3 </a> </div> </div> </div> 

Thanks. 谢谢。

Let .navbarLink also have a flex layout. .navbarLink也有一个flex布局。

.navbarItem {
    flex: 1 1 100%;
}

.navbarLink {
    height: 100%;
    width: 100%;
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: center;
    text-align: center;
}

By the way, here is a great resource that really helped me when I learned to use flexbox layouts. 顺便说一句,这是一个很好的资源,当我学会使用flexbox布局时,它真的帮助了我。 https://css-tricks.com/snippets/css/a-guide-to-flexbox/ https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Follow this link https://flexboxfroggy.com/ there is a really nice game which will teach you flexbox while playing in no time. 点击此链接https://flexboxfroggy.com/有一个非常好的游戏,可以在玩游戏的同时教你Flexbox After that you will be able to fix your navigation. 之后,您将能够修复导航。

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

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