CSS: aligning elements inside a div - css

I have a div that contains three elements, and I am having problems correctly positioning the last one. The div at the left has to be at the left, the one in the middle needs to be centered, and the third one needs to be to the right.
So, I have something like:
#left-element {
margin-left: 9px;
margin-top: 3px;
float:left;
width: 13px;
}
#middle-element {
margin:0 auto;
text-align: center;
width: 300px;
}
#right-element {
float:right;
width: 100px;
}
My html looks like this:
<div id="container-div">
<div id="left-element">
<img src="images/left_element.png" alt="left"/>
</div>
<div id="middle-element">
I am the text inside the middle element
</div>
<div id="right-element">
I am the text in right element
</div>
</div>
Any ideas?
Thanks!

You haven't included css for your container div, but whenever it contains floating elements you should hide overflow like so:
#container {
overflow: hidden;
width: 100%; /* for good measure */
}
When you position the middle div you are setting margins that span the entire container, so any further elements are getting positioned on the line below. Note, at least for most modern browsers, further. If you reorder your elements, like so:
<div id="container">
<div id="left-element">
<img src="images/left_element.png" alt="left"/>
</div>
<div id="right-element">
I am the text in right element
</div>
<div id="middle-element">
I am the text inside the middle element
</div>
</div>
You should find it works. Better method, as I'm not quite sure whether that is supposed to work, would be to use css positioning. Apply the following css:
#container {
overflow: hidden;
width: 100%;
min-height: 36px; /* Remember absolute positioning is outside the page flow! */
position: relative;
}
#left-element {
position: absolute;
left: 9px;
top: 3px;
width: 13px;
}
#middle-element {
margin: 0 auto;
text-align: center;
width: 300px;
}
#right-element {
position: absolute;
right: 0px;
top: 0px;
width: 100px;
}

I think you problem is that you floated the left and right element but not the center one. Try something like this:
CSS:
<style>
#container { display:block; margin:0; padding:0; width:640px; height:400px; outline:1px solid #000; }
#left-element { float:left; display:block; margin:10px; padding:0; width:200px; height:380px; background:#ccc; }
#middle-element { float:left; display:block; margin:10px 0; padding:0; width:200px; height:380px; background:#eaeaea; }
#right-element { float:left; display:block; margin:10px; padding:0; width:200px; height:380px; background:#ddd; }
</style>
HTML:
<div id="container">
<div id="left-element">Left Element</div>
<div id="middle-element">Middle Element</div>
<div id="right-element">Right Element</div>
</div>

The problem is specifically that the middle div has a width set but is not floated, meaning it is still a block level element. Even though the div itself does not go the entire width of the container, it is still treated as such. You need to do 2 things - float the middle div, then clear the floats so the container grows with the height of the child divs. The clearfix method is preferred since it does not cause issues with CSS3 properties that naturally extend outside the bounds of the element they are applied to (box-shadow, text-shadow, etc).
http://perishablepress.com/press/2009/12/06/new-clearfix-hack/

I had the exact same issue. I used this approach. I made the center element display inline-block. That way I didn't have to give the elements specific width or the main container a specific height. The blocks only took up as much space as the content inside. Hope this helps.
.main-nav {
text-align: center;
padding: 2em 3em;
background: #f4f4f4;
}
#logo {
margin: 0;
width: 50px;
float: left;
margin-top: 18px;
}
#logo-link {
float: left;
display: inline-block;
}
.name {
display: inline-block;
}
.nav {
float: right;
margin-top: 18px;
}
.nav li {
float: left;
margin-right: 15px;
margin-top: 5px;
}
.nav li:last-child {
margin-right: 0;
}
<header class="clearfix">
<div class="main-nav">
<img src="img/site-logo.svg" alt="Munchies" id="logo">
<div class="name">
<h1>The Munchies Family Site</h1>
<h2>Designer</h2>
</div>
<nav class="nav clearfix">
<ul>
<li>Gallery</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
strong text

Related

Centering the middle of three divs and positioning the other two relative to the middle one

Sorry if the title is confusing. Basically, I'm working on a tumblr theme where I need three adjacent divs wrapped in a fixed-width container. None of their contents are fixed, so they all have variable widths. The middle div should always be centered to the container, while the divs to the left and right will always be "touching" the middle div, and, thus, move around as the middle div's width changes (the left and right s may be images, so text-align doesn't always work). Plus, I may also need to hide the left, right, or both the left and right divs.
Here's a conceptual image:
I can obtain this using flexboxes easily (JFiddle), but flex only has 86% global support.
This is the closest I could get without using flexboxes, but I can't get that middle div (with the text) centered to the title div, while preserving the relative positions of the two images on either side: JFiddle
* {
height: 100%;
position: relative;
}
body {
height: 200px;
}
/* just to get rid of scrollbar */
p {
margin: 0;
}
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
}
.container {
background: #abc;
float: left;
}
.lr {
transform: translate(0, -100%);
}
.left {
background: green;
float: left;
}
.left img {
transform: translate(-100%);
}
.center {
background: red;
display: inline-block;
z-index: 2;
}
.right {
background: blue;
float: right;
}
.right img {
transform: translate(100%);
}
.left img, .right img {
height: 100%;
}
<div class="title">
<div class="container">
<div class="center">CENTERCENTERCENTERCEN</div>
<div class="lr">
<div class="left">
<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
</div>
</div>
Other people have mentioned trying to display the title as a table, but that would require centering the middle cell to the whole row, and having the cells to the left and right take up the rest of the space, and I'm not sure if you can do that when their widths aren't fixed.
Anyone know of any other solutions?
If you can change your HTML then apply this:
First move the left and right elements inside center:
<div class="center">
CENTERCENTERCENTERCEN
<div class="left">
testtest<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
Then on the CSS :
/*Keep the center container on the middle*/
.title {
text-align:center;
}
.center {
position:relative;
display:inline-block;
}
/*Position elements based on the relative center parent*/
.left {
position:absolute;
top:0;left:0;
transform:translateX(-100%)
}
.right {
position:absolute;
top:0;right:0;
transform:translateX(100%)
}
Check this DemoFiddle
Using position: absolute should help in this.
I changed your HTML to following:
<div class="title">
<div class="container">
<img class="left" src="http://i.imgur.com/7bvErJN.jpg" />
<div class="center">CENTERCENTERCENTERCEN</div>
<img class="right" src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
CSS
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
text-align: center;
}
.container {
background: #abc;
display: inline-block;
margin: 0 auto;
position: relative;
text-align: left;
}
.center {
background: red;
}
.left, .right {
position: absolute;
top: 0px;
}
.left {
right: 100%;
}
.right {
left: 100%;
}
Working Fiddle
Updated to show OP Update
No need for flex here, why not just use percentages? Float all the containers and put the percentages as relative to the sizes you want. (50% for the middle, 25% for the outside containers).
You can use the outside containers as wrappers so you can still use a border on the inner containers without messing up the sizing. Then just float the inner containers within the outside containers (if that makes sense). The example below just floats the inner p tags to the outer containers.
This makes it always hug the inner container, while keeping relative sizes and also keeping the middle centered.
Example below:
Fiddle
HTML
<div class="container">
<div class="flexa">
<div class="left">
<p>leftleft</p>
</div>
<div class="center"><p>CENTERCENTdsfdfdERCENTsdfdfsfERCEN</p></div>
<div class="right">
<p>ri</p>
</div>
</div>
<div class="bottom">BOTTOMOMOM</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
div {
background: #aaaaaa;
overflow: hidden;
}
p{
border: 1px solid black;
}
.container {
width: 500px;
/* for example */
margin: 0 auto;
}
.right p{ /* This is what makes it work. This could be a div with class of inner or something like that. */
float:left;
}
.left p{
float:right;
}
.flexa div{
float:left;
}
.left {
width:25%;
}
.center {
width: 50%;
}
.right {
width:25%;
}
.bottom {
display: block;
margin: 0 auto;
text-align: center;
}

Parent div not stretching to fit children

So I'm working on a tumblr theme, and I have a parent div with 4 children in two rows. The parent will only stretch to the height of the first child, but not to contain all of the children. This causes the div below the parent div to cover up the second row children.
The relevant HTML:
<div class="leftcol">
<div class="sidebar">
<div class="sideblock" id="sideimg"><div class="borda"><div class="circle"></div></div></div>
<div class="sideblock" id="about">
<div class="sidetitle">{lang:About me}</div>
<div class="description"><div class="textbox">{Description}</div></div>
</div>
<div class="sideblock" id="nav">
<div class="sidetitle">{lang:Navigate}</div>
<div class="description">links here</div>
</div>
{block:IfExtraBox}
<div class="sideblock" id="extra">
<div class="sidetitle">{text:Extra Title}</div>
<div class="description">{text:Extra Text}</div>
</div>
{/block:IfExtraBox}
</div>
</div>
The relevant CSS (I removed colors, border-radius, and other things that didn't seem important):
.leftcol {
overflow:auto;
clear:both;
}
.sideblock{
margin-bottom:10px;
overflow: auto;
height: auto;
}
.borda {
border-width: 3px;
padding: 6px;
width: 136px;
height: 136px;
margin: 0px auto;
}
.circle {
width: 128px;
height: 128px;
margin-bottom: 5px;
border-width: 4px;
}
#sideimg{
width:30%;
}
#about{
height:154px;
position:absolute;
left:30%;
top:10px;
width:67%;
}
#nav{
position:absolute;
top:164px;
left:3%;
width:45%;
}
#extra{
position:absolute;
top:164px;
left:52%;
width:45%;
}
.description {
padding:5px 5px 10px 10px;
overflow: auto;
}
.sidetitle {
padding:0 10px;
height:34px;
}
.textbox {
max-height: 100px;
overflow: auto;
padding-right: 5px;
}
I've tried a couple of different solutions. The children were originally floated, so I gave them all ids and made them to be absolutely positioned instead. I tried adding "clear:both;" "overflow:auto;" and "height:auto;" but it still hasn't fixed the problem. Does anyone know what I'm doing wrong? Thanks!
UPDATE: Here is a jsfiddle
UPDATE: I changed all of the children to floated divs and used margins to position them instead, and it now works.

Place text to the bottom instead of right below the other text

I'm using different DIV tags to act as tables and I want to get another DIV at the bottom of the content DIV.
<div class="table">
<div class="table-row">
<div class="table-information">
Content
</div>
<div class="table-content">
More content
<div class="extra">
Here's the extra content
</div>
</div>
</div>
</div>
Here's the CSS:
.table {
display: table;
}
.table-row {
display: table-row;
}
.table-information {
background-color: #eaeaea;
height: 150px;
padding: 10px 15px;
width: 150px;
}
.table-content {
display: table-cell;
height: 150px;
padding: 10px 15px;
}
.extra {
vertical-align: bottom
}
As you can see I have vertical-align: bottom; for the extra class. I want to have the content within that DIV at the bottom and not right below the text More content. But nothing happens when I'm trying this solution.
http://jsfiddle.net/edgren/3jjbV/
How can I solve this problem?
Thanks in advance.
Here is 1 way to achieve that
.table-content {
display: table-cell;
height: 150px;
padding: 10px 15px;
position:relative;
width:150px;
}
.extra {
vertical-align: bottom;
bottom:0;
position:absolute;
}
I do it by adding relative position to the container table-content and then absolute positioning bottom to the extra.
here is a fiddle
Another way is like this:
.extra {
vertical-align: bottom;
display:table-cell;
height: 150px;
width:150px;
}
Here is a fiddle for the 2nd option.
From these 2 options personally i would go with the 1st one,
but you should be careful with the fixed widths and heights (because of the absolute positioning).

logo on left and menu on right in same row

I want to have in the same raw, in the left a logo, and in the right a menu like Contact Us. If I make 3 divs for this and I allign the first left, and the 3rd right, it doesn't work. How can you have this?
Float would be a clean, simple way to do it. Try floating your logo div left, and your menu div right, like so:
HTML:
<div id="navbar">
<div id="logo">Logo</div>
<div id="navmenu">Nav menu</div>
</div>
CSS:
div#logo {
float: left;
}
div#navmenu {
float: right;
}
Without any actual markup to look at, the following is a very simple three-column setup. This is not meant as a three-column page layout, only three columns stretching across the top. Note the use of float to send the DIV's to the same row, left to right*.
* You could set the last right. Also, you will have to clear as well for any content following the #menu-row DIV (this is the overflow: auto part).
CSS
#menu-row {
overflow: auto;
}
#menu-logo {
width: 10%;
float: left;
}
#menu-logo img {
width: 100%;
}
#menu-content {
width: 80%;
background: #ddd;
float: left;
}
#menu-right {
width: 10%;
height: 1.3em;
background: #dfd;
float: left;
}
#menu-content li {
display: inline;
list-style-type: none;
height: 128px;
}
#page-content {
background: #ddf;
}
HTML
<div id="menu-row">
<div id="menu-logo">
<img src="http://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG"/>
</div>
<div id="menu-content">
<ul>
<li>Home</li>
<li>About</li>
<li>Stuff</li>
</ul>
</div>
<div id="menu-right"></div>
</div>
<div id="page-content">
<p>This is stuff.</p>
</div>
http://jsfiddle.net/LYJUB/1/
I dont fully understand your question, but you might be able to fix it by positioning the divs absolute.
in the HTML: <div id="leftdiv"></div>
in the CSS:
#leftdiv{
width:10%;
height:100%;
position:absolute;
left:0%;
top:0%;
}
#rightdiv{
width:10%;
height:100%;
position:absolute;
right:0%;
top:0%;
}
#centerdiv{
width:80%;
height:100%;
position:absolute;
left:10%;
top:0%;
}

CSS: how to get two floating divs inside another div

I'm sure this a common problem, but couldn't find the exact answer :)
I have two divs inside another div. I want the two divs to be on the same level, one floating to the left and the other to the right. But they won't get inside the parent div unless I use position: absolute on the parent. But then the child-divs won't stay on the same level :S
#main {
margin-left: 30px;
margin-top: 20px;
position: absolute;
}
#left_menu {
width: 150px;
float: left;
}
#content {
margin-left: 20px;
float: right;
border: 1px solid white;
}
<div id ="main">
<div id ="left_menu>&blablabal</div>
<div id ="content">blablb</div>
</div>
your margin-left of #content should include the width of #left_menu. Thus should be
#content {
margin-left: 170px;
/* float: right; */ /* no need for a float here */
border: 1px solid white;
}
You also don't need a position:absolute for your #main (unless other purposes)
So finally:
<style type="text/css"><!--
#main {
margin-left: 30px;
margin-top: 20px;
}
#left_menu {
width: 150px;
float: left;
}
#content {
margin-left: 170px;
border: 1px solid white;
}
.c{clear:both;}
--></style>
<div id="main">
<div id="left_menu>&blablabal</div>
<div id="content">blablb</div>
</div>
<div class="c"></div>
.c is to clear and pushes the bottom content off the floats.
What about this its all to do with your width on your container.
This works for me.
<style type="text/css"><!--
.Content{
Width:100%;
}
.FloatLeft{
float:left;
}
.FloatRight{
float:Right;
}
-->
</style>
<div class="Content">
<div class="FloatLeft"></div>
<div class="FloatRight"></div>
</div>
you will need to 'float' the main div, or use a clearing <div> or <br> after your content and left menu <div>s.
The problem is not "staying on the same level", but it's about the size of the container div.
This might help you: http://archivist.incutio.com/viewlist/css-discuss/63079
The nicest and easiest thing to do is to set overflow: hidden on the container, #main. I don't think this works in IE6 though.
try giving the main div an overflow: hidden; and taking away it's position: absolute;
which will give it a height equivalent to the greater height of the floating divs
Also, I don't know if you copied it from your page, but you're missing a close quotation in your left_menu id=""
#main{
display:inline-block;
width:100%;
}
and remove absolute to the parent;
#left_menu,#content{
....
vertical-align:top;
}

Resources