I have three divs, all inline under a parent div. And my goal is to make middle div ALWAYS in the centre of the parent div. While rest two side divs are responsive. In left hand side div, text is align to right while in left hand side div, its aligned to left. And middle div's width is fixed, say 80px. Parent div's max and min width are also set. I have this:
<div style="max-width: 500px;min-width:450px;">
<div style="display:inline-block;text-align:right;">Posted by</div>
<div style="display:inline-block;text-align:center;width:80px;">
<img src="default.png" style="width:80px;height:20px;border:2px solid #fff;border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%;">
</div>
<div style="display:inline-block;">Johnathan Bestualzaukazoa</div>
</div>
I want to have something like this:
But middle div is not always in center. As side divs content push them.So how can I achieve it?
I suggest to use this CSS table layout for it. I set 50% on both left and right sides, and middle one with an image. Because it's table layout it won't break, instead it will re-calculate the value of 50% and display the best width "(100% - image width) / 2" available automatically.
jsfiddle
.container {
display: table;
border: 2px solid grey;
margin: 20px auto;
}
.container > div {
display: table-cell;
}
.left, .right {
width: 50%;
padding: 0 10px;
}
.left {
text-align: right;
}
.middle img {
vertical-align: middle;
}
.right {
text-align: left;
}
.container1 { width: 500px; }
.container2 { width: 400px; }
.container3 { width: 300px; }
<div class="container container1">
<div class="left">L</div>
<div class="middle"><img src="//dummyimage.com/80x40"></div>
<div class="right">R</div>
</div>
<div class="container container2">
<div class="left">L</div>
<div class="middle"><img src="//dummyimage.com/80x40"></div>
<div class="right">R</div>
</div>
<div class="container container3">
<div class="left">L</div>
<div class="middle"><img src="//dummyimage.com/80x40"></div>
<div class="right">R</div>
</div>
Related
I have the following page that uses the table-cell css style to achieve a three column layout:
HTML:
<html>
<body>
<div id="container" class="table, fullwidth">
<div id="header" class="table, fullwidth">Header</div>
<div id="main" class="table, fullwidth">
<div id="left" class="cell">Left</div>
<div id="center" class="cell">Center Center Center Center Center Center Center </div>
<div id="right" class="cell">Right</div>
</div>
<div id="footer" class="table, fullwidth">Footer</div>
</div>
</body>
</html>
CSS:
div.table { display: table; position: inherit; }
div.fullwidth { width: 100%; }
div.cell { display: table-cell; }
div#header { background-color: #ffcccc; }
div#footer { background-color: #ccffff; }
div#left, div#right { width: 17%; background-color: #ccffcc; }
div#center { width: 51%; background-color: #ffccff; }
JSFiddle here: https://jsfiddle.net/q7g8k5dy/
I am trying to get the left and right columns to be a third of the width of the center column, and I am able to get this to work in the JSFiddle window if I set the percentage widths to be below 17% (for the left and right columns) and 51% (for the center column).
But the moment I set the left and right widths to more than 17% (and the center to more than 51%), the three columns no longer occupy the entire width of the page, and there is a gap between the right edge of the right div and the right edge of the page. The total width occupied gets less and less until I set the left/right divs to 24% width and the center to 72%, whereupon I get a very uneven layout, with the left div being much wider than the right.
What is going on here? Should I just accept that this is some kind of 'magic', and the best thing to do when using percentage widths to achieve a desired layout (when using the table-cell style) is to use as low percentages as possible?
You are not giving class properly. There is no , between 2 class names. Also, you don't need to specify width of the center dic in case you want it to be adjusted automatically.
I gave left & right divs width of 20% (i.e, 1/3 of 60%) so center will be remaining 60%.
div.table { display: table; position: inherit; }
div.fullwidth { width: 100%; }
div.cell { display: table-cell; }
div#header { background-color: #ffcccc; }
div#footer { background-color: #ccffff; }
div#left, div#right { width: 20%; background-color: #ccffcc; }
div#center { background-color: #ffccff; }
<html>
<head></head>
<body>
<div id="container" class="table fullwidth">
<div id="header" class="table fullwidth">Header</div>
<div id="main" class="table fullwidth">
<div id="left" class="cell">Left</div>
<div id="center" class="cell">Center Center Center Center Center Center Center </div>
<div id="right" class="cell">Right</div>
</div>
<div id="footer" class="table, fullwidth">Footer</div>
</div>
</body>
</html>
Hope this is what you are looking for.
I have been trying to solve this for a couple of hours now and I cannot get it to work.
I have a floated left & right div. The right div contains an image and the left div contains text. The image & text swap left & right according to the users preference, which is why i am using float.
How do I position the text to be at the bottom of the wrapper but next to the image?
I have a visual display below:
Here is my html code:
<div class="wrapper">
<div class"text">Text aligned at bottom</div>
<div class="photo"><img id="photograph" src="" width="140px" height="140px" /></div>
</div>
Added a <div class="inner">, and set it as display:table-cell + vertical-align:bottom, see the following demo and snippet.
Edit: also added calc() to make the photo div is only as wide as the width of the photo and the text div takes up all of the remaining width automatically.
JsFiddle Example
.wrapper {
border: 1px solid blue;
overflow: auto;
}
.wrapper .text {
float: left;
width: calc(100% - 140px);
}
.wrapper .photo {
float: right;
}
.wrapper .inner {
display: table-cell;
height: 140px;
vertical-align: bottom;
}
.wrapper .inner img {
display: block;
}
<div class="wrapper">
<div class="text">
<div class="inner">Text aligned at bottom, text aligned at bottom, text aligned at bottom, text aligned at bottom, text aligned at bottom.</div>
</div>
<div class="photo">
<div class="inner"><img id="photograph" src="//dummyimage.com/140" /></div>
</div>
</div>
My question is about CSS and DIV tags. I have a dynamic page, and I would like one container DIV. There are two scenarios: in one case, this container DIV will just have one DIV in it, with a width of 50%, and should be centered. In the other case, there will be two DIVs in it, and they should be side by side, each taking up 50%.
I have tried float:center (using overflow: hidden on the container DIV), and that works for 1 DIV in it, but with two, they are stacked on top of each other. If I use float: left, then the 2 DIVS appear correct, but the single DIV is left aligned, not centered.
Any help on how to achieve this effectively would be greatly appreciated!
<div style="width:800; margin: 2px; background-color:blue">
<div style="width:50%; background-color:orange;">
Text
</div>
<div style="width:50%; background-color:red;">
Text
</div>
</div>
jsFiddle
For the two-div scenario:
<div style="width:800; margin: 2px; background-color:blue; display: table;">
<div style="background-color:orange; display: table-cell;">
Text
</div>
<div style="background-color:red; display: table-cell;">
Text
</div>
</div>
Now for the one-div scenario:
<div style="width:800; margin: 2px; background-color:blue; display: table;">
<div style="background-color:orange; display: table-cell;">
Text
</div>
</div>
In each case, the inner divs, whether there are 1 or 2, will take up a combined 100% of the outer div. Essentially, it acts like the <table> element without having the semantics of a <table>.
check this fiddle
HTML
<div class="wrapper">
<div class="divholder">
<div style="background-color:orange;">DIV 1</div>
<div style="background-color:red;">DIV 2</div>
</div>
</div>
CSS
.divholder div{
display:inline-block;
margin:auto;
width:49%;
}
.divholder {
text-align:center;
margin: 0 auto;
position:relative;
}
.wrapper{
width:100%;
background-color:blue;
}
This perfectly deals with your need..While there is only one div, the div gets centered and if two divs come then both will be equally divided and floated left.Please see the fiddle..
Similar to chharvey's answer, this can be achieved nicely with display: table;
In my example it is centered horizontally and will work with any number of columns as they will fit themselves to the full width of div.wrap. The columns are currently given a height of 100%, this can be adjusted.
Have a jsBin example!
HTML
<div class="wrap">
<div class="column">
</div>
<div class="column">
</div>
</div>
CSS
html,body {
height: 100%;
}
.wrap {
display: table;
width: 800px;
height: 100%;
margin: 0 auto;
}
.column {
display: table-cell;
background: #FF0;
}
.column:first-child {
background: #F00;
}
I want to place divs next to each other. I dont't know number of divs, since this is dynamically created and changed. I would like to have one parent div which will have scrollbar in case there are many child divs (and their width is greater than parent).
Here's the jsFiddle example. So, basically I would like to have all this three columns, next to each other and with scrollbar on the bottom of parent div.
HTML:
<div class="content">
<div class="column">Column</div>
<div class="column">Column</div>
<div class="column">Column</div>
</div>
CSS:
content {
width: 100px;
background- color: #355E95;
overflow: visible;
}
.column {
width: 50px;
display: inline-block;
}
Add white-space:nowrap to your parent div.
FIDDLE
You would need to use a content div for the scroll and then a wrapper for the columns, adjusting the width of the wrapper to fit all 3 of your columns (150px in your example).
The column structure is made by floating div's left, but it will float to the width of your parent container, in this case your parent container is only 100px so we need to add a wrapper for them to fit inside.
If you also want a vertical scroll you will need to set a height to your container.
Jsfiddle: http://jsfiddle.net/tYnH3/
css:
.content {
width: 100px;
background-color: #355E95;
overflow: auto;
}
.content-wrapper {
width: 150px;
}
.column {
width: 50px;
float: left;
}
html:
<div class="content">
<div class="content-wrapper">
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
<div class="column">
Column
</div>
</div>
</div>
Fiddle: http://jsfiddle.net/bdssw/
use float:left;
.column {
width: 50px;
display: inline-block;
float:left;
}
Try the following JS fiddle
http://jsfiddle.net/jT6SW/1/
#wrapper
{
float: left;
height: 150px;
width: 250px;
margin: auto;
border: 1px black solid;
overflow-y: hidden;
overflow-x: scroll;
}
use width:auto;
.content {
width: auto;
background-color: #355E95;
overflow:scrolling;
position:fixed;
}
.column {
width: 50px;
float:left;
}
http://jsfiddle.net/XqSJG/6/
I'm having some trouble finding the way to create 3 DIVs side by side, where the middle DIV has a static size (let's say 900px), and the ones on the left and right side have auto-adjustable widths.
I need to do this in order to put backgrounds on the side DIVs, because the middle div already has a transparent background, so I cannot create a wrapper DIV for the middle one holding the other background. As you might imagine, you'll be able to see through the middle DIV's background.
Any ideas? :)
Is this you want?
HTML:
<div class="wrapper">
<div class="side" style="float:right;width:50px;">side</div>
<div class="side" style="float:left;width:50px;">side</div>
<div class="middle" style="margin:0 50px;">content middle</div>
<div style="clear:both; height: 40px;"></div>
<div id="container">
<div class="left">side</div>
<div class="center">center</div>
<div class="right">side</div>
</div>
</div>
CSS:
.wrapper {
max-width: 1000px;
}
.side, .middle {
text-align: center;
}
.side {
background: red;
}
.middle {
background: blue;
color: white;
}
#container {
display: table;
width: 100%;
}
.left, .center, .right {
display: table-cell;
text-align: center;
background: red;
}
.center {
width: 500px;
background: blue;
color: white;
}
jsfiddle
I am guessing you need a website with 3 columns where the left one is the menu, the middle has the content and the right one has room for advertisement or additional information.
I would go for something like:
<div class="side" style="width:10%; float:left;">content</div>
<div class="middle" style="width:900px; float:left;">content middle</div>
<div class="side" style="width:10%; float:left; clear:both; ">content</div>
Depending on size and placement use the side classes for the sidebars and the middle class for your main content.