Flexbox overflow hidden - css

How can I achieve overflow hidden when using flexbox instead of floats?
My flex example on codepen: using flexbox
My float example on codepen, this is what I want but with flexbox: using floats

The problem is that flexbox will make the columns equal height or at least the largest will take precedence when expanding the parent.
So, we need to collapse the left div by effectively removing the content..and the easiest method for that is position:absolute.
So we wrap the content in an extra element inside the left div and position it as mentioned...then allow overflow scrolling as required.
img {
max-width: 100%;
}
.container {
display: flex;
width: 400px;
border: 5px solid blue;
}
.thumbs {
display: flex;
flex-direction: column;
flex: 1 0 26%;
border: 10px solid green;
position: relative;
overflow-Y: auto;
}
.wrap {
position: absolute;
top: 0;
}
.large {
flex: 1 1 auto;
border: 10px solid red;
}
<div class="container">
<div class="thumbs">
<div class="wrap">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-8.jpg">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-5.jpg">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-4.jpg">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-10.jpg">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-10.jpg">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-9.jpg">
</div>
</div>
<div class="large">
<img src="http://lorempixel.com/image_output/food-q-c-700-700-8.jpg">
</div>
</div>

Related

How can I display an element to the left and other to the center using flexbox? [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
I want to accomplish something like the image, but without the third element, having one element fixed on the left and another in the center.
Is there an easier way without flexbox?
<div style="display: flex; justify-content: space-between">
<button>Left Header</button>
<button>middle</button>
</div>
Im sure there is a better way, but how about just hiding last column?
.container {
display: flex;
justify-content: space-between;
}
.item {
/* no important; just to visualize*/
width: 50px;
height: 50px;
background-color: blue;
border: 1px solid;
}
.item.last { /* :last-child doesn't work */
visibility: hidden;
background-color: red;
}
<div class="container">
<div class="item">A</div>
<div class="item">B</div>
<div class="item last">C</div>
<div>
You can use postion for first div and margin for the second div, like this
.container {
postion: relative;
}
.item {
width: 50px;
height: 50px;
background-color: teal;
border: 1px solid grey;
}
.item.one {
position: absolute;
}
.item.two {
margin: auto
}
<div class="container">
<div class="item one">A</div>
<div class="item two">B</div>
<div>
you can follow this code
<div style="display: flex; justify-content: space-between">
<button>Left Header</button>
<button style="margin-right: auto; margin-left: auto">middle</button>
</div>
You can just use flexbox "self-align".
Just check it here.

Using flex i need to create a horizontal div and underneath div in vertical [duplicate]

This question already has an answer here:
How to exclude the first item in a flexbox wrap?
(1 answer)
Closed 2 years ago.
Using Flex how can i create a layout , that has main parent div (container , display:flex) set.
The div1 to be in horizontal
div 2 and div 3 to be vertical as seen in the image.
I am new to flex and still learning
You can Achieve this even without flex, But if you need to do all the 3 div with flex then you can use this.
Here we put all the div in one contaner called main. And then we use flex property to make it a row. and then use flex-wrap to break apart. and then we give 100% to the first div as you wanted that in full width
HTML
<div id="main">
<div style="background-color:coral;" id="one">RED</div>
<div style="background-color:lightblue;">BLUE</div>
<div style="background-color:lightgreen;">Green div with more content.</div>
</div>
CSS
#main {
width: 100%;
height: 300px;
border: 1px solid black;
display: flex;
flex-wrap:wrap;
}
div{
width:200px;
}
#one{
flex:100%;
}
You can do something like this:
#MainDiv {
border: 1px solid black;
display: flex;
flex-direction: column;
height: 500px;
width: 700px;
}
.Column {
border: 1px solid red;
height: 30px;
margin: 10px;
}
.Rows {
display: flex;
flex-direction: row;
height: 450px;
width: 650px;
border: 1px solid teal;
margin: 10px;
}
.row {
height: 400px;
width: 300px;
border: 1px solid red;
margin: 10px;
}
<div id="MainDiv">
<div class="Column">Horizontal </div>
<div class="Rows">
<div class="row">Vertical Left</div>
<div class="row">Vertical Right</div>
</div>
</div>
You can use flex: 1 without specifying size in pixel for each box
.row {
display: flex;
flex-direction: row;
width: 100%;
}
.col {
border: 1px solid red;
flex: 1;
}
<div class="row">
<div class="col">One</div>
</div>
<div class="row">
<div class="col">Two</div>
<div class="col">Three</div>
</div>

How can I shrink a img to fit its flex container?

In this JSFiddle how can I downsize the img / img-container to be only as wide as its widest sibling div?
.outer {
display: inline-flex;
flex-flow: column;
}
.outer span {
display: flex;
}
div {
border: 1px dotted black;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div>
<span>this should determine width</span>
</div>
</div>
I'm not sure how cross-browser compatible this solution is, but it works on Chrome 64, Safari 11, and Firefox 57.
Give the element containing the img a width: 0; min-width: 100%; max-width: 100%;, and the img itself a width: 100%;.
Like this:
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column wrap;
}
.child {
width: 0;
min-width: 100%;
max-width: 100%;
}
.img {
width: 100%;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div class="child">
<img class="img" src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded" />
</div>
<div class="main">
<span contenteditable>this should determine width</span>
</div>
</div>
Another Solution
Use a background-image instead of an img. This allows us to make the image scale with the width of the widest element in the flexbox.
The trick is to set a padding-bottom on the element with the image proportional to the image proportions. In this case the image is square, so I'll set `padding-bottom: 100%; so it creates a square element.
If the image was a wide rectangle, 200 x 100 px, I would set padding-bottom: 50%. Or, if the image was a tall rectangle, 100 x 200 px, I would set padding-bottom: 200%.
Like this:
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column wrap;
}
.img {
background-image: url(https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded);
background-repeat: no-repeat;
background-size: cover;
padding-bottom: 100%;
}
<div class="outer">
<div>
<span>text</span>
<span>more text</span>
</div>
<div class="img">
</div>
<div>
<span contenteditable>this should determine width</span>
</div>
</div>
You can do this with CSS table layout and set width: 1% on table and white-space: nowrap on text elements.
.outer {
display: table;
width: 1%;
}
.outer span {
white-space: nowrap;
}
div {
border: 1px dotted black;
}
img {
max-width: 100%;
}
<div class="outer">
<div><span>text</span><span>more text</span></div>
<div class="image">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div><span>this should determine width</span></div>
</div>
As you asked about it for flexbox layout particularly, here is trick playing with pseudo and positions. Note, it only works if you know the image aspect ratio already, example below for a square image.
div {
border: 1px dotted black;
}
.outer {
display: inline-flex;
flex-flow: column;
}
.image {
position: relative;
}
.image:before {
content: "";
display: block;
padding-top: 100%;
/*https://stackoverflow.com/a/10441480/483779*/
}
.image img {
position: absolute;
left: 0;
top: 0;
max-width: 100%;
}
<div class="outer">
<div class="image">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png?v=73d79a89bded">
</div>
<div>this should determine width</div>
</div>
Your CSS container is already as wide as its widest sibling div. You just need to shrink the border of the picture with paint or photoshop.

min-width for wrapper DIV not working

I've two floated DIVs (two columns) which are nested in an "clear-float"-DIV, which itself is nested in an centered DIV ("wrapper" DIV).
<div id="content">
<div class="block2">
<div id="slot_left">
CONTENT-LEFT
</div>
<div id="slot_right">
*CONTENT-RIGHT*
</div>
</div>
</div>
The right column has min-width and max-width CSS option set. But the wrapper DIV, which has min-width and max-width also, is always expanded to max width.
#content {
min-width: 300px;
min-height: 80px;
max-width: 350px;
margin: 0 auto;
background: #c00;
}
.block {
overflow: hidden;
_overflow: visible;
_overflow-x: hidden;
_height: 0;
}
#slot_left {
width: 200px;
background: #ff0;
float: left;
position: relative;
}
#slot_right {
float: left;
background: #cc0;
min-width: 100px;
max-width: 150px;
position: relative;
}
What's the reason for that? I want the wrapper DIV to has minimum width required but to be centered on screen.
Here is an fiddle.
use display:inline-block
why this is happening?? div is by default block level element, so when you have given max-width, it will always obey it to occupy max area possible....
http://jsfiddle.net/sHB7g/3/
CSS
#content {
min-width: 300px;
min-height: 80px;
max-width: 350px;
margin: 0 auto;
background: #c00;
display:inline-block
}
.block {
display:inline-block;
overflow: hidden;
border:1px solid #000;
}
http://jsfiddle.net/sHB7g/1/
#content {
display: inline-block;
}
and then added a content wrapper
#contentwrapper {
text-align: center;
}
the html then is like this
<div id="contentwrapper">
<div id="content">
<div class="block">
<div id="slot_left">
CONTENT-LEFT
</div>
<div id="slot_right">
*CONTENT-RIGHT*
</div>
</div>
</div>

How to increase size of div according to its contents [duplicate]

This question already has answers here:
Wrapper div won't expand with Content div
(2 answers)
Closed 1 year ago.
I have four divs on my page. outer_div contains the other three: header , left-container and right-container. I have no concern with header and left-container. Actually my right-container div contains a dynamic table.
The problem is that when size of table grows, right-container div does not grows automatically. I mean its size stay static.
html code:
<html>
<body>
<div id="outer_div" style="background-color:Gainsboro; position:relative; top:50px; left:50px;height:550px;border-radius:8px; border:groove; width:1240px">
<div id="header" style="background-color:Khaki ; position:relative; top:5px; left:5px;height:50px;border-radius:8px; border:groove; width:1225px">
<h1 style="left:550px; position:relative; top:-7px">Admin Panel</h1>
</div> <!-- header ends-->
<div id="lef-container" style="background-color:LightSteelBlue ; position:absolute; top:65px; left:4px;height:475px;border-radius:8px; border:groove; width:280px">
</div> <!--left-container ends -->
<div id="right-container" style="background-color:LightSteelBlue ; position:absolute; top:65px; left:294px;height:475px;border-radius:8px; border:groove; width:936px">
<!-- this div contains dynamica table -->
</div> <!--right-container ends -->
</div> <!--outer div ends -->
</body>
</html>
how to fix it ?
here is css :
border:1px solid #000000;
border-collapse:collapse;
width:200px;
}
.mytable td{
background:#cccccc;
border:1px solid #000000;
}
css of table :
var tab=document.createElement('table');
tab.style.width='800';
You need to have a dynamic width and height attribute such as fit-content. The problem is that you are using a fixed width of 936px and also a fixed height of 475px so the div will never stretch to be larger than that. You can do this instead:
<div id="right-container" style="...">
</div>
Then in css:
.right-container {
min-width: 936px;
min-height: 475px;
width: fit-content;
height: fit-content;
}
When the elements inside of right-container grow, then right-container will stretch to fit them inside.
As the others have pointed out, you are using fixed widths and heights, so the elements aren't going to grow to fit the content.
But I would suggest that you're going about this in generally a wrong way.
Some larger principles to consider:
Avoid inline styling. Use CSS to style your elements instead.
Avoid fixed sizing. Using things like flexbox instead.
For example, here's how I would do what you're doing here:
$("button").click(() => {
console.log("foo");
$(".right").append("<table/>");
});
.main {
display: flex;
flex-flow: column nowrap;
/* I'm doing fixed size here, as we need an initial container size*/
height: 100px;
width: 500px;
}
header {
/**
A specific height on header seems ok.
*/
flex: 0 0 4em;
background-color: #ddf;
}
.content {
/* Content fills the rest of the container*/
flex: 1 0 auto;
display: flex;
flex-flow: row nowrap;
}
.left {
/*Have a fixed width on the left. Maybe a percentage would be better*/
flex: 0 0 10em;
background-color: #fdd;
}
.right {
/* Right fills the remainder*/
flex: 1 0 auto;
background-color: #dfd;
}
table {
/*Fixed size for demonstration.*/
border: solid 1px black;
width: 150px;
height: 150px;
}
button {
margin: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button> click me to add content</button>
<div class="main">
<header> admin panel </header>
<div class="content">
<section class="left"> left </section>
<section class="right"> right </section>
</div>
</div>
You have 3 options here:
You can use floats and clearfix.
You can use display:flex
You can use display :grid and then use flex
If you are targeting modern browsers only then option 2 and 3 will work great.
Please find below the HTML and CSS for the same - I hope it resolves your issue:
HTML
<!--outer div ends -->
<div id="outer_div" class="outer_div1">
<div id="header" class="header1">
<h1>Admin Panel</h1>
</div>
<div class="sec">
<!-- header ends-->
<div id="lef-container" class="left1">Left
</div>
<!--left-container ends -->
<div id="right-container" class="right1">Right
<!-- this div contains dynamica table -->
</div>
<!--right-container ends -->
</div>
</div>
CSS
.outer_div1 {
background-color: Gainsboro;
display: flex;
flex-direction: column;
margin: 0px auto;
max-width: 1240px;
}
.sec{
display: flex;
flex-direction: row;
}
.header1 {
background-color: Khaki;
text-align: center;
}
.header1>h1 {
}
.left1 {
background-color: LightSteelBlue;
width:calc(30% - 1.5rem);
margin: 1rem 1.5rem 1rem 1rem;
min-height: 50vh;
border-radius: .5rem;
padding:1rem;
}
.right1 {
background-color: LightSteelBlue;
width:calc(70% - 1.5rem);
min-height: 50vh;
margin: 1rem 1.5rem 1rem 1rem;
border-radius: .5rem;
padding:1rem;
}
<div style="display:inline-block;"></div>
This will increase div size according to contents in it.

Resources