热门标签 | 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.

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


推荐阅读
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 颜色迁移(reinhard VS welsh)
    不要谈什么天分,运气,你需要的是一个截稿日,以及一个不交稿就能打爆你狗头的人,然后你就会被自己的才华吓到。------ ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
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社区 版权所有