I have a structure that is something like this fiddle http://jsfiddle.net/qqqh1agy/1/
HTML:
<div class="outer">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
</div>
CSS:
.outer{
width: 100px;
height:20px;
overflow: auto;
}
.inner{
background:red;
width:50px;
float:left;
height:20px
}
I want the inner divs to be on one line with a horizontal scrollbar. Is this possible?
Any ideas greatly appreciated
C
Add white-space:nowrap; to the outer div, then replace float:left with display:inline-block on the inner divs
This forces the inner elements to display on a single line, whilst preventing them from wrapping to the next.
Demo Fiddle
.outer {
width: 100px;
height:20px;
overflow-x: scroll;
white-space:nowrap; /* <-- force items to display on the same line */
}
.inner {
background:red;
width:50px;
height:20px;
display:inline-block; /* <-- display 'in a row' */
}
That said, to properly display your scrollbar and content, you may want to change your CSS to:
Demo Fiddle
.outer {
width: 100px;
overflow-x: auto; /* <-- show horizontal scrollbar */
overflow-y:hidden; /* <-- hide vertical scrollbar */
white-space:nowrap;
}
.inner {
background:red;
width:50px;
height:20px;
display:inline-block;
}
yes, you can use inline-block to do this in combination with a white-space: nowrap. It will place all the elements on one row without wrapping them.
fiddle: http://jsfiddle.net/1zmeztt3/1/
.outer{
width: 100px;
height:40px;
overflow-x: auto;
white-space: nowrap;
}
.inner{
background:red;
width:50px;
height:20px;
display: inline-block;
}
You should also use overflow-x to prevent vertical scrolling and increase the height of the row to make sure the scrollbar will fit inside of it.
The problem you are having is the three floating divs calculate their positions based on the width of the parent element. A reasonable solution would be to nest 2 outer divs, with the inner one being wide enough for all 3 floating divs and the outer one having the restricted width and scrolling.
HTML:
<div class="outer">
<div class="nest">
<div class="inner"></div>
<div class="inner blue"></div>
<div class="inner"></div>
</div>
</div>
CSS:
.outer{
width: 100px;
height: 30px;
overflow-x: scroll;
overflow-y: hidden;
}
.nest{
width: 150px
}
.inner{
background:red;
width:50px;
float:left;
height:20px
}
.blue{
background: blue;
}
jsfiddle
Problems might arise in that the width of the inner div depends on how many floating divs you have and how wide they are. So this solution may not be the best if these factors may be arbitrary.
Edit: I would go with SW4's solution but it might not work on older IE browsers.
You could try to use:
clear: none;
Here is a link:
http://www.w3schools.com/cssref/pr_class_clear.asp
No longer valid response after the edit of the original post.
Related
Trying to add a tooltip to a div with position:absolute and overflow hidden.
Plunker: http://plnkr.co/edit/qH5nHheCoyOmvRKLVx2o?p=preview
<div class="container">
<div class="inner">
Why am I not overlapping?
</div>
</div>
.container{
height:300px;
width:300px;
position: absolute;
overflow: hidden;
z-index: 1;
background-color:black;
color: blue;
}
.inner{
height:500px;
width:500px;
top:100px;
left:50px;
background-color:yellow;
position:absolute;
text-align: right;
z-index:10;
}
I had a similar problem in the past and couldn't find a solution except maybe for hacks. I ended with adding the inner div to an element higher in the DOM tree and positioning using Javascript.
I can't figure out why the height of the #container div is calculated correctly at 200px when a display:table-row is applied to the #header div and it's too large when a display:table-caption is applied to the #header div.
I've tested this in Chrome 35
Does someone know why this is the case and/or is there a simple fix?
( preferably without javascript or adding extra divs)?
I want the #header div's height to be as small as it's content, and it's width to be 100% of the #container div, and the #container to fit exactly in the #main div.
jsfiddle: http://jsfiddle.net/2SKY4/
CSS:
#main {
width: 200px;
height: 200px;
background-color:#ff0;
}
#container {
background-color: #0f0;
width: 90%;
display:table;
height:100%;
}
#header {
background-color:#F0F;
display:table-caption;
}
#splitpanel {
display:table-row;
background-color:#0ff;
}
#leftpanel {
background-color: #f00;
overflow: scroll;
display: table-cell;
}
#rightpanel {
background: #00f;
overflow: scroll;
display: table-cell;
}
HTML:
<div id="main">
<div id="container">
<div id="header">Header</div>
<div id="splitpanel">
<div id="leftpanel"></div>
<div id="rightpanel"></div>
</div>
</div>
</div>
I am not sure what you want. the fiddle already has like you want it seems.
you are better off showing snapshots and say what you want. you are using "display" of different types which also doesnt make much sense. can you please describe what structure you want in snapshot?
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 8 years ago.
How can we center (horizontally and vertically) the text in this div?
HTML
<div class="text">
hello is the the testhello is the the testhello is the the testhello is the the testhello is the the testhello is the the
testhello is the the tes
</div>
CSS
.text {
width:150px;
background:red;
float:left;
height:150px;
margin:10px;
text-align:center;
word-wrap:break-word;
overflow:hidden;
color:white;
}
Here is the full explanation here. Read this.
http://css-tricks.com/centering-in-the-unknown/
If you want to vertical & horizontal center to unknown height, width element. you must add the style for parent as display:table and the style for child as display:table-cell.
//UPDATED
if you know the height & width of the element.
Try this.
.parent {
display:block;
position:relative;
}
.child {
display:block;
height:x;
width:y;
position:absolute;
top:50%;
left:50%;
margin-top:-x/2; //Half of the height with minus direction
margin-left:-y/2; //Half of the width with minus direction
}
Example on codepen: http://codepen.io/anon/pen/BFqfx/
div {
width:150px;
height:150px;
line-height: 150px;
background:red;
color: #fff;
}
div p {
display: inline-block;
vertical-align: middle;
line-height: normal;
width: 100%;
text-align: center;
}
Screenshot
DEMO
HTML
<div class="foo">
<div class="bar">
Unknown stuff to be centered.
</div>
</div>
CSS
.foo{
display: table;
width: 100%;
}
.bar {
display: table-cell;
text-align: center;
vertical-align: middle;
}
Article
There is another common technique, based upon "absolute positioning" and negative top margin.
HTML:
<div class="foo">
<div class="bar">
Unknown stuff to be centered.
</div>
</div>
CSS:
.foo{
width:150px;
height:150px;
background:red;
position:relative;
}
.bar {
text-align: center;
position:absolute;
top:50%;
height:40px;
margin-top:-20px;
border:solid 1px blue;
}
The idea is that you move down to 50% from the top the inner div, and then push it up of half its height with negative margin-top.
Is very stable and cross-platform, the only constraint is you must know height of inner div.
To avoid this costraint, a workaround is replacing negative margin-top with a transform: translateY(-50%);. In this case, negative translation to center DIV is obtained visually with CSS transformation.
It's useful because in this way you can use it without knowing height of inner DIV, but remember that transformation are extracted from DOM rendering.
I have layout comprising of a 100% width header, 2 column content divs (30-70% width) and a 70% width footer (visible only in the bottom of right div).
My HTML mark up is like:
<section id="mySection" >
<header id="headerTop">
</header>
<div id="wrapperLeft">
</div>
<div id="wrapperRight">
</div>
<footer id="footerRight">
</footer>
</section>
My CSS is
#mySection
{
margin:0 auto;
padding:0;
text-align:center;
vertical-align:middle;
overflow:hidden;
}
#headerTop
{
position:absolute;
top:0;
left:0;
height:40px;
width:100%;
overflow:hidden;
}
#wrapperLeft
{
position:absolute;
top:40px;
left:0;
width:30%;
bottom:0;
overflow:auto;
}
#wrapperRight
{
position:absolute;
top:40px;
left:30%;
width:70%
bottom:30px;
overflow:auto;
}
#footerRight
{
position:absolute;
left:30%;
bottom:0;
width:70%;
overflow:hidden;
}
I would like to know if I can design this better such that if i hide the left or right div, the other div is displayed at 100%. I think i can change the CSS dynamically via javascript and adjust the left and width values for the other div, but it is getting messy and would like to avoid it if possible.
Ideally would love to call show or hide on the div and the other div automatically adjusts itself to 100% width.
I have no control over the height of the content in either div and would want the browser to display scrollbar when the content height exceeds the window.
Thanks in advance for your help.
I would add a wrapper to the divs so you can float then instead of positioning then absolutely. This way you can make at least one div 100% wide. For instance the right div. If you want both divs to be dynamic in size you will have to use jquery. For instance adding classes if you want to keep the jquery to a minimal.
example HTML:
<div id="header"></div>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="footer"></div>
example CSS :
#main{
position:relative;
overflo:hidden // This will make the container grow with the children
width:960px;
}
#left{
width:200px;
float:left;
height:100%;
}
#right{
float:left;
width:100%;
height:100%;
}
Example of CSS with additional classto toggle divs
#main.only-left #left{
width:100%;
}
#main.only-left #right{display:none;}
I think I know what you're talking about. I've created a little example here. Basically set 30% on the sidecolumn, and display: block; on the main column. Click on the body anywhere to toggle the side column to show how the main column adapts... is this going in the right direction?
Codepen sketch
HTML
<div class='wrapper'>
<header>Header</header>
<section>
<aside>Sidebar</aside>
<article>Main article</article>
</section>
<footer>Footer</footer>
</div>
CSS
body {
margin: 0;
padding: 0;
}
section {
overflow: hidden;
position: relative;
}
header {
background: crimson;
height: 100px;
width: 100%;
}
aside {
background: #efefef;
float: left;
height: 300px;
width: 30%;
}
aside.hide { display: none; } /** For demo purposes **/
article {
background: #ccc;
display: block;
height: 300px;
}
footer {
background: crimson;
float: right;
height: 100px;
width: 70%;
}
jQuery (just for hideToggle example)
$('html').on('click', function(){
$('aside').toggleClass('hide');
});
UPDATE: Here's an example with a little assitance from jQuery for class toggling. Could probably be generalized more... http://codepen.io/kunalbhat/pen/kuAcg
I need help in centering one DIV withing a DIV.
I want to have one container DIV that is auto width to take up the whole width of the screen (lets call it headerContainer.
Within headerContainer, I want 3 more DIVs:
A Left DIV (400px wide)
A Center DIV (100px wide)
A right DIV (200px wide).
I want the center DIV directly in the middle of the screen. Right now I can only get it to center between the left and right DIV.
Thanks for any help.
CSS:
.leftDiv{
float: left;
width: 400px;
}
.rightDiv{
float: right;
width: 200px;
}
.centerDiv{
width: 100px;
margin: 0 auto;
}
HTML:
<div>
<div class="leftDiv">left</div>
<div class="rightDiv">right</div>
<div class="centerDiv">center</div>
</div>
DEMO:
Code: http://jsfiddle.net/Xxwrm/6/
Fullscreen: http://jsfiddle.net/Xxwrm/6/show
This works.
.headerContainer{
width:auto !important;
}
.leftDiv{
float:left;
width:400px;
}
.rightDiv{
float:right;
width:200px;
}
.centerDiv{
display:inline;
width:100px;
margin-left:auto;
margin-right:auto;
}
.
<div class="headerContainer">
<div class="leftDiv"></div>
<div class="centerDiv"></div>
<div class="rightDiv"></div>
</div>
What you could do is add another div at the end which makes both sides equal, and set visibility: hidden; (not display: none;); this way it would centre the middle div.
For example in this case you'd have one # 400px, another # 100px, another # 200px and another one, hidden, # 200px.
Regards,
Richard
<div class="headerContainer">
<div class="leftDiv">left</div>
<div class="rightDiv">right</div>
<div class="centerDiv">center</div>
</div>
This HTML with this CSS will work. I colored the DIV's to make it obvious.
.headerContainer{
width:auto;
}
.leftDiv{
float:left;
width:400px;
background:pink;
}
.centerDiv{
width:100px;
/*
margin-left:auto;
margin-right:auto;
*/
margin:0 auto;
background:cyan;
}
.rightDiv{
float:right;
width:200px;
background:lightgray;
}
However, if the screen is not 700px wide, you will get some wrapping.
Here is a fiddle for it, too: http://jsfiddle.net/johnpapa/9bN2p/
You can use a modern solution due the flex concept of css3.
.parent {
display: flex;
height: 300px;
/* Or whatever */
background-color: green;
}
.child {
width: 100px;
/* Or whatever */
height: 100px;
/* Or whatever */
margin: auto;
/* Magic! */
background-color: yellow;
}
<div class="parent">
<div class="child ">Div1</div>
</div>