I have three divs. A container div, that contains two child divs inside. The first child div has a fixed width, but height should be determined by the text inside (it's a message box). Right after that div on the same line, should be another div one line high (basically, the time) and it should be located at the bottom of the parent div (like, if the first child div contains say 5 lines, then the second child div must be right after the first child div, at the level of the lowest line of the first child (of the message box))
Basically, I'm trying to recreate this thing:
Notice how the time is right next to the message?
Anyway, for this I figured I needed to make my time div the size of the parent div. How would I go about doing it?
Thanks in advance
So, this would be the HTML
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod </p>
</div>
<div class = "dateandtime">11:37AM</div>
</div>
And CSS:
.yellowmessage { */ Parent div /*
padding:0 2% 0 0;
float:left;
}
.themessage {
font-size:2.5em;
display: inline-block;
border-radius:20px;
padding:3% 0 3% 0;
max-width:90%;
background-color:rgb(246,227,143);
float:left;
}
.dateandtime {
float:left;
}
You can use this, There is an answer here about your question.
I have added time to that. I have done some changes.
Live Demo **http://jsfiddle.net/mek5Z/1348/**
Fiddle
Assuming that this is your HTML structure:
<div class="yellowmessage clear">
<div class="themessage">
<p contenteditable="true">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod</p>
</div>
<div class="dateandtime">11:37AM</div>
</div>
<div class="bluemessage clear">
<div class="themessage">
<p contenteditable="true">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ornare risus vulputate consequat semper. Quisque facilisis facilisis pulvinar. Maecenas aliquam, orci id rhoncus tempus, lacus tellus mattis diam, eget elementum eros dui vel leo.</p>
</div>
<div class="dateandtime">11:49AM</div>
</div>
This could be your CSS
.yellowmessage, .bluemessage {
position: relative;
margin: 5% 2%;
}
.themessage {
font-size:1.85em;
border-radius:20px;
padding: 0 2%;
max-width: 90%;
box-shadow: inset 0 -4px rgba(0, 0, 0, 0.07), inset 0 22px 25px rgba(255, 255, 255, 0.58), 0 3px 11px rgba(0, 0, 0, 0.28);
}
.yellowmessage > .themessage {
float: left;
background-color: #FFF980;
}
.bluemessage > .themessage {
float: right;
background-color: #A7DBD8;
}
.dateandtime {
position: absolute;
bottom: 0;
font-size: 80%;
}
.bluemessage > .dateandtime {
left: 0;
}
.yellowmessage > .dateandtime {
right: 0;
}
Related
So I'm trying to create a timeline with CSS for my online resume. My plan is to make a grid with two basic columns and a third column between them for the line and its styling. I want to place every other element on the right side of the line (the third column of the grid). Is there a way to use :nth-child(odd), or something similar, to achieve this? I can of course use grid-area, but I'd rather not place every element individually.
This is my current grid:
.timeline {
display: grid;
grid-template-columns: 49% 2px auto;
grid-template-rows: repeat(5, 1fr);
grid-template-areas:
"event1 line . "
" . line event2"
"event3 line . "
" . line event4"
"event5 line . "
" . line event6";
text-align: center;
}
.line {
grid-area: line;
background-color: black !important;
margin: 0 !important;
padding: 0 !important;
}
.timeline :nth-child(2) {
grid-area: event1;
}
.timeline :nth-child(3) {
grid-area: event2;
}
.timeline :nth-child(4) {
grid-area: event3;
}
.timeline :nth-child(5) {
grid-area: event4;
}
.timeline :nth-child(6) {
grid-area: event5;
}
.timeline :nth-child(7) {
grid-area: event6;
}
It works, but there must be a more efficient way to do this.
One approach is as follows.
const addTask = () => {
const D = document,
timeline = D.querySelector('ol.timeline'),
taskElement = D.createElement('li'),
span = D.createElement('span'),
div = D.createElement('div');
span.classList.add('connection');
div.textContent = timeline.children.length + 1;
taskElement.append(span, div);
timeline.appendChild(taskElement);
}
document.querySelector('#addEntry').addEventListener('click', addTask);
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
button {
display: block;
width: 80vw;
margin: 0.5em auto;
height: 2em;
line-height: 2em;
}
:root {
--timeline_connector_position: 30%;
--timeline_margin: auto;
--timeline_gapSize: 0.2rem 0rem;
--timeline_centre: 5vw;
--timeline_width: 90vw;
--timeline_padding: 0.5rem;
--timeline_taskBackground: #fff;
--timeline_taskBackground_even: #f90;
--timeline_taskBackground_odd: limegreen;
--timeline_taskBorder: 1px solid #000;
--timeline_taskBorder_even: 1px solid #000;
--timeline_taskBorder_odd: 1px solid #000;
--timeline_color: brown;
}
ol,
li {
list-style-type: none;
}
ol.timeline {
display: grid;
width: var(--timeline_width);
margin: var(--timeline_margin);
grid-template-columns: 1fr var(--timeline_centre) 1fr;
grid-gap: var(--timeline_gapSize);
background: linear-gradient( to right, transparent, transparent calc(50% - var(--timeline_centre)/6), var(--timeline_color) calc(50% - var(--timeline_centre)/6), var(--timeline_color) calc(50% + var(--timeline_centre)/6), transparent calc(50% + var(--timeline_centre)/6), transparent);
}
li {
display: grid;
grid-gap: var(--timeline_gapSize);
}
li:nth-child(odd) {
grid-area: auto / 1 / auto / span 2;
grid-template-columns: 1fr var(--timeline_centre);
grid-template-areas: "text connection";
}
li:nth-child(even) {
grid-area: auto / 2 / auto / span 2;
grid-template-columns: var(--timeline_centre) 1fr;
grid-template-areas: "connection text";
}
li>div {
background-color: var(--timeline_taskBackground);
padding: var(--timeline_padding);
grid-area: text;
}
li:nth-child(odd)>div {
background-color: var(--timeline_taskBackground_odd);
border: var(--timeline_taskBorder_odd, --timeline_taskBorder);
}
li:nth-child(even)>div {
background-color: var(--timeline_taskBackground_even);
border: var(--timeline_taskBorder_even, --timeline_taskBorder);
}
span.connection {
grid-area: connection;
background: linear-gradient( to bottom, transparent, transparent calc(var(--timeline_connector_position) - 3px), var(--timeline_color) calc(var(--timeline_connector_position) - 3px), var(--timeline_color) calc(var(--timeline_connector_position) + 3px), transparent calc(var(--timeline_connector_position) + 3px), transparent);
background-size: 50%;
background-repeat: no-repeat;
}
li:nth-child(odd)>span.connection {
background-position: left;
}
li:nth-child(even)>span.connection {
background-position: right;
}
<button id="addEntry">Add a new entry</button>
<ol class="timeline">
<li>
<span class="connection"></span>
<div>1 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut explicabo quas esse, odio accusantium minus. Asperiores mollitia est, iusto corrupti quibusdam ipsa, assumenda nam id animi quod, nesciunt placeat. Perferendis!</div>
</li>
<li>
<span class="connection"></span>
<div>2</div>
</li>
<li>
<span class="connection"></span>
<div>3</div>
</li>
<li>
<span class="connection"></span>
<div>4</div>
</li>
<li>
<span class="connection"></span>
<div>5</div>
</li>
<li>
<span class="connection"></span>
<div>6</div>
</li>
<li>
<span class="connection"></span>
<div>7</div>
</li>
<li>
<span class="connection"></span>
<div>8</div>
</li>
<li>
<span class="connection"></span>
<div>9</div>
</li>
<li>
<span class="connection"></span>
<div>10</div>
</li>
</ol>
JS Fiddle demo.
In the demo, above, we mark up the timeline and its entries as follows:
<!-- use of an ordered list for the timeline itself: -->
<ol class="timeline">
<!-- each timeline entry is nested within and <li> element: -->
<li>
<!-- this could have been replaced by a pseudo-element and, perhaps
should have been; its use is purely presentational and serves to
connect the 'task' to the 'timeline' graphic: -->
<span class="connection"></span>
<!-- the <div> is used to wrap the text of the timeline entry: -->
<div>1 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut explicabo quas esse, odio accusantium minus. Asperiores mollitia est, iusto corrupti quibusdam ipsa, assumenda nam id animi quod, nesciunt placeat. Perferendis!</div>
</li>
<!-- other entries omitted for brevity -->
</ol>
The reason we've nested the timeline entries is largely because CSS subgrid is, as yet, unsupported in the majority of browsers and this is a simple means by which we can offset the <li> entries appropriately, as the CSS – hopefully – illustrates:
/* I've used quite a lot of CSS variables for purposes of theming,
retain or discard at your pleasure! */
:root {
--timeline_connector_position: 30%;
--timeline_margin: auto;
--timeline_gapSize: 0.2rem 0rem;
--timeline_centre: 5vw;
--timeline_width: 90vw;
--timeline_padding: 0.5rem;
--timeline_taskBackground: #fff;
--timeline_taskBackground_even: #f90;
--timeline_taskBackground_odd: limegreen;
--timeline_taskBorder: 1px solid #000;
--timeline_taskBorder_even: 1px solid #000;
--timeline_taskBorder_odd: 1px solid #000;
--timeline_color: brown;
}
ol.timeline {
display: grid;
/* Using some of the defined CSS custom properties to set
the width and margin: */
width: var(--timeline_width);
margin: var(--timeline_margin);
/* Here we define the three-column layout, comprising of
column 1: 1 fractional unit,
column 2: defined by the --timeline_centre custom property,
column 3: 1 fractional unit: */
grid-template-columns: 1fr var(--timeline_centre) 1fr;
grid-gap: var(--timeline_gapSize);
/* This background defines the 'timeline' vertical stroke: */
background: linear-gradient(
/* the gradient moves from left to right: */
to right,
/* is transparent at the start of the gradient: */
transparent,
/* remains transparent until the point calculated by the
CSS calc function; here we're calculating a pixel-value of
50% width of the element, minus the width defined by the
--timeline_centre value divided by six (the six is arbitrary
but provided a good-enough visual in the JS Fiddle and Snippet,
adjust to taste): */
transparent calc(50% - var(--timeline_centre)/6),
/* at the same point as above the colour changes to the colour
defined by the --timeline_color custom property: */
var(--timeline_color) calc(50% - var(--timeline_centre)/6),
/* the change here is that we add, rather than subtract the value
in order that the --timeline_color spans across the 50% mark: */
var(--timeline_color) calc(50% + var(--timeline_centre)/6),
/* at the same point that the --timeline_color ends the transparent
value resumes; this creates hard colour stops rather than
fading gradients: */
transparent calc(50% + var(--timeline_centre)/6),
transparent
);
}
li {
display: grid;
grid-gap: var(--timeline_gapSize);
}
li:nth-child(odd) {
/* here the odd <li> elements are placed with the syntax of:
row-start / column-start / row-end / column-end
in this case the row is placed into its naturally-occuring
row ('auto'), starts in the first column of the grid (1), ends
in the naturally-occurring row ('auto') and spans two columns
('span 2'): */
grid-area: auto / 1 / auto / span 2;
/* we define a two-column grid, of 1 fractional unit, and one
column equal to the size defined by the --timeline_centre
custom property: */
grid-template-columns: 1fr var(--timeline_centre);
/* we name the two columns of this two-column grid: */
grid-template-areas: "text connection";
}
li:nth-child(even) {
/* exactly as above, but the even <li> elements start
in the second column instead of the first: */
grid-area: auto / 2 / auto / span 2;
/* exactly as above but the columns are reversed in order
that the var(--timeline_centre)/'connection' column
overlaps the column of the same width in the parent
grid: */
grid-template-columns: var(--timeline_centre) 1fr;
grid-template-areas: "connection text";
}
li>div {
/* here we use some of the theming options to style the
element: */
background-color: var(--timeline_taskBackground);
padding: var(--timeline_padding);
/* positioning the timeline entry text in the grid
area identified by the name 'text': */
grid-area: text;
}
li:nth-child(odd)>div {
background-color: var(--timeline_taskBackground_odd);
border: var(--timeline_taskBorder_odd, --timeline_taskBorder);
}
li:nth-child(even)>div {
background-color: var(--timeline_taskBackground_even);
border: var(--timeline_taskBorder_even, --timeline_taskBorder);
}
span.connection {
grid-area: connection;
background: linear-gradient( to bottom, transparent, transparent calc(var(--timeline_connector_position) - 3px), var(--timeline_color) calc(var(--timeline_connector_position) - 3px), var(--timeline_color) calc(var(--timeline_connector_position) + 3px), transparent calc(var(--timeline_connector_position) + 3px), transparent);
background-size: 50%;
background-repeat: no-repeat;
}
/* here we position the connection background in order that it
visually connects the timeline entry text to the timeline
itself: */
li:nth-child(odd)>span.connection {
background-position: left;
}
li:nth-child(even)>span.connection {
background-position: right;
}
If I understand correctly what you want, you can use nth-child to set the column start for the .line and the .event. However, this will create holes, so you'll also need to set grid-auto-flow: row dense; on the .grid. The line should fill two columns, so you'll need to move the background to the right position for each case.
:root {
--line-width: 5px;
--line-height: 3px;
--main-line-width: calc(var(--line-width) * 2 + 2px);
--odd-line-color: gold;
--even-line-color: silver;
}
.timeline {
display: grid;
grid-template-columns: 1fr var(--main-line-width) 1fr;
grid-auto-flow: row dense;
text-align: center;
}
.line {
grid-column-start: 2;
grid-column-end: span 2;
background: linear-gradient(currentColor, currentColor),
linear-gradient(to right, transparent var(--line-width), black var(--line-width), black calc(var(--line-width) + 2px), transparent calc(var(--line-width) + 2px));
background-repeat: no-repeat;
background-size: var(--line-width) var(--line-height),
var(--main-line-width) 100%;
background-position: center left;
color: var(--odd-line-color)
}
.line:nth-child(4n + 4) {
grid-column-start: 1;
background-position: center right;
color: var(--even-line-color);
}
.event {
grid-column-start: 1;
border: var(--line-height) solid var(--odd-line-color);
}
.event:nth-child(4n + 3) {
grid-column: 3;
border-color: var(--even-line-color);
}
<div class="timeline">
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
<div class="event">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dui elit, tincidunt sit amet dictum ut, tempus vel diam. Integer ac sem congue, ullamcorper libero sit amet, viverra dui.</div>
<div class="line"></div>
</div>
I need to design the below elements:
1st Parent element - Menu
Child element - popup
2nd parent element - Body
I need to bring popup to the front then body and then menu.
is this possible?
Html
<div class="menu">
<div class="popup">
Test content
</div>
</div>
<div class = "body-content"></div>
CSS
.menu {
z-index: -1;
}
So now my body content will come front ,menu will go back. But now i tried to click popup div. it just behind the body content. I need to bring that front.
Stack layer
1. Popup
2. Bodycontent
3. Menu
Thanks in advance.
Your Question is not that easy to understand without an example, code showcase, or any other example.
But I think you can do what you want to do by using negative z-index.
Edit: I see you have edited your question, but it is still not that easy to know what you mean.
But here is my best guess:
http://codepen.io/Type-Style/pen/rjOzWa
.popup {
/* decoration */
border: 2px dotted;
background: rgba(255,125,0,0.6);
/* place above menu and body-content */
position: relative;
z-index: 1;
top: 30px; /* create overlap for demo */
}
.body-content {
background: rgba(255,0,0,0.6);
position: relative;
z-index: -1;
bottom: 30px; /* create overlap for demo */
}
div {
/* just decoration styles */
width: 350px;
margin: 0 auto;
padding: 10px 0;
}
.menu {
background: rgba(50,50,255,0.6); /* opaque background color for understanding */
}
.popup {
/* smaller for decoration */
width: 300px;
border: 2px dotted;
background: rgba(255,255,0,0.6);
/* place above menu and body-content */
position: relative;
z-index: 2;
top: 25px; /* create overlap for demo */
}
.body-content {
background: rgba(255,0,0,0.6);
position: relative;
z-index: 1;
bottom: 40px; /* create overlap for demo */
}
<div class="menu">
Menu Test Content: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
<div class="popup">
Test content popup. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</div>
</div>
<div class="body-content">
Body-Content and more random Text:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. <br />body-content test-content: malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
</div>
I think what you want is a popup over everything.
I don't understand the need of having the content above the menu.
But this is the way I did it.
Note: the position: relative; is needed in order to make z-index work on the particular element.
By using relative, the dimensions the element occupies still remain.
If you don't want that, you can define
position: absolute;
This makes positioning a little bit trickier, since it is placed to its nearest non-static parent element.
So I have two divs next to each other which have the class .category and they are supposed to be responsive.
<div class="content">
<div class="category">
<img src="images/category1.jpg" alt="" />
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor.
</p>
</div
<div class="category">
<img src="images/category2.jpg" alt="" />
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo.
</p>
</div>
</div>
This is my CSS:
.content {
width: 100%;
background: red;
}
.category {
max-width: 470px;
background: #ffffff;
display: inline-block;
vertical-align: top;
position: relative;
}
When I start resizing the window, the second .category block moves underneath the first .category block. However, I want both the .category blocks to reduce in width and stay next to each other.
Anybody got any suggestions?
First, you have some typographic errors in your HTML Markup (you are missing the > sign on the closing div tag of the first category div).
Second, you should be using percentage widths for responsive elements like this :
FIDDLE
CSS :
.content {
width: 100%;
background: red;
}
.category {
max-width:470px;
width: 50%;
background: #ffffff;
float:left;
vertical-align: top;
position: relative;
}
add float:left; to .category in css and use either % or a css media query
#media(min-width:something;){
.category {
width: something;
}
}
to set the width of the elements.
like in the title i can't put some text centered vertically near a div with CSS, i searched on google and on stackoverflow so i decided to make a question here.
This is an example of what i need done with Paint:
I tried display table cell and box solutions but it works only without the floating div on top left.
When the text is longer than the blue div it should go under the div just like a normal text with a floating div.
I'm searching an only CSS solution, it can be done or not?
I am not completely sure if that is possible, but here is my best attempt at it, at least works for the first 2 examples.
<div class="wrap">
<div class="invisible"></div>
<img src="http://placehold.it/140x100">
<p>Lorem ipsum.</p>
</div>
<div class="wrap">
<div class="invisible"></div>
<img src="http://placehold.it/140x100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur viverra, nibh in molestie sodales, risus turpis vehicula tellus, vitae lobortis ligula tortor in enim.</p>
</div>
<div class="wrap">
<div class="invisible"></div>
<img src="http://placehold.it/140x100">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur viverra, nibh in molestie sodales, risus turpis vehicula tellus, vitae lobortis ligula tortor in enim. Proin venenatis arcu id enim rutrum eget condimentum urna venenatis. Suspendisse at tortor nisi, in tempus ligula. Maecenas nisl felis, bibendum ut luctus nec, bibendum sit amet erat.</p>
</div>
CSS:
.wrap {
width:500px;
border:1px solid red;
margin:10px;
}
.wrap:before {
content:'';
display:inline-block;
height:100%;
vertical-align:middle;
margin-left:-0.25em; /* adjusts spacing */
}
p {
display:inline-block;
vertical-align:middle;
width:350px;
}
img {
float:left;
}
.invisible {
height:100px;
display:inline-block;
vertical-align:middle;
}
A fiddle.
This is possible with pure CSS.
body {
background: url("http://img08.deviantart.net/b5aa/i/2015/140/7/c/chalkboard_by_lorelinde-d8u2phm.jpg") no-repeat;
}
.container {
color: rgba(255, 255, 255, .9);
font-family: "Chalkduster", "Baskerville";
font-size: 18px;
padding: 0 10px;
width: 550px;
}
#user_portrait {
border-radius: 13px;
border: 3px solid rgba(255, 255, 255, .9);
float: left;
margin: 0 20px 0 0;
max-height: 300px;
max-width: 300px;
filter: sepia(50%);
}
#overview_text {
letter-spacing: 1px;
line-height: 1.3rem;
padding: 0 0 0 10px;
white-space: pre-line;
}
<body>
<p class="container">
<img id="user_portrait" src="https://pbs.twimg.com/profile_images/704337993293815810/PmkKs6yw.jpg">
<span id="overview_text">“Never hate your enemies. It affects your judgment.”
“My father held a gun to his head, and my father assured the bandleader that either his signature or his brains would be on the contract.”
“There are many things my father taught me here in this room. He taught me: keep your friends close, but your enemies closer.”
</span>
</p>
</body>
The key point is to put both image and text into non-inline parent tag and make them float.
This is impossible with css only. (i would be happy to be proved wrong.)
My requirement is simple: 2 columns where the right one has a fixed size. Unfortunately I couldn't find a working solution, neither on stackoverflow nor in Google. Each solution described there fails if I implement in my own context. The current solution is:
div.container {
position: fixed;
float: left;
top: 100px;
width: 100%;
clear: both;
}
#content {
margin-right: 265px;
}
#right {
float: right;
width: 225px;
margin-left: -225px;
}
#right, #content {
height: 1%; /* fixed for IE, although doesn't seem to work */
padding: 20px;
}
<div class="container">
<div id="content">
fooburg content
</div>
<div id="right">
test right
</div>
</div>
I get the following with above code:
|----------------------- -------|
| fooburg content | |
|-------------------------------|
| | test right |
|----------------------- -------|
Please advise. Many thanks!
Remove the float on the left column.
At the HTML code, the right column needs to come before the left one.
If the right has a float (and a width), and if the left column doesn't have a width and no float, it will be flexible :)
Also apply an overflow: hidden and some height (can be auto) to the outer div, so that it surrounds both inner divs.
Finally, at the left column, add a width: auto and overflow: hidden, this makes the left column independent from the right one (for example, if you resized the browser window, and the right column touched the left one, without these properties, the left column would run arround the right one, with this properties it remains in its space).
Example HTML:
<div class="container">
<div class="right">
right content fixed width
</div>
<div class="left">
left content flexible width
</div>
</div>
CSS:
.container {
height: auto;
overflow: hidden;
}
.right {
width: 180px;
float: right;
background: #aafed6;
}
.left {
float: none; /* not needed, just for clarification */
background: #e8f6fe;
/* the next props are meant to keep this block independent from the other floated one */
width: auto;
overflow: hidden;
}
Example here: http://jsfiddle.net/jackJoe/fxWg7/
See http://www.alistapart.com/articles/negativemargins/ , this is exactly what you need (example 4 there).
<div id="container">
<div id="content">
<h1>content</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus varius eleifend tellus. Suspendisse potenti. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nulla facilisi. Sed wisi lectus, placerat nec, mollis quis, posuere eget, arcu.</p>
<p class="last">Donec euismod. Praesent mauris mi, adipiscing non, mollis eget, adipiscing ac, erat. Integer nonummy mauris sit amet metus. In adipiscing, ligula ultrices dictum vehicula, eros turpis lacinia libero, sed aliquet urna diam sed tellus. Etiam semper sapien eget metus.</p>
</div>
</div>
<div id="sidebar">
<h1>sidebar</h1>
<ul>
<li>link one</li>
<li>link two</li>
</ul>
</div>
#container {
width: 100%;
background: #f1f2ea url(background.gif) repeat-y right;
float: left;
margin-right: -200px;
}
#content {
background: #f1f2ea;
margin-right: 200px;
}
#sidebar {
width: 200px;
float: right;
Best to avoid placing the right column before the left, simply use a negative right-margin.
And be "responsive" by including an #media setting so the right column falls under the left on narrow screens.
<div style="background: #f1f2ea;">
<div id="container">
<div id="content">
<strong>Column 1 - content</strong>
</div>
</div>
<div id="sidebar">
<strong>Column 2 - sidebar</strong>
</div>
<div style="clear:both"></div>
<style type="text/css">
#container {
margin-right: -300px;
float:left;
width:100%;
}
#content {
margin-right: 320px; /* 20px added for center margin */
}
#sidebar {
width:300px;
float:left
}
#media (max-width: 480px) {
#container {
margin-right:0px;
margin-bottom:20px;
}
#content {
margin-right:0px;
width:100%;
}
#sidebar {
clear:left;
}
}
</style>
Simplest and most flexible solution so far it to use table display:
HTML, left div comes first, right div comes second ... we read and write left to right, so it won't make any sense to place the divs right to left
<div class="container">
<div class="left">
left content flexible width
</div>
<div class="right">
right content fixed width
</div>
</div>
CSS:
.container {
display: table;
width: 100%;
}
.left {
display: table-cell;
width: (whatever you want: 100%, 150px, auto)
}
.right {
display: table-cell;
width: (whatever you want: 100%, 150px, auto)
}
Cases examples:
// One div is 150px fixed width ; the other takes the rest of the width
.left {width: 150px} .right {width: 100%}
// One div is auto to its inner width ; the other takes the rest of the width
.left {width: 100%} .right {width: auto}
I'd like to suggest a yet-unmentioned solution: use CSS3's calc() to mix % and px units. calc() has excellent support nowadays, and it allows for fast construction of quite complex layouts.
Here's a JSFiddle link for the code below.
HTML:
<div class="sidebar">
sidebar fixed width
</div>
<div class="content">
content flexible width
</div>
CSS:
.sidebar {
width: 180px;
float: right;
background: green;
}
.content {
width: calc(100% - 180px);
background: orange;
}
And here's another JSFiddle demonstrating this concept applied to a more complex layout. I used SCSS here since its variables allow for flexible and self-descriptive code, but the layout can be easily re-created in pure CSS if having "hard-coded" values is not an issue.
This is a generic, HTML source ordered solution where:
The first column in source order is fluid
The second column in source order is fixed
This column can be floated left or right using CSS
Fixed/Second Column on Right
#wrapper {
margin-right: 200px;
}
#content {
float: left;
width: 100%;
background-color: powderblue;
}
#sidebar {
float: right;
width: 200px;
margin-right: -200px;
background-color: palevioletred;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
Fixed/Second Column on Left
#wrapper {
margin-left: 200px;
}
#content {
float: right;
width: 100%;
background-color: powderblue;
}
#sidebar {
float: left;
width: 200px;
margin-left: -200px;
background-color: palevioletred;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
Alternate solution is to use display: table-cell; which results in equal height columns.
Hey, What you can do is apply a fixed width to both the containers and then use another div class where clear:both, like
div#left {
width: 600px;
float: left;
}
div#right {
width: 240px;
float: right;
}
div.clear {
clear:both;
}
place a the clear div under left and right container.
I have simplified it : I have edited jackjoe's answer. The height auto etc not required I think.
CSS:
#container {
position: relative;
margin:0 auto;
width: 1000px;
background: #C63;
padding: 10px;
}
#leftCol {
background: #e8f6fe;
width: auto;
}
#rightCol {
float:right;
width:30%;
background: #aafed6;
}
.box {
position:relative;
clear:both;
background:#F39;
}
</style>
HTML:
<div id="container">
<div id="rightCol">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
</div>
<div id="leftCol">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.
</div>
</div>
<div class="box">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
</div>