I have the following code;
<div id="first" style="float: left">
</div>
<div id="second">
<div id="1" style="display: inline-block;">
</div>
<div id="2" style="display: inline-block;">
</div>
......
<div id= "n" style="display: inline-block;">
</div>
</div>
The problem is that when the content in #second is taller than the content in #first,
then the child div's in #second start a new line below #first.
I want them to come below the second div only.
One simple way of doing this without having to set any widths or margins is to trigger a block formatting context for #second.
Apply the following CSS:
#second {
border: 1px solid blue;
overflow: auto;
}
See the demo at jsfiddle
For more information about block formatting contexts, see:
http://www.w3.org/TR/CSS2/visuren.html#block-formatting
if you are aware of the maximum width/width of the first div then just apply that width value as margin-left to the second div.
Working Fiddle
Updated: (if you are not aware of the width)
#second{
display: table-cell;
}
Fiddle
Use inline-block instead of float.
try:
#first, #second{
display: inline-block;
vertical-align: top;
}
That will make sure the two are separate as long as they are next to each other, so have to have a width.
EXAMPLE FIDDLE
EDIT
or yes, if you don't know width
#first, #second{
display: table-cell;
}
EXAMPLE FIDDLE 2
Related
I need to vertically align a box with CSS without using display: inline-block. As I don't know the height of my elements I used the way here described (CSS Table Method).
<div style="background-color: black">
<div style="background-color: aqua; display: table; margin: auto;">
<div style="background-color: aliceblue; width: 200px; float: left; display: table-cell; vertical-align: middle;">
<p>A</p>
<p>A</p>
<p>A</p>
</div>
<div style="background-color: green; height: 20px; width: 100px; float: left; display: table-cell; vertical-align: middle;"></div>
<div style="background-color: aliceblue; width: 250px; float: left; display: table-cell; vertical-align: middle;">
<p>B</p>
<p>B</p>
<p>B</p>
<p>B</p>
</div>
</div>
</div>
However, the vertical alignment on this test page has no effect at all. According to the computed CSS of these divs the display value is set to block! How can I make these divs appear in the middle?
erase the float:left from the DIVs - display:table-cell and float:left together makes no sense
Codepen: http://codepen.io/anon/pen/ORrNdW
ADDITION AFTER COMMENT:
Like this: codepen.io/anon/pen/NRxALO ?
I inserted /nested another DIV in each table-cell that contains the smaller elements, and assigned the background-color to the table element itself.
One way, out of many ways, you can achieve this is by setting the parent to position:relative; and its child to position:absolute; top:50%; transform:translateY(-50%);. Although this question has been answered many many times before.
Ok, I finally found the answer. You have to use the flexbox styling (display: flex). Using this method, you can center elements without specifing a height. This page describes how to use it.
I have three columns, but in the center one I can display only limited number of paragraphs.
If I add, for example, 15 paragraphs, only first 11 paragraphs will be displayed. It's like the div have set height parameter. Does anyone know how to fix this?
<div id="left" style="float:left; width:250px;"></div>
<div id="right" style="float:right; width:250px;"></div>
<div id="center" style="margin:0;"></div>
Add float:left; also to your #center element
or if you don't want it floated than: overflow:auto;
An alternative approach would be to use display: table; and display: table-cell; instead:
CSS
#container {
display: table;
width: 100%;
}
.cell {
display: table-cell;
vertical-align: top;
}
.side {
width: 250px;
}
HTML
<div id="container">
<div class="cell side">Left</div>
<div class="cell">Center</div>
<div class="cell side">Right</div>
</div>
I'm assuming these are the building blocks for your layout, with a centered area and sidebars on the left and right. Using table has the benefit that all cells maintain the same height; float can be fickle.
I have this code: http://jsfiddle.net/spadez/TPKLv/
<div style="background-color:white; width: 400px;">
<span class="status" id="active"></span>Title
6
</div>
<div style="width: 100%; background-color: white; float:right;">
Test test
</div>
I'm trying to make the right column fluid, taking up the remaining space, and the left column fixed width. Can anyone tell me what I am doing wrong with my code, and if this is the best way of doing it?
Simplest is to use display:table-cell; (IE8 and above supported )
this way, you can fix the width of one div and next div will take up the remaining space
Once added, you can even use inline native methods like vertical-align, and since its not floating, adjusting the position of divs is easy through margin and padding depending on you layout! :)
check this demo
It is most compatible and cleanest you can get for fixed width and dynamic width in a page
for calc, it is incompatible with IE9 still
if you have to use it on regular basis, create a span as below :
span.fake_width{
display:block;
width:20px;
}
then just add it to the existing layout Demo
I suggest this, with the condition always left div width is 400px. And i assume is cause you use inline-style
html
<div style="background-color:white; width: 400px;float:left;">
<span class="status" id="active"></span>Title
6
</div>
<div id="rightCol" style="background-color: white; float:right;">
Test test
</div>
css
body {background-color: gray;}
#rightCol{
width: calc(100% - 400px);
}
fiddle
The most native way of doing this is to manipulate the box model:
I've added a float to the first div, and removed it from the second one.
this way the first div is treated as an inline-block and the second one is a block, which tries hard to ignore other inline blocks.
i've updated your fiddle: http://jsfiddle.net/TPKLv/3/
<div style="background-color:white; width: 400px; float:left">
<span class="status" id="active"></span>Title
6
</div>
<div style="width: 100%; background-color: white;">
Test test
</div>
Try like this: Demo
CSS:
.container {
background-color:blue;
height: auto;
overflow: hidden;
}
.sidebar {
float:left;
background-color:grey;
width: 100px;
}
.content {
background-color:green;
float: none;
width: auto;
overflow: hidden;
}
HTML:
<div class="container">
<div class="sidebar">
<span class="status " id="active"></span>
Title
6
</div>
<div class="content">Test test</div>
</div>
UPDATED FIDDLE LINK.
As you need, I added padding and margin for the div's and its working fine.
I have a <div> containing an <img> and a <textarea> like this:
<div style="width: 50%">
<img />
<textarea>...</textarea>
</div>
<img> has variable width. How to make the textarea fillup the remaing space?
Thanks!
You can use display: table-cell and help achieve this like so:
HTML
<div class="container">
<div class="containerRow">
<div class="imageContainer">
<img href="[your_image_address]" alt="[your_image_alt]" />
</div>
<div class="textareaContainer">
<textarea>[your_textarea_content]</textarea>
</div>
</div>
</div>
CSS
.container {
display: table;
width: 100%;
}
.containerRow {
display: table-row;
}
.imageContainer {
display: table-cell;
vertical-align: top;
width: 1px;
}
.textareaContainer {
display: table-cell;
vertical-align: top;
}
.textareaContainer textarea {
width: 100%;
}
Demo
Ensure that the parents and children that are supposed to fill up the space use width:100%, which is relative to their parent container. So the <textarea> need to be defined as having 100% width, along with the image. Likewise they would need to be defined as display:inline or inline-block versus block so that they appear next to each other. Another option is to use a CSS table as: <div> with display:table, <div> with display:table-row and then <span> with display: table-cell for the columns that have the <image> and <textarea>; respectively. That will fit those elements into a CSS table that if defined as width:100% will fill up its parent.
I have this HTML code:
<body>
<div id="div0" style="display:inline; background-color:green; width:100%">
<div id="div1" style="display:inline; background-color:aqua;width:33%"> </div>
<div id="div2" style="display:inline; background-color:red;width:33%"> </div>
<div id="div3" style="display:inline; background-color:yellow;width:33%"> </div>
</div>
</body>
I want to fill the page with div1, div2 and div3 but they don't fill the entire width.
What it's happening?
Taken from display declaration:
display: inline means that the element
is displayed inline, inside the
current block on the same line. Only
when it's between two blocks does the
element form an 'anonymous block',
that however has the smallest possible
width.
You cannot give an inline element set width or height dimensions, they will be ignored. An element must have a display type of block to do that. Setting display: block however will not achieve what you want since each element will fill the entire width. float: left will cause them to stack to the left and also forces display: block.
<head>
<style type="text/css">
#wrap {
width:100%;
}
#wrap:after {
/* Prevent wrapper from shrinking height,
see http://www.positioniseverything.net/easyclearing.html */
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
#wrap .container {
float: left;
width:33%;
}
</style>
</head>
<body>
<div id="wrap">
<div class="container"> </div>
<div class="container"> </div>
<div class="container"> </div>
</div>
</body>
Mmmmm, semantics
See answer from Phunky for further comments on floating.
Use relative positioning on the outer <div> and float the inner <div>s. Don't use display: inline.
<body>
<div id="div0" style="border: 1px solid red; background-color: green; width: 100%; position: relative;">
<div id="div1" style="background-color: aqua; width: 33.33%; float: left;">a</div>
<div id="div2" style="background-color: red; width: 33.33%; float: left;">b</div>
<div id="div3" style="background-color: yellow; width: 33.33%; float: left;">c</div>
</div>
</body>
display:inline shrink wraps the content. You might want to try float:left instead.
Rory Fitzpatrick more or less has the ideal answer for you, although there is no need to set pos:rel on the #wrapper as it is already a relative block element and will span the full width by default.
When you float a block element it mimics the alignment functionality of display:inline and in an ideal world we would have access to the very useful display:inline-block which would have done exactly what you was expecting it to do.
But one thing you should remember when floating elements is that they will only take up the space they require (this includes margin and padding) unless you set a fixed width.
This is why Rory used width:33%; as that is the best you are ever going to get :)
Ideally this would have been a comment on Rorys post, but i've not got a high enough post count yet.
<body>
<div id="div0" style="float: left; background-color:green; width:100%">
<div id="div1" style="float: left; background-color:aqua;width:33%"> </div>
<div id="div2" style="float: left; background-color:red;width:33%"> </div>
<div id="div3" style="float: left; background-color:yellow;width:33%"> </div>
</div>
</body>
This should work for you. And the reason IIRC is that display: inline does not take % width.
Instead of using float you could use flexbox for a more responsive resizing. Also this forces the elements to remain in a row.
Example:
<head>
<style type="text/css">
#wrap {
width:100%;
display:inline-flex;
}
#wrap:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
display: inline-flex;
flex-direction: row;
}
.container1 {
width:20%;
}
.container2{
width:80%;
}
</style>
</head>
<body>
<div id="wrap">
<div class="container1"> </div>
<div class="container2"> </div>
</div>
The best way to accomplish this, contrary to all the answers given before, can be found referencing the answer to this question:
3 inline-block divs with exactly 33% width not fitting in parent
The quickest and easiest way is not the prettiest to look at (putting your div's on the same line to remove the automatic single white space provided normally), but will work tremendously for what you want. The answer I am referencing list plenty of other way that, in my opinion, are better than any provided before, and address the true problem you are having.
Here is the code working exactly how you'd like, and a link to the fiddle!
<body>
<div id="div0" style="float: left; background-color:green; width: 100%;">
<div id="div1" style="margin: 0px; display: inline-block; background-color:aqua;width:33.33%"> </div><div id="div2" style="margin: 0px; display: inline-block; background-color:red;width:33.33%"> </div><div id="div3" style="margin: 0px; display: inline-block; background-color:yellow;width:33.33%"> </div>
</div>
https://jsfiddle.net/stopitdan/uz1zLvhx/