Terrible title, I know.. it's just different than any other "Child expanding outside of parent" problem I've ever had or saw.
Anyway, I have two children with the same structure and same CSS applied to both.
HTML:
<div id='base'>
<div id='banner'>
Link 1
Link 2
Link 3 with longer width
Link 4
Link 5 dif size
Link 6
</div>
<form id='form'>
<div>
<div class='col-6-'>
<p>dd</p>
<!--<p>dsadosih doijasd oouhvodus ouhfoiusdif oiudhaoudd
oinoiuasd onhasoiduf ou oud oijod</p>-->
</div>
<div class='col-6-'>
<p>dsadosih doijasd oouhvodus ouhfoiusdif oiudhaoudd
oinoiuasd onhasoiduf ou oud oijod</p>
<br>
<p>dsadosih doijasd oouhvodus ouhfoiusdif oiudhaoudd
oinoiuasd onhasoiduf ou oud oijod</p>
</div>
</div>
</form>
</div>
CSS:
*
{
margin: 0;
padding: 0;
-ms-box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html, body
{
height: 100%;
color: #0068ac;
}
body {
text-align: center;
background: #689976;
}
.col-6-{width: 48%;}
body
{
background: gray;
}
#base
{
display: inline-block;
padding-top: 5%;
height: 90%;
/* width: 90%; */
}
#form
{
height: 98%;
background: #E6F0F7;
border-radius: 0 0 5px 5px;
}
#banner a
{
text-decoration: none;
outline: none;
display: block;
float: left;
font-size: 12px;
line-height: 36px;
position: relative;
padding: 0 10px 0 60px;
color: #ffe143;
background: #32a252;
height: 2em;
}
.col-6-
{
display: inline-block;
}
#form div div
{
text-align: left;
vertical-align: top;
padding: 1%;
}
#form div p
{
word-break: break-all;
}
I've never tried to share a jsfiddle, but hopefully this works: https://jsfiddle.net/12rLb42a/
Bottom Line
As you can see there are two divs at 48% width of the parent. The second div contains two p's that wrap within the 48% space and don't breach it.
If you uncomment the p element in the first div, you will see that is not the case for that one because it does expand the parent. Why is this happening?
Related
First div element is facing downwards,i've had this kind of problem and could not understand it, i'm just making simple grid layout 24% | 50% | 24% but as you see first div is doing weird thing, if you solve it and explain why this phenomenon happens and am i doing this simple grid correctly please give your opinions,thanks in adnvance.
html {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
border: 0;
font-size: 25px;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.nav-top {
width: 100%;
background-color: #f00000;
}
.row {
width: 100%;
display: inline-block;
color: #fff;
border: 1px solid gray;
}
.row p {
font-size: 90%;
float: left;
}
.nav-top-col-sm {
width: 24%;
display: inline-block;
}
.nav-top-col-sm span {
float: right;
}
.nav-top-col-lg {
width: 50%;
display: inline-block;
background-color: black;
}
<nav class="nav-top">
<div class="row">
<div class="nav-top-col-sm">
<span>En</span>
</div>
<div class="nav-top-col-lg">
<p>FB,Twitter,Google,Wifi,Youtube</p>
<p>market</p>
</div>
<div class="nav-top-col-sm">
<p>My Account</p>
</div>
<!-- Col-->
</div>
<!-- Row -->
</nav>
When you set the display property to inline or inline block, you should set
vertical-align: middle; for them.
Also u dont need to set the width to 24% to get them on one line.
U need to set the font size:0 on the parent, and give individual font sizes to each inline-block.
This happens because, the inline blocks have some white-space, which pushes them on next line, when 25% width is given.
html {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
border: 0;
font-size: 25px;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.nav-top {
width: 100%;
background-color: #f00000;
}
.row {
width: 100%;
display: inline-block;
color: #fff;
border: 1px solid gray;
}
.row p {
font-size: 90%;
float: left;
}
.nav-top-col-sm {
width: 24%;
display: inline-block;
vertical-align: middle;
font-size: 20px;
}
.nav-top-col-sm span {
float: right;
}
.nav-top-col-lg {
width: 50%;
display: inline-block;
background-color: black;
vertical-align: middle;
font-size: 20px;
}
<nav class="nav-top">
<div class="row">
<div class="nav-top-col-sm">
<span>En</span>
</div>
<div class="nav-top-col-lg">
<p>FB,Twitter,Google,Wifi,Youtube</p>
<p>market</p>
</div>
<div class="nav-top-col-sm">
<p>My Account</p>
</div>
<!-- Col-->
</div>
<!-- Row -->
</nav>
**Here, I wanna have header-top and pages-navigation fit perfectly inside header.
**
Here, header is 100px with 20px top padding, header-top is 32px, so pages-navigation should be 48px to fit. But it fits at line-height 58px. Also when I alter the line-height: 58px, it breaks. Anything I'm missing?
body {
margin: 0;
padding: 0;
font-family: "Open sans", sans-serif;
box-sizing: border-box;
}
#container {
height: 100%;
}
header {
padding: 20px 20px 0 20px;
background-color: #f8faee;
position: relative;
height: 100px;
box-sizing: border-box;
border-bottom: 1px solid #eeeeee;
}
.header_top {
height: 32px;
}
.learning-site-name {
float: left;
font-size: 18px;
color: #000;
padding-top: 5px;
line-height: 18px;
}
.pages_navigation {
line-height: 58px;
font-size: 14px;
margin-left: -5px;
clear: both;
box-sizing: border-box;
}
.highlighted,
.not-highlighted {
padding: 0 5px 8px 5px;
}
.highlighted {
border-bottom: 1px solid black;
color: black;
}
.not-highlighted {
cursor: pointer;
color: #999999;
}
.pages_navigation span:nth-child(2) {
display: inline-block;
width: 30px;
}
<body>
<div id="container">
<header>
<div class="header_top">
<div class="learning-site-name">Learning Site Name</div>
</div>
<!-- creating an empty span inline block to make space in between 2 spans -->
<div class="pages_navigation">
<span id="learning-board" class="highlighted">Learning Board</span>
<span></span>
<span id="content-files" class="not-highlighted">Content Files</span>
</div>
</header>
</div>
</body>
It is because of box-sizing: border-box; of your header. When you set this property and set the height 100px; padding-top: 20px;, It means the real height of header is 80px. In another word box-sizing: border-box; make the padding and border count as the height of element.
The better way is to remove the height of header and let its children make it's height.
Why does block with text shift to the bottom? I know how to fix this issue (need to add "overflow: hidden" to the box), but I don't understand why it shift to the bottom, text inside the box is short, margins in browser-inspector are same as margins of example without text.
Example of the problem
HTML:
<div class="with-text">
<div class="box1">
SIMPLE TEXT
</div>
<div class="box2">
</div>
</div>
<div class="without-text">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
CSS:
html, body {
font-size: 10px;
margin: 0;
height: 100%;
}
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* Fix the problem */
/* overflow: hidden; */
color: white;
}
.box2 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: red;
}
.with-text:before {
display: block;
content: "with-text";
text-transform: uppercase;
margin: 1rem;
}
.with-text {
box-sizing: border-box;
height: 50%;
border: 1px solid;
}
.without-text:before {
display: block;
content: "without text";
text-transform: uppercase;
margin: 1rem;
}
.without-text {
box-sizing: border-box;
height: 50%;
border: 2px solid black;
}
The problem is that by default vertical alignment of inline elements – baseline,
The text inside element affects it and pushes div to the bottom.
Use vertical-align: top to solve issue.
You can try to add vertical-align:
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* overflow: hidden; */
color: white;
vertical-align:top;
}
Code:
HTML
<body>
<div class="wrap">
<div class="box">???</div>
</div>
</body>
CSS
.wrap {
background-color: #0000FF;
display: block;
height: 600px;
margin: 0px auto;
padding: 0px;
width: 600px;
}
.box {
border: solid 20px #FF0000;
color: #FFFFFF;
display: block;
height: 100%;
padding: 0px;
margin: 0px;
text-align: center;
width: 100%;
}
JSFiddle: http://jsfiddle.net/5k0ddtdn/4/
I'm expecting the red border to wrap completely around the blue parent div considering this isn't a border-box.
Why doesn't it do that?
Add box-sizing: border-box; to .box.
.box {box-sizing: border-box;}
http://jsfiddle.net/5k0ddtdn/8/
In your code, the inner element has width 600px + 40px border, the parent element (.wrap has 640px in total). You need to change box-model, or set correct size to inner element (width: 560px; height: 560px;). You can remove width for inner element and set just height: 560px;.
http://jsfiddle.net/5k0ddtdn/10/
update your box like so :
.box {
border: solid 20px #FF0000;
color: #FFFFFF;
display: block;
height: 100%;
padding: 0px;
margin: 0px;
text-align: center;
width: 100%;
box-sizing: border-box;
}
Live Demo
I have the following:
HTML:
<div id="about" class="menu1"> About</div>
<div id="aboutsub">
<div id="team" class="menu2"> Team</div>
<div id="experience" class="menu2"> Experience</div>
<div id="difference" class="menu2"> Difference</div>
</div>
CSS:
.menu1
{
position: absolute;
background: red;
width: 60px;
height: 21px;
padding: 15px 20px;
}
.menu1 a
{
display: inline-block;
position: absolute;
color: white;
text-decoration: none;
}
.menu2
{
position: absolute;
background: purple;
width: 80px;
height: 42px;
left: 115px;
}
.menu2 a
{
padding: 15px 20px;
color: white;
text-decoration: none;
}
Fully viewable here:
http://jsfiddle.net/snk42/1/embedded/result/
The problem I am having, is when I check the <a> element inside #about, I see the area start at the top left of the word "about", rather than filling the whole div.
How can I more easily center the text vertically while making the whole div clickable?
Also, I'm wrestling with why the menu2 tags aren't clickable if someone knows.
You need to make the anchor the width and height of the parent div, I also recommend box-sizing this element
.menu1 a {
display: block;
position: absolute;
color: white;
text-decoration: none;
width: 100%; height: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
top: 0; left: 0;
}
.menu2 a {
display: block;
padding: 15px 20px;
color: white;
text-decoration: none;
width: 100%; height: 100%;
-webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;
}
http://jsfiddle.net/sjZe4/1/ <- Fiddle
<a><div></div></a>
Just increasse the height and width of the menu2 and add some padding to the top.
Demo:http://jsfiddle.net/snk42/4/embedded/result