I'm experiencing strange behavior in Firefox (v35 v39 v52) regarding percentage padding. I cannot reproduce this issue in Chrome.
I have an element with top padding set as a percentage, like this:
p {
padding:10% 0 0;
margin:0 0 1em;
background-color:#CCC;
}
Percentage padding on an element is relative to its parent's width. So, I expect that the padding at the top of the element will grow as the window's width is enlarged. This is indeed the result for my simple <p> tag.
However, when that element is floated or has width, the percentage padding does not behave as expected when the window is resized. The padding is calculated correctly upon load. But, as the window is resized, the total height of elements that are floated or have width seems to remain the same. Text in the element is inexplicably placed at the bottom of an area that gets mysterious height. This happens for elements like this:
p {
padding:10% 0 0;
margin:0 0 1em;
background-color:#CCC;
float:left;
}
p {
padding:10% 0 0;
margin:0 0 1em;
background-color:#CCC;
width:150px;
}
Here is an image to illustrate what I'm seeing. Color coding is added by Firebug; purple is padding, yellow is margin, and blue is the content of the element.
What causes this inconsistency? Can anyone else reproduce this issue in Firefox (or any other browser)?
Here's a fiddle to demonstrate. In Firefox, try expanding or contracting the result pane to see the elements resize.
I have not added a runnable code snippet, as I couldn't find an easy way of resizing the snippet area on-the-fly.
I've added a stack snippet to demonstrate the issue. Use the "Full page" button so you can stretch the window's width.
html,body {
margin: 0;
}
div#container {
width: 100%;
}
p {
padding: 10% 0 0;
margin: 0 0 1em;
background-color: #CCC;
}
p.width_limited {
width: 150px;
}
p.floated {
float: left;
}
<div id="container">
<p>NORMAL</p>
<p class="floated">FLOATED</p>
<div style="clear:both;height:0;"></div>
<p class="width_limited">HAS WIDTH</p>
</div>
That's strange. I'm not sure if it is a bug. But, by changing the display to flex seems to solve the problem. http://jsfiddle.net/vsvp71rw/4/
p {
display: -webkit-flex;
display: -moz-flex;
display: flex;
padding:10% 0 0;
margin:0 0 1em;
background-color:#CCC;
}
you may use a pseudo element to avoid the bug and clear those height of 10%'s width you wish.
html,
body {
margin: 0;
}
div#container {
width: 100%;
}
p {
margin: 0 0 1em;
background-color: #CCC;
}
p:before {
content: '';
padding: 5% 0;
display: block;
}
p.width_limited {
width: 150px;
}
p.floated {
float: left;
<div id="container">
<p>NORMAL</p>
<p class="floated">FLOATED</p>
<div style="clear:both;height:0;"></div>
<p class="width_limited">HAS WIDTH</p>
</div>
Related
I have a problem with scrollbar appearing at wrong pixel point on my website. I narrowed problem to simple codepen structure.
html,
body {
margin: 0;
padding: 0;
}
.cont {
margin: 0;
background: #333;
}
.cont__inner {
width: 1100px;
margin: 0 auto;
}
#media screen and (max-width: 1170px) {
.cont__inner {
width: 950px;
}
}
.cont__inner_main {
padding: 0 45px;
}
.why {
height: 115px;
}
<div class="cont">
<div class="cont__inner cont__inner_main ">
<div class="why">adssd</div>
</div>
</div>
The problem comes when i apply height 700px to div.why.
You can see that width of container is 1100px + 45px padding on both sides , so its 1190px total
I've set media screen breakpoint lower just to track when scrollbar appears.
You can test yourself, it comes up at 1206px and no element overlap document width at that moment. Removing heigth 700px solves problem, but what did it even influence width ?
Simply add width: auto in &_main
&_main
padding: 0 $mn * 3
width: auto
I would like to have 4 panels (sections).
The 2 top ones should be along the whole width.
The 2 others should be next to each other.
The top one should have no margins at all.
The 2nd panel should have no top-margin (and no bottom-margin to
avoid a double one with the ones below).
The problem here is: The 2 panels next to each other have a double margin in the middle. In this case it's 10 + 10. I need them to be 5 both in order to get a total of 10px. However, when I apply this to my code then the extra space left over would end up on the right..
Can anyone help with a solution? Rewriting any code to improve (making it easier to maintain) is also welcome :).
EDIT:
I had my container width to 240px for testing the flex i made. This caused the extra space on the right when applying the margin of 5. However, there should be a way to avoid this double margin with any container width. Does anyone have a solution?
EDIT:
Had to set the flex-grow to true. Now the margin of 5 + the grow possibility seems to fix the problem. No matter what container size I have.
body {
background-color: #cccccc;
}
#container {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
background-color: #444444;
width: 270px;
}
#container > * {
width: 100px;
height: 100px;
background-color: #228822;
flex-basis: auto;
}
#container .neighbour {
flex: 1 1 auto;
margin: 10px 0;
}
#container .solo {
flex: 1 1 100%;
}
#container .solo.red {
background-color: #ff0000;
}
#container .solo.small {
height: 30px;
margin: 0;
}
#container .solo.no-top {
margin: 0 10px;
}
#container .left {
margin-left: 10px;
margin-right: 5px;
}
#container .right {
margin-left: 5px;
margin-right: 10px;
}
<div id="container">
<section class="solo red small"></section>
<section class="solo no-top"></section>
<section class="neighbour left"></section>
<section class="neighbour right"></section>
</div>
.no-right{
margin-right:5px;
}
.no-left{
margin-left:5px;
}
Does tagging that to the end of your stylesheet work?
I have been trying to determine the cause of the following issue on a web page.
With the code below, when any browser is resized and the page becomes horizontally scrollable, a white space appears on the right.
How can I remove this white space? Thanks for all help given!
body {
margin: 0;
padding: 0;
}
.wrap {
background-color: #2a2c67;
}
.main {
height: 50px;
text-align: center;
width: 1300px;
margin-right: auto;
margin-left: auto;
color: #fff;
}
<div class="wrap">
<div class="main">Content Goes Here</div>
</div>
the container "wrap" has no width set so, as a block element it will take 100% of the ACTUAL window size. when you horizontal scroll because the child has a fixed width bigger than his parent and the actual window, you will scroll the child, but the parent will remain with whatever window size it's atm and scrolling out of the window. It won't dinamically change his current width (as browsers understand) to fill the child width.
Imo you just have to change the background color to the children to fix it (not that it's broken).
Edited: Or as other people said... set the width to the wrap and not to the "main"
body {
margin: 0;
padding: 0;
}
.wrap {
}
.main {
height: 50px;
text-align: center;
width: 1300px;
margin-right: auto;
margin-left: auto;
color: #fff;
background-color: #2a2c67;
}
<div class="wrap">
<div class="main">Content Goes Here</div>
</div>
I have a parent div (for sake of test we'll call it #parent) and a child div (test reasons #child). #parent is absolutely positioned to the bottom of the page, with a fixed width of 100% and a height of 75px.
child is a div that holds dynamic content (being changed with jQuery). Seeing as it is dynamic, the width of the div is always different. What is the most efficient way to center this div horizontally, since the width is always unknown & different? Any help would be awesome.
The correct way to do this would be the following:
#child {
margin: 0 auto;
}
This sets the top/bottom margins to 0, and then the left/right margins to auto - which means "as large as possible". So you have two equal margins on the left and the right, filling up the space completely, and hence you have a centred div.
This will only work on block elements like divs though - inline elements cannot have auto margins. If you need to centre an inline element (like a span or some text), use text-align: center; on the parent:
#parent {
text-align: center;
}
You could set the margins to: margin: 0, auto;
For fun you could use the CSS Flexible Box Layout Module. Here is a jsFiddle demonstrating what you could do:
See working jsFiddle demo
HTML
<footer>
<div class="dynamic-content">Here is some child dynamic content</div>
</footer>
CSS
body
{
background: #ccc;
}
footer
{
/* As of August 2012, only supported in Chrome 21+ */
display: -webkit-flex;
display: flex;
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: #232323;
}
footer .dynamic-content
{
flex: 1;
padding: 10px;
margin: 0 auto;
background: #545454;
color: white;
font-family: verdana;
}
Centering a div using CSS:
HTML:
<div class="center">
.... more content ....
</div>
CSS:
.center {
margin: 0 auto;
}
OR
.center {
margin-left: auto;
margin-right: auto;
}
The margin: 0 auto; sets the left and right margin to whatever pixel left on the left and right of the page.
Try in jsfiddle
Make it display as an inline element and give the parent the property of text-align center
problem solved
#parent{
text-align:center;
}
#child{
display:inline-block;
}
Edit:
check how it works http://jsfiddle.net/ECMau/1/
This is my CSS code;
#wrap {
width:50em;
max-width: 94%;
margin: 0 auto;
background-color:#fff;
}
#head {
width:50em;
height:10em;
max-width: 100%;
margin: 0 auto;
text-align:center;
position: relative;
}
#css-table {
display: table;
margin: 1em auto;
position: relative;
width:50em;
max-width: 100%;
}
#css-table .col {
display: table-cell;
width: 20em;
padding: 0px;
margin: 0 auto;
}
#css-table .col:nth-child(even) {
background: #fff;
}
#css-table .col:nth-child(odd) {
background: #fff;
border-right: 4px double #b5b5b5;
}
And my HTML code;
<div id="cont">
<div id="css-table">
<div class="col">123</div>
<div class="col">123</div>
</div>
</div>
When I scale the Firefox window, the table scales fine even down to 300px width viewport...just like I want to. But in Chrome, the table looks normal only when the viewport is wider than 50em. If I narrow the Chrome window, the table bleeds out on the right side of the wrap.
Is there a reason why is Chrome doing this?
Technically Chrome is following the rules because max-width should only apply to block elements.
From MSDN docs:
The min-width/max-width attributes apply to floating and absolutely
positioned block and inline-block elements, as well as some intrinsic
controls. They do not apply to non-replaced inline elements, such as
table rows and row/column groups. (A "replaced" element has intrinsic
dimensions, such as an img or textArea.)
The table (or in your case display:table) should technically not work or be supported. FF apparently obeys it fine, but you'll probably need to come up with another solution, either removing the display:table or the max-width.
max-width property
MSDN Doc
The solution I found was using table-layout: fixed and width: 100%
Create a div and give it a styling to display block and a max width. You may use traditional <table> and give it a styling of 100% width.
I was able to use a mixin(SASS) to fix the issue.
#mixin clearfix {
&::after{
content: "";
display: table;
clear: both;
}
}