热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CSS规则,如果内容不适合,则切换内容-CSSrulestoswitchcontentifitdoesnotfit

Havingamenulikethis(emphasizedbyaredrectangle)有这样的菜单(用红色矩形强调)……howcanI(inpureC

Having a menu like this (emphasized by a red rectangle)...

有这样的菜单(用红色矩形强调)……

enter image description here

how can I (in pure CSS) make this responsive so that, if there is not enough width available, the menu turns into a drop-down list (or anyway something smaller).

我如何(在纯CSS中)使这个响应,以便,如果没有足够的宽度,菜单会变成一个下拉列表(或者更小的东西)。

The question is not about implementing the drop-down list itself, but how to switch from one content to the other depending on available space.

问题不是如何实现下拉列表本身,而是如何根据可用空间从一个内容切换到另一个内容。

I know this is rather simple when using @media max-width queries, but the problem is that I do not know the actual with of the menu items at "design time" - especially because the text gets translated and/or changed, leading to different widths depending on the actual language displayed.

我知道这在使用@media max-width查询时非常简单,但问题是我不知道“设计时”菜单项的实际含义——特别是因为文本被翻译和/或更改,导致根据实际显示的语言不同的宽度。

Perhaps, there is some CSS trick that makes a whole "text" line / content disappear if it does not fit the parent container?

也许,如果不适合父容器,会有一些CSS技巧使整个“文本”行/内容消失?

2 个解决方案

#1


2  

Here's a solution I've just come up with that should do the job. I added some style to make the structure more evident but it's not pixel perfect, you'll have to take care of that. Run the snippet in full screen and resize the window to see it in action.

这是我刚刚想出的一个解决办法。我添加了一些样式以使结构更明显,但它不是像素完美的,你需要注意这点。在全屏中运行代码片段并调整窗口大小以查看它的运行情况。

.table-row{
    display: table-row;
}
.table-cell{
    display: table-cell;
    vertical-align: middle;
    white-space: nowrap;
}

.wrapper{
   height:75px; /*the height of your menu, it's critical to define this value equal to the horizontal menu height*/
   overflow:hidden; /*this will hide the horizontal menu when the screen width is too small*/
}

.top_nav{
   padding-right:120px; /*allow space for right section*/
   background-color: green;
   color:white;
}
.top_nav_background{ /* this serves as a background for the rest of the structure, pay attention if you have other z-indexed elements */
   background-color:green; 
   height:85px; 
   width:100%; 
   position:absolute; 
   top:0px; 
   z-index:-1;
}
.floating-box {
    height: 55px;
    padding:10px;
    float: left;
    border: 1px solid #73AD21;
    background-color:green;
}
.h-menu{
    height: 75px;
    float: left;
    min-width: 150px;
    background-color:yellow;
}
.h-menu-item{
   height: 55px;
   padding:10px;
   border: 1px solid #73AD21;
}
.v-menu{
    margin-top:20px;
    height: 20px;
    background-color:red;
}
.right-items{
   position:absolute; 
   right:20px; 
   top:20px; 
   color:white;
}






Left section.
v-menu
Right section.

#2


1  

In one project I did I came up with a solution where you show part or all of the menu and only show it as a dropdown/side-menu when the screen gets smaller.

在我做的一个项目中,我提出了一个解决方案,你可以显示部分或全部菜单,当屏幕变小时,只显示为下拉/侧菜单。

The sub-menu is optional and you can just use the main menu for your effect.

子菜单是可选的,你只需要使用主菜单就可以了。

http://codepen.io/anon/pen/LRJoEB

http://codepen.io/anon/pen/LRJoEB


Just change the bits and parts to fit your needs, should you get stuck just leave a comment ;)

只要把你需要的部分和部分改变一下,你就可以留下评论了;

edit

Just realised you don't want to be using any media queries. I'll see if I can come up with something in that direction, not off the top of my head.

刚意识到你不想使用任何媒体查询。我看看能不能从那个方向想出点什么,而不是从我的头顶上。


推荐阅读
author-avatar
漂浮胖_大卍宝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有