How to stop Inline-block from taking up more width than child elements

My main (colored in skyblue) element appears to be taking up more width than its child elements take up. It’s display is set to inline-block which makes me curious why it’s width isn’t automatically set to auto. The immediate child elements have a display of inline-block as well but their width’s combined = less than 100% width. What’s going on here? How can I fix this and make the main element only take up as much space as it needs and not the whole 100% width?

/* ///////////////////////////////// IMPORTS /////////////////////////////// */
@import url('https://necolas.github.io/normalize.css/latest/normalize.css');

/* ///////////////////////////////// INITIAL /////////////////////////////// */
* {
  box-sizing: border-box;
}
body {
  background-color: red;
  text-align: center;
}
main {
  display: inline-block;
  width: 90%;
  background-color: skyblue;
  text-align: left;
}
main p {
  margin: 0;
  padding: 10px;
  color: #bbb;
  font-size: 0.9rem;
}
a {
  color: #fff;
  text-decoration: none;
}
a[href="#"] {
  display: inline-block;
  width: 25%;
  margin: 2.2%;
  background-color: #f1f1f1;
  vertical-align: top;
}
img {
  max-width:100%;
}
<html>
  <body> <!-- red -->
    <main> <!-- skyblue -->
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>mmm random text caption.</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Is that a sheep or a goat?</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>A ram maybe? Is that wood tiling behind it?</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Definitly an animal on grass...on wood</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Umm. k.</p>
        </a>
    </main>
  </body>
</html>

Edit: I thought it might be main { width: 90%; } but this seems to force main to take up 100% of the width. Making the issue worse.

Edit 2: I could use flexbox or floats but I’m also curious as to is this standard inline-block behavior? Usually (in my experience) inline-block styled elements have a width of auto and adjust to the size of their content. Not being any larger than they need.

3

Whenever the contents of an inline-block wrap, even once, it will take up the entire available width.

As for the desired behavior, I suppose you are looking for this: …?

* {
  box-sizing: border-box;
}
body {
  background-color: red;
  text-align: center;
}
main {
  display: inline-block;
  margin: 5vw;
  background-color: skyblue;
  text-align: left;
  padding: .8rem;
  overflow: hidden;
}
main p {
  margin: 0;
  padding: 10px;
  color: #bbb;
  font-size: 0.9rem;
}
a {
  color: #fff;
  text-decoration: none;
}
a[href="#"] {
  display: inline-block;
  width: calc(33.3333% - 1.6rem);
  margin: .8rem;
  background-color: #f1f1f1;
  vertical-align: top;
  box-sizing: border-box;
  float: left;
}
img {
  max-width:100%;
}
<link href="https://necolas.github.io/normalize.css/latest/normalize.css" rel="stylesheet"/>
<main> <!-- skyblue -->
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>mmm random text caption.</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Is that a sheep or a goat?</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>A ram maybe? Is that wood tiling behind it?</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Definitly an animal on grass...on wood</p>
        </a>
        <a href="#">
          <div><img src="https://i.imgur.com/7WBWShL.jpg" alt=""></div>
          <p>Umm. k.</p>
        </a>
    </main>

Besides changing margins into rem from %, there is one other change I made to your original code and you might want to understand why:

Normally, you would think having

display:inline-block;
width:25%;
margin: 0;
box-sizing: border-box;

on children should make them wrap in lines of 4, right?

But most often they do not. Here’s why: picture them as big letters in a line of text. Markup has line breaks and elements are placed on next row, for readability. Those line breaks and the tabs at the start of new lines are reduced by the browser into a single space character. And those space chars are not accounted for in CSS. The options are:

a) to suppress the spaces between inline-block elements using html comment blocks:

<parent><!--
  --><child></child><!--
  --><child></child><!--
  --><child></child><!--
  --><child></child><!--
  --><child></child><!--
--></parent>

This will work, they will be 4/row, no extra spaces added. Or…

b) use float:left on the children. If you also want the parent to expand its height (background) to include all the floating children, you should set overflow:hidden on it. Which is what I used in the above example and is the typical solution for floating inline containers.

2

Remove width of main and give fixed width to your child elements not %. Then you will get what you need.

You can’t do it by giving % width to both parent and child element.

main {
  display: inline-block;
  /*width: 90%;*/
  background-color: skyblue;
  text-align: left;
}

a[href="#"] {
   width: 200px;
}

You can use flexbox to resolve the issue of the extra blank space on the right hand side.

Simply change the style of .main:

main {
   display: flex;
   flex-flow: row wrap;
   justify-content: space-around;
   width: 90%;
   background-color: skyblue;
   text-align: left;
}

JsFiddle: https://jsfiddle.net/rgbxr0sx/

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật