I have a div than spans the entire width of the page. Then within that I have two images, one that's always 50x50 pixels, and then another one that is 400x90. The smaller image is floated to the right, and the larger one just remains on the left side.
I have it how I want it, except when I resize the window to smaller than the widths of the two images. When this happens, I would like the larger image to scale down, just to fill the remaining width. Instead now, it keeps its current width and jumps down underneath the smaller image.
What's the simplest way to make it scale down instead?
Here's a fiddle to illustrate the issue I'm having: https://jsfiddle.net/zdaujbaL/
And here's my code:
HTML
<div class="clearfix">
<a class="button">
<img src="//placehold.it/50x50">
</a>
<a class="logo">
<img src="//placehold.it/400x90">
</a>
</div>
CSS
div {
background-color: lightblue;
padding: 5px;
margin: 5px;
}
a.button {
float: right;
}
a.logo {
display: inline-block;
}
a.logo img {
width: 100%;
}
You should make the non-float element as block level and add overflow:auto
a.logo {
display: block;
overflow: auto;
}
a.logo img {
max-width: 100%;
}
JSFiddle: https://jsfiddle.net/zdaujbaL/5/
Alternatively, you could do it with CSS table + table-cell layout.
div {
background-color: lightblue;
display: table;
width: 100%;
padding: 5px;
}
a.button, a.logo {
display: table-cell;
vertical-align: top;
}
a.logo {
width: 100%;
}
a.logo img {
max-width: 100%;
}
(you'll need to update the order - logo first, button second in the markup)
JSFiddle: https://jsfiddle.net/zdaujbaL/6/
Related
I need need to have each of my div's align to the edge of the main content div and stay on one line unless the dynamic content loaded is wider than a fixed width. Let's say 300px. I would like the main container to auto re-size according to content width with a margin of 20px on each side of the content. I want the content to go to automatically place it self on a new line if it exceeds the maximum width of the main container div. Here is my jsfiddle. I can't seem to get it to align correctly to the left or auto scale.
<div class='info_content'>
<div class='dealerName'><h3>{{dealerName}}</h3></div>
<div class='address'>{{address}}</div>
<div class='addressCont'>{{city}}, {{state}} {{zip}}</div>
<div class='telephone'><label for='phone'>Phone:</label>{{phone}}</div>
<div class='tags'><label for='Tags'>Tags:</label>{{tags}}</div>
<div class='dealerWebsite'><a href='{{href}}'>{{href}}</a></div>
</div>
CSS:
#DealerInfoContainer {
float: left;
margin-left: 50px;
max-width:400px;
height: auto;
border: 1px solid #000000;
}
#DealerInfo {
}
#DealerInfo p {
margin-top: -20px;
}
.telephone {
float: left;
}
.address {
float: left;
}
.addressCont {
float: left;
}
.tags {
float: left;
}
.dealerWebsite {
float: left;
}
.dealerName {
float: left;
}
.info_content {
width: 300px;
}
A different way, is to use display: inline-block on each div you want to float. like this this could be usefull if your content (as a can see) is variable
.inline{
display: inline-block;
}
If you want to use float:left, don't forget the clearfix at the end.
EDIT :
To make you code work, you must remove margin for the H3 and set a line-heigth with a vertical-align : jsfiddle
.clearfix{
clear:both;
}
.dealerName h3{
margin:0;
}
.align{
line-height :30px;
vertical-align: baseline;
}
For me the cleanest method is with display: inline-block.
Use this CSS:
div {float:left;display:-moz-inline-stack;display:inline-block;zoom:1;*display:inline;}
It's compatible with all major browsers and does everything you want.
I'm trying to create a list of items where each item in the list contains essentially two columns ... the left column some text, and the right column 2 buttons for yes/no. I want the two buttons on the right to be vertically aligned with the text. For aesthetic reasons, I want a min-height on the list item. I finally figured out that a floating div must be inside an absolute div for the 100% height to work. The problem is now that I have an absolute div inside my original relative div, it no longer expands to accommodate text longer than min-height. I've read so many articles and tried so many different combinations of height/relative/absolute/float/clear/overflow and nothing has worked for my situation. Is there a solution to this?
In my example here http://jsfiddle.net/THBFY/4/ I need the red box to be the same height as the blue box so that the vertical align works.
<div class="list_container">
<div class="list_item">
<div class="item_text">
My text in this item. This could be a variable length creating a div ranging from about 75-150px in height. This is a lot of text to make it longer although I am not really saying anything here. It is only to make the blue box taller than the red box.
</div>
<div class="item_buttons">
<div class="buttons_inner">
<div class="button button_yes">Y</div>
<div class="button button_no">N</div>
</div>
</div>
</div>
</div>
.list_container { position: relative; width: 400px; }
.list_item { position: relative; min-height: 70px; overflow: hidden; border: #000000 solid 1px; }
.item_text { float: left; width: 340px; background-color: #0066BB }
.item_buttons { display: table; float: right; width: 50px; height: 100%; background: #FF0000; }
.buttons_inner { display: table-cell; vertical-align: middle; }
.button { display: block; margin-left: auto; margin-right: auto; height: 40px; width: 40px; background-repeat: no-repeat; }
.button_yes { background-image: url("images/yes.gif") }
.button_no { background-image: url("images/no.gif") }
When I add in the inner div with position:absolute http://jsfiddle.net/THBFY/5/ the problem is the height no longer increases to show all of the text.
<div class="list_item_inner">...
.list_item_inner { position: absolute; height: 100%; }
But if I now change the min-height of the outer div from 70 to 200 http://jsfiddle.net/THBFY/6/, you can see that the 100% height on the red box is in fact working, so my problem is either in the first situation without the absolute position, I need the red box to stretch, or in the 2nd situation with the absolute div, I need the container to stretch.
HTML:
<div class="list_container">
<div class="list_item">
<div class="item_text">My text in this item. This could be a variable length creating a div ranging from about 75-150px in height. This is a lot of text to make it longer although I am not really saying anything here. It is only to make the blue box taller than the red box.
</div>
<div class="item_buttons">
<div class="buttons_inner">
<div class="button button_yes">Y</div>
<div class="button button_no">N</div>
</div>
</div>
</div>
</div>
CSS:
.list_container { position: relative; width: 400px; }
.list_item { border: #000000 solid 1px; display:table; }
.item_text { display:table-cell; width: 340px; background-color: #0066BB }
.item_buttons { display:table-cell; width: 50px; background: #FF0000; }
.button { display: block; margin-left: auto; margin-right: auto; height: 40px; width: 40px; background-repeat: no-repeat; }
.button_yes { background-image: url("images/yes.gif"); }
.button_no { background-image: url("images/no.gif"); }
fiddle
I want a row of images evenly distributed and justified, after some research I've found this elegant method:
#container {
text-align: justify;
background-color: #FF0000;
}
#container img {
display: inline-block;
}
#container:after {
content: "";
width: 100%;
display: inline-block;
}
<div id="container">
<img src="http://placehold.it/150x100" />
<img src="http://placehold.it/100x150" />
<img src="http://placehold.it/250x50" />
<img src="http://placehold.it/150x150" />
</div>
See the result here: http://codepen.io/naio/pen/vbgrm
Why that little margin on the right?
How can I get rid of the empty space added below the images?
You can remove the bottom whitespace using the following adjustments to your CSS:
#container {
text-align: justify;
background-color: #FF0000;
line-height: 0;
}
#container img {
display: inline-block;
}
#container:after {
content: "";
width: 100%;
display: inline-block;
vertical-align: bottom;
}
The generated content from the pseudo-elememt (empty string) creates an inline box that has a height equal to the line height of the containing block (#container in this example).
By setting line-height: 0, this forces any inline boxes to shrink to zero height.
Footnote
In Chrome (and similar webkit browsers), you will see some extra space to the right of the right-most element on the line. This extra space is not seen in Firefox (where I tested the code).
The extra space is the result of the white space in the original HTML mark-up. The right-most element has a white space character (CR/LF) before the closing </div> tag and this generated content is placed after the white-space, which shows up in some browsers.
You can get rid of it by modifying the HTML as follows:
<div id="container">
<img src="http://placehold.it/150x100" />
<img src="http://placehold.it/100x150" />
<img src="http://placehold.it/250x50" />
<img src="http://placehold.it/150x150" /></div>
that is, keep the closing </div> tag right next to the final img tag.
If you put some text in the #container:after's content property you will notice what it does
#container {
text-align: justify;
background-color: #FF0000;
}
#container img {
display: inline-block;
}
#container:after {
content: "This is some text";
width: 100%;
display: inline-block;
}
http://www.w3schools.com/cssref/sel_after.asp
Check out this link for more details. #container:after adds content after everything else in the container is loaded.
One way to solve your problem is to set the container's height to 150px, but that's not a very flexible solution. If I were you I wouldn't use these styles. When you create a div and not set it's width and height it resizes automatically to fit it's children. Somehow the #container:after is blocking this feature. Just use something else :)
another approach that doesn't have this whitespace & padding issue would be:
http://jsfiddle.net/eNKhy/3/
#container {
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
}
.stretch {
width: 100%;
display: inline-block;
font-size: 0;
vertical-align: bottom;
}
#container, #container img, .stretch{
line-height:0;
}
and add <span class="stretch"></span> in the end of your div
I'm looking for a way to devide my screen perfectly into two divs.
One small fixed sized on the left and one with dynamic width on the right.
I didn't figured out how to do this yet.
Because the width in percentage is not proportional.
For example:
http://jsfiddle.net/acmnU/2/
If you resize the result field or the overall width you see that the green
div will not resize in proportion with the screen.
If the field gets to small the green div slips under the red one.
what I need is some kind of anchor. So that the green div fill the entire screen without
getting to big.
HTML:
<body>
<div id="content">
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
CSS:
body {
height: 300px;
}
#content {
height: 100%;
}
.left {
float: left;
margin-left: 10px;
background-color: red;
height: 100%;
width: 200px;
}
.right {
float: left;
margin-left: 10px;
background-color: green;
height: 100%;
width: 80%;
}
I hope I have interpreted your question correctly. You can try this fiddle
body {
height: 300px;
}
#content {
height: 100%;
}
.left {
float: left;
margin-left: 10px;
background-color: red;
height: 100%;
width: 200px;
}
.right {
margin-left: 200px;
background-color: green;
height: 100%;
}
I have set the margin-left of the .right to equal that of the width of .left. But don't float the right panel and it will fill the remaining space.
I advise using a layout framework to ease this type of think. Bootstrap is a good one but there are lots of others.
If you want to do it manually, you need to give the Content class a width, and use relative positioning.
I'm working on a chat module for a project, everything is working but the css. I have a global container for the chat elements, this div has fixed position. Inside I have two divs, one for the chat windows and one for the contacts list, both the chat window and the contact list are floating to the right and can be "minimized" by clicking on the title (this hides the body and only leaves the title visible). The problem is if I minimize just one of the divs it remains on the top at the same height as the other div (see the image).
This is what I'm getting:
This is what I want:
Relevant code:
<body>
<!--boring code-->
<div class="chat_container">
<div class="contactos show">
<div class="titulo">contactos</div>
<div class="container">
<div class="contacto online" id="contacto_3">juan an orozco</div>
</div>
</div>
<div class="chat_wdow_container">
<div class="chat_wdow " id="chat_wdow_3">
<div class="title_area">juan an orozco</div>
<div class="container">
<div class="msg_area"></div>
<input type="text" name="msg">
</div>
</div>
</div>
</div>
</body>
and css
div.chat_container
{
position: fixed;
bottom: 0px;
left: 0px;
right: 0px;
border: 1px dashed gold;
}
div.chat_container > div
{
float: right;
}
div.chat_container div.contactos div.titulo
{
text-align: center;
}
div.chat_container div.contactos
{
min-width: 150px;
background: dimgrey;
padding: 5px;
border-radius: 10px 10px 0 0px;
}
div.chat_container div.contactos div.container
{
display: none;
min-height: 145px;
padding: 10px;
}
div.chat_container div.contactos.show div.container
{
display: block;
}
div.chat_container div.chat_wdow
{
margin: 0 5px;
min-width: 190px;
background: dimgrey;
padding: 5px;
border-radius: 10px 10px 0 0px;
float: left;
}
div.chat_container div.chat_wdow div.title_area
{
text-align: center;
}
div.chat_container div.chat_wdow div.container div.msg_area
{
background-color: white;
height: 120px;
padding: 10px;
}
div.chat_container div.chat_wdow div.container
{
display: none;
}
div.chat_container div.chat_wdow.show div.container
{
display: block;
}
.chat_wdow input[type="text"]
{
width: 186px;
}
To collapse the window I toggle via mootools the class .show. When this class is missing the container area of the windows has display:none and when it gets applied it has display:block.
What I have tried so far:
setting the fixed parent to a height of 0 and overflow visible
seting the inner container to position relative and the child to absolute
using clear and overflow hacks
changing margins to auto values
changing vertical sizes and minimun heights of the inner containers and childs
changing display to inline and inline block
changing chat container to absolute and inner containers to relative
I have been searching for a while on google and SO but I have only found the options that I have already tried, I also looked at facebook's chat css but I can't find anything to help me, so I am looking for new ideas to bring down the collapsed div.
One solution is to use display:inline-block or display:inline instead and then set the vertical-align:bottom.
Ex: http://jsbin.com/uhubeh/1/edit
If you know the widths of both, however, you could also just use absolute positioning.