简体   繁体   English

彼此相邻漂浮3个div,并堆叠在较小的屏幕上

[英]Float 3 divs next to each other and stack on smaller screens

I have searched the forum and tried numerous ways of doing this based on current answers but nothing seems to work how I need it to so any help is GREATLY appreciated as I am pulling my hair out! 我已经搜索了论坛,并根据当前答案尝试了多种方法来执行此操作,但是我似乎并没有采取任何措施,因此,在我拔头发时,我们将不胜感激!

I am working in Joomla 3 and have created a custom html module to display a download with sign up form. 我正在Joomla 3中工作,并创建了一个自定义html模块以显示带有注册表单的下载。

I need to have some text on the left, the sign up module I am calling with Modules Anywhere in the center and an image on the right, so like this: 我需要在左侧添加一些文本,在中间使用Modules Anywhere调用的注册模块,在右侧添加一个图像,如下所示:

Text  |  Module  |  Image

And then on small screens it should stack so like this: 然后在小屏幕上,它应该像这样堆叠:

Text
Module
Image

The code I have which isn't working is: 我有不起作用的代码是:

 <div style="float: left;">Blah blah blah</div> <div>{module Newsletter Signup}</div> <div><img src="images/content/ebook.png" alt="ebook" width="150" /></div> 

I would like to have the spacing as a percentage so it looks nice and even on all screen sizes when in horizontal view. 我想以百分比表示间距,以便在水平查看时甚至在所有屏幕尺寸上都看起来不错。

Thanks in advance for any advice 预先感谢您的任何建议

Try this -> http://jsfiddle.net/tm4trhsw/ 试试这个-> http://jsfiddle.net/tm4trhsw/

 #wrapper { overflow:hidden; } .item { float:left; width:27%; outline:1px solid blue; margin:0 0.5%; padding:2%; } @media only screen and (max-width: 40em) { .item { float:none; width:100%; margin-bottom:5px } } 
 <div id="wrapper"> <div id="text" class="item">TEXT BLA BLA</div> <div id="module" class="item">MODULE BLA BLA</div> <div id="image" class="item">Image BLA BLA</div> </div> 

To have a different layout at a certain breakpoint, you'll need CSS media queries: 要在某个断点处具有不同的布局,您将需要CSS媒体查询:

HTML 的HTML

<div class="row">
  <div class="col">
    blah blah blah
  </div>
  <div class="col">
    {module}
  </div>
  <div class="col alignImgRight">
    <img src="images/content/ebook.png" alt="ebook" width="150" />
  </div>
</div>

CSS 的CSS

.row:after { clear: both; }

.row:before, .row:after {
  content: " ";
  display: table; }

@media (min-width: 768px) {
  .col { 
    float: left;
    width: 33.33333333%; }

  .alignImgRight { text-align: right; }
}

Of course you can change the breakpoint to whatever you want. 当然,您可以将断点更改为所需的任何值。 In this situation the 1/3 width and float of the column will only apply above 768px screen width. 在这种情况下,列的1/3宽度和浮点数仅适用于768px屏幕宽度以上。 Below that, the divs will take their default 100% width with no float (ie stacked). 在此之下,div将采用其默认的100%宽度,没有浮点(即堆叠)。 The .row parent element simply contains all the columns with a clear on it, so they don't all spill out and mess up layouts with the floats. .row父元素只包含所有带有清除.row的列,因此它们不会全部溢出并弄乱浮点数的布局。

I would highly recommend look at Bootstrap as a responsive framework. 我强烈建议您将Bootstrap视为响应框架。 It's an industry standard for handling layouts like this. 这是处理此类布局的行业标准。

Floats can be tricky. 浮动可能很棘手。 You have a couple of other options: 您还有其他几种选择:

1. Display: inline-block and display: block 1.显示:内联块和显示:块

At small screen sizes: 在小屏幕上:

.text, .module, .image {
    display: block;
    width: 100%;
}

Then use a min-width media query from whatever width you want to have them sit side by side. 然后,使用任何宽度的最小宽度媒体查询来让它们并排放置。 The width you choose (here 700px) is whatever works for your design: 您选择的宽度(此处为700px)适合您的设计:

@media (min-width: 700px) { 
    .text, .module, .image {
        display: inline-block;
        width: 30%; /*you'll have to play with this % */
    }
 }

Mozilla Developer Info on Media Queries Mozilla开发人员有关媒体查询的信息

2. Flexbox 2. Flexbox

Setup the parent container of the three sections to display as flex and the children to behave the way you want them. 设置三个部分的父容器以显示为flex,子容器以所需的方式运行。

.parent {
    display: flex;
    flex-direction: row;
    justify-content: center; /*you might prefer space-between. It depends on how it looks*/
}

There are lots of options for flexbox if you're able to use it (that is, not supporting some jurassic browser). 如果可以使用flexbox,则有很多选项(也就是说,不支持某些侏罗纪浏览器)。 Try some generators - there are lots of good ones - and check out CSS-tricks for a great explanation of flexbox. 尝试一些生成器-有很多不错的生成器-并查看CSS技巧以获取有关flexbox的详细说明。 Good luck! 祝好运!

 *{ padding: 0; margin: 0; box-sizing: border-box; } .table{ width: 100%; display: table; } .table-cell{ display: table-cell; vertical-align: middle; border-left: 1px solid #000; width: 33.333%; text-align: center; padding: 15px; background: #f7f7f7; } .table-cell:nth-child(1){ border-left: none; } @media screen and (max-width: 767px){ .table-cell{ width: 100%; border-left: none; display: table-caption; } .table-cell:nth-child(1){ border-left: none; } .table-cell:nth-child(2){ background: #ccc; } } 
 <div class="table"> <div class="table-cell">Text</div> <div class="table-cell">Module</div> <div class="table-cell">Image</div> </div> 

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

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