Floating different height divs [duplicate] - css

i am using left floating DIVs to simulate a two column layout (each div contains textfield to edit different data, like name, hobbies,...). So it should look like this
1 2
3 4
5 6
Now my div-boxes aren't always the same, since some DIVs have more elements than the other ones. Now my layout looks likes this
1 2
2
3 4
5 6
You can also see the effect on this example if you scale your so that only four or three colums are shown. E.g. if 4 columns are shown in a row there is much space between Float 1 and Float 6. This doesn't look good on my UI. What I want is to have Float 6 following Float 1 with no space in between (except the margin I define)
Edit: My DIVs basically just contain a float:left and a width:40%, so that two fit on a screen
Here's a screenshot showing more

Here is a pure CSS solution. I took this example
Check it out if you want to learn more. He also use jQuery Masonry as fallback.
CSS:
.masonry { /* Masonry container */
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
column-gap: 1em;
}
.item { /* Masonry bricks or child elements */
background-color: #eee;
display: inline-block;
margin: 0 0 1em;
width: 100%;
}
HTML:
<div class="masonry">
<div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
<div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
<div class="item">Incidunt sit unde minima in nostrum? Incidunt sit unde minima in nostrum?</div>
<div class="item">Ducimus, voluptates, modi, delectus animi maiores consequuntur repellat quisquam fugiat eum possimus enim culpa totam praesentium magni quae!</div>
<div class="item">Lorem ipsum dolor sit amet, dicta dolore adipisci hic ipsam velit deleniti possimus cumque accusantium rerum quibusdam.</div>
<div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
<div class="item">Incidunt sit unde minima in nostrum?</div>
<div class="item">Incidunt sit unde minima in unde minima in unde minima in nostrum?</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae, praesentium consequatur ducimus commodi quam ex illo omnis dicta reiciendis vel nesciunt deserunt aut sequi nam mollitia perferendis ipsam possimus temporibus!</div>
<div class="item">Ab, adipisci, temporibus eaque quis harum perferendis incidunt cupiditate doloribus dolor numquam voluptates ipsum dolore aspernatur et voluptate ipsam beatae animi culpa.</div>
</div>
Hope this will help you. Thanks.

The jQuery Masonry plugin will do exactly what you want.
If you wanted to stick with pure CSS, you could do something like the following, but I don't think it's what you're going for:
<div class="col">
<div class="one"></div>
<div class="three"></div>
<div class="five"></div>
<div class="seven"></div>
</div>
<div class="col">
<div class="two"></div>
<div class="four"></div>
<div class="six"></div>
<div class="eight">who do we appreciate</div>
</div>
And the CSS:
.col {
float: left;
width: 200px;
}

if you can use php, here is little trick ;)
<?php
$dir = "your/images/dir/";
$img = scandir($dir); // read images to array, or make script which read it from db
unset($img[0], $img[1]); // remove unnecessary ;)
$columns = 5; // define how many columns you want to use
$margin = 5; // define page margin and margin between images in %
// create columns..
for ($c = 0; $c < $columns; $c ++)
{
$main_counter = 0;
foreach ($img as $file)
{
if ($main_counter % $columns == $c)
{
$column[$c][] = $file;
}
$main_counter ++;
}
}
?>
<! -- show images -->
<div style="margin: <?php echo $margin; ?>%;">
<?php
foreach ($column as $key => $data)
{
?>
<div style="float: left; width: <?php echo (100 / $columns); ?>%;">
<?php
foreach ($data as $image)
{
?>
<div style="margin-bottom: <?php echo $margin; ?>%; margin-right: <?php echo $margin; ?>%; background-image: url(<?php echo $dir . $image; ?>); background-size: cover;"><img src="<?php echo $dir . $image; ?>" style="visibility: hidden; width: 100%;"></div>
<?php
}
?>
</div>
<?php
}
?>
</div>
maybe help you ;)
working demo on http://www.showcase.glirp.sk/

Related

Vertically align buttons below caption to longest text of three captions

Just started creating a website in WordPress with Bootstrap 4 and I've been puzzling on an issue for days now.
I'm trying to vertically align read more buttons for a 'related posts' section on a website. I want to have the read more button automatically aligned to the longest caption text. I've tried multiple ways with relative/absolute, but I can't seem to get it to work.
Here is my current code:
<div class="row">
<?php $cat = new WP_Query( 'cat=6&posts_per_page=3' ); ?>
<?php while($cat->have_posts()) : $cat->the_post(); ?>
<div class="col-xs-12 col-md-4 readmore">
<img class="w-100" src="<?php the_post_thumbnail_url(); ?>">
<div class="col-12" style="margin-top:15px;">
<h2 class="h6 text-uppercase text-center text-nowrap"><strong><?php the_title(); ?></strong></h2>
<p><?php the_content(); ?></p>
<p><?php //the_excerpt(); ?></p>
</div>
<div class="col-6 mx-auto" style="bottom: 10px;margin-bottom: 20px">
Read More
</div>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
And this is how it look currently:
The thing that makes the even more difficult is that ideally it should still be responsive. So on a smaller screen the buttons should be directly after the caption, like this:
Hopefully it's clear what the problem is and hopefully someone can share their thoughts. Thanks in advance!
You don't need all child elements to initially be the same height. You can use flexbox to achieve the desired results you are looking for.
Here is a link to a codepen that shows the solution and there is an explanation below.
https://codepen.io/sugarbuzzdesigns/pen/ZjwQZb
Create a container that will contain all of your posts. This should be set to display: flex. Set the flex direction to column so we can set it to row on larger screens with a media query.
Create containers for each post. These posts are the flex items in the container we just created
Wrap your image, title, and content in one div so it can be evenly spaced separately from the read more button
Now you can set each post to flex: 1 so they take up equal space. Next you can set each post to flex-direction: column and justify-content: space-between.
HTML
<div class="container">
<div class="blog-post">
<div>
<h3>Heading Here</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quaerat explicabo cumque reiciendis error hic soluta rem enim amet laborum consequuntur, delectus mollitia molestiae maiores ullam possimus non corporis. Animi, sit. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis expedita laudantium ut earum sint, numquam adipisci autem cupiditate dolorem aspernatur illo laboriosam, dolores quis? Perspiciatis debitis nesciunt corporis dicta eveniet!<p>
</div>
Read More
</div>
<div class="blog-post">
<div>
<h3>Heading Here</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quaerat explicabo cumque reiciendis error hic soluta rem enim amet laborum consequuntur, delectus mollitia molestiae.<p>
</div>
Read More
</div>
<div class="blog-post">
<div>
<h3>Heading Here</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quaerat explicabo cumque reiciendis error hic soluta rem enim amet laborum consequuntur, delectus mollitia molestiae maiores ullam possimus non corporis. Animi, sit.<p>
</div>
Read More
</div>
</div>
CSS
.container {
display: flex;
}
#media (min-width: 600px) {
.container {
flex-direction: row;
}
}
.blog-post {
display: flex;
flex: 1;
flex-direction: column;
justify-content: space-between;
}
You will need to specify a div where all child elements are of equal height. I believe there are some features for responsive design too. Check this article out:
How do I keep two side-by-side divs the same height?
The top comment under the answer suggests: If you are developing a responsive design and you want your second div to go down in smaller screens you will need to set .row to display: block; in your media query.
I will run some testing for you now and post the code snippet

Fixed width sidebar on bootstrap 4 where main area disappears on mobile

There are a lot of examples for how to have a fixed width sidebar on Bootstrap 4, but I haven't found one that will cause the main content to disappear on mobile.
I've tried the following:
<div class="row no-gutters flex-nowrap">
<div class="col-md col-12 d-none d-xs-block d-sm-block ">
Main area
</div>
<div class="col-md-4 col-12 sidebar">
Sidebar
</div>
</div>
.sidebar {
max-width: 600px; min-width: 600px
}
And this mostly works, but what happens is that between 768 to 576 pixels the sidebar disappears entirely.
What I'm after is for the sidebar to be a fixed width with the main area shrinking and eventually disappearing.
With few lines of CSS you can achieve this with Flexbox. Please have a look in snippet I added with this answer.
To know more about Flexbox you can checkout those links.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://yoksel.github.io/flex-cheatsheet/
.wrapper {
display: flex;
height: 100vh;
}
.sidebar {
min-width: 150px;
max-width: 150px;
height: 100%;
background: grey;
padding: 20px;
}
.content {
padding: 20px;
height: 100%;
background: lightgrey;
}
<div class="wrapper">
<div class="sidebar">
sidebar
</div>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis modi adipisci autem illo labore ipsum numquam explicabo excepturi similique, perspiciatis doloremque, quo asperiores at veniam culpa aperiam maiores, dolores eligendi. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis modi adipisci autem illo labore ipsum numquam explicabo excepturi similique, perspiciatis doloremque, quo asperiores at veniam culpa aperiam maiores, dolores eligendi.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis modi adipisci autem illo labore ipsum numquam explicabo excepturi similique, perspiciatis doloremque, quo asperiores at veniam culpa aperiam maiores, dolores eligendi.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis modi adipisci autem illo labore ipsum numquam explicabo excepturi similique, perspiciatis doloremque, quo asperiores at veniam culpa aperiam maiores
</div>
</div>
I believe you're looking for this:
#media (min-width:768px) {
.sidebar {
max-width: 600px; min-width: 600px;
}
}
.sidebar {
background-color: #f5f5f5;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row no-gutters flex-nowrap">
<div class="col d-none d-md-block">
Main area
</div>
<div class="col sidebar">
Sidebar
</div>
</div>
I wrapped the fixed size rule in a #media query condition only applying on md and up and I revised classes applied to columns accordingly.
Since you're setting the custom width of your sidebar anyway, you don't need col-* classes on your columns.

Responsive box grid with bootstrap-4 different box heights

I tried to make a responsive grid of 6 textboxes with the box class and bootstrap.
My problem is , that with different amount of text in it the box they have different widths / heights, but i want them to have the same max-height / max-width as the box with the most text in it.
Is there a chance to get this done without hard-coding the width / height for all boxes?
Here's a code snippet:
/***** Box *****/
.flex-box {
position: relative;
text-align: center;
margin-top: 10px;
margin-right: auto;
margin-bottom: ;
margin-left: auto;
padding: 40px;
transition: all 0.3s ease;
background-color: rgb(170, 230, 255);
}
.flex-box:hover {
margin-top: 5px;
margin-bottom: 5px;
-webkit-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
-moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.25);
}
<!-- Textbox-Sections -->
<section id="about_me" class="section-padding title">
<div class="container-fluid">
<div class="container pt-4">
<h1 class="title">About me</h1>
<div class="row">
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti fuga iusto aspernatur magni rem quas deserunt repellendus hic fugit amet tempora labore perferendis, voluptate possimus itaque cum molestias dolores. Omnis?</div>
</p>
</div>
</div>
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Esse voluptatum soluta cum provident explicabo fugiat dolores odit blanditiis dolorem magni. Quaerat temporibus doloremque beatae voluptatum dignissimos? Eligendi voluptatum similique
incidunt!
</div>
</p>
</div>
</div>
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Voluptate ipsam neque recusandae similique. Inventore repellendus aliquid delectus explicabo dolore sapiente voluptas nihil dolorem tempore voluptates, nemo ipsum cumque animi quidem.</div>
</p>
</div>
</div>
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod illo incidunt nihil praesentium minus itaque hic, soluta dicta cupiditate quos dolorum esse, harum placeat, qui veritatis animi sunt? Ab, nobis.</div>
</p>
</div>
</div>
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni, fuga vel. Maxime hic tempore quo animi ex vero doloribus dolores quos, sapiente delectus dolor et laborum amet quam aspernatur earum.</div>
</p>
</div>
</div>
<div class="col col-12 col-sm-6 col-lg-4">
<div class="flex-box">
<h1 class="boxtitle">My way</h1>
<p class="lead">
<div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Dicta, repellendus distinctio ratione autem consequatur rerum libero nesciunt ullam eaque molestias odit architecto nisi incidunt quasi omnis ut. Natus, exercitationem alias.</div>
</p>
</div>
</div>
</div>
</div>
</div>
</section>
Here's a link to the CodePen site
Thanks
Just make the .flex-box height:100% so that it fills the col-*...
https://codepen.io/anon/pen/GQORQV?editors=1100
.flex-box {
position: relative;
text-align: center;
margin-top: 10px;
margin-right: auto;
margin-bottom:;
margin-left: auto;
padding: 40px;
height: 100%;
transition: all 0.3s ease;
background-color: rgb(170, 230, 255);
}
I don't think that is possible with only with css but yes if you are making these boxes vertical I mean one to each other then definitely its possible but not horizontally. Same height possible in the grid if section are align one to each other like --
As #Abhijeet stated it is only possible to use css if the boxes are horizontally aligned. Otherwise you can use this jQuery snippet:
var max_height = 0;
var max_width = 0;
$('.equal-size').each(function(e) {
h = $(this).height();
if(typeof(h) != "undefined") {
if(h > max_height) {
max_height = h;
}
}
w = $(this).width();
if(typeof(w) != "undefined") {
if(w > max_width) {
max_width = w;
}
}
});
if(max_height > 0) {
$('.equal-size').height(max_height);
}
if(max_width > 0) {
$('.equal-size').width(max_width);
}
You need to give the elements the class equal-size.
It is looking for the max height/width and applying it to every element.
This is only working if you have only one group of elements that should have same height/width. If you have more you need to give the group numbers and change the classes appropriately (equal-size-1, equal-size-2, ...) and use this snippet:
var numberOfGroups = 2;
for(var i=1; i<numberOfGroups+1; i++) {
var max_height = 0;
var max_width = 0;
$('.equal-size-'+String(i)).each(function(e) {
h = $(this).height();
if(typeof(h) != "undefined") {
if(h > max_height) {
max_height = h;
}
}
w = $(this).width();
if(typeof(w) != "undefined") {
if(w > max_width) {
max_width = w;
}
}
});
if(max_height > 0) {
$('.equal-size-'+String(i)).height(max_height);
}
if(max_width > 0) {
$('.equal-size-'+String(i)).width(max_width);
}
}

How block-stop the padding div child?

I've this example code:
<div class="container">
<div class="row">
<div class="left-block" style="background-color:#000000">menu</div>
<div class="text various">text article</div>
</div>
</div>
the left-block class have a background-color, but the background stop by height menu, but i like backgroud continued and stop end page, but example if insert in left-block padding-bottom=100% tha height left-block increase height by class="row" and class text-various, I like the padding stop height row or height text-various
help me please
As I understand you need something like this:
.row {
display: flex;
}
.left-block {
color: #fff;
padding: 8px;
}
.text {
padding-left: 8px;
}
<div class="container">
<div class="row">
<div class="left-block" style="background-color:#000000">menu</div>
<div class="text various">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit quia aperiam, ex fugiat! Ad odio voluptatem magnam, alias, voluptatibus vero corporis adipisci quos dicta, asperiores consequatur maiores iste unde dolore. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit quia aperiam, ex fugiat! Ad odio voluptatem magnam, alias, voluptatibus vero corporis adipisci quos dicta, asperiores consequatur maiores iste unde dolore.
</div>
</div>
</div>

CSS Floating Divs with different height are aligned with space between them

i am using left floating DIVs to simulate a two column layout (each div contains textfield to edit different data, like name, hobbies,...). So it should look like this
1 2
3 4
5 6
Now my div-boxes aren't always the same, since some DIVs have more elements than the other ones. Now my layout looks likes this
1 2
2
3 4
5 6
You can also see the effect on this example if you scale your so that only four or three colums are shown. E.g. if 4 columns are shown in a row there is much space between Float 1 and Float 6. This doesn't look good on my UI. What I want is to have Float 6 following Float 1 with no space in between (except the margin I define)
Edit: My DIVs basically just contain a float:left and a width:40%, so that two fit on a screen
Here's a screenshot showing more
Here is a pure CSS solution. I took this example
Check it out if you want to learn more. He also use jQuery Masonry as fallback.
CSS:
.masonry { /* Masonry container */
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
column-gap: 1em;
}
.item { /* Masonry bricks or child elements */
background-color: #eee;
display: inline-block;
margin: 0 0 1em;
width: 100%;
}
HTML:
<div class="masonry">
<div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
<div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
<div class="item">Incidunt sit unde minima in nostrum? Incidunt sit unde minima in nostrum?</div>
<div class="item">Ducimus, voluptates, modi, delectus animi maiores consequuntur repellat quisquam fugiat eum possimus enim culpa totam praesentium magni quae!</div>
<div class="item">Lorem ipsum dolor sit amet, dicta dolore adipisci hic ipsam velit deleniti possimus cumque accusantium rerum quibusdam.</div>
<div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
<div class="item">Incidunt sit unde minima in nostrum?</div>
<div class="item">Incidunt sit unde minima in unde minima in unde minima in nostrum?</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae, praesentium consequatur ducimus commodi quam ex illo omnis dicta reiciendis vel nesciunt deserunt aut sequi nam mollitia perferendis ipsam possimus temporibus!</div>
<div class="item">Ab, adipisci, temporibus eaque quis harum perferendis incidunt cupiditate doloribus dolor numquam voluptates ipsum dolore aspernatur et voluptate ipsam beatae animi culpa.</div>
</div>
Hope this will help you. Thanks.
The jQuery Masonry plugin will do exactly what you want.
If you wanted to stick with pure CSS, you could do something like the following, but I don't think it's what you're going for:
<div class="col">
<div class="one"></div>
<div class="three"></div>
<div class="five"></div>
<div class="seven"></div>
</div>
<div class="col">
<div class="two"></div>
<div class="four"></div>
<div class="six"></div>
<div class="eight">who do we appreciate</div>
</div>
And the CSS:
.col {
float: left;
width: 200px;
}
if you can use php, here is little trick ;)
<?php
$dir = "your/images/dir/";
$img = scandir($dir); // read images to array, or make script which read it from db
unset($img[0], $img[1]); // remove unnecessary ;)
$columns = 5; // define how many columns you want to use
$margin = 5; // define page margin and margin between images in %
// create columns..
for ($c = 0; $c < $columns; $c ++)
{
$main_counter = 0;
foreach ($img as $file)
{
if ($main_counter % $columns == $c)
{
$column[$c][] = $file;
}
$main_counter ++;
}
}
?>
<! -- show images -->
<div style="margin: <?php echo $margin; ?>%;">
<?php
foreach ($column as $key => $data)
{
?>
<div style="float: left; width: <?php echo (100 / $columns); ?>%;">
<?php
foreach ($data as $image)
{
?>
<div style="margin-bottom: <?php echo $margin; ?>%; margin-right: <?php echo $margin; ?>%; background-image: url(<?php echo $dir . $image; ?>); background-size: cover;"><img src="<?php echo $dir . $image; ?>" style="visibility: hidden; width: 100%;"></div>
<?php
}
?>
</div>
<?php
}
?>
</div>
maybe help you ;)
working demo on http://www.showcase.glirp.sk/

Resources