Div - "fill the rest of the line" [duplicate] - html

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>

Related

Can’t get the text to appear below the Image

I am trying to make the text appear below the image but it is not budging at all. My goal is it make the text appear below the image in the container
.left-col p {
text-align: justify;
width: 300px;
}
.left-col img {
margin: 0 auto;
left: 5%;
width: 300px;
height: 130px;
position: absolute;
text-align: center;
}
</head>
<body>
<div class="header">
<h1>The 3 Column Layout</h1>
</div>
<div class="container">
<div class="left-col">
<img src="Cyber.jpg" width="200" height=150"/>
<p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec iaculis neque nec luctus maximus. Donec eu eleifend libero, nec scelerisque metus. Morbi volutpat turpis pretium
</p>
Instead of using position absolute, remove it. Reason is that the element is positioned relative to its first positioned (not static) ancestor element. So, you could of course mess with top, right and left values to make it work but it would not be responsive at all.
Read more about it here: MDN Position CSS
The default value of position is static, this way the elements renders in a specific order(its what you want, render img and p after).
This is the pen if you need:
<div class="header">
<h1>The 3 Column Layout</h1>
</div>
<div class="container">
<div class="left-col">
<img src="https://via.placeholder.com/200x150" width="200" height="150" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec iaculis neque nec luctus maximus. Donec eu eleifend libero, nec scelerisque metus. Morbi volutpat turpis pretium </p>
</div>
</div>
.left-col p{
text-align: justify;
width:300px;
}
.left-col img{
width:300px;
height: 130px;
}
Also, instead of setting width 300px to paragraph and img, you could set only one time to your .left-col div. I have also removed other properties that you were not using.
another note is that you forgot the " on height attribute.
In css there is use [ position absolute ] For the image and is not used in the text You must set the position in the image and the text or leave it to the default setting I deleted it from the image properties in css
.left-col p{
text-align: justify;
width:300px;
}
.left-col img{
margin: 0 auto;
left: 5%;
width:300px;
height: 130px;
text-align:center;
}
<body>
<div class="header">
<h1>The 3 Column Layout</h1>
</div>
<div class="container">
<div class="left-col">
<img src="Cyber.jpg" width="200" height=150"/>
<p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec iaculis neque nec luctus maximus. Donec eu eleifend libero, nec scelerisque metus. Morbi volutpat turpis pretium </p>
</body>
Remove the line 'position: absolute;' from CSS. Complete (close) the DIV and P tags. You may introduce '.container{...}' where you may position (or whatever) the image-and-text together. You may wish to use 'margin: 0;' to glue the text to the image. Good luck!

How to place two img next to each other with text below responsive

I'm making my second test HTML file from PSD file.
In this picture you may see my issue.
Could you please guide me how to sit two images next to each other which have text below?
Also I want it be responsive.
For example in large screens, the two images sit next each other. In small screens each image in one separate line.
Thanks a bunch
first have the image and text in a box like this:
<div class="contentBox">
<img>
<h3>some title</h3>
<p>some text</p>
</div>
then float those boxes.
.contentBox{
float:left;
}
I made a quick snippet to show you how you could use it:
#boxes{
text-align:center;
}
.contentBox {
display: inline-block;
width: 200px;
border: 1px solid black;
margin: 20px;
}
.contentBox img {
width: 100%;
}
.contentBox h3 {
margin: 5px;
}
.contentBox p {
text-align: justify;
margin: 5px;
}
<div id="boxes">
<div class="contentBox">
<img src="http://via.placeholder.com/350x250">
<h3>some title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin facilisis mauris sem, in elementum tortor eleifend vel.</p>
</div>
<div class="contentBox">
<img src="http://via.placeholder.com/350x250">
<h3>some title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin facilisis mauris sem, in elementum tortor eleifend vel.</p>
</div>
</div>

fixed-width and auto-width divs in one line

I have chat window where I want to put photo and message next to photo. Conversation window must be responsive and message div auto-adjustable to the screen. But I can't find any way to do this, because once message has few lines of text, it drops to the next line.
If I use table, I can't make fixed-width photo TD. If I use DIVS, I can't do auto-width message DIV :)
Here is JSFiddle with an example:
https://jsfiddle.net/s95tdcLw/3/
HTML:
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit
</div>
</div>
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin et mauris eget est maximus condimentum nec a turpis.
</div>
</div>
<div class="receiver">
<div class="receiverPhoto"></div>
<div class="receiverMessage">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin et mauris eget est maximus condimentum nec a turpis. Nulla nulla est, feugiat vitae posuere et, efficitur ac justo. Suspendisse pulvinar, urna quis vehicula malesuada, lorem lacus luctus odio,
eu mattis nisi turpis vel lectus.
</div>
</div>
CSS:
.receiver {
clear: both;
padding-top: 1rem;
}
.receiverPhoto {
float: left;
width: 40px;
height: 40px;
background: blue;
border-radius: 20px;
}
.receiverMessage {
float: left;
width: auto;
background: rgb(230, 230, 230);
border-radius: 10px;
margin-left: 0.5rem;
padding: 10px;
}
Leave the float settings, use these instead:
.receiver {
position: relative;
}
.receiverPhoto {
position: absolute;
left: 0;
}
.receiverMessage {
margin-left: 45px;
}
jsFiddle
Your .receiverMessage element should not float and it should reserve left-margin space for the .receiverPhoto element.
.receiverMessage {
/* should not float */
width: auto;
background: rgb(230, 230, 230);
border-radius: 10px;
margin-left: 50px; /* reserve space of .receiverPhoto width */
padding: 10px;
}
See the forked Fiddle: https://jsfiddle.net/092b077c/
In response to your comment how to make it work for the opposite...
I'd use the classes on the wrapping div elements to determine the message type. In my example I introduce a new class .sender. Now I create four selectors that determine whether the photo element floats left or right and whether the message element has left or right padding:
New CSS:
.sender .receiverPhoto {
float: right;
}
.sender .receiverMessage {
margin-right: 50px;
}
.receiver .receiverPhoto {
float: left;
}
.receiver .receiverMessage {
margin-left: 50px;
}
HTML:
<div class="sender">
<div class="receiverPhoto"></div>
<div class="receiverMessage">...</div>
</div>
Now the .receiverPhoto and .receiverMessage styles do not need to declare margin or float.
See the updated Fiddle: https://jsfiddle.net/092b077c/1/

How to display a <p> within a <div>element with the display property of inline-block?

This is what I am currently working on...
.scake_one {
color: #cc3300;
display: inline-block;
}
.scake_two {
color: #cc3300;
display: inline-block;
}
.scake_three {
color: #cc3300;
display: inline-block;
}
<div class="scake_one">
<h1>CAKE ONE</h1>
<p>Lorem ipsum etiam porttitor ultrices
<br>tortor tempus vehicula.</p>
</div>
<div class="scake_two">
<h1>CAKE TWO</h1>
<p>Lorem ipsum eu quisque velit
<br>quam convallis massa tellus.</p>
</div>
<div class="scake_three">
<h1>CAKE THREE</h1>
<p>Lorem ipsum sed mauris aenean
<br>pretium pulvinar.</p>
</div>
I was able to use this property well with an other section that had < h1 > and < li > elements. I was able to used inline-block because they are block level elements? I saw this info on the w3 site
http://www.w3schools.com/htmL/html_blocks.aspI also saw that < div > tags are also block level elements. I can't seem to understand why it isn't working.
Here you go! You need float-left, and the width needs to tell it to take up a third of the screen.
Also, just as a style thing, you can give all of your "scake" the same class name, because the CSS styling is the same for each one. Then if you change the style on one, it'll update it for all of them.
.scake {
color: #cc3300;
display: inline-block;
float: left;
max-width: 32%;
margin-left: .5%;
margin-right: .5%;
}
<div class="scake">
<h1>CAKE ONE</h1>
<p>Lorem ipsum etiam porttitor ultrices
<br>tortor tempus vehicula.</p>
</div>
<div class="scake">
<h1>CAKE TWO</h1>
<p>Lorem ipsum eu quisque velit
<br>quam convallis massa tellus.</p>
</div>
<div class="scake">
<h1>CAKE THREE</h1>
<p>Lorem ipsum sed mauris aenean
<br>pretium pulvinar.</p>
</div>

background image with overlapping content depending on mobile or desktop view

Running into a layout issue between mobile/tablet and desktop.
For mobile: I need the hero image to be first, then the content to be beneath it. (sample here)
For tablet/desktop: I need the content to lay on top of the hero image. (sample here)
I've tried using a background image but found that adjusting the layout of the page effects the background image size and proportions
I've tried absolutely position one on top of the other but run into a scenario where I end up fiddling with the layout between views (using bootstrap) more than what should probably be manageable.
Would love any thoughts on how to produce this result.
Will try to provide code samples of what I've done but not sure it's relevant since none of it seems to produce the desired results.
Here's a quick example that I've created for you, please check:
#container {
background: red;
text-align: center;
padding: 20px 0px;
}
#image, #content {
display: inline-block;
width: 45%;
vertical-align: middle;
}
#image img {
width: 100%;
}
#media (max-width: 500px) {
#image, #content {
display: block;
width: 100%;
}
}
<div id="container"><div id="image"><img src="http://placehold.it/350x150" /></div><div id="content">Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. </div>
Click on Run Code Snippet and view the above snippet in Full Page and then reduce your browser window to 500px or smaller, you'll see that the content will automatically get under the image. Initially, both the <div> containing the image and the content are being displayed as inline-block but for screens smaller than 500px, we use #media-queries to change their display property to block which is why the content goes underneath the image.
Here's a CodePen example I set up to accomplish what you're asking: http://codepen.io/trevanhetzel/pen/pyCnf
Essentially, if you use an inline image as compared to a background image, you can set it's position to absolute at a certain media query breakpoint. With it's container positioned relative and the image's sibling (.hero-content) positioned relative, you can just float it right at that same breakpoint, change the width to however wide you want it, and it sits right on top of the image.
HTML
<div class="hero-contain">
<img src="http://lorempixel.com/1000/300/" class="hero-img">
<div class="hero-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In a enim eu risus accumsan venenatis. Donec venenatis nunc ac tellus tempor, ac rutrum lorem semper. Suspendisse pulvinar elit sed luctus pulvinar. Donec id ultricies dui. Pellentesque suscipit nulla maximus, mollis urna laoreet, tempor tortor.</p>
</div>
</div>
CSS
.hero-contain {
position: relative;
margin: 0 auto;
max-width: 1000px;
}
.hero-img {
max-width: 100%;
}
.hero-content {
position: relative;
background: rgba(255, 255, 255, .5);
padding: 20px;
}
#media (min-width: 800px) {
.hero-img {
position: absolute;
}
.hero-content {
float: right;
width: 45%;
margin: 20px;
}
}
Use media queries.
When width of window will be less, than your tablet/desktop content, switch media query and just display your divs block. For displaying them in row use display:inline-block;
Example. Try to resize result window.