Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to make 2 full width columns in one box. first column has width: auto and I want second column in 100% minus first column's width, so they can display inline. could someone help me, please?
UPDATED
checkout this fiddle:
http://jsfiddle.net/cvxr31xo/2/
<div id="major">
<div id="one">Hello</div>
<div id="two">World</div>
</div>
#major {
width: 100%;
border: 1px solid black;
}
#one {
width: auto;
float: left;
background-color: green;
height: 100px;
}
#two {
width:auto;
min-width:100%;
background-color: blue;
height: 100px;
}
if you need additional features let me know I will update
I'm not sure if you're after Table or straight DIVs for this.
<div style="width:100%; border:1px solid red;">
<div style="display: inline; border: 1px solid black;">Some content</div>
<div style="display: inline; border: 1px solid black;">Some more content</div>
</div>
The second div inside the box div isn't 100% wide, but both divs will expand to content, if that's what you're after...
To achieve this I think you either have to use a table, or mimic one.
Table:
<table style="width: 100%">
<td>col1</td>
<td>col2</td>
</table>
Divs
<div style="width: 100; display: table">
<div style="display: table-cell">col1</div>
<div style="display: table-cell">col2</div>
</div>
Both solutions should make the columns add up to 100% and have automatic widths for both columns. The browser will assign width to them as it sees fit, depending on their contents, so it won't actually first size column 1 independently and then assign column 2 the remaining space, but will find the "best compromise" for both columns.
Edit: The float based solution of #Charlie is actually better in a sense, as it does size the left column independently. However, if the left columns grows too big, the layout may break.
To achieve what you want, #Charlie answered very well, perfect answer. https://stackoverflow.com/a/26757381/3964593
I dont know if you want make it dynamically, or what goal are you trying to achiev, but, still, if the content of one div goes bigger than the other, you should be prepared to handle it.
Like making sure you put a max-width for one or both divs, setting the right amount of height px's or percents, etc. to make it behave right.
try this code.....
<div style="width:100%;">
<div style="float:left; width:49.7%; border: 1px solid black;">Some content </div>
<div style=" float:right; width:49.6%; border: 1px solid black;">Some more content </div>
</div>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have the following simple layout where I have the main content in a centered div that can expand dependning on content and helptext in two columns on either side. However when zooming or resizing the window the left one disappears and cant be scrolled to while the right one stays in the window and can be scrolled to.
I would have guessed the problem to be part of the absolute positioning but that doesnt explain the right one remaining visible as I zoom in.
Ultimately I want a centered div, expanding depending on content, and one div on either side of it, preferably without fixed (max)width.
<body style="text-align: center">
<div style="display: inline-block; position:relative">
<div style="display:block; border:solid red;">
Here's some text to fill the void in this centered div.
Here's some text to fill the void in this centered div.
<div style="position: absolute; right:100%; top:0; border:solid black;">
This text disappears when window is resized/zoomed and cant be seen
</div>
<div style="position: absolute; left:100%; top:0; border:solid blue;">
This div remains visible when resizing/zooming
</div>
</div>
</div>
</body>
fiddle http://jsfiddle.net/w774hfay/
I've taken your comments into consideration in developing my answer.
Your fiddle, updated: http://jsfiddle.net/w774hfay/7/
<body>
<div style="position:relative; width: 100%;">
<div style="border: 3px solid red; width: 50%; margin: 0 auto;">
Here's some text to fill the void in this centered div.
Here's some text to fill the void in this centered div.
<div style="position: absolute; right:-10px; top:0; border: 3px solid black;
width: 25%;">
This text disappears when window is resized/zoomed and cant be seen
</div>
<div style="position: absolute; left:-10px; top:0; border: 3px solid blue;
width: 25%;">
This div remains visible when resizing/zooming
</div>
</div>
</div>
</body>
Hope this helps. If you need something adjusted just leave a comment below.
UPDATE
Based on your questions in your comment, I have updated my answer below.
Why does the black left positioned box disappear outside the view but
the blue one can be scrolled to?
Because this line in your code:
<div style="display:block; border:solid red;">
tells the browser to give this element a width of 100%. That's what block elements do by default, they occupy the full width of their container.
Then you absolutely positioned the two child divs. Absolute position takes the elements out of the normal flow, so the parent element essentially ignores them.
Then you positioned the two child divs with a value of 100% each, which results in their location off-screen.
You can only scroll right because the browser reads from left to right starting with the first element in the normal flow.
Is it possible to have it only stretch to fit the content of the
left/right containers, no fixed width, and still "glued" to the sides
of the center div?
Yes, there are various ways to do this. One simple and reliable way to keep elements next to each other on the same line is to use tables (either HTML or CSS). In my example below I've used CSS table properties.
REVISED DEMO
<div style="display: table;">
<div style="display: table-row;">
<div style="display: table-cell; border: 3px solid black;">
This text disappears when window is resized/zoomed and cant be seen
</div>
<div style="display: table-cell; border: 3px solid red;">
Here's some text to fill the void in this centered div.
Here's some text to fill the void in this centered div.
</div>
<div style="display: table-cell; border: 3px solid blue;">
This div remains visible when resizing/zooming
</div>
</div>
</div>
In adjusting your table cells make sure to consider these important properties:
border-collapse
border-spacing
padding
Good luck!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a question regarding placing two <div> containers side by side.
The code should later look like this:
<div class="wrapper">
<div class="leftColumn">navigation</div>
<div class="rightColumn">content</div>
</div>
http://jsfiddle.net/9NFDS/
Question:
Which of the two provided examples is more W3C conformant? Which way is better for creating a distance between the two container?
If you target the browser support for lower versions for IE (like IE7 or even lower) you need to work with the second approach (padding).
There will be an issue with your first approach as one of your container is left floated and one is right so the height of the parent div (wrapperExampleOne) will collapse and will result in 0px (which should be equal to the height of its children).
The better approach that works with most of the browsers is to use margin. An example is mentioned here in the third example of your modified fiddle here http://jsfiddle.net/9NFDS/4/
HTML:
<div class="wrapperExampleThree">
<div class="leftColumn">
</div>
<div class="rightColumn">
test
</div>
</div>
CSS:
.wrapperExampleThree
{
margin: 0 auto;
width: 900px;
}
.wrapperExampleThree .leftColumn
{
width: 200px;
height: 50px;
background: pink;
float: left;
}
.wrapperExampleThree .rightColumn
{
width: 650px;
margin-left:50px;
height: 50px;
background: green;
float: left;
}
i think 1st one.
<div class="wrapperExampleOne">
<div class="leftColumn">
</div>
<div class="rightColumn">
</div>
</div>
Making distance is common in today era. just like facebook, youtube, google.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble finding a solution to my problem on the Web.
My problem is this :
Say I have three elements:
div#container with a varying width.
The container contains two elements - div#text with an unknown width and div#img with a fixed width. Both elements are inline:
<div id="container" style="width:auto">
<div id="text"> some varying text here... </div>
<div id="img" style="width: 10px; background: url(img.png)">
</div>
Where the styling for the text is somewhere along:
#text { width:auto; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
I want the location of the image to be close to the text, but at most at the rightmost border of the container, as illustrated by the image here
Is there a way to achieve this without using javascript? Though the container is appended to the document dynamically, and so width calculations cannot be used.
Thanks in advance
Found the answer. Took me a lot of time, and brought me to the darkest corners of CSS.
And yet, I've emerged enlightened - and the answer is so simple....
<div id="container" style="width:auto">
<div id="text"> some varying text here... </div>
<div id="img" style="width: 10px; background: url(img.png)">
</div>
And The CSS:
#text {
width:auto;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
margin-right: -10px;
box-sizing: border-box;
}
As shown in this fiddle
This was as close as I got to your requisitions:
Fiddle
Unfortunately, due to the behaviour of text-overflow: ellipsis, it's really hard to get the desired behaviour without setting a max-width. And then, the image won't be next to a shorter text-block. An option would be text-align: right as you see in my example.
It would be far more easier to just place the text on the right side:
Fiddle
I'm trying to do a layout with two top divs, and one below them. See here:
<div style="width: 49%; float: left; border: 1px solid">
<label>blabla</label>
</div>
<div style="width: 49%; float: left; border: 1px solid">
<label>blabla</label>
<br/>
<label>blabla</label>
</div>
<div style="width: 100%; float: left; border: 1px solid">
<label>blabla</label>
<label>blabla</label>
</div>
http://jsfiddle.net/DAYaM/
Problem is I would like the top left div (or right one) to resize to the same dimension as the other one, even if the number of elements are different. So basically to scale to the biggest one. Any suggestions?
Regards,
Bogdan
Here you go
I wrapped everything into a section, gave it a fixed width, and gave the first two div a width of 100%.
I also converted your inline styling to external, because inline is depreciated in HTML5.
Also, there is no need to use 49% for your width. If you add box-sizing:border-box, then everything will fit like you want it to.
I'm trying to float two divs inline with each other inside a div of set width. Additionally they have borders and content that requires wrapping. It stops working when there's more content than fits on one line.
I'm trying to be avoid using tables to solve this (see solution below) but but no luck so far. Any one got any ideas?
Edited question: expanding requirements to include:
the divs should minimise their total width and not expand beyond the boundarys of the two main 50% columns. I've managed to achieve the first and second part (please see my own answer below) however I have an additional third requirement in that as these can be nested, the content then still stays within the two main columns but doesn't expand to fill up to a maximum width of 50% of the columns width. I'm working on a javascript solution which I won't be able to post back for some time.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
body {
width: 1024px;
}
.a_column {
width: 50%;
float:left;
}
.some_text {
float:left;
display:inline;
border: thin solid green;
}
.a_block {
float:left;
display:inline;
border: thin solid red;
/*width: I can't set this as I don't know how much some_text will need, this can vary from nothing to a lot.*/
word-wrap: break-word; /* this doesn't work without a width specified*/
}
/*solution when using tables */
.some_text_in_table, .a_block_in_table {
vertical-align:top;
}
.some_text_in_table div {
border: thin solid green;
}
.a_block_in_table div {
border: thin solid red;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="a_column">
<div class="some_text">
some text here.
</div>
<div class="a_block">
Less text and there's no problem.
</div>
</div>
<div class="a_column">
<div class="some_text">
some text here.
</div>
<div class="a_block">
Putting a lot of text into a div that you want a border around will
cause it to move down one line. Instead I'd like it to float inline
with its sibling div; you can remove the float:left but then it
completely messes up the border. An_additional_thing_I'd_like_is_for_long_sentences_to_be_broken_by_the_word_wrap,_but_this_requires_that_the_width_of
a_column be set and I can't do this as I don't always know how much
room some_text will need.
</div>
</div>
<div style="clear:both;"></div>
<h3> With tables, solution with in 7 minutes. So much easier:</h1>
<table style="table-layout: fixed; width: 100%;">
<tr>
<td colspan="2" style="width: 50%;">
</td>
<td colspan="2" style="width: 50%;">
</td>
</tr>
<tr>
<td class="some_text_in_table">
<div>
some text here.
</div>
</td>
<td class="a_block_in_table">
<div>
some text here.
</div>
</td>
<td class="some_text_in_table">
<div>
Less text and there's no problem.
</div>
</td>
<td class="a_block_in_table">
<div>
Putting a lot of text into a div that you want a border around will cause it to move down one line. Instead I'd like it to float inline with its sibling div; you can remove the float:left but then it completely messes up the border. An_additional_thing_I'd_like_is_for_long_sentences_to_be_broken_by_the_word_wrap,_but_this_requires_that_the_width_of a_column be set and I can't do this as I don't always know how much room some_text will need.
</div>
</td>
</tr>
</table>
</body>
</html>
Fiddle with my code here: http://jsfiddle.net/cdepZ/
display:table-cell;
Example: http://jsfiddle.net/TAhAv/
You are right in wanting to avoid tables with this layout - as you mentioned, this is not tabular data which you are chosing to display.
You mention in your CSS that you cannot set a width on .a_block because you do not know how much space you need. However, when you use a table you are actually setting a width (25%) as each cell is equally split amongst the over-all width.
So to achieve what you want to do (which will match the tables layout), you will have to set a width on these elements.
Here is a JSFiddle of how you could achieve this:
http://jsfiddle.net/ndhrd/39/
Set your widths properly with the space you have. Borders take 2px vertically and horizontally as well.
.a_column {
width: 512px;
float:left;
}
.a_block, .some_text{
width: 254px;
float: left;
word-wrap: break-word;
}
.a_block{
border: 1px solid green;
}
.some_text{
border: 1px solid red;
}
I got it working here:
http://jsfiddle.net/cdepZ/7/
Putting a lot of text into a div is now no problem, it will wrap and break any long sentences that go over 50% of it's parent divs' width. And it will minimise any content that it can whilst maintaining good looking borders.
Nesting this structure can keep it with in the limits of the .a_column but then doesn't allow all elements to expand fully.
I think the only solution is a javascript one :|
http://jsfiddle.net/uHEVJ/1/
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
body {
width: 1024px;
}
.a_column {
width: 49%; /* 49% rather than 50% to cope with the 1 pixel width borders*/
float:left;
border: thin solid blue;
}
.a_container{
display:inline;
}
.a_container > div{
max-width: 49%; /* 49% rather than 50% to cope with the 1 pixel width borders*/
float: left;
word-wrap: break-word;
}
.some_text {
border: thin solid green;
}
.a_block {
border: thin solid red;
}
</style>
</head>
<body>
<h3> Used a "display:inline;" div as a container to position each Div inside which has float:left (to minimise it's size)</h3>
<div class="a_column">
<div class="a_container">
<div class="some_text">
some text
</div>
</div>
<div class="a_container">
<div class="a_block">
Less text and there's no problem.
</div>
</div>
</div>
<div class="a_column">
<div class="a_container">
<div class="some_text">
some text here.
</div>
</div>
<div class="a_container">
<div class="a_block">
Putting a lot of text into a div is now no problem, it_will_wrap_and_break_any_long_sentences_that_go_over_50%_of_it's_parent divs' width. And it will minimise any content that it can whilst maintaining good looking borders
</div>
</div>
</div>
<div class="a_column">
<div class="a_container">
<div class="some_text">
Nesting this structure can keep it with in the limits of the .a_column but then doesn't allow all elements to expand fully.
</div>
</div>
<div class="a_container">
<div class="some_text">
Nesting this structure can keep it with in the limits of the .a_column but then doesn't allow all elements to expand fully.
<div>
<div class="a_container">
<div class="a_block">
some text
</div>
</div>
<div class="a_container">
<div class="a_block">
Nesting this structure can keep it with in the limits of the .a_column but then doesn't allow all elements to expand fully.
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>