html - 使用媒体查询修复移动设备的 div 位置

标签 html css media-queries

嘿,我在移动端遇到了问题。那是我的网站:dev site

今天我做了一个带有标志的下拉菜单,这样我就可以切换到不同的语言。问题是移动设备的下拉菜单位于汉堡包导航栏下方...我想过用媒体查询解决它(因为它只发生在移动设备上)对任何人有帮助吗?

代码如下:

            <div class="navbar-header">
                <button type="button" id="navbar-toggle" class="navbar-toggle" data-toggle="collapseX" data-target=".navbar-responsive-collapseX"
                style="margin-top: 0px;">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>


  <!--try for flag dropdown menu-->

<!--position the dropdown in it's place in the navbar-->
<div class="image-dropdown" id="image-dropdown" style=" float: right; position: relative; top: 30px;width: 45px;">

<a href="http://www.uprightpose.com/">
<input checked="checked" type="radio" id="line1" name="line-style" value="1" onclick='window.location="http://www.uprightpose.com/";' />
<!--hyperlink to the language page-->
<label for="line1"></label>
</a>

<a href="http://www.uprightpose.com/home-es-new/">
<input type="radio" id="line2" name="line-style" value="2" onclick='window.location="http://www.uprightpose.com/home-es-new/";' />
<label for="line2"></label>
</a>
</div>
<!--end of flag dropdown menu-->

这就是 CSS:

<style>
#image-dropdown {
    /*style the "box" in its minimzed state*/
    width:80px; height:50px; overflow:hidden;
    /*animate collapsing the dropdown from open to closed state (v. fast)*/
    -moz-transition: height 0.1s; 
    -webkit-transition: height 0.1s; 
    -ms-transition: height 0.1s;  
    -o-transition: height 0.1s;  
    transition: height 0.1s;
}
#image-dropdown:hover {
    height:200px; /*overflow-y:scroll;*/
    /*nice and smooth expand - speed this up at your preference or remove animation altogether*/
    -moz-transition: height 0.5s; 
    -webkit-transition: height 0.5s; 
    -ms-transition: height 0.5s;  
    -o-transition: height 0.5s;  
    transition: height 0.5s;
}
#image-dropdown input {
    /*hide the nasty default radio buttons. like, completely!*/
    position:absolute;top:0;left:0;opacity:0;
}

#image-dropdown label[for="line1"] {
    /*style the labels to look like dropdown options, kinda*/
    display:none; margin:2px; height:46px; opacity:0.2; 
    /*setting correct backgrounds - add additional rules with selectors based on "for" attribute, something like label[for=line2]{background-image:...}*/
    background:url("https://lipis.github.io/flag-icon-css/flags/4x3/gb.svg");
    background-size: 40px;
    background-repeat: no-repeat;
    border-radius: 7px;
    width: 40px;
    height: 30px;
    }
    #image-dropdown label[for="line2"] {
    /*style the labels to look like dropdown options, kinda*/
    display:none; margin:2px; height:46px; opacity:0.7; 
    /*setting correct backgrounds - add additional rules with selectors based on "for" attribute*/
    background:url("https://lipis.github.io/flag-icon-css/flags/4x3/es.svg");
    background-size: 40px;
    background-repeat: no-repeat;
    border-radius: 7px;
    width: 40px;
    height: 30px;
    }   
#image-dropdown:hover label{
    /*this is how labels render in the "expanded" state. we want to see only the selected radio button in the collapsed menu, and all of them when expanded*/
    display:block;
}
#image-dropdown label:hover {
    opacity:0.5;
}
#image-dropdown input:checked + label {
    /*tricky! labels immediately following a checked radio button (with our markup they are semantically related) should be fully opaque regardless of hover, and they should always be visible (i.e. even in the collapsed menu*/
    opacity:1 !important; display:block;
}

/*pfft, nothing as cool here, just the value trace*/
#trace {margin:0 0 20px;}
</style>

最佳答案

我会尝试在两个元素上使用 float:right 将它们“粘在一起”——将它们放在同一个容器下。请注意我所做的更改(它是用内联样式编写的,因为我只能通过 DEV 工具修改它)。

<div class="image-dropdown" id="image-dropdown" style="float: right; position: relative; top: 30px; width: 45px;">

  <a href="http://www.uprightpose.com/">
    <input checked="checked" type="radio" id="line1" name="line-style" value="1" onclick="window.location=&quot;http://www.uprightpose.com/&quot;;">
    <!--hyperlink to the language page-->
    <label for="line1" style=""></label>
  </a>

  <a href="http://www.uprightpose.com/home-es-new/">
    <input type="radio" id="line2" name="line-style" value="2" onclick="window.location=&quot;http://www.uprightpose.com/home-es-new/&quot;;">
    <label for="line2"></label>
  </a>
</div>

<div class="navbar-header" style="display: inline-block;float: right;">
    <button type="button" id="navbar-toggle" class="navbar-toggle" data-toggle="collapseX" data-target=".navbar-responsive-collapseX" style="margin-top: 0px; position: relative;top: 30px;">
       <span class="icon-bar"></span>
       <span class="icon-bar"></span>
       <span class="icon-bar"></span>
    </button>
</div>

我认为对于宽度较短的屏幕,结果是您所需要的(它们彼此相邻对齐):

enter image description here

关于html - 使用媒体查询修复移动设备的 div 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698635/

相关文章:

html - 如何让 Bootstrap Carousel 100% 适合屏幕?

jquery - 获取隐藏的单选按钮值并使用ajax插入表单

css - 为什么我无法在 Firefox 中对生成的内容进行绝对定位?

html - 内联 block 元素换行时的CSS,父包装器不适合新宽度

javascript - 禁用 Iframe 中的滚动

CSS @media 打印计数器(页面)问题

html - 为什么我的 HTML 文档总是加载我的两个外部样式表的最后一次出现?

html - CSS 媒体查询正确用法

javascript - CSS JS 显示无改变 div

javascript - 如何在 Html/css 中在新窗口(而不是新标签页)中打开链接