How to ensure three DIV are the same height regardless of content - html

<div class="section group">
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2790_image" class="imgArtThumb" title="" alt="" src="artOne.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Care on the Fast Track</div>
<div class="textHolderBottom">The National Cancer Institute</div>
</div>
</div>
</div>
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2792_image" class="imgArtThumb" title=" alt="" src="artThree.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Stay Connected</div>
<div class="textHolderBottom">tool for interacting with your provider and following your healthcare</div>
</div>
</div>
</div>
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2791_image" class="imgArtThumb" title="" alt="" src="artTwo.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Know When Antibiotics Work and When They Don't</div>
<div class="textHolderBottom">If you or your child has a virus, antibiotics will not help you or him/her</div>
</div>
</div>
</div>
</div>
CSS:
.imgArtThumb
{
max-width: 100%;
height: auto;
}
.imgHolder
{
float: left;
display: inline-block;
width: 43%;
padding-right: 2%;
height: 100%;
}
.textHolder
{
float: left;
display: inline-block;
width: 55%;
height: 100%;
}
.textHolderTop
{
width: 100%;
height: 48%;
text-align: left;
padding-bottom: 2%;
overflow: hidden;
}
.textHolderBottom
{
width: 100%;
height: 48%;
overflow: hidden;
text-align: left;
padding-bottom: 2%;
}
.test2n
{
text-align: left;
height: 95%;
width: 95%;
padding: 2%;
overflow: hidden;
}
.articleTeaserBoxColor
{
box-shadow: inset 0 -1px 1px rgba(0,0,0,0.5), inset 0 1px 1px rgba(238,146,85,1);
}
/* COLUMN SETUP */
.col {
display: block;
/*float:left;*/
display: inline-block;
margin: 1% 0 1% 0;
}
.col:first-child {
margin-left: 0;
}
.span_1_of_3 {
width: 32.2%;
}
/* GROUPING */
.group:before, .group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1; /* For IE 6/7 */
}
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
height: auto;
overflow: auto;
text-align: center;
width: 100%;
}
displays this:
How can I modify my CSS to ensure the three boxes (the one with the box shadow) height matches that of the box with the most text (The middle box should be the same height, but because of content it is shorter).
Any content within a box, which is less than the box with the most text, will have blank space, which is fine.
This is what I would like it to be:

There are several ways and methods, but here's a quick one:
.col {
display: table-cell;
vertical-align: top:
}
Demo http://jsfiddle.net/Lg9y9s93/1

Are Flexbox or jQuery possibilities? As others have mentioned the only pure CSS way (at the moment) is via table-cell which I'm not a huge fan of.
If jQuery is possible there's a fairly simple script I use to make heights match:
CSS:
.item{
float: left;
width: 280px;
}
jQuery:
// Set 'x' number of items to the tallest height
var tallestBox = 0;
$(".item").each(function() {
var divHeight = $(this).height();
if (divHeight > tallestBox){
tallestBox = divHeight;
}
});
// Apply height & add total vertical padding
$(".item").css("height", tallestBox + 30);
Or if Flexbox is possible (modern browsers) this is crazy easy now:
CSS:
.contain{
display: flex;
flex-direction: row;
}

You can use pseudo table layout: http://jsfiddle.net/8rvdkyke/
.content {
display: table;
width: 400px;
}
.content > div {
display: table-cell;
height: 100%;
vertical-align: top;
}
This listing of code is emulating behavior of table layout causing all children div's to be the same height. You can tweak the sizes and other styles for your needs.
UPD: For responsive layout out, you can switch between table-cell and table-row to make them arranged horizontally (table-cell) and vertically (table-row): http://jsfiddle.net/8rvdkyke/1/

This is how I do this (as of this date)
http://jsfiddle.net/sheriffderek/ae2sawnn/
HTML
<section class="your-section">
<ul class="block-list">
<li class="block">
<a href="#">
<div class="image-w">
<img src="http://placehold.it/1600x900" alt="your-image" />
</div>
<div class="text-w">
<p><strong>Might as well have the whole thing a link</strong> and not just that little bitty part of a sentence... etc... etc...</p>
</div>
</a>
</li>
<li class="block">
{{ ... }}
</li>
<li class="block">
{{ ... }}
</li>
</ul>
</section> <!-- .your-section -->
CSS
* {
box-sizing: border-box;
}
body {
margin: 0;
}
a {
text-decoration: none;
color: inherit;
}
.your-section {
width: 100%;
float: left;
}
.block-list {
width: 100%;
float: left;
list-style: none;
margin: 0; padding: 0;
}
.block {
width: 100%;
float: left;
padding: .5rem;
margin-bottom: 1rem;
border: 1px solid red;
}
.block a {
display: block;
}
.image-w {
width: 100%;
float: left;
}
.image-w img {
display: block;
width: 100%;
height: auto;
}
.text-w {
width: 100%;
float: left;
}
#media (min-width: 600px) {
.block {
width: 33%;
margin-bottom: 0;
}
} /* end break-point 1 */
JS
// resources
// http://stackoverflow.com/questions/14167277/equal-height-rows-for-fluid-width-elements
// #popnoodles 's answer on S.O.
$.fn.extend({
equalHeights: function(){
var top = 0;
var row = [];
var classname = ('equalHeights'+Math.random()).replace('.','');
$(this).each(function(){
var thisTop = $(this).offset().top;
if ( thisTop > top ) {
$('.'+classname).removeClass(classname);
top = thisTop;
}
$(this).addClass(classname);
$(this).outerHeight('auto');
var h = (Math.max.apply(null, $('.'+classname).map(function(){ return $(this).outerHeight(); }).get()));
$('.'+classname).outerHeight(h);
}).removeClass(classname);
}
});
$(function(){ // call the function
$(window).resize(function() {
// on resize... but also... trigger that once by default right off the bat
$('.block').equalHeights();
}).trigger('resize');
});

Related

How to hide elements out of div bounds

At the beginning I want to say that I just study frontend so I need to help with something.
I want to prepare some tab slider.
I have something in left and right so my tab slider will be on center.
My tab slider have two buttons: < and > (slide to left; slide to right).
In my tab slider are a lot of elements which sum of widths exceeds a max-width of parent div (it has "content" class).
I want to hide all elements which are out of parent div bounds so I try to use
overflow: hidden;
but unfortunately those elements fell to second row.
div.menu {
width: 100%;
display: block;
float: left;
}
div.content {
display: inline-block;
width: 50%;
max-width: 50%;
background-color: lightgray;
max-height: 40px;
overflow: hidden;
}
div.left,
div.right {
display: inline-block;
width: 24%;
background-color: green;
}
div.flex {
display: flex;
}
ul,
li {
display: inline-block;
}
li {
background-color: yellow;
width: 30px;
}
<div class="menu">
<div class="left">something in left</div>
<div class="content">
<div class="flex">
<i><</i>
<ul>
<li>i-1</li>
<li>i-2</li>
<li>i-3</li>
<li>i-4</li>
<li>i-5</li>
<li>i-6</li>
<li>i-7</li>
<li>i-8</li>
<li>i-9</li>
<li>i-10</li>
<li>i-11</li>
<li>i-12</li>
<li>i-13</li>
<li>i-14</li>
<li>i-15</li>
<li>i-16</li>
</ul>
<i>></i>
</div>
</div>
<div class="right">something in right</div>
</div>
What I'm doing wrong?
I expect that if I use overflow and max-height my exceeds elements will be hide (on right - on one row) and not will be fall to second row.
There is my example.
Will you help me with that?
You need to use white-space: nowrap in .flex so that all items remains in the same line
div.menu {
width: 100%;
display: block;
float: left;
}
div.content {
display: inline-block;
width: 50%;
max-width: 50%;
background-color: lightgray;
max-height: 40px;
overflow: hidden;
}
div.left,
div.right {
display: inline-block;
width: 24%;
background-color: green;
}
div.flex {
display: flex;
white-space: nowrap;
position: relative;
}
ul {
padding-left: 10px;
padding-right: 10px;
}
ul,
li {
display: inline-block;
}
li {
background-color: yellow;
width: 30px;
}
.flex i {
position: absolute;
top: 10px;
}
.flex i.prev {
left: 0;
}
.flex i.next {
right: 0;
}
<div class="menu">
<div class="left">something in left</div>
<div class="content">
<div class="flex">
<i class="prev"><</i>
<ul>
<li>i-1</li>
<li>i-2</li>
<li>i-3</li>
<li>i-4</li>
<li>i-5</li>
<li>i-6</li>
<li>i-7</li>
<li>i-8</li>
<li>i-9</li>
<li>i-10</li>
<li>i-11</li>
<li>i-12</li>
</ul>
<i class="next">></i>
</div>
</div>
<div class="right">something in right</div>
</div>

How do I get a float left image and float right text aligned inside a box?

I am trying to create a box with an image on the left and text (title, price & description) aligned on the right. The problem is, the text is always displayed outside the box. What am I doing wrong here?
.photo {
width: 100%
}
.menu__item {
width: 100%;
border: 1px solid #c4c4c4;
display: block;
}
.menu__item__photo {
width: 40%;
padding-right: 16px;
display: block;
}
.menu__item__info__photo {
width: 60%;
display: block;
float: right;
}
.menu__item__info__title {
float: left;
width: 78%;
}
.menu__item__info__price {
float: right;
width: 21%;
text-align: right;
}
<div class="menu__item">
<div class="menu__item__photo">
<img src="http://placehold.it/350x150" class="photo">
</div>
<div class="menu__item__info__photo">
<div class="menu__item__info__title">Product Title Here</div>
<div class="menu__item__info__price">$9.99</div>
<div class="menu__item__info__description">Description here..</div>
</div>
</div>
Here is a fiddle: https://jsfiddle.net/pxanzefe/
You can float your left item too:
Float the .menu__item__photo item and add box-sizing to include the padding inside the 40% width.
Use a clearfix method on your container.
.photo {
width: 100%
}
.menu__item {
width: 100%;
border: 1px solid #c4c4c4;
display: block;
}
.menu__item:after {
content: "";
display: table;
clear: both;
}
.menu__item__photo {
width: 40%;
padding-right: 16px;
display: block;
float: left;
box-sizing: border-box;
}
.menu__item__info__photo {
width: 60%;
display: block;
float: right;
}
.menu__item__info__title {
float: left;
width: 78%;
}
.menu__item__info__price {
float: right;
width: 21%;
text-align: right;
}
<div class="menu__item">
<div class="menu__item__photo">
<img src="http://placehold.it/350x150" class="photo">
</div>
<div class="menu__item__info__photo">
<div class="menu__item__info__title">Product Title Here</div>
<div class="menu__item__info__price">$9.99</div>
<div class="menu__item__info__description">Description here..</div>
</div>
</div>
If you just want the text contained within the box, add overflow: auto; to the containing div:
.photo {
width: 100%
}
.menu__item {
width: 100%;
border: 1px solid #c4c4c4;
display: block;
overflow:auto;
}
.menu__item__photo {
width: 40%;
padding-right: 16px;
display: block;
}
.menu__item__info__photo {
width: 60%;
display: block;
float: right;
}
.menu__item__info__title {
float: left;
width: 78%;
}
.menu__item__info__price {
float: right;
width: 21%;
text-align: right;
}
<div class="menu__item">
<div class="menu__item__photo">
<img src="http://placehold.it/350x150" class="photo">
</div>
<div class="menu__item__info__photo">
<div class="menu__item__info__title">Product Title Here</div>
<div class="menu__item__info__price">$9.99</div>
<div class="menu__item__info__description">Description here..</div>
</div>
</div>
When you float elements they're removed from the flow of the document and adding the overflow rule restores the behavior you're after.

CSS Put all floated elements in middle of main wrapper DIV

I am trying to align my .homepagewebsitefeatures DIVs so they show in the center of its wrapper #homepagewebsitefeatures.
I have tried margin-left: auto; and margin-right: auto;, but without much luck.
Here is my code:
.homepagewebsitefeaturescontent {
float: left;
margin-left: 15px;
width: auto;
font-size: 20px;
}
.homepagewebsitefeaturesimage {
float: left;
width: auto;
display: flex;
}
.homepagewebsitefeatures {
display: flex;
align-items: center;
}
#homepagewebsitefeatures {
background-color: rgba(242, 242, 242, 0.7);
padding: 35px;
margin-top: 45px;
clear: both;
overflow: auto;
}
.homepagewebsitefeatures {
float: left;
width: auto;
clear: none;
margin-right: 5%;
}
.homepagewebsitefeatureswrap {
overflow: auto;
}
<div id="homepagewebsitefeatures">
<div class="margin homepagewebsitefeatureswrap">
<div class="homepagewebsitefeatures">
<div class="homepagewebsitefeaturesimage">
<img src="/wp-content/uploads/2016/04/tech-support.png" alt="Website Technical Support">
</div>
<div class="homepagewebsitefeaturescontent">
Technical Support
</div>
</div>
<div class="homepagewebsitefeatures">
<div class="homepagewebsitefeaturesimage">
<img src="/wp-content/uploads/2016/04/edit-website.png" alt="Edit website">
</div>
<div class="homepagewebsitefeaturescontent">
Edit your website
</div>
</div>
<div class="homepagewebsitefeatures">
<div class="homepagewebsitefeaturesimage">
<img src="/wp-content/uploads/2016/04/globe-domain-hosting.png" alt="Globe Hosting & UK Domain Name">
</div>
<div class="homepagewebsitefeaturescontent">
UK domain & hosting
</div>
</div>
</div>
</div>
Try changing #homepagewebsitefeatures to display: flex and use the following CSS attributes for centering content:
align-items: center;
justify-content: center;
If your homepagewebsitefeatures elements are floated. In order to get all of the floated elements centered(horizontally) you are going to want to make their wrapper, homepagewebsitefeatureswrap have auto left and right margins (and display: inline-block) and have their parent container, homepagewebsitefeatures, have text-align: center. Something like so:
# homepagewebsitefeatures {
text-align: center;
}
.homepagewebsitefeatureswrap {
display: inline-block;
margin-left: auto;
margin-right: auto;
}
FIDDLE DEMO
.wrap {
background: lightblue;
text-align: center;
}
.container {
display: inline-block;
background: grey;
margin: 0 auto;
}
.container::before,
.container::after {
content: '';
display: table;
}
.container::after {
clear: both;
}
.float {
padding: 10px;
float: left;
}
.float > span {
background: red;
}
.container {
display: inline-block;
}
<div class="wrap">
<div class="container">
<div class="float"><span>float</span></div>
<div class="float"><span>float</span></div>
<div class="float"><span>float</span></div>
</div>
</div>

Adding colored block below image for text

I've been searching online for an answer and can't really find a solution. I'm trying to display an image and then directly below have a colored block (that is attached to the bottom of the image) for a caption on the image.
Below are my CSS code and HTML for the page.
I have this css code:
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}
/* COLUMN SETUP */
.col {
display: block;
float:left;
margin: 1% 0 1% 5.0%;
}
.col:first-child { margin-left: 0; }
/* GROUPING */
.group:before,
.group:after { content:""; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }
/* GRID OF FOUR */
.span_4_of_4 {
width: 100%;
}
.span_3_of_4 {
width: 73.75%;
}
.span_2_of_4 {
width: 47.5%;
}
.span_1_of_4 {
width: 21.25%;
}
/* GO FULL WIDTH BELOW 480 PIXELS */
#media only screen and (max-width: 480px) {
.col { margin: 1% 0 1% 0%; }
.span_1_of_4, .span_2_of_4, .span_3_of_4, .span_4_of_4 { width: 100%; }
}
/***** HTML CODE: *****/
<div class="section group">
<div class="col span_1_of_4">
<img src="XXX" style="border-width: 1px; border-style: solid; width: 190px; height: 255px;" /></a>
<br />
TEXT HERE
</div>
</div>
.img-wrapper {
background: #888;
border-width: 1px;
border-style: solid;
}
.img-wrapper img {
width: 100%;
padding-bottom: 50px;
}
<div class="section group">
<div class="col span_1_of_4">
<div class="img-wrapper">
<img src="xxx">
<br>
TEXT HERE
</div>
</div>
</div>
Wrap the image and the caption in the same div, and give that div a background color.
Here's a simple example:
<div id="wrapper">
<img src="XXX" style="width: 190px; height: 255px;" />
Text
</div>
#wrapper {
background: #ccc;
display: inline-block;
}
#wrapper a {
text-align: center;
display: block;
padding: 10px;
}
http://jsfiddle.net/eo9wfx5c/1/

Why media queries not using the correct CSS

I have the following HTML:
<div class="section group">
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2790_image" class="imgArtThumb" title="" alt="" src="artOne.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Care on the Fast Track</div>
<div class="textHolderBottom">The National Cancer Institute</div>
</div>
</div>
</div>
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2792_image" class="imgArtThumb" title=" alt="" src="artThree.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Stay Connected</div>
<div class="textHolderBottom">tool for interacting with your provider and following your healthcare</div>
</div>
</div>
</div>
<div class="col span_1_of_3 articleTeaserBoxColor">
<div class="test2n">
<div class="imgHolder"><img id="NewsArticle_2791_image" class="imgArtThumb" title="" alt="" src="artTwo.png" /></div>
<div class="textHolder">
<div class="textHolderTop">Know When Antibiotics Work and When They Don't</div>
<div class="textHolderBottom">If you or your child has a virus, antibiotics will not help you or him/her</div>
</div>
</div>
</div>
</div>
CSS:
.imgArtThumb
{
max-width: 100%;
height: auto;
}
.aTitle
{
font-size: 16px !important;
font-weight: bold;
color: #F67D24;
margin-top: 15px;
margin-bottom: 15px;
}
.test2
{
padding: 8px;
text-align: left;
box-shadow: inset 0 -1px 1px rgba(0,0,0,0.5), inset 0 1px 1px rgba(238,146,85,1);
}
.test2n
{
text-align: left;
height: 95%;
width: 95%;
padding: 2%;
overflow: hidden;
}
.articleTeaserBoxColor
{
vertical-align: top;
box-shadow: inset 0 -1px 1px rgba(0,0,0,0.5), inset 0 1px 1px rgba(238,146,85,1);
}
.test2 p, .test2n p
{
text-align: left;
}
.setP p
{
text-align: left;
padding: 10px 10px 0 0;
}
.setP article
{
text-align: left;
padding: 10px 0 0 0;
}
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
height: auto;
overflow: auto;
text-align: center;
width: 100%;
}
/* COLUMN SETUP */
.col {
display: block;
/*float:left;*/
display: inline-block;
margin: 1% 0 1% 0;
}
.col:first-child {
margin-left: 0;
}
/* GROUPING */
.group:before, .group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1; /* For IE 6/7 */
}
/* GRID OF THREE */
.span_3_of_3 {
width: 100%;
}
.span_2_of_3 {
width: 66.1%;
}
.span_1_of_3 {
width: 32.2%;
}
/* GO FULL WIDTH AT LESS THAN 480 PIXELS */
#media only screen and (max-width: 820px) {
.col {
margin: 1% 0 1% 0%;
padding-bottom: 10px;
}
}
#media only screen and (max-width: 820px) {
.span_3_of_3 {
width: 100%;
}
.span_2_of_3 {
width: 100%;
}
.span_1_of_3 {
width: 98%;
}
.imgArtThumb {
width: 100%;
height: 110px;
}
.setTableCell
{
display: block;
}
}
.imgHolder
{
float: left;
display: inline-block;
width: 43%;
padding-right: 2%;
height: 100%;
}
.textHolder
{
float: left;
display: inline-block;
width: 55%;
height: 100%;
}
.textHolderTop
{
width: 100%;
height: 48%;
text-align: left;
padding-bottom: 2%;
overflow: hidden;
}
.textHolderBottom
{
width: 100%;
height: 48%;
overflow: hidden;
text-align: left;
padding-bottom: 2%;
}
.setTableCell
{
display: table-cell;
}
At full desktop width page this is what I see:
At smaller screen (mobile) this is what I see:
If I remove the text or unchecked table-cell from the developer tool from the image below, it works fine. But I am not sure why the display: block is cancelled out if the screensize matches the condition.
How can I resolve it
In your css, .setTableCell is assigned display:table-cell below the media query part. Move it higher or give the media query a narrower selector; just being inside a media query block does not give it higher priority.