Nikita Moiseev
Nikita Moiseev

Reputation: 27

Why div containing img does not stretch his parent's div height

I wonder why mainCountainerHeadLogo does not stretch parent div mainCountainerHead height?

If I scale the page, both mainCountainerHeadTitle and mainCountainerHeadMenu stretch mainCountainerHead just fine.

Sorry for my english and thanks in advance!

http://jsfiddle.net/gvcs0r6b/

* {
  margin: 0;
  padding: 0;
}
html, body {
  height: 100%;
}
.mainCountainer {
  min-height: 100%;
  width: 70%;
  margin: 0 auto;
}
.mainCountainerHead {
  background-color: aqua;
  height: auto;
}
.mainCountainerHeadLogo {
  height: 100px;
  width: 20%;
  background-color: blue;
  float: left;
  position: relative;
  overflow: hidden;
}
.mainCountainerHeadLogo img {
  position: absolute;
  width: 100%;
  height: auto;
  top: -50%;
  right: -50%;
  bottom: -50%;
  left: -50%;
  margin: auto
}
.mainCountainerHeadTitle{
  margin-left: 20%;
  width: 80%;
  height: auto;
  text-align: center;
  padding-top: 3%;
}
.mainCountainerHeadMenu{
  margin-left: 20%;
  text-align: center;
  background-color: orange;
  width: 80%;
  height: auto;
  padding-top: 2%;
  text-align: center;
}
.mainLink {
  display: inline-block;
  padding: 5px;
}
.mainLinkButton {
  width: 90px;
  height: 30px;
  background-color: green;
  font-size: 16px;
  border: none;
  color: white;
  padding: 5px;
}
.mainLinkButton:hover {
  background-color: darkgreen;
}
.mainLinkDropdown {
  position: relative;
  display: inline-block;
  padding: 5px;
}
.dropdownContent {
  display: none;
  position: absolute;
  min-height: 30px;
  min-width: 130px;
  text-align: left;
  background-color: #f1f1f1;
  z-index: 10;
}
.dropdownContent a {
  display: block;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
}
.mainLinkDropdown:hover .dropdownContent{
  display: block;
}
.dropdownContent a:hover{
  background-color: #ddd;
}
<div class="mainCountainer">
 <div class="mainCountainerHead">
    <div class="mainCountainerHeadLogo">
      <img src="https://i.ibb.co/cYzWJFM/logo-Copy.jpg" title="logo" />
    </div>
    <div class="mainCountainerHeadTitle">
      <h4>Welcome aboard!</h4>
    </div>
    <div class="mainCountainerHeadMenu">
      <div class="mainLink">
        <button class="mainLinkButton">Main</button>
      </div>
      <div class="mainLinkDropdown">
        <button class="mainLinkButton">Dropdown</button>
        <div class="dropdownContent">
          <a href="">Link 1</a>
          <a href="">Link 2</a>
          <a href="">Link 3</a>
        </div>
      </div>
      <div class="mainLink">
        <button class="mainLinkButton">Contacts</button>
      </div>
    </div>
  </div>
  </div>

Upvotes: 0

Views: 49

Answers (2)

Gerard Reches
Gerard Reches

Reputation: 3154

In answer to your question:

That's because the float property puts the HTML elements out of the normal page flow, and this causes what you're experiencing. Its effect is similar to position: absolute which is to move the element to "a different layer".

How to solve it?

Well... there are a lot of ways to achieve what you want, and almost all of them requires to refactorize your code. Actually, you have a lot of code that makes it difficult to achieve your goal. You should get rid of float and start using other technics like Flexbox.

I could show you a solution if you provide a sketch of the layout you want.

Upvotes: 1

Iqbal
Iqbal

Reputation: 219

change the CSS for img to this

.mainCountainerHeadLogo img {
   width: 100%;
   height: 100%;
   margin: auto
}

Upvotes: 0

Related Questions