CSS Put all floated elements in middle of main wrapper DIV - html

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>

Related

Alternate Floats in Flexbox/Container

I have a series of flexbox items that contain an image and content. I'd like to alternate which side of the flexbox item the image appears (i.e., the left side of the box in one, the right side in the next). I can't seem to get it to work. Any suggestions are appreciated!
CSS
.offerings {
width: 100%;
height: auto;
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
align-items: stretch;
}
.offeringsItem {
width: 90%;
height: auto;
margin: 10px;
padding: 15px;
border: 2px solid #dedede;
}
.offeringsContent {
margin: 10px;
position: relative;
}
.offeringsImg {
margin: 10px;
float: left;
}
.offeringsImg img {
max-width: 100%;
height: auto;
display: block;
}
.offeringsImg:nth-of-type(odd) img {
float: right;
}
.offeringsImg:nth-of-type(even) img {
float: left;
}
HTML
<div class="offerings">
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
<div class="offeringsContent">
Hi.
</div>
</div>
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
</div>
If you are about using flex, i believe using it on .offeringsItem is plenty enough. and flex-flow:row-reverse will do the job.
.offeringsItem {
display: flex;
flex-wrap: wrap;
margin: 10px;
padding: 15px;
border: 2px solid #dedede;
}
.offeringsItem:nth-of-type(odd) {
flex-flow: row-reverse;
}
.offeringsContent,
.offeringsImg {
margin: 10px;
}
.offeringsContent {
flex: 1;
}
.offeringsImg img {
max-width: 100%;
}
<div class="offerings">
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
<div class="offeringsContent">
Hi.
</div>
</div>
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
<div class="offeringsContent">
Hi.
</div>
</div>
</div>
I 'have removed some styles that did not seem necessary, it also makes the CSS update easier to read ;)
Do you want something like this?
The CSS class should be corrected to.
.offeringsItem:nth-of-type(odd) img {
float: right;
}
.offeringsItem:nth-of-type(even) img {
float: left;
}
Instead of applying on the div with the class offeringsImg, you need to apply the nth-of-type() selecor to the div with the class offeringsItem because only these elements have a common parent and the index will be taken properly, before they had separate parents hence it did not work!, One more correction is that you had missed the final closing div in the HTML.
.offerings {
width: 100%;
height: auto;
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
align-items: stretch;
}
.offeringsItem {
width: 90%;
height: auto;
margin: 10px;
padding: 15px;
border: 2px solid #dedede;
}
.offeringsContent {
margin: 10px;
position: relative;
}
.offeringsImg {
margin: 10px;
}
.offeringsImg img {
max-width: 100%;
height: auto;
display: block;
}
.offeringsItem:nth-of-type(odd) img {
float: right;
}
.offeringsItem:nth-of-type(even) img {
float: left;
}
<div class="offerings">
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
<div class="offeringsContent">
Hi.
</div>
</div>
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
</div>
<div class="offeringsItem">
<div class="offeringsImg">
<img src="https://s3.amazonaws.com/tpd-files/images/Artboard-2.png">
</div>
</div>
</div>
Instead of using float, try using margin-left: auto and margin-right: auto on the boxes with your images

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.

How to make a grid with images inside a div with HTML?

I am trying to make a grid of pictures with padding in between inside the main_block div. I cant get the images to aline next to eachother and then break it with a becouse they go inline. inline block does not work. I tried making a new div for these images but i cant resize the pictures nor give them padding. I tried to make the pictures resizable but without results. iut is as if something is overriding the size of the pictures. The pictures stack upon eachother and im trying to maaake a grid.
Thanks in advance for any help!
This would be the optimal solution.
Here is the fiddle
https://jsfiddle.net/q2cr9ttL/1/
<style>
body {
margin: 0;
padding: 0;
}
#header {
background-color: #ff6600;
color: white;
text-align: left;
padding: 2px;
}
#nav {
line-height: 30px;
background-color: #fff000;
height: 350px;
width: 125px;
float: left;
padding: 5px;
}
#section {
width: 350px;
float: left;
padding: 10px;
}
#footer {
background-color: #737373;
color: white;
clear: both;
text-align: center;
}
#container {
margin: auto;
width: 900px;
text-align: left;
overflow: hidden;
}
.inner_block {
display: inline-block;
text-align: center;
width: 350px;
height: 200px;
}
.main_block {
text-align: center;
width: 750px;
}
.grid_block {
display: inline-block;
text-align: center;
width: 29%px;
height:100px;
}
</style>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="stylesheet.css" />
<body>
<div id="container">
<!---container--->
<div id="header">
<h1>JORDAS</h1>
</div>
<!--header-->
<div id="nav">
Etusivu
<br>
Teltat
<br>
Palvelut
<br>
Yhteistiedot
<br>
</div>
<div id="section">
<div class="main_block">
<div class="grid_block">
<img src=Grafik/basictalt.png>
</div>
<div class="grid_block">
<img src=Grafik/basictalt.png >
</div>
<div class="grid_block">
<img src=Grafik/basictalt.png>
</div>
</div><!--mainblock-->
</div>
<div id="footer">
<h3>POP-UP TELTTOJEN YKKÖNEN </h3>
</div>
<!--footer-->
</div>
<!--container-->
</body>
You could use the flex display property.
You will need to include some prefixes for cross browser compatibility.
* {
box-sizing: border-box;
}
.main_block {
display: flex;
flex-wrap: wrap;
}
.grid_block {
width: 33%;
padding: 1.4em
}
.grid_block img {
max-width: 100%
}
/* ORIGINAL STYLES */
body {
margin: 0;
padding: 0;
}
#header {
background-color: #ff6600;
color: white;
text-align: left;
padding: 2px;
}
#nav {
line-height: 30px;
background-color: #fff000;
height: 350px;
width: 125px;
float: left;
padding: 5px;
}
#section {
width: 350px;
float: left;
padding: 10px;
}
#footer {
background-color: #737373;
color: white;
clear: both;
text-align: center;
}
#container {
margin: auto;
width: 900px;
text-align: left;
overflow: hidden;
}
.inner_block {
display: inline-block;
text-align: center;
width: 350px;
height: 200px;
}
.main_block {
text-align: center;
width: 750px;
}
.grid_block {
display: inline-block;
text-align: center;
width: 29%px;
height: 100px;
}
<div id="container">
<!---container--->
<div id="header">
<h1>JORDAS</h1>
</div>
<!--header-->
<div id="nav">
Etusivu
<br>
Teltat
<br>
Palvelut
<br>
Yhteistiedot
<br>
</div>
<div id="section">
<div class="main_block">
<div class="grid_block">
<img src=http://lorempixel.com/image_output/city-q-c-640-480-9.jpg>
</div>
<div class="grid_block">
<img src=http://lorempixel.com/image_output/city-q-c-640-480-9.jpg >
</div>
<div class="grid_block">
<img src=http://lorempixel.com/image_output/city-q-c-640-480-9.jpg>
</div>
</div><!--mainblock-->
</div>
<div id="footer">
<h3>POP-UP TELTTOJEN YKKÖNEN </h3>
</div>
<!--footer-->
</div>
<!--container-->

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

<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');
});

How to stretch parent div to fit children div?

How to stretch parent div to fit children div?
I tried to add element with clear: both; after content but it didn't work for me.
HTML:
<div id="wrapper">
<div class="left-menu">
</div>
<div class="right-bar">
<div class="right-content">
<div class="content">
<div class="content-wrapper">
<div class="content-body">
Here is content
</div
</div>
</div>
</div>
</div>
</div>
CSS:
.left-menu {
background-color: #0B0C0E;
width: 20%;
height: 100%;
float: left;
}
.right-bar {
background-color: #F0F0F0;
height: 100%;
width: 100%;
}
.right-content {
float: left;
width: 80%;
}
.right-content > .content {
padding: 21px 0 0 42px;
}
.right-content > .content > .content-wrapper {
width: 98%;
height: 70%;
}
.right-content > .content .content-body {
background-color: #FAFAFA;
padding: 20px;
font-size: 24px;
border: 1px solid #D0D0D0;
}
sandbox for test: http://roonce.com/en/room/SwZuEJYB
Thanks in advance.
Use "clear-fix" technique. http://css-tricks.com/snippets/css/clear-fix/
This will allow the parent div to be the appropriate size of the floated elements within. Note this works specifically on #wrapper. (http://jsbin.com/huqehuta/1/edit)
.clear-fix:after {
content: "";
display: table;
clear: both;
}