0

I want to place two or more containers inline here is my HTML code:

<div id="containers-place">
      <div style="padding: 16px;">
       <a href="https://google.com/" target="blank" style="text-decoration: none;"><div class="container" style="width: 20%;">
       <img src="picture1.png" width="100%">
       <h4>Picture One</h4>
       <p>As you can see this is picture One</p>
       </div></a>
      </div>
      <div id="idk" style="padding: 16px;">
        <a href="https://google.com/" target="blank" style="text-decoration: none;"><div class="container" style="width: 20%;">
        <img src="picture2.png" width="100%">
        <h4>Picture2</h4>
        <p>As you can see this is picture Two</p>
        </div></a>
      </div>

And the CSS:

.container {
  padding: 16px;
  background-color: #333333;
  color: #fff;
  display: block;
}

I took a screenshot of the browser: screenshoot

vargaking
  • 51
  • 1
  • 7

6 Answers6

0

You are making the containers take whole row by declaring their display css property as block. That's wrong, You have to use something like that:

/* Well, there is the easiest way to make them inline and also correctly positioned */
/* But there are also several ways to make them well-fitting HTML elements using Flexbox, Grid, Float properties and etc. */

display: inline-block; /* it could be only  "inline"  too, but you have to use it when you want your element positioned "absolute" */

So, replace it with your display: block property of .container class

  • it doesn't works, but thanks for the answer – vargaking Apr 02 '20 at 16:50
  • hmm doesn't work? why? Is there any property in parent class that is blocking them to be inline blocks? If it doesn't work, you can use other Grid, Flex/Flexbox systems too =D. WIsh you luck in your projects! – azizbek masharibov Apr 02 '20 at 16:52
0

use flex for this kind of thing:

#container{
display:flex;
}
<div id="container">
      <div style="padding: 16px;">
       <a href="https://google.com/" target="blank" style="text-decoration: none;"><div class="container" style="width: 20%;">
       <img src="picture1.png" width="100%">
       <h4>Picture One</h4>
       <p>As you can see this is picture One</p>
       </div></a>
      </div>
      <div id="idk" style="padding: 16px;">
        <a href="https://google.com/" target="blank" style="text-decoration: none;"><div class="container" style="width: 20%;">
        <img src="picture2.png" width="100%">
        <h4>Picture2</h4>
        <p>As you can see this is picture Two</p>
        </div></a>
      </div>
DCR
  • 14,737
  • 12
  • 52
  • 115
0

You can use flex for that

<div id="containers-place"class='flexrow'>
      <div style="padding: 16px;" class='flexcolumn'>
       <a href="https://google.com/" target="blank" style="text-decoration: none;"><div style="width: 20%;">
       <img src="picture1.png" width="100%">
       <h4>Picture One</h4>
       <p>As you can see this is picture One</p>
       </div></a>
      </div>
      <div id="idk" style="padding: 16px;" class='flexcolumn'>
        <a href="https://google.com/" target="blank" style="text-decoration: none;"><div style="width: 20%;">
        <img src="picture2.png" width="100%">
        <h4>Picture2</h4>
        <p>As you can see this is picture Two</p>
        </div></a>
      </div>

css

.flexrow{
 display: flex;
 flex-direction: row

}
.flexcolumn{
 //you can adjust width if you want
}
Kesav
  • 151
  • 4
0

add display:flex to div with id container-place.

.container {
  padding: 16px;
  background-color: #333333;
  color: #fff;
}
#containers-place {
  display:flex;
  flex-direction:row;
}
<div id="containers-place">
    <div style="padding: 16px;">
        <a href="https://google.com/" target="blank" style="text-decoration: none;">
            <div class="container" style="width: 20%;">
                <img src="picture1.png" width="100%">
                <h4>Picture One</h4>
                <p>As you can see this is picture One</p>
            </div>
        </a>
    </div>
    <div id="idk" style="padding: 16px;">
        <a href="https://google.com/" target="blank" style="text-decoration: none;">
            <div class="container" style="width: 20%;">
                <img src="picture2.png" width="100%">
                <h4>Picture2</h4>
                <p>As you can see this is picture Two</p>
            </div>
        </a>
    </div>
</div>
saitama
  • 699
  • 1
  • 9
  • 21
0

Here is what you need:

I also removed your inline code for container and made a selector class for easy maintenance, you can increase/decrease the width or height as per your need.

#containers-place {
  display: flex;
  flex-direction: row;
}

.container {
  padding: 16px;
  background-color: #333333;
  color: #fff;
  /*Change the width as per your requirement */
  width: 80%;
}
<div id="containers-place">
  <div style="padding:16px;">
    <a href="https://google.com/" target="blank" style="text-decoration: none;">
      <div class="container">
        <img src="http://placekitten.com/301/301" width="100%">
        <h4>Picture One</h4>
        <p>As you can see this is picture One</p>
      </div>
    </a>
  </div>
  <div id="idk" style="padding: 16px;">
    <a href="https://google.com/" target="blank" style="text-decoration: none;">
      <div class="container">
        <img src="http://placekitten.com/301/301" width="100%">
        <h4>Picture2</h4>
        <p>As you can see this is picture Two</p>
      </div>
    </a>
  </div>
</div>

Adding Space-between between items:

justify-content

#containers-place {
  display: flex;
  flex-direction: row;
  width: 800px; /* if you need distance between container then fix this */
  border: 1px solid red;
  justify-content: space-between; /* this will move item */
}

.container {
  padding: 16px;
  background-color: #333333;
  color: #fff;
  width: 200px;
}
<div id="containers-place" class="MainClass">

  <div style="padding:16px;">
    <a href="https://google.com/" target="blank" style="text-decoration: none;">
      <div class="container">
        <img src="http://placekitten.com/301/301" width="100%">
        <h4>Picture One</h4>
        <p>As you can see this is picture One</p>
      </div>
    </a>
  </div>
  <div id="idk" style="padding: 16px;">
    <a href="https://google.com/" target="blank" style="text-decoration: none;">
      <div class="container">
        <img src="http://placekitten.com/301/301" width="100%">
        <h4>Picture2</h4>
        <p>As you can see this is picture Two</p>
      </div>
    </a>
  </div>

</div>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
0

is a block element and it's acting like a block.You should change that behavior of the tag using custom css.

 <div id="containers-place">
          <div class="Main_Container" style="padding: 16px;">
           <a href="https://google.com/" target="blank" style="text-decoration:none;">
              <div class="container" style="width: 100%;">
               <img src="picture1.png" width="100%">
               <h4>Picture One</h4>
            <p>As you can see this is picture One</p>
           </div> 
           </a>
          </div>
          <div class="Main_Container" id="idk" style="padding: 16px;">
         <a href="https://google.com/" target="blank" style="text-decoration:none;"> 
           <div class="container" style="width: 100%;">
            <img src="picture2.png" width="100%">
            <h4>Picture2</h4>
            <p>As you can see this is picture Two</p>
            </div></a>
          </div>

    custom CSS Codes 


.container {
      padding: 16px;
      background-color: #333333;
      color: #ffffff;
      display: inline-block;
}

.Main_Container{
      display: inline-block;
}