如何在div中使用图像而不移动其他元素

时间:2019-02-11 13:08:32

标签: html css css3 flexbox

我正在使用flex-box,并且正在尝试使我的网站具有响应能力。现在,我需要在黄色div内放置一个图像,但是当我这样做时,所有结构都会改变。我尝试玩一下宽度和高度,但可以使它工作。希望你们中的一个能帮助我

:root {
  --main-color: #0077be;
}

body {
  font-family: "Open Sans", sans-serif;
  text-align: right;
}

ul,
li {
  list-style: none;
  padding: 0;
  margin: 0;
}

div {
  border: 1px solid gray;
}

.screen {
  padding: 100px;
  background-color: #ededed;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper {
  width: 1200px;
  height: 450px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
}

.section-title {
  flex-grow: 1;
}

.news-conainer {
  flex-grow: 11;
  display: flex;
  flex-direction: column;
  padding: 17px;
}

.main-news {
  flex-grow: 11;
  display: flex;
  flex-direction: row-reverse;
}

.foot-news {
  flex-grow: 1;
}

.right {
  flex-grow: 7;
  display: flex;
  flex-direction: row-reverse;
  margin-left: 20px;
}

.left {
  flex-grow: 10;
  display: flex;
  flex-direction: column;
}

.img {
  max-height: 100%;
  overflow: hidden;
}

.img img {
  max-width: 100%;
  min-height: 100%;
}

.info {
  flex-grow: 1;
}

.left-news {
  flex-grow: 1;
  display: flex;
  flex-direction: row-reverse;
}

.left-news:nth-child(2) {
  margin: 10px 0;
}

.left-news-img {
  flex-grow: 1.5;
  background-color: yellow;
}

.left-news-info {
  flex-grow: 3;
}
<div class="screen">
      <div class="wrapper">
        <div class="section-title">title</div>
        <div class="news-conainer">
          <div class="main-news">
            <div class="right">
              <div class="img">
                <img src="./new1.JPG" alt="" />
              </div>
              <div class="info">info</div>
            </div>

            <div class="left">
              <div class="left-news">
                <div class="left-news-img">
                <img src="./new1.JPG" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
              <div class="left-news">
                <div class="left-news-img">
                <img src="./new1.JPG" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
              <div class="left-news">
                <div class="left-news-img">
                <img src="./new1.JPG" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
            </div>
          </div>
          <div class="foot-news"></div>
        </div>
      </div>
    </div>

我需要的结果是在黄色div处包含一个标签,而不会破坏其他大小和位置

谢谢!

enter image description here

添加结果

.left-news-img {
max-width: 30%;
}
.left-news-img > img {
  max-width: 100%;
   }

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须使用max-width

.left-news-img {
    max-width: 30%;
}
.left-news-img > img {
    max-width: 100%;
}

:root {
  --main-color: #0077be;
}

body {
  font-family: "Open Sans", sans-serif;
  text-align: right;
}

ul,
li {
  list-style: none;
  padding: 0;
  margin: 0;
}

div {
  border: 1px solid gray;
}

.screen {
  padding: 100px;
  background-color: #ededed;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper {
  width: 1200px;
  height: 450px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
}

.section-title {
  flex-grow: 1;
}

.news-conainer {
  flex-grow: 11;
  display: flex;
  flex-direction: column;
  padding: 17px;
}

.main-news {
  flex-grow: 11;
  display: flex;
  flex-direction: row-reverse;
}

.foot-news {
  flex-grow: 1;
}

.right {
  flex-grow: 7;
  display: flex;
  flex-direction: row-reverse;
  margin-left: 20px;
}

.left {
  flex-grow: 10;
  display: flex;
  flex-direction: column;
}

.img {
  max-height: 100%;
  overflow: hidden;
}

.img img {
  max-width: 100%;
}

.info {
  flex-grow: 1;
}

.left-news {
  flex-grow: 1;
  display: flex;
  flex-direction: row-reverse;
}

.left-news:nth-child(2) {
  margin: 10px 0;
}

.left-news-img {
  flex-grow: 1.5;
  background-color: yellow;
}

.left-news-info {
  flex-grow: 3;
}
.left-news-img {
    max-width: 30%;
}
.left-news-img > img {
    max-width: 100%;
}
.right > .img{
   max-width: 30%;
}
<div class="screen">
      <div class="wrapper">
        <div class="section-title">title</div>
        <div class="news-conainer">
          <div class="main-news">
            <div class="right">
              <div class="img">
                <img src="https://via.placeholder.com/250x130" alt="" />
              </div>
              <div class="info">info</div>
            </div>

            <div class="left">
              <div class="left-news">
                <div class="left-news-img">
                <img src="https://via.placeholder.com/250x130" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
              <div class="left-news">
                <div class="left-news-img">
                <img src="https://via.placeholder.com/250x130" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
              <div class="left-news">
                <div class="left-news-img">
                <img src="./new1.JPG" alt="" />
                </div>
                <div class="left-news-info"></div>
              </div>
            </div>
          </div>
          <div class="foot-news"></div>
        </div>
      </div>
    </div>

更新

对于您的右侧部分,您必须提供一些max-width,例如

.right > .img{
   max-width: 30%;
}

并从height: 100%选择器中删除.img img,请检查上面的演示,因为我已经在那里更新了代码