back to line when using float left [duplicate] - html

This question already has answers here:
Floated elements of variable height push siblings down
(3 answers)
What is a clearfix?
(10 answers)
What methods of ‘clearfix’ can I use?
(29 answers)
Closed 3 years ago.
I should align the fixed width divs.
I have a container with a fixed width
I have dynamic divs inside but they also have a fixed width.
I would like to put 4 divs per line.
So I used CSS's float rule
My actual problem is that when the last div (card) on the line has a short height the next card gets under it.
What I'm expecting is the next div get back to a complete line.
.card {
width:200px;
height:200px;
border: 1px solid black;
float: left;
}
.container {
width: 900px;
}
.shortcard {
height:50px
}
<div class="container">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card shortcard">
</div>
<div class="card">
It should be in next line
</div>
<div class="card">
</div>
</div>
When the height is longer, the element is getting to another line
.card {
width:200px;
height:200px;
border: 1px solid black;
float: left;
}
.container {
width: 900px;
}
.shortcard {
height:50px
}
<div class="container">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card ">
</div>
<div class="card">
I'm ok here
</div>
<div class="card">
</div>
</div>

I think that what you are trying to achieve is that, you don't want the long div to come under the short div. So for that, you have to use inline-block with float: left like
.card {
width: 200px;
height: 200px;
border: 1px solid black;
display: inline-block; /* to line up the divs in a single line */
float:left; /* to make them stick to the top */
}
.card {
width: 200px;
height: 200px;
border: 1px solid black;
display: inline-block;
float: left;
}
.container {
width: 100%;
}
.shortcard {
height: 50px;
}
body,
html {
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
<div class="container">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card shortcard">
</div>
<div class="card containerdivNewLine">
beside short div but not under it
</div>
<div class="card">
</div>
</div>

Okay, I try to fix this in your code, so that you understand better!
so basic changes are css, create a new class .container and use display: flex properties.
have a look:
<div class="container">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
I'm ok here
</div>
<div class="card">
</div>
</div>
.card {
width:200px;
height:200px;
border: 1px solid black;
display: inline-flex;
}
.container
{
display: flex;
width: 900px;
}
.card {
width:200px;
height:200px;
border: 1px solid black;
display: inline-flex;
}
.container
{
display: flex;
width: 900px;
}
<div class="container">
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
</div>
<div class="card">
I'm ok here
</div>
<div class="card">
</div>
</div>
run the code, and let me know if you want a design like this or not?

Related

Flex nowrap container not wrapping child. when the width of those are in mentioned in percentage

I have a flex container that wraps some cards. I have implemented a horizontal scroller with flex(using flex-nowrap). The flex wrapper container is subjected to have a 36px left spacing and a 0px right spacing initially. The catch here is after the last card is scrolled I need to have a 36px right spacing.
here is what I did so far
*{
box-sizing: border-box;
}
h2 {
margin: 0;
}
body {
background: cadetblue;
}
.container {
width: 500px;
margin: 0 auto;
background: #000;
padding: 20px 36px;
}
.scroll-wrapper {
overflow: hidden;
margin-right: -36px;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: 0px -16px;
}
.card {
height: 250px;
width: 75%;
flex: 1 0 75%;
padding: 0px 16px;
}
.inner-wrapper {
background: #fff;
height: 250px;
}
<div class="container">
<div class="scroll-wrapper">
<div class="scroll-inner">
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
</div>
</div>
</div>
I have given a negative right margin for the wrapper to make it look like it's flowing from the right.
Once the last element is scrolled/reached I want it to look something like this
enter image description here
One thing I noticed is my scroll-inner(flex-nowrap) is not wrapping up the entire children. I presumed if we have five children each having width 50px. The scroll-inner should show a scrollable width of 250px, Unfortunately, it's not how flex is behaving. Any help or suggestion is much appreciated.
Updating few images that show what I'm really looking for
During scrolling
After scrolling till the last card
Try
.scroll-wrapper {
overflow: hidden;
margin: auto;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: auto;
}
I have found a fix and hope this help someone.
*{
box-sizing: border-box;
}
h2 {
margin: 0;
}
body {
background: cadetblue;
}
.container {
width: 500px;
margin: 0 auto;
background: #000;
padding: 20px 36px;
}
.scroll-wrapper {
overflow: hidden;
margin-right: -36px;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: 0px;
}
.card {
height: 250px;
width: 75%;
flex: 1 0 75%;
padding-right: 40px;
margin-right: -8px;
}
.inner-wrapper {
background: #fff;
height: 250px;
}
<div class="container">
<div class="scroll-wrapper">
<div class="scroll-inner">
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
</div>
</div>
</div>
Please do answer if someone has a better solution!! Thanks

2 column layout with an extra scroll bar for left column using only css

I have a list of flatterned div elements (Unfortunately I can't edit html markup to add structures). Because the div#1 is very long, I'd like to put it in a seperate column with its own scrollbar; all the other div's (the number of div's is flexible) are stacked in the right column. What is the best css setting to achieve this? Ideally, I don't want to use fixed/absolute position.
<div id='1'>1</div>
<div id='2'>2</div>
<div id='3'>3</div>
.
.
.
<div id='n'>n</div>
You can use Flex to achieve this result check this example:
* {
box-sizing: border-box;
}
.row {
display: flex;
}
/* Create two equal columns that sits next to each other */
.column {
flex: 50%;
padding: 10px;
height: 300px; /* Should be removed. Only for demonstration */
}
.column div {
background-color:#bbb;
height:50px;
}
.column div:not(:first-child) {
margin-top:5px;
}
<div class="row">
<div class="column">
<div style="height:500px;">div 1</div>
</div>
<div class="column">
<div>div 2</div>
<div>div 3</div>
<div>div n</div>
</div>
</div>
You can use this structure for this. In this I used the display:flex property so you don't need to use any position.
.main {
display: flex;
}
.blue {
height: 600px;
background-color: blue;
width: 200px;
border: 3px solid black;
margin-right: 30px;
}
.red {
height: 150px;
background-color: red;
width: 150px;
border: 3px solid black;
margin-bottom: 10px;
}
<div class="main">
<div class="left">
<div class="blue"></div>
</div>
<div class="right">
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
</div>
</div>

how to make div of fixed height inside div

<style type="text/css">
#displayHeader {
img {
width: 100%;
max-width: 100%;
max-height: 100%;
}
.header-img-inner {
max-height:100%;
}
}
</style>
<div id="displayHeader" style="height: 83.03px;">
<div class="header-inner">
<div class="row">
<div id="headerCol0" class="col-xs-4 text-center header-img-wrapper">
<div class="header-img-inner">
<img src="test.svg">
<p>TEST</p>
</div>
</div>
<div id="headerCol1" class="col-xs-4 text-center header-img-wrapper">
<div class="header-img-inner">
<img src="test.svg">
</div>
</div>
<div id="headerCol2" class="col-xs-4 text-center header-img-wrapper">
<div class="header-img-inner">
<img src="test.svg">
<p>tt</p>
</div>
</div>
</div>
</div>
</div>
<div id="displayContent" style="height: 264.8px;">
<div class="inner">
</div>
<div>
</div>
<div>
</div>
<div>
Image and text go out of the div block, why is this happening?
No float left, right used. It's not working with position:relative for parent and position absolute for child, but still not work well.
I only set maximum height for image when I set in on the header-img-inner
Why not use position: inherit; so as to make the image and whatever you want inside the div to inherit the position. This might help, give it a try.
Do you want something like this?
.block {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
margin: 20px;
}
.block:before {
content: '\200B';
/* content: '';
margin-left: -0.25em; */
display: inline-block;
height: 100%;
vertical-align: middle;
}
.centered {
display:inline-block;
vertical-align: middle;
width: 300px;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
}
<div class="block" style="height: 300px;">
<div class="centered">
<h1>Some Text</h1>
<img src="Untitled-2.jpg" height="150px">
</div>
</div>
Please check this link:-https://css-tricks.com/centering-in-the-unknown/

How can i center align two divs?

How can I align two inline divs so that the their adjacent edges are center?
So I'd like it to look like this:
div1div1 div2div2
div1 div2
div1div1div1 div2div2
I tried using inline block like like below but it is merely centering each line.
.container {
text-align: center;
}
.left-div {
display: inline-block;
}
.right-div {
display: inline-block;
}
<div class="container">
<div class="left-div">div1div1</div>
<div class="right-div">div2div2</div>
</div>
<div class="container">
<div class="left-div">div1</div>
<div class="right-div">div2</div>
</div>
<div class="container">
<div class="left-div">div1div1div1</div>
<div class="right-div">div2div2</div>
</div>
https://jsfiddle.net/fkfmh7md/ to change the distance between divs, change 5px in padding:0 5px to any other value
.container {
display: table;
table-layout: fixed;
width: 100%;
}
.container div {
display: table-cell;
padding: 0 5px;
}
.container div.left {
text-align: right;
}
<div class="container">
<div class="left">div1div1</div>
<div>div2div2</div>
</div>
<div class="container">
<div class="left">div1</div>
<div>div2</div>
</div>
<div class="container">
<div class="left">div1div1div1</div>
<div>div2div2</div>
</div>

Adapt parent div height to child div height in flexbox [duplicate]

This question already has answers here:
How to disable equal height columns in Flexbox?
(4 answers)
Closed 6 years ago.
I would like to adapt the parent div height to children height in CSS only.
In my code, both columns have same height.
I would like column 2 to have a smaller height as it contains less divs to show.
Let me show you on an example:
#mainPane {
display: flex;
background-color: blue;
}
.column {
width: 250px;
background-color: gray;
margin: 5px 15px 5px 15px;
display: inline-block;
}
.news {
background-color: white;
}
<section id="mainPane">
<div class="column">
<div class="colHeader">Col1</div>
<div class="news">Line1</div>
<div class="news">Line2</div>
<div class="colFooter">Footer</div>
</div>
<div class="column">
<div class="colHeader">Col2</div>
<div class="colFooter">Footer</div>
</div>
</section>
Add align-items:flex-start; to #mainPane:
#mainPane{
display: flex;
align-items: flex-start;
background-color: blue;
}
.column {
width: 250px;
background-color: gray;
margin: 5px 15px 5px 15px;
}
.news{
background-color: white;
}
<section id="mainPane">
<div class="column">
<div class="colHeader">
Col1
</div>
<div class="news">
Line1
</div>
<div class="news">
Line2
</div>
<div class="colFooter">
Footer
</div>
</div>
<div class="column">
<div class="colHeader">
Col2
</div>
<div class="colFooter">
Footer
</div>
</div>
</section>