简体   繁体   English

使div与响应式设计保持一致

[英]Aligning divs with responsive design

I have 2 divs that I'm trying to align a certain way with it being responsive. 我有2个div,正在尝试以某种方式使其具有响应性。 I basically want the right div to be on top of the left div when the width of the screen reaches a certain width. 当屏幕的宽度达到一定宽度时,我基本上希望右div在左div的顶部。 Right now they are setup splitting the 100% width of the container. 现在,他们正在设置拆分容器的100%宽度。

The left side is the form and the right side is the content. 左边是表格,右边是内容。 I want the content on top of the form to fit the width of a mobile screen. 我希望表单顶部的内容适合移动屏幕的宽度。 I like how the width is setup to be viewed on the desktop/laptop setting. 我喜欢如何在台式机/笔记本电脑设置中查看宽度。 I hope this makes sense. 我希望这是有道理的。

Is there a way to do this with CSS or do I need JQuery for this? 有没有办法用CSS做到这一点,或者我需要JQuery吗? Please let me know if this is unclear. 如果不清楚,请告诉我。

CSS 的CSS

#left {
float:left;
width:65%;
border-radius: 25px;
background-color: #F8F8F8 !important;   
}

#right {
float:right;
width:35%;
background-color: white !important;
padding-left: 40px;
}

If I understand you correctly, this JSFiddle is doing what you'd like it to do. 如果我对您的理解正确,那么此JSFiddle会按照您的意愿进行操作。

http://jsfiddle.net/isherwood/wgbn8c0d/1/ (Thanks to iSherwood for fixing the CSS) http://jsfiddle.net/isherwood/wgbn8c0d/1/ (感谢iSherwood修复了CSS)

Here's a way to do this with only CSS and CSS's @media queries: 这是仅使用CSS和CSS的@media查询来执行此操作的方法:

#left {
    float: left;
    border: 1px solid black;
    width:calc(65% - 26px);
    border-radius: 25px;
    background-color: #F8F8F8 !important;
}
#right {
    border: 1px solid black;
    float: right;
    width:calc(35% - 40px);
    background-color: white !important;
    padding-left: 40px;
}
@media screen and (max-width: 500px) {
    #left {
        display: block;
        width: 100%;
    }
    #right {
        display: block;
        width: 100%;
    }
}

The short answer is no you don't need jQuery and yes you can use CSS. 简短的答案是:否,您不需要jQuery,是的,您可以使用CSS。

What you need is a grid system. 您需要的是网格系统。 The underlying grid will be using media queries to get the screen size, which you can do as well, but a grid system would be better as far as covering edge cases and you wouldn't have to reinvent the wheel. 底层网格将使用媒体查询来获取屏幕大小,您也可以这样做,但是就覆盖边缘情况而言,网格系统会更好,并且您不必重新发明轮子。

A couple of popular grid systems to take a look at: 几个流行的网格系统让您看一下:

In my humble opinion if you are trying to do something simple Skeleton would be the best place to start because it will give you an efficient grid without getting in your way with other opinionated features (like colored buttons). 以我的拙见,如果您想做一些简单的事情,Skeleton将是最好的起点,因为它可以为您提供高效的网格,而不会妨碍您使用其他自带的功能(例如彩色按钮)。

But if you want your CSS framework to define other parts of your style as well feel free to go for Foundation and Bootstrap which are (arguably) more popular. 但是,如果您想让CSS框架定义样式的其他部分,还可以随意使用Foundation和Bootstrap(可以说是更受欢迎)。

Ideally you could accomplish this using media queries to detect at what screen width you want them to stack. 理想情况下,您可以使用媒体查询来完成此操作,以检测要它们堆叠的屏幕宽度。

At that width say it's 768px for instance. 以该宽度为例,例如为768px。 You could then remove the float values and set the widths to 100%. 然后,您可以删除浮点值并将宽度设置为100%。 If you mark up your HTML so that the right section of content is coded before the left then it will just naturally be rendered first above the left side. 如果您对HTML进行标记,以使内容的右侧部分在左侧之前被编码,那么它自然会首先呈现在左侧上方。

 #container { display: flex; } #left { order: 1; width:65%; border-radius: 25px; background-color: #F8F8F8 !important; } #right { order: 2; width:35%; background-color: white !important; padding-left: 40px; } @media only screen and (max-width: 767px) { #left, #right { width: 100%; } #left { order 2; } #right { order: 1; } } 

If you don't have to support IE9 and below you can use display: flex and then just change the order of the items on the breakpoint where you want them to stack 如果不必支持IE9及以下版本,则可以使用display: flex ,然后只需更改要堆叠它们的断点上的项目顺序即可

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

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