I'm doing some HTML and am running into an issue with my left and right column divs not scaling based on the content inside them as well as in proportion to the rest of the page.
Here's my example:
http://i.imgur.com/9vi2EK9.png
And my CSS for each of the divs:
#wrapper{
background:white;
width:1280px;
position:relative;
border: 1px solid black; /* black border */
margin:auto; /* centre this div */
}
#header{
height:90px;
text-align:center;
padding: .5em;
background-color: grey;
border-bottom: 2px solid black;
}
#leftmenu{
width:100px;
float:left;
font-size: 75%;
padding:.5em;
border-right:2px solid black;
border-left:2px solid black,
}
#rightmenu{
width:180px;
float:right;
font-size: 75%;
padding:.5em;
border-left:2px solid black;
border-right:1px solid black;
}
#content{
background-color:white;
margin-left:120px;
font-size: 80%;
}
#footer{
clear:both; /* push footer under other divs */
height: 70px;
background-color:white;
border-top:1px solid black;
border: 1px solid black;
padding-top:40px;
text-align:center;
font-size: 70%;
}
How can I ensure that my divs resize based on the content in my other divs?
Thanks!
Without seeing your HTML, here's a stab at it:
JSFiddle here: http://jsfiddle.net/2hf8q/
CSS
html, body {
height: 100%;
}
#wrapper {
height: calc(100% - 2px); /* for border */
}
#leftmenu, #rightmenu {
height: calc(100% - 234px); /* for header, footer */
}
#wrapper {
background:white;
width:100%;
position:relative;
border: 1px solid black;
/* black border */
margin:auto;
/* centre this div */
}
#header {
height:90px;
text-align:center;
padding: .5em;
background-color: grey;
border-bottom: 2px solid black;
}
#leftmenu {
width:100px;
float:left;
font-size: 75%;
padding:.5em;
border-right:2px solid black;
border-left:2px solid black;
}
#rightmenu {
width:180px;
float:right;
font-size: 75%;
padding:.5em;
border-left:2px solid black;
border-right:1px solid black;
}
#content {
background-color:white;
margin-left:120px;
font-size: 80%;
}
#footer {
clear:both;
/* push footer under other divs */
height: 70px;
background-color:white;
border-top:1px solid black;
border: 1px solid black;
padding-top:40px;
text-align:center;
font-size: 70%;
}
HTML
<div id="wrapper">
<div id="header"></div>
<div id="leftmenu"></div>
<div id="content"></div>
<div id="rightmenu"></div>
<div id="footer"></div>
</div>
You can do it by wrapping #leftmenu, #content and #rightmenu in a div that is displayed as a table. And display the three children as a table-cell:
HTML:
<div id="header">#header</div>
<div id="wrapper">
<div id="leftmenu">#leftmenu</div>
<div id="content">#content</div>
<div id="rightmenu">#rightmenu</div>
</div>
<div id="footer">#footer</div>
CSS (without colors, padding, font sizes and stuff):
html, body {
margin: 0;
padding: 0;
}
#header{
height:90px;
}
#wrapper {
display: table;
table-layout: fixed;
width: 100%;
}
#wrapper > div {
display: table-cell;
}
#leftmenu{
width:100px;
}
#rightmenu{
width:180px;
}
#content{
}
#footer{
height: 70px;
}
And a working demo.
Related
Why is the two boxes to the right side, and not centered align?
fiddle: http://jsfiddle.net/gh76q/1/
I, tried change the position of box 2 to relative, it will do the trick but the box moves when hovering the box 1.
HTML:
<div align="center" id="top-link">
<div class="switch">
<div class="avg_display">Box 1</div>
<div class="avg_hide">Box1 info<br></div></div>
<div class="link-block">Box2</div>
</div>
CSS:
#top-link {padding-bottom:40px;}
.switch {display:inline-block;}
.link-block {
display: inline-block;
padding: 20px 20px 20px 20px;
border: 2px solid #e7e7e7;
border-radius: 5px;
height:25px;
width:200px;
text-align:center;
margin-top:10px;
margin-right:20px;
position:absolute;
color:#555;
}
.link-block a {
text-align: center;
vertical-align: middle;
line-height: 25px;
color:#555;
}
.link-block:hover {
background-color:#1396d0;
border:2px solid #1396d0;
color:#fff;
margin-top:10px;
margin-right:20px;
}
.link-block:hover a{
color:#fff;
}
.avg_display {
display:inline-block;
padding: 20px 20px 20px 20px;
border: 2px solid #e7e7e7;
border-radius: 5px;
height:25px;
width:200px;
text-align:center;
margin-top:10px;
margin-right:20px;
}
.avg_hide {
display:none;
padding: 20px 20px 20px 20px;
background-color:#1396d0;
border:2px solid #1396d0;
color:#fff;
border-radius: 5px;
height:250px;
width:200px;
text-align:center;
margin-top:10px;
margin-right:20px;
}
.avg_display a {
text-align: center;
vertical-align: middle;
line-height: 25px;
color:#555;
}
Fiddle:
Remove position:absolute from the box2 class.
.link-block {
display: inline-block;
padding: 20px 20px 20px 20px;
border: 2px solid #e7e7e7;
border-radius: 5px;
height:25px;
width:200px;
text-align:center;
margin-top:10px;
margin-right:20px;
/*position:absolute;*/
vertical-align:top; /* Keep the box always on top */
color:#555;
}
DEMO
I am trying to make a basic button box with an arrow at the top of it...
http://jsfiddle.net/8K4qB/ --this is what it comes out to.. i can't make it align at the top in the middle of the bottom.
HTML Code
Read More
CSS Code
.button {
font-size: 15px;
color: white;
text-align: center;
padding:10px 30px 10px 30px;
margin:0 auto;
background: #2ecc71;
}
.test:before {
content:'';
position: absolute;
width: 0;
height: 0;
border-bottom: 20px solid #3498db;
border-right:50px solid transparent;
border-left:50px solid transparent;
}
I'd like it to look like the pic
In order to achieve the specific behaviour you're after you need to change your HTML as well as your CSS- otherwise centrally justifying a pseudo element correctly will elude you.. Change the elements in the example as required (e.g. the top level div can be changed to a.button.test)
REVISED FIDDLE
HTML
<div>
<div></div>
<div>text goes here</div>
</div>
CSS
div {
display:inline-block;
text-align:center;
}
div div:first-child {
display:block;
width: 0;
height: 0;
border-bottom: 10px solid darkorange;
border-right:30px solid transparent;
border-left:30px solid transparent;
margin: 0 auto;
}
div div:last-child{
display:block;
font-size: 15px;
color: white;
text-align: center;
padding:10px 30px 10px 30px;
margin:0 auto;
background: darkorange;
position:relative;
text-transform:uppercase;
}
Try this ,display:block so that your arrow still in the middle: DEMO
CSS:
.button {
font-size: 15px;
color: white;
text-align: center;
padding:10px 30px 10px 30px;
margin:0 auto;
background: #2ecc71;
display:block;position:relative;top:20px;
}
.test:before {
content:'';
position: absolute;
top:-20px;
width: 0;
height: 0;
border-bottom: 20px solid #3498db;
border-right:50px solid transparent;
border-left:50px solid transparent;
}
.middle{
padding-left:15px;
}
HTML:
<span class="middle">Read More</span>
I am working on a mobile site where the page needs to change based on portrait and landscape mode. I have that part working. When the mobile device is in portrait mode it's suppose to display 4 boxes as such:
[] []
[] []
When in landscape mode, they are to be like this:
[] [] [] []
I have this mostly accomplished, however in both portrait and landscape mode, they are centered left. I have scoured this site and have found similar questions, but when trying to implement the solution, it doesn't seem to work. I would like them to be centered (using percentages instead of fixed width so it shows up correctly on any mobile device). I am not too familiar with CSS, so pardon me, but here is my code below:
/* Landscape mode (default) */
div.my_wrapper{
width: auto;
position:relative;
padding:20px;
}
div.my_innerBox {
width:100%;
float:left;
border: 1px solid green;
}
div.my_left_box{
float: left;
margin-right:10px;
padding: 20px;
/*margin-left:90px;*/
width: 16%;
border: 1px solid gray;
}
div.my_right_box{
float: left;
margin-right:10px;
padding:20px;
width: 16%;
border: 1px solid gray;
}
div.my_header{
padding:10px;
float:left;
width: 100%;
border: 1px solid gray;
margin-bottom:12px;
}
#media only screen and (orientation:portrait){
/* portrait mode */
div.my_wrapper{
width: inherit;
margin-left:auto;
margin-right:auto;
padding:20px;
}
div.my_innerBox {
width: 95%;
display:inline;
}
div.my_left_box{
float: left;
padding: 20px 10px;
/*margin-left:90px;*/
margin: 0 10px 10px 0;
width: 30%;
border: 1px solid gray;
}
div.my_right_box{
float: left;
margin: 0 10px 10px 0;
padding:20px 10px;
width: 30%;
border: 1px solid gray;
}
div.my_header{
padding:10px;
border: 1px solid gray;
margin-bottom:12px;
text-align:left;
float:left;
}
div.my_desc {
text-align:left;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
vertical-align:middle;
position:absolute;
float:left;
width:500px;
}
div.my_header_container {
width:100%;
float:left;
}
}
<div class="my_header_container">
<div class="my_header">Title Will Go here
<div class="my_desc">This is where the description content will go</div>
</div>
</div>
<div class="my_wrapper">
<div class="my_left_box">
Button 1
</div>
<div class="my_right_box">
Button 2
</div>
<div class="my_left_box">
Button 3
</div>
<div class="my_right_box">
Button 4
</div>
</div>
Also, I need where it says "this is where the content description will go" to be to the right of "Title Will Go here", and to be vertically centered.
Any help would be greatly appreciated. I am trying to learn css as I am more of a programmer, not a designer. Thank you, I appreciate any help you can send my way.
You should really consider using a SASS preprocessor and harness the power of numerous Compass extensions.
Here's how your issue is solved with SASS: http://sassbin.com/gist/5785963/
Note how much shorter and easier to read the code is.
#fredsbend said that you can't center something floated. Well, this is not true when SASS automatically sets the widths of all elements precicely to your liking, while you only provide column numbers.
First, you are missing the close bracket for the #media query.
You cannot expect to center something when a float has been applied to it.
I changed the float:left; on the .my_left_box and the .my_right_box to text-align:left; and added display:inline-block;. I then added margin: 0 auto; and text-align:center; to .my_wrapper and changed the width to 90%. I did not make these changes to the (orientation:portrait) section.
Here is the css:
/* Landscape mode (default) */
div.my_wrapper{
width: 90%;
position:relative;
padding:20px;
margin: 0 auto;
text-align:center;
}
div.my_innerBox {
width:100%;
float:left;
border: 1px solid green;
}
div.my_left_box{
margin-right:10px;
padding: 20px;
/*margin-left:90px;*/
display:inline-block;
width: 16%;
border: 1px solid gray;
text-align:left;
}
div.my_right_box{
margin-right:10px;
padding:20px;
display:inline-block;
width: 16%;
border: 1px solid gray;
text-align:left;
}
div.my_header{
padding:10px;
float:left;
width: 100%;
border: 1px solid gray;
margin-bottom:12px;
}
#media only screen and (orientation:portrait){
/* portrait mode */
div.my_wrapper{
width: inherit;
margin-left:auto;
margin-right:auto;
padding:20px;
}
div.my_innerBox {
width: 95%;
display:inline;
}
div.my_left_box{
float: left;
padding: 20px 10px;
/*margin-left:90px;*/
margin: 0 10px 10px 0;
width: 30%;
border: 1px solid gray;
}
div.my_right_box{
float: left;
margin: 0 10px 10px 0;
padding:20px 10px;
width: 30%;
border: 1px solid gray;
}
div.my_header{
padding:10px;
border: 1px solid gray;
margin-bottom:12px;
text-align:left;
float:left;
}
} /* Added here but maybe you want it somewhere else. */
div.my_desc {
text-align:left;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
vertical-align:middle;
position:absolute;
float:left;
width:500px;
}
div.my_header_container {
width:100%;
float:left;
}
Here is the jsfiddle: http://jsfiddle.net/tspT4/10/
I was not able to test this on a mobile but it should work
This is the html part of my page:
<div class="edenKomentar">
<div class="rejtingVoKomentar" onmouseover="alert('asd');">
<div class="thumbUp"> </div>
<div class="thumbDown"> </div>
</div>
<div class="imeVoKomentar">Name</div>
<div class="tekstVoKomentar">Comment</div>
<div class="datumVoKomentar">Date</div>
</div>
and this is my css code:
div.edenKomentar:hover {
// border-bottom: dashed 1px gray;
border-left: solid 8px #003366;
}
div.edenKomentar:hover div.imeVoKomentar {
color:#003366;
}
.edenKomentar div.rejtingVoKomentar {
position: absolute;
overflow:hidden;
right:5px;
top:15px;
height:35px;
width: 100px;
padding: 5px;
border: solid 1px green;
}
.edenKomentar div.rejtingVoKomentar:hover{
border: solid 2px green;
background-color:#66FF00;
}
.rejtingVoKomentar .thumbUp,.thumbDown {
position: relative ;
height: 25px;
width: 25px;
border: solid 1px red;
top: 10px;
}
.rejtingVoKomentar .thumbDown {
right: 5px;
background:url(sliki/Ikoni/Thumb-down-icon.png);
background-repeat:no-repeat;
}
.rejtingVoKomentar .thumbUp {
left: 5px;
background:url(sliki/Ikoni/Thumb-up-icon.png);
background-repeat:no-repeat;
}
.rejtingVoKomentar .thumbDown:hover{
border: solid 2px red;
}
The problem is that hover (and onmouseover) on .rejtingVoKomentar ( and .thumbUp and .thumbDown also) is not working while .rejtingVoKomentar has the
position:absolute;
attribute. What can be the problem here?
.headermenu li a{
text-align:center;
padding:0px 0 0 12px;
margin-left:20px;
display:block;
color: #ccc;
text-decoration:none;
font-size:12px;
float:right;
background:url(../images/arrow.png) no-repeat left center;
}
.headermenu li a:hover{
text-align:center;
display:block;
color: #0066CC;
text-decoration:none;
float:right;
background:url(../images/arrow_hover.png) no-repeat left center;
}
You can use this short Jquery code:
$(document).ready(function() {
$(".rejtingVoKomentar").hover(
function () {
//On mouseOver
alert('asd');
},
function () {
//On mouseOut
}
);});
you will use jQuery to activate hover on DIV
or just want to use it by CSS only you will need to use ul,li instead
UPDATE1
check this link in SO Jquery Div Hover change other Div Class
Please see the demo here: http://jsfiddle.net/Freewind/Vkp4U/
The html:
<div class="outer">
<div class="x">x</div>
<div class="y">y</div>
<div class="z">z</div>
</div>
The css:
.outer {
border: 1px solid black;
overflow: scroll-x;
}
.x {
width: 400px;
border: 1px solid red;
float:left;
}
.y {
width: 400px;
border: 1px solid yellow;
float:left;
}
.z {
width: 400px;
border: 1px solid blue;
float:left;
}
How to let the 3 inner divs displayed in line, won't wrap if I resize the browser to smaller?
Limitations:
the width of outer div should not be specified
the width of inner divs are fixed
Hi you can define display table and table-cell in you css properties as like this
Css
.outer {
border: 1px solid black;
overflow: scroll-x;
display:table;
}
.x {
width: 400px;
border: 1px solid red;
display:table-cell;
}
.y {
width: 400px;
border: 1px solid yellow;
display:table-cell;
}
.z {
width: 400px;
border: 1px solid blue;
display:table-cell;
}
HTML
<div class="outer">
<div class="x">x</div>
<div class="y">y</div>
<div class="z">z</div>
</div>
Live demo http://jsfiddle.net/Vkp4U/5/
this may work:
The idea here is to set the outer as relative postion. now all the absolute sub div will refer to their parent relative div. (demo here:http://jsfiddle.net/Vkp4U/7/)
.outer {
border: 1px solid black;
overflow: scroll-x;
position:relative;
}
.x {
width: 400px;
border: 1px solid red;
float:left;
position:absolute;
left:0px;
}
.y {
width: 400px;
border: 1px solid yellow;
float:left;
position:absolute;
left:400px;
}
.z {
width: 400px;
border: 1px solid blue;
float:left;
position:absolute;
left:800px;
}
Use % in css width...
.outer {
border: 1px solid black;
overflow: scroll-x;
}
.x {
width: 30%;
border: 1px solid red;
float:left;
}
.y {
width: 30%;
border: 1px solid yellow;
float:left;
}
.z {
width: 30%;
border: 1px solid blue;
float:left;
}