Yamaha32088 Yamaha32088 - 6 months ago 25
CSS Question

Expand absolute position div height to size of content inside

I am having trouble getting a div that is positioned absolutely to expand to the size of the content inside of it. I have the code from https://css-tricks.com/functional-css-tabs-revisited/ and a fiddle here https://jsfiddle.net/76360pfv/

As you can see in the fiddle the pictures of cats are taller than the tab content area so they get cutoff. I basically want them to expand to the different heights of the images with some padding on the bottom.

I have tried to set the

.content
to
height:100%
and I have followed answers similar to my problem and none of them are accomplishing my end goal. Maybe there is a better way to do it or I am missing something. Hopefully someone here can help me out. Thanks in advance.

below is my code:

<div class="tabs">

<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>

<div class="content">
<img src="http://placekitten.com/300/400">
</div>
</div>

<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>

<div class="content">
<img src="http://placekitten.com/300/500">
</div>
</div>

<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>

<div class="content">
<img src="http://placekitten.com/300/600">
</div>
</div>

</div>

.tabs {
position: relative;
min-height: 200px; /* This part sucks */
clear: both;
margin: 25px 0;
}
.tab {
float: left;
}
.tab label {
background: #eee;
padding: 10px;
border: 1px solid #ccc;
margin-left: -1px;
position: relative;
left: 1px;
}
.tab [type=radio] {
display: none;
}
.content {
position: absolute;
top: 28px;
left: 0;
background: white;
right: 0;
bottom: 0;
padding: 20px;
border: 1px solid #ccc;
overflow: hidden;
}
[type=radio]:checked ~ label {
background: white;
border-bottom: 1px solid white;
z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
z-index: 1;
}

Answer

Try the following solution (https://jsfiddle.net/76360pfv/3/):

.tabs {
  position: relative;   
  min-height: 200px; /* This part sucks */
  clear: both;
  margin: 25px 0;
}
.tab {
  float: left;
}
.tab label {
  background: #eee; 
  padding: 10px; 
  border: 1px solid #ccc; 
  margin-left: -1px; 
  position: relative;
  left: 1px; 
}
.tab [type=radio] {
  display: none;   
}
.content {
  position: absolute;
  top: 28px;
  left: 0;
  background: white;
  right: 0;
  padding: 20px;
  border: 1px solid #ccc; 
  overflow: hidden;
}
[type=radio]:checked ~ label {
  background: white;
  border-bottom: 1px solid white;
  z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
  z-index: 1;
}
[type=radio]:not(:checked) ~ label ~ .content {
  display:none;
  visibility:hidden;
}
<div class="tabs">
  <div class="tab">
    <input type="radio" id="tab-1" name="tab-group-1" checked>
    <label for="tab-1">Tab One</label>

    <div class="content">
      <img src="http://placekitten.com/300/400">
    </div> 
  </div>
  <div class="tab">
    <input type="radio" id="tab-2" name="tab-group-1">
    <label for="tab-2">Tab Two</label>

    <div class="content">
      <img src="http://placekitten.com/300/500">
    </div> 
  </div>
  <div class="tab">
    <input type="radio" id="tab-3" name="tab-group-1">
    <label for="tab-3">Tab Three</label>

    <div class="content">
      <img src="http://placekitten.com/300/600">
    </div> 
  </div>
</div>

Comments