CSS help positioning divs inline - html

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 :-)

Related

Sidebar is floating over the footer

I'm trying to create a sidebar (which sometime is longer than the content section).
I've the demo here: http://jsfiddle.net/y9hp4evy/1/
THe second case: http://jsfiddle.net/y9hp4evy/2/ (If I add a height to sidebar)
But when the sidebar content is longer, it goes over the footer. And some content is below the end of the sidebar section.
.container {
width: 800px;
}
.right-pane {
float: right;
width: 300px;
}
.left-pane {
margin-right: 320px;
position: relative;
min-width: 300px;
}
footer{
background-color: #cdc;
}
What am I missing here? Can't find the right solution on the search results too. Any help would be greatly appreciated.
In cases like this, make sure your sidebar is contained in the main content wrapper. ie:
<div id="container">
<div id="article"></div>
<div id="sidebar"></div>
</div>
<footer></footer>
You then make sure your CSS has a clearfix for the main 'container'. ie:
#container:after{
content: "",
display: block;
clear: both;
}
Here's your second JSfiddle edited: http://jsfiddle.net/y9hp4evy/3/
Note that I removed the height attribute for the sidebar. Never enforce height unless you are ready to handle overflow cases. Much better to use min-height
Here is the first one fixed:
.right-pane {
float: right;
width: 300px;
}
.left-pane {
float:left;
position: relative;
width: 300px;
}
http://jsfiddle.net/6929kcc7/2/
Here is the second one:
(I removed height of sidebar)
http://jsfiddle.net/y9hp4evy/8/
Update:
http://jsfiddle.net/6929kcc7/3/
so use col-sm-4 and inside it add sidebar that has fixed width.

2 divs side by side, each 50%, same height

I would like to place two divs within a container side by side and - thanks to SO - I feel I'm almost there, but there is something I really don't understand.
The html looks like this:
<div class="parent">
<div class="font" id="left"></div>
<div class="font" id="right"></div>
</div>
CSS looks like this:
body {
margin: 0;
}
#left {
width: 50%;
background: lightblue;
display: inline-block;
height: 100%;
}
#right {
width: 50%;
background: orange;
display: inline-block;
height: 100%;
}
.parent{
font-size:0;
margin: 0;
height: 40px;
}
.font{
font-size:16px;
}
font-size needs to be 0 to account for the whitespaces. display is set at inline-block (I'd rather use display than float).
This works fine. It keeps working when I add content to both the left and the right block. However, when I add content to only one block, this block gets strangely offset from the top. It's like adding margin-top: 50px or something. And I don't get why.
Here's the JSFiddle with content in the left block: https://jsfiddle.net/dave_s/phon1tws/
I've also tried overflow:hidden, but that shrinks the block with the content.
Any help would be much appreciated! Also if someone could explain to me what happens here, that'd be really great!
Thanks a lot!
One way is do use flexbox. Codepen example. Note the support for flexbox and use prefixes.
.parent {
display: flex;
}
add this in css
#left, #right{float:left;}
Alternatively, you can use CSS tables. Your mark-up lends itself nicely to the technique.
The main advantage is that you don't have to alter the font sizes to compensate for the white space that can show up between inline blocks.
Having said that, both approaches will work in your situation.
body {
margin: 0;
}
.parent {
display: table;
width: 100%;
height: 40px;
}
#left, #right {
display: table-cell;
vertical-align: top;
width: 50%;
height: 100%;
}
#left {
background: lightblue;
}
#right {
background: orange;
}
<div class="parent">
<div class="font" id="left">Left Blue</div>
<div class="font" id="right">Right Orange</div>
</div>
Take a look to this really nice guide about Flexbox. Nowadays it's the clearest way to build a layout.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This will work:
.font {
font-size: 16px;
vertical-align: top;
}
By default baselines are vertically aligned. If the <div> is empty, its bottom line will be its baseline. Otherwise the baseline of the first line of text is the baseline to be aligned with.
This problem exists even when there are words in both <div>s but having different font-sizes.

How to shrink wrap floating content?

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.

How to fit elements in 100% width without absolute positioning?

I'm trying to do a 4-frame design with css, as in this code:
http://jsfiddle.net/7qBKJ/1/
But I don't want to use position:absolute;, and I'm trying to do it like this:
topframe: block;
left,right and centerframes: inline-block;
And I want to ensure there is, say, 200px of width in both rightframe and leftframe, and the remaining parts should be filled by centerframe. How can I manage this without absolute positioning?
I tried this, but it moves the frames up and down, when the screen width decreases :
http://jsfiddle.net/V4vAc/2/
in this fiddle, centerframe aligns with leftframe, since they are both inline-block, with centerframe rule margin-left:0px; but I have no idea how to set centerframe's right to align with rightframe's left, without specifying a width.
So how can I make #centerframe's width equal to screen width - 400 px ?
Thanks !
What you have to do is to put both sidebars first in the flow of the document. Then you float the first sidebar right and the second one left.
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
/* main.css */
#left {
width: 200px;
height: 100px;
background-color: blue;
float: left;
}
#right {
width: 200px;
height: 100px;
background-color: red;
float: right;
}
#center {
width: auto;
height: 100px;
background-color: green;
margin: 0 200px;
}
http://jsfiddle.net/samgh/JjsFF/
If you want center to be the full width underneath the two sidebars, you can remove the margins. Hope this helps.

how to make 100% width for 2 div

I have 2 div in on page
<div id="main"></div>
<div id="panel"></div>
and using float to make main at left, and panel at right
#photos-main {
float: left;
width: 800px;
position: relative;
}
#panel {
float: left;
margin-left: 830px;
position: absolute;
}
I want to let panel fill with the left page space, which css should I use?
Just don't float it, and make it relatively positioned. Take out the margin as well. Floating "main" means that it will simply be to the left of "panel" all the time. If you define "main" how you want, "panel" will automatically take up the remaining space.
#photos-main {
float: left;
width: 800px;
position: relative;
}
#panel {
}
Looks like you're trying to build some layout, right? If that's the case, consider implementing (hence learning from) some of the techniques presented in these links:
40 Tutorials and tips about CSS Layouts
CSS Positioning
The perfect three columns layout
Hope it helps!
You could do it with floating with this approach:
#photos-main {
float: left;
width: 800px;
}
#panel {
float: right; /*to have the panel on the right side*/
width: 100px; /*with a width of 100px*/
}
Then you have to wrap the two Tags with another , which get a total width of both elements.
To clarify this two column layout and put e.g. a footer beneath, put another in your HTML-Structure and set into the css simple a "clear:both;", so the floating will be stopped.
Complete Sample
HTML
<div id="wrap">
<div id="photos-main"></div>
<div id="panel"></div>
<div id="clear"></div>
</div>
CSS
#wrap {
width: 900px;
}
#photos-main {
float: left;
width: 800px;
}
#panel {
float: right; /*to have the panel on the right side*/
width: 100px; /*with a width of 100px*/
}
#clear {
clear:both;
}