如何在div右侧显示图像而不破坏flex布局?

时间:2018-06-22 02:11:31

标签: html css css3 flexbox

如何在div框的右侧插入图像而不破坏div中其余元素的弹性布局。这是参考图片:enter image description here

我想在右侧有空白处(白色)的位置显示图像,但是不确定如何执行此操作。

HTML

git log master..origin/master --oneline
git log origin/master..master --oneline

CSS

<div class="main-content">
        <div class="main-box">
          <div class="kills-container">
            <h1>5600</h1>
          </div>
          <div class="label-container">
            <p>Kills</p>
          </div>
        </div>
      </div>

谢谢! :)

2 个答案:

答案 0 :(得分:1)

.main-content {
  width: 100%;
}

img {
  float: right;
    padding-left: 20%;
    position: relative;
    top: 0;
    margin-top: -100%;
    height: 80px;
}

.second-div {
  display : flex;
  flex-direction: row;
  padding-left: 40%;
  height: 10px;
}
.main-box {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  background-color: white;
  height: 120px;
  width: 23%;
  box-shadow: 0 2px 4px 0 rgba(0,0,0,0.1);
}

.kills-container {
  display: flex;
  flex: 1;
  justify-content: flex-end;
  background-color: red;
  width: 50%;
}

.kills-container h1 {
  margin: 0;
  position: relative;
  right: 10%;
  align-self: flex-end;
  font-weight: normal;
}

.label-container {
  display: flex;
  flex: 2;
  justify-content: flex-end;
  width: 50%;
  background-color: green;
}

.label-container p {
  margin: 0;
  position: relative;
  right: 12%;
  top: 20%;
  align-self: flex-start;
  font-weight: lighter;
}
<div class="main-content">
   <div class="main-box">
       <div class="kills-container">
          <h1>5600</h1>
        </div>
        <div class="label-container">
          <p>Kills</p>
        </div>
        <div class="second-div">
      <img src="http://s7.orientaltrading.com/is/image/OrientalTrading/13577317?$PDP_VIEWER_IMAGE$" />
    </div>
    </div>
    
</div>

答案 1 :(得分:1)

您要在主机箱中有两个div,每一侧各有一个div,然后使用flex-row将其放置在主机箱中。

HTML

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Recipe Book</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
          aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse " id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto header_nav">
      <li class="nav-item ">
        <a class="nav-link" href="#">Recipes </a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Shopping List</a>
      </li>
      <li class="nav-item dropdown float-right">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
           aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a href="#">Save Data</a>
          <a href="#">Fetch Data</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

CSS

<div class="main-content">
    <div class="main-box">
        <div class="left-side">
            <div class="kills-container">
                <h1>5600</h1>
            </div>
            <div class="label-container">
                <p>Kills</p>
            </div>
        <div class="right-side">
            <img src="yourimage"
        </div>
    </div>
</div>