【问题标题】:Reducing window size moves elements减小窗口大小移动元素
【发布时间】:2014-10-29 17:12:01
【问题描述】:

你好很棒的程序员,

一段时间以来,我一直在努力使用 CSS。我在调整窗口大小时遇到​​问题,我的一些 div 开始在页面下方折叠。 (如图)

之前:

before http://411metrics.com/pics/before.PNG

之后:

before http://411metrics.com/pics/after.PNG

我尝试将各种 div 的最小宽度设置为 100%,还尝试将溢出设置为隐藏。

有人对如何解决这个问题有任何建议吗?

我的 HTML:

<div id="navigation">
    <div id="branding-logo"><img src="/Portal/images/sharktek-logo.png" width="35" height="35"></div>
    <div id="branding">Sharktek Tracking</div>
    <div id="link-wrap">
        <div id="active-nav"><a href="/Portal/dashboard">Dashboard</a></div>
        <a href="/Portal/statistic">Reports</a>
        <a href="/Portal/record">Call Logs</a>
        <a href="/Portal/manage">Manage Campaigns</a>';         
    </div>
    <div id="nav-user">
        Welcome<br>
        <a href="/Portal/account">Account Settings</a>
        <a href="/Portal/auth/logout">Logout</a>
    </div>
</div>
<div id="nav-accent"></div>

我的 CSS:

#navigation {
    z-index:3;
    position: fixed;
    top: 0;
    width: 100%;
    min-width:100%;
    color: #ffffff;
    height: 60px;
    text-align: center;
    /* Adds the transparent background */
    background-color: rgba(22, 29, 37,1);
    color: rgba(1, 172, 237, 1);
}


#navigation a {
    float:left;
    font-size: 14px;
    padding: 25px 25px 0 25px;
    color: white;
    text-decoration: none;
}

#link-wrap {
    float: left;
    overflow: hidden;
    margin-left: 15%;
}

#active-nav{
    z-index: 2;
    float:left;
    color:white;
    height: 60px;
    background: -webkit-linear-gradient(#346c83, rgba(1, 172, 237, 1)); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(#346c83, rgba(1, 172, 237, 1)); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(#346c83, rgba(1, 172, 237, 1)); /* For Firefox 3.6 to 15 */
    background: linear-gradient(#346c83, rgba(1, 172, 237, 1)); /* Standard syntax */
}

#active-nav a:hover {
    color:white;
}

#navigation a:hover {
    color: grey;
} 

#branding-logo {
    margin-top: 15px;
    margin-left: 10px;
    float: left;
}

#branding{
    margin-top: 20px;
    margin-left: 10px;
    font-size:1.4em;
    color: white;
    float: left;
    padding: 0px;
}


#nav-accent {
    z-index:2;
    position: fixed;
    top: 60px;
    width: 100%;
    color: #ffffff;
    height: 2px;
    padding-top: 1px;
    /* Adds shadow to the bottom of the bar */
    -webkit-box-shadow: 0px 0px 8px 0px #000000;
    -moz-box-shadow: 0px 0px 8px 0px #000000;
    box-shadow: 0px 0px 8px 0px #000000;
    /* Adds the transparent background */
    background-color: rgba(1, 172, 237, 0.95);
    color: rgba(1, 172, 237, 1);
}
#nav-user {
    color: white;
    font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
    padding: 15px 30px 0 0;
    font-size: .8em;
    float:right;
}

#nav-user a{
    margin: 0;
    padding: 0 0 0 10px;
    font-size:.8em;
}

【问题讨论】:

  • 你需要在这里放弃一些东西。如果你想要响应,那么你需要为响应行为编写代码,定义在特定大小下会发生什么。很明显,无论如何,在某些时候元素都不适合。在这种情况下……你想发生什么?基于此,您可能会有不同的答案。现在,您可以使用 position:absolute 的元素,它会起作用....直到您看到该元素在调整大小时与其他所有内容重叠
  • 好吧,我没有浏览你的代码,只是看一眼你的问题,我建议你切换到引导网格系统来设计你的布局。我敢肯定,一旦你习惯了它,你就永远不会回头。 getbootstrap.com/css/#grid

标签: html css resize collapse


【解决方案1】:

在我开始理解和应用绝对定位之前,我也遇到过类似的问题。即相对于你所在的 div 定位。

对于绝对定位,必须将父 div 设置为相对定位,然后将内部元素固定到您喜欢的任何一侧,而无需浏览器接管流量控制。

例如在你的情况下,...

#link-wrap {
    position: absolute;
    width: 500px;
    /* ... the rest */
}

...您的导航链接将停止在整个页面上跳转。我在这个小提琴http://jsfiddle.net/xb9cdu34/2/ 中做了一些调整。

【讨论】:

  • 这是一个可怕的建议,无论是针对特定情况还是总体而言。绝对定位不应该被喜欢或讨厌,它必须在需要时使用,在不需要时不惜一切代价避免。当然,这是不需要绝对位置的最重要示例,除非您添加更多代码,包括媒体查询
  • 我的意思是,该解决方案只有在您添加比这更多的代码时才有效,包括所有元素行为以及所有尺寸的所有媒体查询。所以是的,这是更多的代码。只需做一个实验:打开你的小提琴并调整它的大小。我没有说太多,只需将其调整为大约 900px 宽度,看看你的 position:absolute 元素会发生什么,然后你就会明白我的意思(以防你没有注意到,你的元素会重叠一切否则)
  • @Fabio,好的,我知道你现在从哪里来。在我看来,第一步是让页面在网络浏览器中工作。现在,一旦调整页面大小,事情就会严重出错。使用我提供的绝对定位解决方案,它看起来(对我来说)就像一个普通的网页。我的解决方案对于所有情况都不够强大,但我仍然认为它适用于大多数情况。我仍然很想知道最佳解决方案可能是什么样子。感谢您的反馈!
猜你喜欢
  • 1970-01-01
  • 2022-11-21
  • 2017-08-31
  • 2012-07-07
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多