I have a problem concerning shrink wrapping a container div, if the content is floating.
I want the container to be only as wide as the floating content of the container (shrink-wrapped). The container should be centered. Because of the context I cannot give the container an absolute width.
HTML
<div class="container">
<div class="content">Some content</div>
<div class="content">Some content</div>
<div class="content">Some content</div>
</div>
CSS
.content {
width: 50px;
float: left;
background-color: #CCC;
margin: 10px 10px 0 0;
overflow:hidden;
}
.container:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.container {
border: 1px solid #FF0000;
display:table;
margin: 0 auto;
}
Please see the problem under https://jsfiddle.net/jackis/05nzo4oc/12/. As soon as the floating content has to break the line the container takes the whole available width, even if a good part of the container remains empty to the right then. If the content does not break the line it works as expected. To see that, change the width of the .content class to 50px:
.content { width: 50px; ...}
I have absolutely no idea how to shrink wrap the container div if the floating content has to break the line.
Edit:
The container should contain as much content divs as possible in one line, but should leave no "phantom space" to the right, if the next content div uses the next line. For the real world problem I am trying to demonstrate with this model the width of the content divs is fixed.
Thanks for your help
I've managed to get it right with the width 270px and other versions, but i feel it to be a hack:
.container {
padding: 0 auto;
border: 1px solid #FF0000;
display:table-caption;
margin-left:-50%;
margin-right:50%;
}
A complete version is here.
To get a container block to expand to fit it's children you need to set it to inline-block. This also gives the possibility to center it using text-align center on a parent element.
.content {
width: 270px;
display:block;
float: left;
background-color: #CCC;
margin: 0;
overflow:hidden;
}
.content:nth-child(odd){
margin-right: 20px;
}
.container {
padding: 0;
border: 1px solid #FF0000;
display: inline-block;
margin: 0;
max-width: 560px;
}
.outer-wrap {
text-align: center
}
I have forked and tidied up your fiddle here:
http://jsfiddle.net/simoncmason/qL611mu6/
I take it this is what you wanted to achieve.
(Edited following comments)
I solved my problem based on knowledge I have about the context of the environment the composition is used. As the container takes the whole width of the screen and I know how wide a content tile is, I know how many can fit in one line. I can then work with media queries and set the width of the container explicitely to get the desired behavior:
`#media all and (min-width: 280px) and (max-width:559px) {
div.product-grid {
width:280px;
}
}
#media all and (min-width: 560px) and (max-width:839px) {
div.product-grid {
width:560px;
}
}
`
The container will then be centered. on the screen. Of course I can not foresee all possible screen widths, but for huge resolutions, I can just live with a container not being centered.
Related
I have this html:
<div class="container">
<div id="sidebar">
<ul>Create Offer</ul>
<ul>Accept Offer</ul>
<ul>Pending</ul>
<ul>Completed</ul>
<ul>Balance</ul>
<ul>Support</ul>
</div>
<div id="items">
Text
</div>
</div>
this is the css:
.container {
margin: 0 auto;
background-color: white;
border: 1px solid black;
width: 1000px;
}
#sidebar {
margin-top: 0px;
padding-top: 0px;
width: 18%;
background-color: #E3E3E3;
height: 100%;
}
.container #items {
width: 82%;
float: right;
background-color: red;
}
output: http://puu.sh/l719c/52f182e1d2.png
why wont the items div show within the container in the white space next to the sidebar?
thanks.
When you float an element, it moves to the side and lets content that follows it move up beside it.
That means the content that follows items (if there was any) would be next to it.
You've done nothing to let items move up beside sidebar.
You need to float sidebar left and not items right.
Also beware of horizontal margins/padding making the total width of the elements add up to more than 100%.
Also note that floated elements do not restrict the height of their container unless you do something about it.
I'd generally look to flexbox for aligning blocks on a row, rather than floats.
You have just missed one line. The float for the sidebar must be set so that other elements can use the empty space. Change your css for #sidebar as follows.
#sidebar {
float: left;
margin-top: 0px;
padding-top: 0px;
width: 18%;
background-color: #E3E3E3;
height: 100%;
}
I'm assuming you want your sidebar set to float:left;. So you can position the "items" right next to the "sidebar" div.
I have a responsive header which is quite complex.
The left block is fixed width and the right block is a percentage (100%) I found this great article here on how to do that except I need it the other way around, this example is right block fixed.
http://radiatingstar.com/make-a-layout-with-fluid-and-fixed-size-columns
I did get it working at one point but can't remember how I did it, there should be no scrollbar the outer container should be 100%. The real issue is that in the right block I have 2 inner divs, 1 div should be horizontally centered on the screen not centered in it's div as the fixed left block has pushed it over already.
http://jsfiddle.net/3519a9p0/1/
<div id="container">
<div id=fixed-width>
</div>
<div id=fluid>
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>
And the other challenge is the responsive part in that the right icon block as you can see that that is floated to the right should move on top of the centered block as the screen width shrinks.
It would appear that I need to float the centered block too but then it needs to the centered middle of the screen too.
The the fixed width left block could potentially be a float too but it doesn't really matter as after the screen gets to small I switch to completely different layout, it's just the 2 inner divs that I need centered and responsive.
You're a genius if you can solve this!
Cheers!
Here is a working example. I just got rid of the margin and the float. However, while the answer was simple, you should read on below to understand why this worked.
Working Example
Because the left div has a float: left attribute, you can just set the right div to take up 100% of the remaining space. You do not need the negative margin to work the div into its place.
Also, a floated element is taken out of the normal flow of the document, so now you can use margin: 0 auto and as long as the right div has 100% width, it will center across the entire screen.
Update
There were post-question requests made via comments. To solve the issue, I added media queries and removed the float on the right-side div. Also, I had to add extra markup so that the inner divs on the right-hand side could be absolutely positioned properly.
Here is revised CSS. The major changes are:
No need to float the fluid column, add left margin instead
To center a box on the screen, set relative positioning on the container (not the fluid box) and use absolute positioning on the box
As for responsiveness, you can simply remove float, width, height and positioning from elements so that they appear as rows.
/* body margin/padding is reset to get media queries right */
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
height: 100px;
}
#fixed-width {
float: left;
width: 250px;
height: 100%;
background-color: blue;
}
#fluid {
margin-left: 250px;
height: 100%;
background-color: green;
}
.farRight {
float: right;
width: 100px;
height: 40px;
color: white;
background-color: black;
}
.centeredBlock {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 200px;
height: 40px;
color: white;
background-color: tomato;
}
/* when screen is narrower than 250+200+250 pixels trigger breakpoint 1 */
#media screen and (max-width: 699px) {
.farRight {
float: none;
width: auto;
}
.centeredBlock {
position: static;
width: auto;
}
}
/* when screen is narrower than whatever-you-want pixels trigger breakpoint 2 */
#media screen and (max-width: 499px) {
#container {
height: auto;
}
#fixed-width {
float: none;
width: auto;
height: auto;
}
#fluid {
margin-left: 0;
height: auto;
}
}
<div id="container">
<div id="fixed-width">fixed width</div>
<div id="fluid">
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>
I feel this question has been answered but I searched and searched and no answer seems to deal with dynamic main content width.
I simply want this scenario:
|-|nav|-|main content|-|
Where nav is a DIV and main content is a DIV and both are placed inside another DIV container which has a width of 100%. - is simpy a spacing between the DIVs, a margin.
nav has a fixed width of 300px and "main content" div should always take the rest of the space available (to fill the 100% of the parent div) - without the use of JavaScript.
Also I want to have some margins left and right of each DIV (nav, main content) so that they have some space between them and the "browser border"/body.
I experimented with table, table-cell but the border-collapsing drove me nuts so I am heading back to god old "float: left" and clearfix. This is what I have so far:
<div id="container" class="cf">
<div id="nav">
Nav stuff
</div>
<div id="main">
Main stuff
</div>
</div>
#container {
padding: 0;
width: 100%;
background-color: orange;
min-height: 50px;
}
#nav {
display: inline;
float: left;
min-width: 300px;
width: 300px;
margin-left: 10px;
margin-right: 10px;
}
#main {
display: inline;
float: left;
background-color: green;
margin-right: 10px;
}
.. /* clearfix stuff omitted (class 'cf') */
So now the problem is, how to make "main content" (#main) fill the rest of the parent (#container). If I use a width of 100% the 100% is of course the full width of the parent and the div will go under the "nav" div. If i use "auto" the same thing happens. It of course works if I pass in a fixed width e.g. in pixels but I don't know the correct pixels in advance and using JS to calculate that seems a bit odd to me.
I've seen a solution where the "nav" was put inside "main" but that leads to problems with the margins. Try to insert a margin to create some space beside a div that is inside another div... I don't think that's anyhow possible in this universe.
Thanks for your help!
Maybe you should create BFC to face this problem.
For example:
#container{
border: 1px solid red;
}
#nav{
float: left;
width: 300px;
border: 1px solid green;
height: 200px;
margin-left: 20px;
margin-right: 20px;
}
#main{
overflow: hidden;
height: 400px;
border: 1px solid blue;
margin-right: 20px;
}
overflow: hidden; is the key to create BFC for #main.
JSFiddle : http://jsfiddle.net/yujiangshui/yMFB6/
More about BFC : https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
For example:
#container {
width: 100%
position: relative;
}
#nav {
position: absolute;
top: 0;
left: 0;
width: 300px;
}
#main {
margin-left: 320px;
}
JSFIDDLE
I need help with a recurring problem that happens a lot. I want to create a header that consists of 3 sections which are positioned inline. I display them inline using the following css code: display: inline & float: leftThe problem is that when I resize my browser window the last div is pushed down and isn't displayed inline. I know it sounds like I'm being picky, but I don't want the design to distort as the visitor change's the monitor screen. I have provided the html and css code below that I am working with below. Hopefully I have explained this well enough. Thanks in advance.
HTML
<div class="masthead-wrapper">
</div>
<div class="searchbar-wrapper">
</div>
<div class="profile-menu-wrapper">
</div>
CSS
#Header {
display: block;
width: 100%;
height: 80px;
background: #C0C0C0;
}
.masthead-wrapper {
display: inline;
float: left;
width: 200px;
height: 80px;
background: #3b5998;
}
.searchbar-wrapper {
display: inline;
float: left;
width: 560px;
height: 80px;
background: #FF0000;
}
.profile-menu-wrapper {
display: inline;
float: left;
width: 200px;
height: 80px;
background: #00FF00;
}
display them inline using the following css code: display: inline & float: left
Aside... You are actually floating the element, not displaying it inline. The display:inline rule is irrelevant here since floated elements are implicitly displayed as block.
But anyway, your problem is that your sections are all of a fixed width (200 + 560 + 200 = 960px), so when the browser window reduces to near this width (960px plus a bit more for your page margins) the design is going to break - your containers wrap.
If you still want these containers to be fixed width and to simply be cropped on a smaller browser window then you could perhaps add overflow:hidden to your #Header. At least then it won't push the #Header height down beyond 80px (which is a problem you seem to be experiencing). But content will be hidden on the smaller screen.
Or, make all your column containers dynamic and give them percentage widths, so that they flex with the available width. eg. 20%, 60% and 20% respectively. Although this might make the widths too small or too large at some window sizes. You could add a min-width and max-width (with an absolute amount) to limit this. But at narrow widths height:80px is not going to be enough, so min-height:80px would perhaps be more appropriate, if your design allows for your #Header to be flexible?
With the percentage, be sure to no have padding on your columns. The padding will be add some width. For your header, you can use the position:fixed, and for IE6 and 7 use position: absolute ( the position :fixed ) doesn't work for them.
For the columns, you can add the clearfix method who can help you for placing without problem the rest of the content.
Your HTML can be something like this :
<div id="header" class="clearfix">
<div id="col01">Column 01</div>
<div id="col02">Column 02</div>
<div id="col03">Colunm 03</div>
</div>
And the CSS :
#header {
position: fixed;
height:80px;
width:100%;
}
#col01,
#col02,
#col03 {
float:left;
}
#col01,
#col03 {
width:20%;
}
#col02 {
width:60%;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
Hope it's helping you :-)
I am trying to create an html interface, where rows will be dynamically added to my web page.
The way I am currently doing it is by using nested DIVs with the CSS display style set to table.
Each row has 3 divs. The left div and the right div have a fixed width, while the middle div should expand to fit the page horizontally regardless of the length of it's content.
My problem is I'm not sure how to make that center div expand the entire remaining width of the page. With the code below, the center div is as small as the content.
I tried a solution that floated the left div left, and the right div right, however that would not let me select a row of text properly. i.e., if I started selecting the right div's content, then dragged towards the left, the left and center div would not be selected.
The solution only needs to target webkit based engines, as my code will only be used in a webkit based environment.
EDIT!
I forgot to mention that I also tried using tables. However I also need to avoid getting horizontal scroll bars appearing on the page when the screen is shrinking. When I use tables and shrink the page, the center div stops shrinking at a certain point (due to the fixed width percentages I guess).
My CSS code:
.chatarea
{
display: table;
height = 100%;
padding-top:50px;
margin: 0px;
}
.row
{
#display: table-row;
}
.nick
{
display: table-cell;
width: 140px;
border-style: solid;
text-align: right;
}
.timestamp
{
display: table-cell;
width 50px;
border-style: solid;
}
.message
{
display: table-cell;
border-style: solid;
}
And the relevant html
<div class="chatarea">
<div class="row">
<div class="nick">
<p>Some Nick</p>
</div>
<div class="message">
<p>Some Message</p>
</div>
<div class="timestamp">
<p>Some Timestamp</p>
</div>
</div>
</div>
I believe this is a solution:
Add border-collapse: collapse; to .chartarea to remove the double border width.
Add width: 100% to .chartarea to cover the entire width of the window.
Add width: 80%; to .message to have it grow as the window width changes.
Add white-space: nowrap; to .nick to control wrapping.
Add white-space: nowrap; to .timestamp to control wrapping.
Uncomment display: table-row in .row
Check out this fiddle.
Note: the fiddle page appears to have been removed by the server.
Remove the display styles. Then just use:
.nick {
width: 50px;
float: left;
}
.timestamp {
width: 50px;
float: right;
}
Then .message will take up the remaining width.
Alternatively, just use a <table> element.
If you can't do it fake it. Just put your .nick and .timestamp inside .message and position them absolutely.
.nick
{
width: 140px;
border-right-style: solid;
text-align: right;
height:100%;
position:absolute;
top:0;
left:0;
}
.timestamp
{
width: 50px;
border-left-style: solid;
position:absolute;
top:0;
right:0;
height:100%;
}
.message
{
border-style: solid;
padding:0 50px 0 140px;
overflow:hidden;
position:relative;
}
I've check the fiddle in recent chrome and safari (on windows) and had no problems with selecting text.