Omar Dajani
Omar Dajani

Reputation: 378

How to display image on the right side of div without ruining flex layout?

How can I insert an image on the right hand side of my div box without ruining my flex layout for the rest of the elements in the div. Here is a reference picture: enter image description here

I want to display an image on the right hand side where there is an empty place (the white) but am unsure on how to do this.

HTML

<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>

CSS

.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: 1;
  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;
}

Thank you! :)

Upvotes: 2

Views: 3601

Answers (2)

Jolanda
Jolanda

Reputation: 173

You want to have two divs inside main-box, one for each side, and then position them using flex-row for the main-box.

HTML

<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>

CSS

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

.left-side {
    display: flex;
    flex-direction: column;
}

right-sire {
    display: flex;
    flex-direction: column;
}

Upvotes: 2

Flutterian
Flutterian

Reputation: 1781

.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>

Upvotes: 1

Related Questions