How to make sidebar with same height as the content div? - css

the code is the following:
(CSS)
#container {
border:1px dashed #000;
overflow:hidden;
}
#content, #sidebar {
float:left;
width:50%;
}
#content {
background:yellow;
}
#sidebar {
background:grey;
}
#sidebar {
height:100%;
}
(HTML)
<div id="container">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>
</div>
<div id="sidebar">
<p>Few words</p>
</div>
</div>
I would like to see a #sidebar div with same height, like the #content div, is it possible without absolute position? Online version: http://jsfiddle.net/yJbUW/

You can do that by displaying the #container as table and displaying #content and #sidebar as table-cells:
#container {
border:1px dashed #000;
display: table;
width: 100%;
}
#content, #sidebar {
display: table-cell;
width:50%;
}
Check your updated Fiddle.

You can also try below code
CSS goes here
#container {
border:1px dashed #000;
overflow:hidden;
}
#content, #sidebar {
float:left;
width:50%;
}
#content {
background:yellow;
}
#sidebar {
background:grey;
}
#Container
{
height: auto;
width: auto;
}
and update HTML as follows,
<div id="Container">
<span id="content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>
</span>
<span id="sidebar">
<p>Few words</p>
</span>
</div>

It can be solved with
#content, #sidebar {
display: table-cell;
width:50%;
}

.table {
display:table;
}
.row {
display:table-row;
}
.one {
width:200px;
border:1px solid #333;
display:table-cell
}
<div class="table">
<div class="row">
<div class="one">gfhgfhgfhgf</div>
<div class="one">gfhgfhgfhgf</div>
</div>
</div>

Just use this code before the container closing Div, works for me!
<p style="clear:both;"></p>

Here's the simplest and most elegant solution:
#container {
display:grid;
grid-auto-flow: column;
}

Related

How to align same height divs

I've this code
.row {
width: 600px;
}
.row div {
display: inline-block;
width: 50%;
float: left;
}
.text {
background-color: lightblue;
}
<div class="row">
<div class="image">
<img src="http://placehold.it/250x150">
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis incidunt.</p>
</div>
</div>
I want my div .text to have the same height as the image.
I want it responsive no I can't set a height to .text
Thanks for your help !
One option is with Flexbox
body, html {
margin: 0;
padding: 0;
}
.row {
width: 600px;
display: flex;;
}
.text {
background-color: lightblue;
}
img {
vertical-align: top;
}
<div class="row">
<div class="image"><img src="http://placehold.it/250x150">
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis incidunt.</p>
</div>
</div>
Other one is CSS table
body, html {
margin: 0;
padding: 0;
}
.row {
width: 600px;
display: table;
}
.text {
background-color: lightblue;
display: table-cell;
vertical-align: top;
}
.image {
display: table-cell;
vertical-align: top;
}
img {
vertical-align: top;
}
<div class="row">
<div class="image"><img src="http://placehold.it/250x150">
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis incidunt.</p>
</div>
</div>
Here is one way using display-table:
.row {
width: 600px;
display: table;
}
.row > div {
width: 50%;
display: table-cell;
vertical-align: top; /* or 'middle' or 'bottom' etc. */
}
.text {
background-color: lightblue;
}
.image img {
display: block;
}
<div class="row">
<div class="image">
<img src="http://placehold.it/250x150">
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis incidunt.</p>
</div>
</div>
Here is one solution which just uses height:auto; :
.row {
width: 600px;
height: auto;
background-color: lightblue;
}
.image, .text {
display: inline-block;
width: 49%;
}
.image {
background-color: white;
}
.text, .image img {
vertical-align:top;
}
<div class="row">
<div class="image">
<img src="http://placehold.it/250x150">
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis incidunt.</p>
</div>
</div>

Position:absolute & float left/right does not work well

I have problem with position: absolute and floats...
As you can see in fiddle, when you resize browser, text from left side goes below the image, but what I want is to go above the image.
When I remove position: absolute on .image-ipad, it works like that, but in my project I need image to stick on left side, and text on right side (normal)
Code:
HTML
<section class="case eat-login">
<div class="row">
<h3 class="stick-right">Login Screen</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<img class="image-ipad" src="http://www2.pcmag.com/media/images/362825-apple-ipad-mini.jpg?thumb=y" width="300px">
<div style="clear:both;"></div>
</div>
</section>
CSS
.stick-right{
float:right;
clear:both;
}
.eat-login {
position: relative;
padding: 130px 0 130px;
clear: both;
}
.eat-login p{
text-align: right;
float: right;
clear: both;
width: 45%;
font-size: 15px;
font-family: "Proxima Nova";
color:#9e9d9d;
line-height: 25px;
}
img.image-ipad {
left:0px;
top:40px;
}
JSFiddle: http://jsfiddle.net/jt9jk2g0/6/
Thanks!
You could do it by using css #media queries for small screen size:
JSFiddle - DEMO
HTML:
<section class="case eat-login">
<div class="row">
<img class="image-ipad" src="http://www2.pcmag.com/media/images/362825-apple-ipad-mini.jpg?thumb=y" width="300px">
<h3 class="stick-right">Login Screen</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<div style="clear:both;"></div>
</div>
</section>
CSS:
.stick-right {
float:right;
clear:both;
}
.eat-login {
position: relative;
padding: 130px 0 130px;
clear: both;
}
img {
position: absolute;
}
.eat-login p {
text-align: right;
float: right;
clear: both;
width: 45%;
font-size: 15px;
font-family:"Proxima Nova";
color:#9e9d9d;
line-height: 25px;
}
#media (max-width: 500px) {
h3 {
margin-top:300px;
}
}

Footer background not showing correctly?

I want to achieve this:
I have achieved this:
Why is the background of footer not showing correctly. Also why is the margin-bottom of 'home-features' section not working? The page is live at http://goo.gl/OpAB4V
Markup:
<section id="home-features">
<article class="home-feature-box">
<img src="images/tellerest-homepage-design_33.png" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed felis porttitor elementum</p>
</article>
<article class="home-feature-box">
<img src="images/tellerest-homepage-design_33.png" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed felis porttitor elementum</p>
</article>
<article class="home-feature-box-right">
<img src="images/tellerest-homepage-design_33.png" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed felis porttitor elementum</p>
</article>
</section>
<footer>
<p>Copyright © 2014, Tellerest. All Rights Reserved.</p>
</footer>
CSS:
/* home features section */
#home-features { margin: 40px auto 0 auto; width: 950px; text-align: center;}
.home-feature-box { width: 256px; float:left; margin-right: 91px;}
.home-feature-box-right { width: 256px; float:right;}
#home-features p { font-family:Verdana, Geneva, sans-serif; font-size: 14px; font-weight: normal; line-height: 24px; color: #4f4f4f;}
/* footer */
footer { background-color: #fafafa; border-top: 1px solid #e0e0e0; text-align: center; font-family:Verdana, Geneva, sans-serif; font-size: 11px; font-weight: normal; color: #a3a3a3; padding: 15px 0 30px 0;}
You must clear your floats.
After the last article, add this div:
<div style="clear:both"></div>
Of course, you should simply create a "clear" class instead of having inline styling, but you get the idea.
Upon observation, I see that your #container div also has a fixed height of 1000px. Simply remove that as well.
You have given height:1000px for #container
Remove that height and clear the div
<section id="container">
.
.
.
<div class="clear"></div>
</section>
CSS for clear
.clear {
clear: both
}
Just change your #container class to following code.
#container {
background-image: url("images/tellerest-homepage-design2_01.png");
background-position: center top;
background-repeat: repeat-x;
overflow: hidden;
}

Align 2 div without size

Is possible auto align in line 2 div without size?
The goal is to get something like the image attached.
I can't set percentages (80% and 20%) because the content is dynamic. I can't use tables. Any idea?
Example: http://jsfiddle.net/hKRyG/
<html>
<head>
<style type="text/css">
.navbar {
width: 500px;
background: #ccc;
display:table;
}
.navbar .breadcrumb {
float:left;
}
.navbar .breadcrumb ul {
margin: 0;
padding: 0;
}
.navbar .breadcrumb li {
list-style: none;
display: inline;
}
.navbar .navbutton {
float: right;
}
</style>
</head>
<body>
<div class="navbar">
<div class="breadcrumb">
<ul>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
</ul>
</div>
<div class="navbutton">
<input type="button" value="BUTTON" />
</div>
</div>
</body>
</html>
Thank you!
There you go! To align without defined size you must use table display properties
.navbar {
width: 500px;
background: #ccc;
display:table;
}
.navbar .breadcrumb ul {
margin: 0;
padding: 0;
}
.navbar .breadcrumb li {
list-style: none;
display: inline;
}
.navbar ul{
display: table-cell;
}
.navbutton {
display: table-cell;
vertical-align: middle;
}
Use the following:
<html>
<head>
<style>
.navbar {
width: 500px;
background: #ccc;
display:table;
float: left;
}
.navbar .breadcrumb {
float:left;
}
.navbar .breadcrumb ul {
margin: 0;
padding: 0;
}
.navbar .breadcrumb li {
list-style: none;
display: inline;
}
.navbar .navbutton {
float: right;
}
</style>
</head>
<body>
<div class="navbar">
<div class="breadcrumb">
<ul>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
</div>
</div>
<div class="navbutton">
<input type="button" value="BUTTON" />
</div>
<body>
</html>
If you move the button to the top of the markup and give it a float: right;, then add overflow: hidden; to the <div class="breadcrumb"> the button should be flush right and the breadcrumb will take up whatever room remains on the left.
jsFiddle
HTML:
<div class="navbar">
<div class="navbutton">
<input type="button" value="BUTTON" />
</div>
<div class="breadcrumb">
<ul>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
<li>-></li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
</ul>
</div>
</div>​
CSS
.navbar {
width: 500px;
background: #ccc;
}
.navbar .navbutton {
float: right;
}
.navbar .breadcrumb {
overflow: hidden;
}
.navbar .breadcrumb ul {
margin: 0;
padding: 0;
}
.navbar .breadcrumb li {
list-style: none;
display: inline;
}​

Div doesn't have height unless I give it min-height

I think this is because I'm floating all three divs "columns" to the left, inside of the main body div.
How can I tell the main body div to expand as big as it needs to fit the content divs?
Here it's with min-height:
And here with the min-height taken away:
Here is my relevant code.
#body
{
border:1px solid blue;
width:950px;
margin-left:auto;
margin-right:auto;
margin-top:15px;
}
#leftcolumn
{
min-height:500px;
float:left;
width:190px;
}
#contactarea
{
font-family:Arial;
}
#contactarea p.heading
{
Color:#000;
font-size:large;
position:relative;
left:14px;
}
#contactarea p.tag
{
color:#000;
font-size:medium;
position:relative;
left:10px;
}
#leftnavigation ul
{
margin:0;
padding: 0;
list-style: none;
}
#leftnavigation ul li {
border-top: 1px solid #333;
border-bottom: 1px solid #111;
}
#leftnavigation ul li:first-child {border-top: none;}
#leftnavigation ul li:last-child {border-bottom: none;}
#leftnavigation ul li a
{
padding: 10px;
display: block;
color: #fff;
background-color:#222222;
text-decoration: none;
}
#leftnavigation ul li a:hover {background: #111;}
#contentarea
{
border:1px solid blue;
min-height:500px;
float:left;
width:594px;
margin-left:5px;
margin-right:5px;
}
#advertisingarea
{
width:150px;
float:left;
min-height:500px;
background-image:url('images/advertisingAreaBackground.png');
background-repeat:repeat-y;
}
.advert
{
height:190px;
overflow:hidden;
}
.advert img
{
padding:0;
margin:0;
position:relative;
left:25px;
top:5px;
}
.advert p
{
font-size:x-small;
font-family:Arial;
margin-left:8px;
margin-right:8px;
margin-top:5px;
}
<div id="body">
<div id="leftcolumn">
<div id="leftnavigation">
<ul>
<li>Automoviles</li>
<li>Clases y Talleres</li>
<li>Electronicos</li>
<li>Inmobiliaria</li>
<li>Libros</li>
<li>Musica, Peliculas y Juegos</li>
<li>Ninos</li>
<li>Otros</li>
<li>Ropa</li>
</ul>
</div>
<div id="contactarea">
<p class="heading">Anuncios Premium</p>
<p class="tag">Destaque sus anuncios con una cuenta premium!</p>
</div>
</div>
<div id="contentarea">sdfg<h1>asdasd</h1></div>
<div id="advertisingarea">
<div class="advert">
<img src="../../Content/images/advertImage.png" alt="Advert" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nibh nisi, volutpat a vehicula eget</p>
</div>
<div class="advert">
<img src="../../Content/images/advertImage.png" alt="Advert" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nibh nisi, volutpat a vehicula eget</p>
</div>
<div class="advert">
<img src="../../Content/images/advertImage.png" alt="Advert" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nibh nisi, volutpat a vehicula eget</p>
</div>
</div>
</div>
The easiest way is just to set the oveflow of the container:
#body
{
...
overflow: auto;
}
For a div to "wrap" around the elements it contains you have to float it (float:left) if you have already floated other elements.
For the floated div to be centered the best cross-browser solution is to wrap it on another not-floated div (invisible) and center that div. Something like this:
<div id="centered">
<div id="floated">
</div>
</div>
This way you can have the inside div wrapping the elements it contains while not specifying any hieght value, while the outer div (invisible, just with width) keeps it centered ;)
you should use firebug to resolve css issues like this. You'd be surprised how useful it is.
In answer to your question -
It has no height because of the floating elements. You need to clear the floats within that element for the parent to display as expected. You can do this a few ways.
make a clear class and simply add it as an element after the floats:
<style>
.clear{clear:both;}
</style>
<div class="parent">
<div class="float_left">left</div>
<div class="float_right">right</div>
<div class="clear"></div>
</div>
a more proper auto clear for any element that has a class of "container":
<style>
.container:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.container {
display: inline-block;
}
html[xmlns] .container {
display: block;
}
* html .container {
height: 1%;
}
</style>
<div class="parent container">
<div class="float_left">left</div>
<div class="float_right">right</div>
</div>

Resources