How do i align a div to top - css

Ok so I set my wordpress content to all float left of each other. So it will be 3 columns.
Now when the title of one div is longer, the boxes are all over the place. Here is what I mean (see image below)
Notice that the james brown title is longer and the other two boxes fall way down.
How can I get them to float up no matter how long a title is.
I have tried vertical-align:top; but that doesn't work

You can get it by two ways.
If you want your boxes top-aligned on each row : simply use a .clear element.
h2 {
font-weight: bold;
clear: left;
}
.box {
width: 50px;
min-height: 50px;
background: #ccc;
margin: 3px;
padding: 3px;
float: left;
}
.clear {
clear: both;
height: 0px;
}
<h2>Without columns</h2>
<div class="box">1 Lorem Ipsum</div>
<div class="box">2 Lorem Ipsum Lorem Ipsum</div>
<div class="box">3 Lorem Ipsum</div>
<p class="clear"> </p>
<div class="box">4 Lorem Ipsum Lorem</div>
<div class="box">5 Lorem Ipsum</div>
<div class="box">6 Lorem Ipsum</div>
<p class="clear"> </p>
<div class="box">7 Lorem Ipsum</div>
<div class="box">8 Lorem Ipsum</div>
If you want your boxes stucked to the above one, use colums (you'll need to modify a little bit your PHP code)
h2 {
font-weight: bold;
clear: left;
}
.box {
width: 50px;
min-height: 50px;
background: #ccc;
margin: 3px;
padding: 3px;
float: left;
}
.column {
width:60px;
float: left;
}
<h2>With columns</h2>
<div class="column">
<div class="box">1 Lorem Ipsum</div>
<div class="box">4 Lorem Ipsum Lorem</div>
<div class="box">7 Lorem Ipsum</div>
</div>
<div class="column">
<div class="box">2 Lorem Ipsum Lorem Ipsum</div>
<div class="box">5 Lorem Ipsum</div>
<div class="box">8 Lorem Ipsum</div>
</div>
<div class="column">
<div class="box">3 Lorem Ipsum</div>
<div class="box">6 Lorem Ipsum</div>
</div>

Using the new nth-child() selector, you can do this:
div:nth-child(3n+1) {
clear:left;
}
This way, the 1st, 4th, 7th, etc, children will move the left of the box 'clearing' all other elements.
The advantage of using this method is that you can adjust the layout responsively. For example, on mobiles you can have two in every row, on large desktops 4 in every row. There's also no non-semantic markup.
The disadvantage is that it doesn't work in IE before version 9, but you can get around that with JavaScript. e.g. jQuery:
// polyfill for browsers that don't support nth-child() CSS selectors
$('.box:nth-child(3n+1)').style('clear', 'left');
See: http://jsfiddle.net/rVHgR/

Related

How to align content in div to other div contents in horizontal flex?

I have couple of Card divs in display: flex, direction: row.
Each card has a title and body.
If title is large, body starts further below;
if title is small, body starts at line 2.
How to ensure that body starts at same line, no matter the size of title.
Expected:
Got:
Assuming bottom of the box is in always same height.
.wrap {
display: flex;
gap: 20px;
}
.box {
border: 1px solid #ddd;
padding: 10px;
flex: 1 1 33.3333%;
position: relative;
padding-bottom: 100px;
text-align: center;
}
.holder {
position: absolute;
bottom: 10px;
left: 10px;
width: calc(100% - 20px);
}
<div class="wrap">
<div class="box">
<p>Lorem ipsum text goes here. Lorem ipsum text goes here.</p>
<div class="holder">
<h2>5000</h2>
<h3>Another stuff here</h3>
</div>
</div>
<div class="box">
<p>Lorem ipsum text goes here.</p>
<div class="holder">
<h2>5000</h2>
<h3>Another stuff here</h3>
</div>
</div>
<div class="box">
<p>Lorem ipsum text goes here.</p>
<div class="holder">
<h2>5000</h2>
<h3>Another stuff here</h3>
</div>
</div>
</div>

Can I have a responsive image with a set height?

I working in a page builder
For a shop, I am creating. I can change the CSS which is great.
I’m struggling to get a responsive resize of the images in this 4 column row. Since the images are different heights I have to have to set a height and have responsive width. Is there any way to get it to scale correctly?
The width is auto and the height is a set height based on the size of the screen.
You can see that when I resize it separates from the box and then sometimes get squished.
object-fit property
I did your design by using display : flex; and object-fit : cover; properties. I think that this object-fit property directly on the image is the only lacking property to make your images still looking good despite the screen resolution.
Notice the use of object-position : center; which makes the resizing always axed on the center of the image.
index.html
<div class="foo">
<div class="bar">
<img src="https://picsum.photos/200/300" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/200" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/200/700" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/400" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
</div>
style.css
body {
background-color: #f7f7f7;
font-family: Arial, Helvetica, sans-serif;
}
.foo {
width: 100%;
display: flex;
gap: 20px;
}
.bar {
background-color: #ffffff;
display: flex;
flex-direction: column;
width: 100%;
border-radius: 15px;
overflow: hidden;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}
.bar > img {
width: 100%;
height: 200px;
object-fit: cover;
object-position: center;
}
h4 {
color:#9ccf74;
}
.bar > div {
padding: 10px;
height: 100px;
}

Bootstrap: Two columns inside container, but one should break the container

I am using latest version of bootstrap and have a design with 2 columns where the first column should contain some text and the second column should contained a background image which fills the whole column up to the users right screen. I want the background image to go outside the column to the right, until it reaches the edge of the browser.
like this:
how can i make second column to go outside the container? That's what i am currently using which is not working (image gets cut off by container)
<div class="container">
<div class="row">
<div style="" class="col-sm-12 col-lg-6">
<p>Example text</p>
</div>
<div class="col-sm-12 col-lg-6 bg-image" style="background-image:url('http://placehold.it/1800x1045');">
</div>
</div>
</div>
Try adding a contain value to the background image.
style="background-image: url('http://placehold.it/1800x1045'); background-size: contain;"
You can create the main container as "container-fluid" to fill the entire page content and then create the columns, one other container with your text and the other with the image, check the code below, ignore the height and the borders because I was trying to create your scenario:
<html>
<head>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid" style="height: 500px;">
<div class="row h-100">
<div class="col-sm-6" style="border: 1px solid pink;">
<div class="row">
<div class="container">
<div style="border: 1px solid blue;" class="col-sm-6 col-lg-6">
<p>Example text</p>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-lg-6 no-gutters bg-image" style="background-image:url('https://dummyimage.com/600x400/666/fff'); border: 1px solid red;">
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
</body>
</html>
What you are trying to do is not contemplated by Bootstrap, but you can use a workaround. Why it is not contemplated? Because for a 2 column layout, it uses up 50% of the div, and you are not asking for a 50/50 layout.
First of all, the container, fluid or not, doesn't entirely touch the side of the page. Also, in both cases it is symetrical, and your design is not.
So... what you want to do is a greater div that occupies 100% of the page, with a container (fluid or not) in it. With absolute positioning, you create another div that occupies 50% of the greater div, and has the image as background.
.greater-container {
position: relative; //needed so that the .image-container can be properly positioned
}
.image-container {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
right: 0;
background-color: green;
background-image: url('http://placekitten.com/g/1800/1045');
background-size: cover;
z-index: 0;
}
.content-column {
border: 1px solid red; //so that we can see it
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="greater-container">
<div class="image-container"></div>
<div class="container">
<div class="col-sm-6 content-column">
Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet. Lorem ipsum sid dolor amet.
</div>
</div>
</div>
As a sidenote, this is not an easy layout for a responsive website. That background-image will get resized a lot and will probably not always work well.

How do i float two divs in a centered container with one of them bleed out to the edge of viewport

Hi i'm sure this has been answered but i can't seem to find what i'm looking for. I would like to have a container with a max-width and margin auto and then inside two divs floated next to each other (both taking 50% of the container) but the one on the right bleeds out the container right to the edge of the viewport?
As below with the dotted white line being the container and the black the browser.
[example here][1]
edit - sorry i forgot to mention the divs need to both be the same height, so if the left has less content than the right it needs to still be the same height as the right, much like if i made them table-cell's.
.container {
margin-left: auto;
margin-right: auto;
max-width: 1260px;
}
.table{
display:table;
table-layout: fixed;
width:100%;
.left, .right{
display:table-cell;
vertical-align: top;
width:50%
}
}
html
<div class="container">
<div class="table">
<div class="left">
content
</div>
<div class="right">
content
</div>
</div>
</div>
I have tried adjusting it based on this answer but it stacks
http://jsfiddle.net/gxG4W/2/
You should try bootstrap for this.
I hope this will help you.
Check out this by maximizing browser window.
Bootstrap Column
.container {
border: solid;
}
h4 { margin-bottom: 30px; }
.row {
margin-bottom: 20px;
}
.row .row {
margin-top: 10px;
margin-bottom: 0;
}
.left-div
{
border: solid red;
}
.right-div
{
border: solid green;
}
<div class="container text-center">
<div class="row">
<h4>Two Columns</h4>
<div class="col-lg-6 col-md-6 left-div">
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
</div>
<div class="col-lg-6 col-md-6 right-div">
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
<p>
test div
</p>
</div>
</div>
</div>
ok i've pretty much fixed this myself now, i'm not sure if its the best way to do it and it doesn't make the div's equal height as with the table trick, so i will need to us js to do that i guess. But here is my solution.
<div class="container">
<div class="left">
Lorem ipsum dolor sit amet
</div>
<div class="right">
Lorem ipsum dolor sit amet
</div>
</div>
css
.container{
max-width:500px;
margin:0 auto;
background:red;
padding:10px 0;
}
.container::after{
content:" ";
clear:both;
display:block;
float:none;
}
.left{
float:left;
width:50%;
background:green;
}
.right{
background:blue;
width:50vw;
margin-left:50%;
}
https://jsfiddle.net/shbkxcgw/4/

Right div on float:right appears a line below the left one

Here's my problem.
I have a wrapper div(width: 800px and height: 250px) which contains two divs occupying all the space in height and dividing their width in half.
I set up my css, float the right div to float: right and this one appears where it should but "below" the other one, exceeding the wrapper div space(which shouldn't even be possibile).
I'm posting both the jdfiddle and the code.
JS Fiddle http://jsfiddle.net/FV9yC/
HTML
<div id="wrapper">
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
CSS
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
#leftDiv {
background-color: purple;
height: 200px;
width: 250px; }
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
Just shift the div with ID rightDiv above the div with ID leftDiv. That's it.
Here is the WORKING SOLUTION
The Code:
<div id="wrapper">
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
http://jsfiddle.net/FV9yC/1/
You should add float: left to your left div.
Add float: left to the other div. Also you can use float: left for both; unless you have other reasons than just positioning there to use it.
Used to this code
#leftDiv{float:left;}
#wrapper:after{
content:"";
clear:both;
display:table;
}
#wrapper {
height:200px; // remove this line
}
Demo
try this
http://jsfiddle.net/FV9yC/5/
#wrapper {
background-color: grey;
height: 200px;
width: 500px;
}
#leftDiv {
background-color: purple;
height: 200px;
width: 250px;
float:left;
}
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
You don't need to float your div to the right — you just need to align each block beside the other, and you can do this using float: left;.
I made a consistent solution for you. See below:
Using a class to remove the DRY of your code, I grouped your blocks into a common class with common behaviors.
See your new CSS:
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
.block {
float: left;
width: 250px;
}
#leftDiv {
background-color: purple;
height: 200px; }
#rightDiv {
background-color: green;
height: 250px; }
And your new HTML:
<div id="wrapper">
<!-- left div -->
<div class="block" id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div class="block" id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
Using classes to execute common behaviors it is a good practice to avoid redundancy and future problems.
To see your code working on jsFiddle, just click here.

Resources