How to fix max-width and float:left interaction - html

http://jsfiddle.net/Rncu6/
The green div has a max-width attribute, and it should shrink when the screen shrinks.
Instead, what happens is that the green div falls off to another line. If I try to remove the float:left on the green div, it suddenly overlaps with the yellow div, which is not what I want.
How do I fix this?

This seems like a really frustrating issue. The best way I can think to solve it is to remove float:left from p and replace it with display: table-cell.
p {
display: table-cell; /* replaces float:left */
max-width: 300px;
background-color: lightgreen;
height: 200px;
}
The only problem with this approach is that it will render all the margin attributes useless. To work around that, you can just add the inverse of those margin attributes to #img1. For example:
p { margin-left: 10px; }
Would be replaced with:
#img1 { margin-right: 10px; }
JS Fiddle Example
Caveat: I don't know how small you want your minimum width to become, but you'll notice that at a certain point the p will still move onto the next line. This is because it is becoming too small for individual words (e.g. longer words like "paragraph") to fit on one line. To work around that, you can use the word-break:break-all; attribute.
p { word-break: break-all }
That way, the width of p will continue to shrink until the width can no longer fit individual characters on one line.
JS Fiddle Example

Give width in percentages
#img1 {
background-color: yellow;
width: 20%;
height: 100px;
float: left;
}
p {
float:left;
margin-top: 0;
max-width: 50%;
background-color: lightgreen;
margin-left: 10px;
height: 200px;
}
http://jsfiddle.net/Rncu6/11/

The overlapping occurs because the size of the DOM is becomes larger than the browser so it gets pushed below the img div. As already mentioned you can use % to compensate for that. Although, if you want to absolutely define the divs in pixels until the browser can't display them any more.
To expand upon the current answer you could use Media queries...
#img1 {
background-color: yellow;
width: 100px;
height: 100px;
float: left;
}
p {
margin-top: 0;
float: left;
max-width: 300px;
background-color: lightgreen;
margin-left: 10px;
height: 200px;
}
p:after {
content: " ";
display: block;
height: 0;
clear: both;
}
#media screen and (max-width: 450px) {
#img1 {
width: 20%;
}
p {
max-width: 50%;
}
}
And here's the jsfiddle - http://jsfiddle.net/SxLCJ/

Related

HTML/CSS - Phantom Margin which I can not find

I'm creating a menu bar on my website. My issue is that there is a small margin at the side of one of my menu items. (I have highlighted this by adding background-color: black; to the container.) I am using safari.
The CSS:
.testMenuOption{
width: calc(100% /3);
height: 100%;
float: left;
margin:auto;
background-color: white;
display: table;
}
Can somebody tell me what my issue is? I have tried removing the text and it is not the issue.
Since you calculate the width by using 100/3, there will be rounding errors, where as a result the widths wont add up 100% again. What you can do to fix it is to set the width of two of the .menuOptionsWraps to 33% and one to 34%.
For example by doing so:
.menuOptionSelectedWrap {
float: left;
width: 33%;
height: 100%;
margin: auto;
margin-right: -4px;
background-color: #d6eef2;
display: table;
}
.menuOptionSelectedWrap:last-of-type {
width: 34%;
}
I'm not sure what you say,that black line change when window resize.check your css, width: calc(100% /3);
change the value 3,you will get idea.

Float: None not working with Media Query

I am working on a responsive website, and am having problems with Media Queries.
I have 2 div's, side by side set at 50% width, with float: left and float: right assigned to them respectively. On resize, I wish to remove these floats and display the divs width at 100%.
This is not working however.
Here's my code;
#media only screen and (max-width: 700px) {
#block-half-left, #block-half-right {
float: none;
width: 100%;
}
}
#block-full {
width: 100%;
height: 200px;
background-color: #fff;
margin-right: auto;
margin-left: auto;
}
#block-half-left {
width: 50%;
height: 200px;
background-color: gray;
float: left;
}
#block-half-right {
width: 50%;
height: 200px;
background-color: green;
float: right;
}
The problem is not in the code itself, but its order of appearance.
First of all: You should avoid using !important to fix problems like this, since it is usually just a matter of being more specific on where the styling points.
In this case, however your Media Query is stated first, and after that you're stating the "other" part. CSS (like most languages) is read from top to bottom. If you just move your Media Query to the bottom of your CSS, your problem should be solved.
Specificity is important in CSS.
The simplest way to increase the specificity of your non-floating blocks are to move the rules to the bottom.
E.g:
#block-half-left {
width: 50%;
height: 200px;
background-color: gray;
float: left;
}
#block-half-right {
width: 50%;
height: 200px;
background-color: green;
float: right;
}
#media only screen and (max-width: 700px) {
#block-half-left, #block-half-right {
float: none;
width: 100%;
}
}
More on specificity: https://developer.mozilla.org/en/docs/Web/CSS/Specificity
Furthermore, you'll have a much easier time dealing with specificity in CSS if you use classes, rather than IDs. I.e. .block rather than #block.

Css float... when screen shrunk it comes out of div

#content_2 {position:
absolute;
top: 13%;
left: -10px;
width: 100%;
height: 100%;
z-index: 1;}
#content_image_2 {float: left;
top: 13%;
background-color: #ffffff;
background-repeat: no-repeat;
width: 70%;
max-width: 70%;
height: 30%;
max-height: 30%;
border-radius: 10px;
white-space: nowrap;}
#content_image_2 img{float: right;
min-height: 40%;
max-height: 40%;
width: auto;}
So i have these images inside a div. They are set to float right.
When i shrink the screen they wrap and readjust their position inside the div. Prefect! that is what i want, but when it gets too small the float images pop out of the div. I want to just not show up if the div is too small, but when i use overflow: hidden, the pages goes crazy and the floats don't work at all.
Does anyone know a way around this? or maybe another way to make this happen?
If you want to hide the images if the div is too small you can use a media query.
#media screen and (max-width: 320px) {
#content_image_2 img{
display: none;
}
}
Let's suppose your div has an id, let's say, it is my-div. Whenever it is small, you want to hide the images inside it:
#my-div.small img {
display: none;
}
Now, let's see how can we add/remove the small class:
function addClass(id, class) {
var element = document.getElementById(id);
if (!!element) {
//prevent duplication of class name
removeClass(id, class);
element.className += " " + class;
}
}
function removeClass(id, class) {
var element = document.getElementById(id);
if (!!element) {
document.getElementById(id).className = document.getElementById(id).className.replace(/\bMyClass\b/,'');
}
}
At the appropriate places check the width and height of the div and if it becomes small, add the small class, otherwise, remove it.
Well I just turned the table into another div, and all the sizing / overflow css works. I knew I shouldn't have used a table :/

DIV gets pulled down even overflow is hidden

I have a header, it has two parts, left - the big breadcrumb, right - control buttons. Problem - when breadcrumb gets too long, right part drops down, but i want to hide breadcrumbs, not all, but the part that covers buttons. Below is image with short breadcrumb
Currently parent div is
width: auto;
text-align: left;
margin-left: 61px;
Breadcrumb is
display: inline-block;
text-align: left;
width: auto;
max-width: 60%;
overflow: hidden;
white-space: nowrap;
And the right button part is
z-index: 99999;
float: right;
display: inline-block;
I don't know why right part gets pulled down, i want just hide breadcrumb, cannot resolve it in chrome dev tool either.
This is what i want,
Maybe there is a little trick out there, noticed many variations of css display, any ideas?
Crappy demo: http://jsfiddle.net/a796joeq/
I suggest this for the "right button part":
z-index: 99999;
position: absolute;
right: 0px;
top: 0px;
display: inline-block;
Try to follow the proper concept so that you can deliver quality output.
You can use float concept to achieve this. For a better understanding, you can use widths initially.
For parent div use: 100%; For child divs use: 50% , 50% (total can be max of 100%)
Here is a fiddle (http://jsfiddle.net/kiranvarthi/ybt5tc8b/3/) of the below:
.parent { width: 100%; background: green; overflow: hidden; }
.child1 { width: 30%; float: left; color: #fff; }
.child2 { width: 30%; float: left; color: #fff; }
HTML
<div class="parent">
<div class="child">
Child 1 content comes here.
</div>
<div class="child">
Child 2 content comes here.
</div>
</div>
the problem is you margin-left on the parent div. Change it to a percentage
Give Positions for your div's :
Parant Div :
position:relative;
Breadcrumb :
position:absolute;
Add media queries:
#media screen and (max-width: 800px) {
.breadcrumb {
max-width: 50%;
}
}
#media screen and (max-width: 750px) {
.breadcrumb {
max-width: 40%;
}
}

css weird margin

Does anyone know why in this example
the inner <div> displayed vertically instead of horizontally?
I use chrome element inspect can see a huge margin area on the right of each <div>, but without any margin value.
Found this example
could be the answer to this question, too. Just in case anyone needs it.
It happens so because you are using block layout for each inner div (display:block as default for div elements). Instead you should use (for example ) display:inline-block and reduce width (to 33% in my example) as it adds some extra pixels as spacing between three inner divs :
.cols_section > div{
display:inline-block;
}
Example
Another option is to use table layout, so you don't have to change width for each inner div:
.cols_section {
display:table;
width:100%;
}
.cols_section > div{
display:table-cell;
}
Example
Set width for each class to auto. That should solve your problem..
Example:
.cols_section {
max-width: 1045px;
height: 100px;
}
.col1 {
background: green;
width: auto;
height: 100px;
}
.col2 {
background: yellow;
width: auto;
height: 100px;
}
.col3 {
background: blue;
width: auto;
height: 100px;
}