CSS float margins between image and text - css

Apologies if this is something trivial, but I can't seem to find a way around this.
So I have this very simple layout. 2 div elements floated to the left and one to the right. On the right there is an image, while on the left there is the text. At the top of the text part (also on the left) I want to have another div just a one liner, with a different background colour with the article's details, such as the author, the date etc.
<div id="mainarticle">
<article>
<div id="mainimgcontainer">
<img id="mainimage" src="theimage.jpg" />
</div> <!-- #mainimgcontainer -->
<div id="mainarticle_details">
<span id="by">BY JOE BLOGGS</span><span class="mainarticle_date">11/04/2014</span>
</div><!-- #mainarticle_details -->
<div id="maintext">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div> <!-- #maintext -->
</article>
</div> <!-- #mainarticle -->
The right column with the image will be 50% of the screen width. The rest will flow beside it and under it.
#mainimgcontainer
{
width: 50%;
float: right;
margin-left: 10px;
}
div#mainarticle_details
{
background-color: #f7f2e7;
border-bottom: 2px solid #dac397;
display: block;
line-height: 22px;
padding-left: 10px;
padding-right: 10px;
vertical-align: bottom;
overflow: auto;
height: 23px;
}
img#mainimage
{
width: 100%;
min-width: 100px;
margin: 0px;
padding: 0px;
max-height: 32%;
}
Now, in order for the main text not to touch the image, I put a margin-left : 10px; on the image. However, This also puts a gap between the top #mainarticle_details and the image. I wish these 2 to touch each other.
On the other hand, if I add a padding-right or margin-right to just to the #maintext, it only applies to when the text is taking the full width, and the text still touches the image.
Is there any way to make the top #mainarticle_details touch the image, but the text spaced 10px from the image?
I created a jsfiddle here: http://jsfiddle.net/A7uSr/29/
UPDATE
Forgot to mention, this is intended for a mobile website. So it needs to work for varying screen widths. fixed positioning is also very buggy on mobile devices like the stock Android browsers (not Chrome) so keep that in mind too. This will eventually be the landscape view of the article (the portrait view will just have the image at the top and text underneath). For this reason the image has to be on top of the text, and CSS will just move it to the side when the device is turned to landscape mode.
I also added some more details from my real scenario which I realised will effect the solution. Mainly the padding of the containing div and the size of the image.
JSFiddle also updated.

I had success by setting the header to position:absolute and width:100%. This allows the header to extend behind the image, making it appear to touch the image while the text retains a 10px margin.
I moved the image above the header in the HTML to avoid having to set z-index.
I also set a CSS definition for the image, making it respect its container's percentage width.
WORKING EXAMPLE (jsfiddle)
HTML:
<div id="mainarticle">
<article>
<div id="mainimgcontainer">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/specoalpurpose/micro/af-s_vr_micro-nikkor_105mmf_28_if/img/sample/sample_l.jpg" />
</div>
<!-- #mainimgcontainer -->
<div id="mainarticle_details"> <span id="by">BY JOE BLOGGS</span></div>
<!-- #mainarticle_details -->
<div id="maintext">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Nam blandit lectus quis vestibulum blandit. Nunc viverra lectus eget tristique tempor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc imperdiet porta tincidunt. In vitae felis volutpat nisi pulvinar aliquam. Ut tempor sagittis congue. Aenean at commodo lorem. Ut faucibus tellus egestas facilisis dictum. Quisque eget mi et elit tincidunt porttitor. Pellentesque egestas libero sit amet urna sodales, non hendrerit lorem tempus. Donec risus enim, convallis in hendrerit sed, dapibus at purus. Vestibulum eu nisl at velit placerat mollis sed quis neque. In hac habitasse platea dictumst. Vestibulum venenatis at lacus a ullamcorper. Integer eget lectus id tortor bibendum imperdiet.</p>
<p>Maecenas consectetur eros erat, quis pharetra nulla ornare eget. Nam ac risus porttitor, fringilla quam eu, cursus lacus. Cras adipiscing enim vitae leo dictum, at varius sapien bibendum. Quisque consequat suscipit purus, ac luctus ipsum volutpat ut. Nam fermentum tristique turpis. Sed semper orci et turpis placerat, non tincidunt est eleifend. Aenean a ligula eu libero facilisis scelerisque sit amet fringilla sapien. Aenean dictum ante orci, vel facilisis metus posuere ut. Maecenas sed odio ut velit tincidunt venenatis in ut lacus. Pellentesque condimentum tellus eu mollis hendrerit. Mauris adipiscing arcu ut fringilla aliquam. Nullam ultricies ac est nec cursus. Morbi vitae leo id nisi placerat tincidunt. Ut pulvinar, justo vel euismod aliquet, urna lorem feugiat metus, sit amet sagittis ipsum nibh quis augue. Vestibulum quis convallis turpis. Aenean fringilla ut nulla et laoreet.</p>
<p>Nunc id nibh neque. Fusce ultricies quam vehicula elit dictum convallis. Aenean auctor, massa venenatis ullamcorper semper, ante purus vestibulum lacus, a ullamcorper arcu enim sit amet ante. Nulla aliquet vel lacus eu vulputate. Morbi elit metus, laoreet vel nunc at, consectetur pellentesque lectus. Ut blandit mi in odio sollicitudin, sit amet sodales lorem sollicitudin. Suspendisse lobortis urna sit amet faucibus posuere. Mauris cursus ac neque vitae malesuada. Curabitur a lorem ut nisi vestibulum scelerisque a id risus. Nulla mattis rhoncus congue. Quisque nec dolor nulla. Sed tempus nisl aliquam, pellentesque lacus in, faucibus felis. Nulla sit amet arcu rhoncus, blandit nunc sit amet, tincidunt erat.</p>
<p>Sed vehicula enim velit, vitae fermentum felis consectetur nec. Sed sollicitudin vehicula leo quis laoreet. Mauris lobortis vitae massa id malesuada. Nam sit amet vulputate est, ut adipiscing orci. Quisque non sapien lectus. Nullam dapibus mauris non nisi lacinia, sodales posuere sem pellentesque. Sed tincidunt nibh id lacus rutrum sodales. Sed consequat elementum quam, sit amet tempus lorem ornare quis. Etiam sit amet aliquam ligula, ut feugiat ipsum. Nunc condimentum turpis nibh, vel faucibus arcu pretium ac. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris quis justo justo. Nam molestie suscipit velit, et ullamcorper mi laoreet in. Cras congue euismod odio, ut sagittis ligula feugiat sit amet.</p>
</div>
</article>
</div>
CSS:
body {
position:relative;
margin:10px;
}
#mainimgcontainer {
position:relative;
width: 50%;
float: right;
margin-left:10px;
}
#mainimgcontainer img {
width:100%;
}
div#mainarticle_details {
background-color: #f7f2e7;
border-bottom: 2px solid #dac397;
line-height: 22px;
height: 23px;
position:absolute;
width:100%;
}
div#mainarticle_details span {
padding:0 0 0 10px;
}
div#maintext {
position:relative;
padding:23px 0 0;
}
(As a personal note, I think it looks better to keep the margin consistent or to put the header above the image and text.)
UPDATE:
As you mentioned, longer headings were not handled well. If they were too long, they disappeared behind the image.
I realized that position:absolute was not neccesary. I removed it from the heading in order to allow wrapped, multi-line headings. The width:100% part keeps the header background extended behind the image while still allowing the header text to wrap to another line.
I also added a media query to demonstrate how the layout can change for mobile.
<div id="mainarticle_details">
<p id="by">BY JOE BLOGGS AND A LONGER HEADING</p>
</div>
div#mainarticle_details {
background-color: #f7f2e7;
border-bottom: 2px solid #dac397;
line-height: 22px;
position:relative;
width:100%;
}
div#mainarticle_details p {
margin:0;
padding:0 0 0 10px;
}
#media (max-width: 600px) {
div#mainimgcontainer {
float:none;
width:100%;
margin:0;
}
}
WORKING EXAMPLE (jsfiddle)

What's Going On
We need the margin of the image to not affect the header div, so we need to position it absolutely. We also need to set the overall wrapper to position:relative to catch the positioning of the header. Finally, we need to add a padding-top to the text.
Code
#mainarticle article {
position:relative;
}
div#mainarticle_details {
width:47%; /* This could be 46%, but due to sub-pixel issues pointed out in comments, if you put 46%, some browsers will show a tiny break. */
padding:0 2%;
position:absolute;
z-index:-1;
}
#maintext {
padding-top: 15px;
}
Working Fiddle
Result

1). Your mainarticle_details is not touching image because of margin-left: 10px; in mainimgcontainer. Make it 0.
2). Once you do a float:right, it remains on top of whatever content is there behind it. That means, your maintext is behind your image. That means, if you don't want it to touch the image, add padding-right: 52%;. Why 52%, because image is taking 50%, so 52 will give some padding and text will not touch image !
jsFiddle

Related

How to translate a whole column on the y axis

I have this grid view , I need to translate the items of the second column on the y axis as you see in the image, I tried to do it using translateY() but it will require you to scroll in order to see the translated items but I don't want any kind of scroll actions.
Design sample
Grid is a grid line/cols, meaning cells are aligned.
if you want this translate, you have first to define a grid. Make the even cell relative, and the div inside this cell absolute. On it you can apply the translate.
* {
margin: 0;
padding: 0;
color: #FFFFFF;
background-color: #131313;
font-family: sans-serif;
}
#container {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr;
gap: 0;
width: 100%;
height: 100%;
}
#container>div {
min-height: 300px;
position: relative;
padding: 1em;
}
#container>div:nth-child(even)>div {
background-color: lightgrey;
position: absolute;
width: calc(100% - 2em);
transform: translateY(100px);
z-index: 1;
}
#div1 {
background-color: rgba(128, 135, 81, 0.5);
}
#div2 {
background-color: rgba(147, 0, 65, 0.5);
}
#div3 {
background-color: rgba(111, 6, 26, 0.5);
}
#div4 {
background-color: rgba(37, 65, 97, 0.5);
}
<div id="container">
<div id="div1">
<div>
<img src="https://picsum.photos/id/237/200/200">
<br> Urna neque viverra justo nec. Tempor commodo ullamcorper a lacus vestibulum sed arcu non odio. Viverra aliquet eget sit amet tellus. Nullam non nisi est sit amet. Rhoncus mattis rhoncus urna neque viverra justo nec ultrices dui. Dapibus ultrices
in iaculis nunc. Suspendisse potenti nullam ac tortor.
</div>
</div>
<div id="div2">
<div>
<img src="https://picsum.photos/id/124/200/200">
<br> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
<div id="div3">
<div>
<img src="https://picsum.photos/id/87/200/200">
<br> Nisl nisi scelerisque eu ultrices vitae. Erat pellentesque adipiscing commodo elit at imperdiet dui. Nulla facilisi cras fermentum odio eu. At imperdiet dui accumsan sit amet nulla facilisi. Ullamcorper sit amet risus nullam eget felis. Ultrices
sagittis orci a scelerisque purus semper eget duis. Eu facilisis sed odio morbi. Dui accumsan sit amet nulla. Quisque id diam vel quam elementum pulvinar etiam non. Praesent semper feugiat nibh sed pulvinar proin gravida.
</div>
</div>
<div id="div4">
<div>
<img src="https://picsum.photos/id/111/200/200">
<br> In hac habitasse platea dictumst quisque sagittis purus sit amet. Ut diam quam nulla porttitor. Sit amet consectetur adipiscing elit ut aliquam purus sit. Malesuada pellentesque elit eget gravida cum sociis. Eu nisl nunc mi ipsum faucibus vitae
aliquet. Sit amet volutpat consequat mauris nunc congue nisi. Libero justo laoreet sit amet.
</div>
</div>
</div>

Flexbox container shrinking below min-height: max-content

I am working on a flexbox side bar menu, but seem to be running into conflicts between flex: 0 1 auto containers with an intrinsic size for min-height, causing the containers to shrink below the allowed minimum.
JSFiddle Link (same as snippet below)
Summary / Intended behavior
The menu spans the entire vertical screen space and consists of an arbitrary number of minimizable-group containers that are neatly stacked on top of each other.
Each group has a flex: none header/title and a flexible container minimizable-group-content
There are two group-content types:
.unshrinkable for important control elements that should remain fully visible to the user (red boxes in snippet)
.shrinkable for potentially huge lists, which are allowed to shrink to some minimum height (blue boxes in snippet)
Each minimizable group can be collapsed by the user (⮟ button) -- that part works fine currently.
Issue
In the snippet you can see all three minimizable groups (in full-screen). The unshrinkable content in Group 2 however gets squeezed together, unless I collapse either Group 1 or Group 3
From looking at the Dev Console, it seems the problem is in minimizable-group-content. The red .unshrinkable box inside shows a reasonable calculated height (18 pixel), but the enveloping container only has a calculated height of ~5px and refuses to resize no matter what CSS I throw at it.
Is there any other property I can set to prevent my flex boxes to shrink past the minimum height? Or can I get the intended behavior with a different approach?
(Not) related:
Parent flexbox container ignores child's flexbox min-width -- used that as well, but covers the exact opposite: elements not shrinking
https://github.com/philipwalton/flexbugs#flexbug-1 -- would match, but it seems that one is fixed already
How do min-content and max-content work? -- explanation for intrinsic dimensions - which I think I am using correctly?
Safari: flexbox and min-height -- tried fit-content/min-content/max-content, but none seem to have any effect
Snippet
Array.from(document.getElementsByClassName("minimizable-group")).forEach((element) => {
element.children[0].addEventListener('click', () => {
element.children[0].classList.toggle('minimized');
element.children[1].classList.toggle('minimized');
});
});
* {
min-height: 0px;
}
html,
body {
position: absolute;
margin: 0px;
padding: 0px;
overflow: hidden;
top: 0px;
left: 0px;
height: 100vh;
width: 300px;
}
body {
display: flex;
flex-direction: column;
}
.container {
display: flex;
flex-direction: column;
flex: 0 1 auto;
overflow-y: auto;
}
.minimizable-group {
display: flex;
flex-direction: column;
flex: 0 1 auto;
overflow-y: hidden;
}
.minimizable-group-title {
flex: none;
}
.minimizable-group-content {
display: flex;
flex-direction: column;
flex: 0 1 auto;
overflow-y: auto;
min-height: min-content !important;
}
.unshrinkable {
flex: none;
min-height: max-content !important;
}
.shrinkable {
flex: 0 1 auto;
overflow-y: auto;
min-height: 1em;
}
/*********************************/
/* IRRELEVANT STUFF BELOW */
.minimizable-group-title {
cursor: pointer;
color: #aaa;
background-color: #333;
font-weight: bold;
}
.minimizable-group-title::before {
content: '⮟ ';
}
.minimizable-group-title.minimized::before {
content: '➤ ';
}
.minimizable-group-content.minimized {
max-height: 0px;
visibility: hidden;
}
.unshrinkable {
background-color: #ffcccb;
}
.shrinkable {
background-color: #add8e6;
}
<div class="container">
<div class="minimizable-group">
<div class="minimizable-group-title">Group 1</div>
<div class="minimizable-group-content">
<div class="unshrinkable">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="shrinkable">Amet venenatis urna cursus eget nunc. Sapien nec sagittis aliquam malesuada. Tortor vitae purus faucibus ornare suspendisse. Cursus eget nunc scelerisque viverra mauris in aliquam sem. Et leo duis ut diam quam. Nisl suscipit adipiscing bibendum
est ultricies integer quis auctor. Volutpat maecenas volutpat blandit aliquam etiam erat. Fames ac turpis egestas integer eget aliquet nibh. Enim praesent elementum facilisis leo vel. Enim facilisis gravida neque convallis a. Et magnis dis parturient
montes nascetur ridiculus mus mauris vitae. Amet est placerat in egestas.
<p /> Vel eros donec ac odio tempor. Purus gravida quis blandit turpis cursus in hac habitasse platea. In aliquam sem fringilla ut morbi tincidunt augue interdum. Diam ut venenatis tellus in metus. Vitae purus faucibus ornare suspendisse sed nisi lacus
sed. Velit sed ullamcorper morbi tincidunt. Tempus quam pellentesque nec nam aliquam sem et tortor consequat. Augue mauris augue neque gravida in fermentum. Nunc aliquet bibendum enim facilisis gravida neque convallis a cras. Sem et tortor consequat
id. Diam sollicitudin tempor id eu nisl nunc mi ipsum. Eros donec ac odio tempor orci dapibus ultrices.
</div>
<div class="unshrinkable">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
<div class="minimizable-group">
<div class="minimizable-group-title">Group 2</div>
<div class="minimizable-group-content">
<div class="unshrinkable">Elementum sagittis vitae et leo duis</div>
</div>
</div>
<div class="minimizable-group">
<div class="minimizable-group-title">Group 3</div>
<div class="minimizable-group-content">
<div class="unshrinkable">Facilisis volutpat est velit egestas dui id ornare arcu odio. Duis ultricies lacus sed turpis tincidunt id.</div>
<div class="shrinkable">Amet venenatis urna cursus eget nunc. Sapien nec sagittis aliquam malesuada. Tortor vitae purus faucibus ornare suspendisse. Cursus eget nunc scelerisque viverra mauris in aliquam sem. Et leo duis ut diam quam. Nisl suscipit adipiscing bibendum
est ultricies integer quis auctor. Volutpat maecenas volutpat blandit aliquam etiam erat. Fames ac turpis egestas integer eget aliquet nibh. Enim praesent elementum facilisis leo vel. Enim facilisis gravida neque convallis a. Et magnis dis parturient
montes nascetur ridiculus mus mauris vitae. Amet est placerat in egestas.
<p /> Vel eros donec ac odio tempor. Purus gravida quis blandit turpis cursus in hac habitasse platea. In aliquam sem fringilla ut morbi tincidunt augue interdum. Diam ut venenatis tellus in metus. Vitae purus faucibus ornare suspendisse sed nisi lacus
sed. Velit sed ullamcorper morbi tincidunt. Tempus quam pellentesque nec nam aliquam sem et tortor consequat. Augue mauris augue neque gravida in fermentum. Nunc aliquet bibendum enim facilisis gravida neque convallis a cras. Sem et tortor consequat
id. Diam sollicitudin tempor id eu nisl nunc mi ipsum. Eros donec ac odio tempor orci dapibus ultrices.</div>
</div>
</div>
</div>

Limit content width in css grid column [duplicate]

This question already has answers here:
Prevent content from expanding grid items
(3 answers)
Closed 5 years ago.
I meet a confused problem with CSS grid. I have a grid with 2 columns, the first is 100px, the second is fit the remaining (grid-template-columns: 100px auto). Everything works right. But if the second column has an extra large element, the width of second column is overflow. I tried to use the max-width but it doesn't work.
Here is my fiddle, please check it: https://jsfiddle.net/truongwp/ka54e7u4/1/
.container {
display: grid;
grid-template-columns: 100px auto;
grid-gap: 30px;
width: 400px;
}
.right {
max-width: 100%;
}
.text {
width: 700px;
max-width: 100%;
}
<div class="container">
<div class="left">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed finibus lorem in iaculis dictum. Etiam nec urna et erat volutpat ultrices non vel elit. Nullam commodo tortor a est luctus, et semper ipsum suscipit. Praesent in ipsum quis odio imperdiet posuere
in vitae leo.
</div>
<div class="right">
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed finibus lorem in iaculis dictum. Etiam nec urna et erat volutpat ultrices non vel elit. Nullam commodo tortor a est luctus, et semper ipsum suscipit. Praesent in ipsum quis odio imperdiet posuere
in vitae leo. Aliquam facilisis at justo vel pellentesque. Quisque vitae lobortis nibh, commodo facilisis ante. Nullam facilisis leo vel aliquet egestas. Etiam commodo porta lorem pretium suscipit. Morbi finibus est ac ex suscipit, at feugiat magna
facilisis. Sed tempor ex interdum lobortis gravida. Sed rutrum semper sapien, at finibus metus maximus in. In turpis augue, pellentesque at lectus nec, porta elementum justo. Sed consequat nec dui et interdum. Aenean placerat orci sem, ut blandit
ex semper sit amet.
</div>
</div>
</div>
Thank you very much!
Try to add
overflow: auto
Like this:
.right {
max-width: 100%;
overflow: auto
}
It will fix your trouble!
https://jsfiddle.net/ka54e7u4/2/

bootstrap equal height divs (Responsive Design)

I have a side-bar div that will have ads. It should have a fixed width of about 333px and need it to be responsive is possible in its height.
It has a background but when content loads in the main div the side-bar div will not extend the length of the main content. I have tried a few things but get varied results in different browsers.
When in the mobile view or when it's in a small view the side bar should fit to the size of the window width wise and only be has high as the content in the side-bar.
I have loaded a few images for examples to show what I need.
Below is the ccs I have
.main
{
padding: 20px;
}
.side-bar
{
background-image: url('../images/BlueBG.jpg');
width: 333px;
display: block;
height: 100%;
}
This has been asked a few times on SO, but you can use flexbox for this. If you are using bootstrap, you most likely will have a container-fluid class. Simply set the display to flex and all the items inside will become flex-items. This will allow you to have them stretch. In your case, you will have to set container-fluid back to block or set your main and side-bar to flex-basis:100% in a media query for your mobile.
Here's the fiddle: http://jsfiddle.net/56we9rmj/2/
HTML
<div class="container-fluid">
<div class="main"><p>Arcu dictumst nec ultricies aptent rhoncus. Sed fermentum ligula. Donec vitae felis. Lectus nec ad. Tempus et quam. Nec dolor eu. Lacus at in eu dolor penatibus. Quis vivamus vehicula. Mauris dui ullamcorper diam eget pretium lectus consectetuer ultrices tincidunt sit nulla. Lobortis lacus et. Dolor ea placerat etiam diam aenean integer nec erat. Suscipit ut elementum. Consectetuer dui id vestibulum cras egestas. Quis nulla nulla. Pariatur pellentesque amet taciti neque lorem fermentum vehicula amet elit blandit pellentesque. Cras sit gravida. Cras vulputate curabitur mauris purus semper mauris lacus et et pulvinar in justo nullam qui sed quam massa. Integer amet ullamcorper. Feugiat quis sed quam fusce non feugiat amet vitae. Arcu elementum eget justo ac sed quis id tellus.</p>
<p>Vivamus non cras. Turpis in eleifend mattis nam arcu aliquam vulputate felis. Dignissim ligula dignissim habitant nonummy proin. Mauris varius varius. Purus lorem ullamcorper dictum cras in felis ullamcorper vitae. Nunc amet interdum nec adipiscing tempus ac vestibulum primis. Nisl purus orci sed sunt mauris. Odio donec nulla. Pellentesque arcu felis. Et varius ornare eros id quisque. Vel dui velit arcu eget in dignissim nunc nec habitasse habitasse elit quis ac aptent duis volutpat facilisis varius ut sem consectetuer erat arcu. Mauris condimentum sodales luctus a ullamcorper amet a pellentesque tellus ac sit. Nam mauris nulla neque aenean tempus. Mi dui ipsum. Laoreet vitae mauris. Arcu at tristique. Quia mi praesent nibh eu est. Dui libero condimentum elementum risus risus. Ut feugiat diam. Quam semper erat felis ultricies vel. Sed proin sollicitudin. Etiam eleifend morbi imperdiet purus pharetra. Vel diam feugiat. Vel volutpat vulputate. Enim ligula fringilla at nunc risus.</p>
</div>
<div class="side-bar">
<img src="http://placehold.it/300x100" />
<img src="http://placehold.it/300x100" />
</div>
</div>
CSS
.container-fluid {
display:flex;
align-items:stretch;
align-content:stretch;
}
.main {
flex-basis:66.66666667%;
padding:20px;
}
.side-bar {
background:red;
flex-basis:33.3333333%;
}
.side-bar img {
width:100%;
}
#media screen and (max-width:Mobile-PX-HERE) {
.container-fluid {
flex-wrap:wrap;
}
.main, .side-bar {
flex-basis:100%;
}
}
Hope that helps!
Giving position:absolute; and 100% height to the sidebar will do. http://jsfiddle.net/fdf01y4b/
Resize the window or add more text to see it in action
.side-bar {
height: 100%;
position: absolute;
top: 0;
right: 0;
}
If you have problems with the sidebar going out of the parent div, just give position:relative; to the parent wrapper like I did on the Jsfiddle example
<div class="wrapper">
<div class="main">Content</div>
<div class="side-bar">Sidebar</div>
</div>
.wrapper{
position:relative;
}
For smaller screens you can just remove the sidebar position:absolute; to position:initial; with media queries:
#media (max-width: 600px){
.side-bar{
position: initial;
}
}

Display: table-cell width of box (percentage)

I have this CSS code:
section.products {
display: table;
width: 100%;
table-layout: fixed;
}
section.products > article {
display: table-cell;
width: 33%;
}
This does not set the width correctly (the width of every article is set automatically to fit all article elements to one row). But when I set <article style="width: 33%;"> (inline declaration), the width is right. What is wrong? Thanks for any advice.
EDIT (DEMO): https://jsfiddle.net/Lt822wkq/
The table layout works if you have three child elements as shown below.
If you only have one child element, it will expand to fill up the entire available width, even if you apply an inline style for the width, see Examples 2 and 3.
However, if you set display: block to the single child article, then the width will be 33%, but that is because the element in no longer behaving like a table cell.
section.products {
display: table;
width: 100%;
table-layout: fixed;
border: 1px dotted gray;
}
section.products > article {
display: table-cell;
width: 33%;
border: 1px dotted gray;
}
section.products > article.blocky {
display: block;
}
<section class="products">
<article>Article One Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
<article>Article Two Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
<article>Article Three Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
</section>
<h2>Example 2</h2>
<section class="products">
<article>Article One Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
</section>
<h2>Example 3</h2>
<section class="products">
<article style="width: 33%;">Article One Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
</section>
<h2>Example 4</h2>
<section class="products">
<article class="blocky" style="width: 33%;">Article One Donec adipiscing, lorem non euismod venenatis, diam orci tincidunt magna, ut interdum magna arcu vel elit. Nunc molestie lacus non urna eleifend mattis. Praesent ipsum nulla, tempor malesuada lacinia quis, elementum et tellus. </article>
</section>
When you set article {width: 33%;} in the stylesheet that applies to every single <article> element. However if you use inline style <article style="width: 33%;"> that applies to that element only.
In your demo, there are 4 table cells total, and you set each to 33%, that won't work correctly because the total width exceeds 100%.

Resources