When specifing a flex-box with a max-height of 400px, this max height is not respected if for example the flex-box contains 3 children having min-height: 300px.
In the following snippet, I was expecting the children to have height=400px/3, and display a scrollbar for overflow.
How can I simply achieve this?
.child {
min-height: 300px;
width: 300px;
}
.parent {
display: flex;
flex-direction: column;
max-height: 400px;
}
<div class="parent">
<div class="child" style="background-color:red"></div>
<div class="child" style="background-color:blue"></div>
<div class="child" style="background-color:green"></div>
</div>
For this you need to use another layer/element for the actual contents inside the flex children. These content elements get the min-height, the flex children don't get a height, but (by default) are allowed to shrink in height. overflow: auto; for the children allows a scrollbar to appear:
.parent {
display: flex;
flex-direction: column;
max-height: 400px;
}
.child {
width: 300px;
overflow: auto;
}
.child_contents {
min-height: 300px;
}
<div class="parent">
<div class="child" style="background-color:red">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
<div class="child" style="background-color:blue">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
<div class="child" style="background-color:green">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
</div>
Simply add overflow-y: scroll; to the .parent.
.parent {
display: flex;
flex-direction: column;
max-height: 400px;
overflow-y: scroll;
}
.child {
min-height: 300px;
width: 300px;
}
EDIT
Since you said you want the children to scroll but not the parent, you just have to add overflow-y: scroll to the children and overflow-y: hidden to the parent.
HTML
<div class="parent">
<div class="child" style="background-color:red">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
<div class="child" style="background-color:blue">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
<div class="child" style="background-color:green">
<div class="child_contents">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
</div>
</div>
</div>
CSS
.parent {
display: flex;
flex-direction: column;
max-height: 400px;
overflow-y: hidden;
}
.child {
width: 300px;
overflow-y: scroll;
}
.child_contents {
min-height: 300px;
}
Click here for codepen.
.parent {
display: flex;
flex-direction: column;
max-height: 400px;
overflow: auto;
}
Related
I'm working on a Shopify site right now and I want the image to scale at the same size of the text. Currently it looks like this:
Image of how the image is larger than the text
This is the code that I'm using.
<div class="bulky-img-section-right">
{%else%}
<div class="bulky-img-section">
{%endif%}
<div class="image-sub-section">
<img class="is-image" src="{{section.settings.image-link | img_url:'master'}}" alt="{{section.settings.image-link}}" />
</div></div>
CSS
.bulky-img-section
{
display: flex;
flex-direction: row;
width: 100%;
}
.bulky-img-section-right
{
display: flex;
flex-direction: row-reverse;
width: 100%;
}
.image-sub-section
{
width: 50%;
padding: 3rem;
}
Thanks in advance.
You can do it with some absolute position, only way I can think to remove the image from the normal flow.
:root {
--imageGap: 3rem;
--imageSize: calc( 50% - var(--imageGap) );
}
.bulky-img-section {
display: flex;
align-items: stretch;
gap: var(--imageGap);
}
.bulky-img-section:not(:last-child) { margin-bottom: var(--imageGap) }
.image-sub-section {
position: relative;
max-width: var(--imageSize); min-width: var(--imageSize);
}
.image-sub-section .is-image {
position: absolute;
height: 100%; width: 100%;
object-fit: cover;
object-position: center;
}
.right .text-sub-section { order: -1 }
<div class="bulky-img-section">
<div class="image-sub-section">
<img class="is-image" src="https://picsum.photos/id/1/200/1000" alt="Some random picture" />
</div>
<div class="text-sub-section">
<p>Eros nisi mi nec natoque erat egestas pharetra dui eu interdum vestibulum est porttitor consectetur a lectus parturient justo fringilla dis a eros dui torquent curabitur molestie dignissim. Ac vestibulum suspendisse a enim tempus accumsan vestibulum nam proin parturient nunc suspendisse adipiscing lacinia a gravida tellus ridiculus a ac at etiam fringilla parturient quis malesuada sociis neque. Ultrices arcu blandit id vestibulum suspendisse a etiam a non condimentum facilisis senectus dapibus suspendisse curae. Ligula parturient mollis natoque a a mauris turpis nec dis dui rutrum suspendisse ad eget mus a sodales blandit.</p>
<p>Gravida scelerisque neque magnis parturient commodo parturient nostra duis a ullamcorper a orci elementum nec mus bibendum justo a. Purus iaculis cubilia pharetra egestas orci rutrum auctor a ut elit himenaeos fames sociosqu eu sed a a senectus aliquet. Rhoncus aliquam erat a nibh nibh mollis suspendisse dui a risus elit nunc dictum a. Aenean integer orci arcu erat varius inceptos pulvinar condimentum etiam mauris purus dui consequat id ipsum odio semper blandit. Vulputate eleifend venenatis ridiculus metus massa non a sed et nunc dolor potenti adipiscing per cras natoque a fermentum ac varius vehicula a a quam vulputate blandit cum. Ad ullamcorper auctor a nam urna phasellus parturient a vestibulum elit fermentum mi dictumst a consectetur nisi id fames.</p>
</div>
</div>
<div class="bulky-img-section right">
<div class="image-sub-section">
<img class="is-image" src="https://picsum.photos/id/2/200/1000" alt="Some random picture" />
</div>
<div class="text-sub-section">
<p>Eros nisi mi nec natoque erat egestas pharetra dui eu interdum vestibulum est porttitor consectetur a lectus parturient justo fringilla dis a eros dui torquent curabitur molestie dignissim. Ac vestibulum suspendisse a enim tempus accumsan vestibulum nam proin parturient nunc suspendisse adipiscing lacinia a gravida tellus ridiculus a ac at etiam fringilla parturient quis malesuada sociis neque. Ultrices arcu blandit id vestibulum suspendisse a etiam a non condimentum facilisis senectus dapibus suspendisse curae. Ligula parturient mollis natoque a a mauris turpis nec dis dui rutrum suspendisse ad eget mus a sodales blandit.</p>
</div>
</div>
how can I create a x scrollable div?
The div should be placed to a fixed position?
With this code it does not scroll. I think it is because of fixed position.
.picdiv {
margin-top: 30px;
margin-left: 30px;
position: fixed;
top: 80;
width: 80%;
height: 60px;
display: inline-block;
white-space: nowrap;
overflow: hidden;
overflow-x: auto;
}
<div class="picdiv">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Bild_der_Wissenschaft_Logo.svg/400px-Bild_der_Wissenschaft_Logo.svg.png">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Bild_der_Wissenschaft_Logo.svg/400px-Bild_der_Wissenschaft_Logo.svg.png">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Bild_der_Wissenschaft_Logo.svg/400px-Bild_der_Wissenschaft_Logo.svg.png">
</div>
<div class="container">
<div class="col-md-12">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla
mauris sit amet nibh. Donec sodales sagittis magna.</p>
</div>
</div>
Thanks for help
I cannot comment but your overflow-x is not working because images are going one under another and their width is under 80% of your .picdiv class which won't use scroll x if images are in line it will work..
I need to support >= IE9
Click Run code snippet... to see my two columns
main {
background-color: limegreen;
}
.row {
overflow: hidden;
border: 1px solid red;
margin-bottom: 15px;
}
section {
float: left;
margin: 0 2%;
width: 46%;
background-color: palegreen;
}
<main>
<div class="row">
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
</div>
<div class="row">
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
</div>
<main>
I would like the each <section> to have a matching vertical height. I'm not sure how to do this using CSS.
You can stick with float if you want, by using CSS pseudo elements + position tricks for the equal height background color, see the following snippet and comments inline.
jsfiddle
main {
background-color: limegreen;
}
.row {
overflow: hidden;
border: 1px solid red;
margin-bottom: 15px;
position: relative;
}
section {
float: left;
margin: 0 2%;
width: 46%;
background-color: palegreen;
position: relative;
z-index: 1; /* top of the pseudo elements */
}
.row:before, .row:after {
content: "";
position: absolute;
top: 0;
background: palegreen;
width: 46%; /* same as section width */
height: 100%;
}
.row:before {
left: 2%; /* left margin */
}
.row:after {
left: 52%; /* 2% + 46% + 2% + 2% */
}
<main>
<div class="row">
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
</div>
<div class="row">
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
</div>
</main>
Another approach would be CSS table + table-row + table-cell, like others already suggested in the comments above. I'm adding it here, plus the extra border-collapse + border-spacing features, for drawing the gaps. When comes to responsiveness or mobile style, you can simple reset the relevant display:table* properties back to display:block or so.
jsfiddle
main {
background-color: limegreen;
display: table;
border-collapse: separate;
table-layout: fixed;
border-spacing: 20px;
width: 100%;
}
.row {
display: table-row;
}
section {
display: table-cell;
vertical-align: top;
background: palegreen;
}
<main>
<div class="row">
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
</div>
<div class="row">
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
</div>
</main>
The outputs between the two solutions are slightly different, but mostly adjustable. One more thing on HTML syntax - the last <main> should be the closing </main>.
EDIT
Added table-layout:fixed for easy equal width of the columns, no matter how many of them.
just add display: flex; to container
main {
background-color: limegreen;
}
.row {
overflow: hidden;
border: 1px solid red;
margin-bottom: 15px;
display: flex;
}
section {
margin: 0 2%;
width: 46%;
background-color: palegreen;
}
<main>
<div class="row">
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
</div>
<div class="row">
<section>
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas sed diam eget risus varius blandit sit amet non magna. Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum.</p>
<p>Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed consectetur. Etiam porta sem malesuada magna mollis euismod. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
</section>
<section>
<p>Nulla vitae elit libero, a pharetra augue. Cras mattis consectetur purus sit amet fermentum.</p>
<p>Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</section>
</div>
<main>
If you want IE 9 support you can use Match Height JS
I am a newbie to CSS. I started exploring CSS float property. I have two <div> elements inside a container. The two divs have equal width and height.
<div class="container">
<div class="one">
</div>
<div class="two">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</p>
</div>
</div>
And CSS for the above is :
.one{
width: 200px;
height: 200px;
background-color: green;
border: 1px solid blue;
float: left;
}
.two{
width: 200px;
height: 200px;
background-color: red;
border: 1px solid black;
overflow:visible;
}
.container{
overflow:hidden;
}
From the above what I can understand is that since the div that is floated is removed from the normal flow and floated to left and the second div which contains the text inside <p> elements merges underneath the .one div. And moreover since both have the same width and height I cannot see the content.
But when I set the overflow property of the .two to scroll I see the second div being placed adjacent to first div. This is confusing. Why is such a behavior? Please explain me. Link to codepen attached below for more details.
Link to Codepen
The float CSS property specifies that an element should be taken from the normal flow. However the non-float element still takes all the available width including the floating element. But if you set overflow: auto; or hidden to it, it will then align next to the floating element. See the demo following, it should explain it clearly.
.a1, .b1 {
background: rgba(0,0,0,.5); /*pure black with alpha*/
width: 50px;
height: 50px;
float: left;
}
.a2 {
background: lime;
}
.b2 {
background: lime;
overflow: auto; /*let the browser decide whether to clip or not*/
}
<div class="a1">hello</div>
<div class="a2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>
<br/>
<div class="b1">hello</div>
<div class="b2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>
What is happening here is because:
.container{
overflow:hidden;
}
What is happening?
Your floating div wants to take up space before your other div. Because your non-floating div has display: block, it wants a whole line to render itself. The floating div is pushing down your non-floating div which is then hidden by the style above. If you change display to inline-block, it will take up only the space it needs using the width and height properties.
Here is an example: http://codepen.io/anon/pen/LVpxZy
Given This markup
CSS
#wrapper {
width: 900px;
}
#column1 {
width: 320px;
float: left;
}
#column2 {
width: 570px;
float: left;
padding-left: 10px;
}
#social {
text-align: center
}
HTML
<div id="wrapper">
<div id="column1">
<div id="image">
<a href="http://stackoverflow.com">
<img width="320" src="http://appharbor.com/assets/images/stackoverflow-logo.png">
</a>
</div>
<div id="social">
Some Social Plugins/Buttons Whatever
</div>
</div>
<div id="column2">
<div>
SOME REALLY LONG CONTENT WHICH IS USER-GENERATED (Can't be styled or this structure of divs and br be expected)
</div>
<br />
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac ante ac dolor interdum tristique vitae sit amet erat. Quisque auctor sagittis augue, vitae eleifend nibh egestas quis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris condimentum varius orci, ac accumsan mauris viverra nec. Nunc ullamcorper dapibus velit, ac vehicula turpis placerat quis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
</div>
<br />
<div>
Vivamus quis sem lectus. Phasellus lobortis semper nulla, ut imperdiet neque aliquam at. In tincidunt, mauris quis condimentum tempor, tellus magna mollis magna, a laoreet purus metus id tellus. Proin sagittis, sapien a tristique consequat, tellus neque feugiat massa, vel vulputate ligula lectus nec enim. Aliquam erat volutpat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
</div>
<br />
<div>
Suspendisse dapibus diam eu felis convallis sed iaculis lorem aliquam. Duis sit amet metus a lectus commodo sodales vel a lacus. Praesent auctor facilisis ligula vitae convallis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam mi leo, ultrices vitae consequat at, luctus in arcu. In tincidunt pellentesque ornare. Sed laoreet porta ante, at euismod ipsum volutpat facilisis. Donec at augue iaculis ligula interdum consequat vitae quis turpis. Donec pulvinar velit nec arcu eleifend faucibus. Praesent placerat magna eget ipsum iaculis nec condimentum nibh egestas. Donec massa tellus, porttitor vitae posuere sed, dapibus quis sapien. Sed elit est, dapibus sit amet pulvinar et, volutpat non quam. Aenean eget est tellus. Mauris at tincidunt dui.
</div>
<br />
<div>
SOME REALLY LONG CONTENT WHICH IS USER-GENERATED (Can't be styled or this structure of divs and br be expected)
</div>
</div>
</div>
Also Hosted At: http://jsfiddle.net/bbQPE/1/
Is It Possible to fill the space in column one with the text in column two?
PS: Without a fixed width and the float for column2 seems to be the answer but seems like a hack more than a pure-css solution or m i wrong.?
Remove floating form column2
#column2 {
width: 570px;
padding-left: 10px;}