简体   繁体   English

如何确保图像和文本具有响应性?

[英]How do I ensure that the images and text are responsive?

I want to have 3 movies per row and for the texts below to be on the centre relative to the image.我希望每行有 3 部电影,并且下面的文本相对于图像位于中心。 Currently, my website looks like this (please ignore the red box, it is just for me to see the size)目前,我的网站是这样的(请忽略红框,仅供我查看大小) 在此处输入图片说明

In addition, when I resize my page, the title and the texts get shifted all over.此外,当我调整页面大小时,标题和文本会全部移动。 How do I prevent this from happening and ensure that everything is responsive?我如何防止这种情况发生并确保一切都响应? 在此处输入图片说明

Codes in PHP file: PHP文件中的代码:

<div class="col-md-3 row movieBox">
            <img src="'.$sub_row["image"].'" class="img-responsive img-thumbnail"/>
            <h4>'.$sub_row["title"].'</h4>
            <p>'.$sub_row["movie_warning"].'</p>
            <p>'.$sub_row["movie_duration"].'</p>
</div>

Codes in CSS: CSS中的代码:

  .movieBox{
  float: left;
  display: flex;
  padding: 50px;
  margin: auto;
  margin-bottom: 10px;
  position: relative;
  border: 1px solid red;
}

/*Responsive layout - makes a two column-layout instead of four columns */
@media screen and (max-width: 900px) {
  .column {
    width: 50%;
  }
}

/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}
  1. You do not need float: left;你不需要float: left; or position: fixed;position: fixed; on either selector.在任一选择器上。

  2. Flex is attempting to fit as many boxes as possible in the available width. Flex 正在尝试在可用宽度中容纳尽可能多的框。 By default it will shrink the elements as much ass possible.默认情况下,它会尽可能地缩小元素。 To fix that you can add a width to your elements and use flex-wrap to make the content wrap.要解决此问题,您可以为元素添加宽度并使用flex-wrap使内容环绕。

Try something like this:尝试这样的事情:

.Row{
  display: flex;
  border: 1px solid red;
  flex-wrap: wrap;
  justify-content: space-between;
}

.Column{
  width: 33%;
  flex-shrink: 0;
  display: flex;
  padding: 50px;
  border: 1px solid red;
}

You should use CSS Grid to get the desired result.您应该使用 CSS Grid 来获得所需的结果。 Firstly, create a three-column layout with:首先,创建一个三列布局:

.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr); /*this will create 3 equal columns*/
    grid-template-rows: auto;
    grid-gap: 1em; /*add only if you want to add some space in between the red boxes*/
}

Adding this, the items inside container class will arrange themselves in rows of threes.添加这个, container类中的项目将按三行排列。

Now, for the responsiveness, use media queries.现在,为了响应能力,使用媒体查询。

@media screen and (max-width: 480px) {
    .container {
        grid-template-columns: 1fr;  /*one-column for mobile devices*/
    }
}

You need two steps: 1- Use flex layout or grid layout.您需要两个步骤: 1- 使用 flex 布局或网格布局。 2- Use media queries. 2- 使用媒体查询。

This simple example may help you CSS Flex Responsive这个简单的例子可以帮助你CSS Flex Responsive

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

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