简体   繁体   English

如何制作响应式div?

[英]How to make a responsive div?

I'm making an application using CSS and div in HTML, it's a restaurant menu, i have a wireframe that i must obey, so i'm using a pixel positioning inside the CSS not a percent ratio, so when opening the website in a maximized state is looks like: 我正在使用CSS和div制作HTML应用程序,它是一个餐馆菜单,我有一个必须遵守的线框,所以我在CSS中使用像素定位而不是百分比,所以当打开网站时最大化状态看起来像:

在此输入图像描述

But when minimizing it of curse it's sizes and positions don't response to that : 但是当最小化它的诅咒时,它的大小和位置不会对此作出反应:

在此输入图像描述

here is the fiddle 这是小提琴

****The Code:** ****编码:**

*CSS: * CSS:

    .buttonClass {
    width: 381px;
    height: auto;
    text-align: center;
    line-height: normal;
    border-radius: 7px;
    z-index: 3;
    background-image: -webkit-linear-gradient(top,#FFF 0%,#91BDD6 100%);
    background-image: -moz-linear-gradient(top, #FFFFFF 0%, #91BDD6 100%);
    background-image: -ms-linear-gradient(top,#FFF,#91BDD6);
    background-image: -o-linear-gradient(top,#FFF,#91BDD6);
    background-image: linear-gradient(top,#FFF,#91BDD6);
    border: solid #91BDD6 5px;
    box-shadow: 0px 0px 0px 5px #fff, 5px 3px 12px #000000;
    position: absolute;
    right:0;
    bottom:0;
}

.totalClass {
    width: 376px;
    height: 30px;
    padding: 10px 10px;
    text-align: center;
    line-height: normal;
    border-radius: 7px;
    z-index: 3;
    background-image: -moz-linear-gradient(top, #FFFFFF 0%, #91BDD6 100%);
    position: absolute;
    right: 0;
    top: 700px;
}

*div: *格:

<div id="confirm" class="buttonClass">
  <div align="center">Confirm</div>
</div>    

<div id="total" class="totalClass">
  <div align="left"></div>
</div>      

The easiest way i can think of is: change the width:381px to max-width:381px and add a width:100%. 我能想到的最简单的方法是:改变宽度:381px到max-width:381px并添加宽度:100%。 In this way if there is enough place for the button it will be 381px wide and, if there is no place it will fit the given width. 这样,如果有足够的按钮位置,它将是381px宽,如果没有地方,它将适合给定的宽度。

The CSS code would be: CSS代码将是:

    .buttonClass {
    max-width: 381px;
    width:100%;
    height: auto;
    text-align: center;
    line-height: normal;
    border-radius: 7px;
    z-index: 3;
    background-image: -webkit-linear-gradient(top, #FFF 0%, #91BDD6 100%);
    background-image: -moz-linear-gradient(top, #FFFFFF 0%, #91BDD6 100%);
    background-image: -ms-linear-gradient(top, #FFF, #91BDD6);
    background-image: -o-linear-gradient(top, #FFF, #91BDD6);
    background-image: linear-gradient(top, #FFF, #91BDD6);
    border: solid #91BDD6 5px;
    box-shadow: 0px 0px 0px 5px #fff, 5px 3px 12px #000000;
    position: absolute;
    right:0;
    bottom:0;
}
.totalClass {
    width: 376px;
    height: 30px;
    padding: 10px 10px;
    text-align: center;
    line-height: normal;
    border-radius: 7px;
    z-index: 3;
    background-image: -moz-linear-gradient(top, #FFFFFF 0%, #91BDD6 100%);
    position: absolute;
    right: 0;
    top: 700px;
}

when you want to change a width or a height onclick="" then you might as well use javascript to change the dimensions. 如果你想改变宽度或高度onclick =“”那么你也可以使用javascript来改变尺寸。 Javascript can also change the positions of objects - Look into it. Javascript还可以更改对象的位置 - 查看它。

HTML HTML

<div id="sidebar"></div>
<button onclick="hide_sidebar()" />

CSS CSS

#sidebar {
width:200px;
height:100%;
float:right;
margin:0 0 0 0;
transition:0.25s
}

JS JS

function hide_sidebar() {
document.getElementById("sidebar").style.width="0";
}

Its not a big issue just give that div in pixel and put an ID for that button and give that button width 100% 它不是一个大问题只是给像素的div,并为该按钮添加一个ID,并给该按钮宽度100%

ex: 例如:

button div id
{
width:20px;
height:20px;
}

#button
{
width:100%;
height:20px;
}

then continue enjoy your coding 然后继续享受你的编码

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

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