The text is deforming my box.
here is what I have:
<div class="BigOne" ></div>
<div class="leftOne" ></div>
<div class="rightOne">This text is deforming the "leftOne"</div>
</div>
And the CSS:
.leftOne {
float: left;
width: 100%;
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
.rightOne {
float: left;
width: 100%;
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
How can I precent the text of right div for example, from deforming left div?
Thank you
What I understand from your question and explanation, you want the width of leftOne and rightOne to always be the same size regardless of BigOne's width. Then you'd want to change the width to 50% instead of 100% like so:
.leftOne, .rightOne {
float: left;
width: 50%; /* Change this to 50% instead of 100% */
height: 100%;
border: 3px dashed #444;
border-radius: 7px;
box-sizing: border-box;
}
Take note that you can refactor the CSS because both classes have the same styling. If you want to apply different styling to each of them, you can separate them back.
I also noticed that you have set the height of both of them to 100%. If you want them to maintain the same height, you need to specify the height of BigOne class. Eg:
.bigOne {
width: 100%; /*or whatever width you want to set it to */
height: 100px; /*or whatever height you want to set it to */
}
Another thing, you have an extra </div> on the first line. Remove it and your code should work.
<div class="bigOne">
<div class="leftOne"></div>
<div class="rightOne">This text is deforming the "leftOne"</div>
</div>
Hope that helps.
Related
Whenever I add margin to any element I get overflow, I tried adding box-sizing, position:relative. but nothing works
searched on google but nothing seems to help me
can anyone know why is this happening?
Sample Image
The margin is outside the element. One way to deal with it is to use calc on width as in the following snippet.
And note that margin is diferent from padding: paddingis inside the border (so it is included in the area covered by the background color), margin is outside:
.x {
box-sizing: border-box;
margin: 30px;
width: calc(100% - 60px);
background: yellow;
border: 5px solid red;
}
<div class="x">margin....</div>
With padding instead of margin, this would be:
.x {
box-sizing: border-box;
padding: 30px;
width: 100%;
background: yellow;
border: 5px solid red;
}
<div class="x">Padding....</div>
You can't add margin to a div that is a sibling of your container or else it'll create an overflow. Use padding instead. See how the text in the margin example shifts the text.
.parent {
width: 100px;
height: 100px;
background: red;
}
.padding-example {
padding: 10px;
}
.margin-example {
margin: 10px;
}
<div class="parent">
<div class="padding-example">Correct</div>
</div>
<hr>
<div class="parent">
<div class="margin-example">Wrong</div>
</div>
I'm trying to create something like the following:
Inside of a container, I want to have a fixed sized header, and then a scrollable div below it that fills all of the remaining space. I seem to be going about this wrong, as when I try setting the scrollable div's height to 100%, it pops out of the containers body.
Here's what i've come up with so far:
HTML
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1
</div>
</div>
CSS
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
}
.scrollable-bottom {
height: 100%;
overflow-y: scroll;
background-color: rgba(255, 255, 247, 0.4);
}
If you checkout the fiddle here, you'll see that the scrollable-bottom shoots outside of the container. I can't figure out why this is..
Doesn't the height property refer to the parent elements height? If so, then why isn't it being constrained?
height: 100%; means 100% of the elements parent height, so if parent height is 400px, child height is going to be 400px as well.
You can get away from calculating sizes of your .scrollable-bottom on your own with use of calc() function. It's supported in modern browsers
You can also get rid of magic numbers by changing box model behavior of .top-part and .scrollable-bottom, setting box-sizing: border-box; for .top-part and .scrollable-bottom
It will change its behavior to contain paddings and borders inside the defined width/height of the box area instead of enlarging it by the padding and border width/height
This way, you no longer need to worry about changing .scrollable-bottom height/width whenever you add or remove padding or border to it or the .top-part
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part,
.scrollable-bottom {
box-sizing: border-box;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
}
.scrollable-bottom {
height: calc(100% - 70px); /* parent height - .top-part height */
overflow-y: scroll;
background-color: rgba(255, 255, 247, 0.4);
}
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1
</div>
</div>
You're absolutely correct. The 100% height refers to the full height of the container. But also note there is the top bar height that should be calculated here.
Giving it a negative top margin the same amount of the top bar should fix it, but it will hide a partial amount of the bottom content itself, that's why we add a top padding with the same amount of the height.
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #cacaca;
}
.top-part {
width: 100%;
height: 70px;
border: 1px solid #cacaca;
position: relative;
z-index: 2;
background: white;
}
.scrollable-bottom {
height: 100%;
overflow-y: scroll;
padding-top: 70px;
margin-top: -70px;
background-color: rgba(255, 255, 247, 0.4);
}
<div class="container">
<div class="top-part">
Hello!
</div>
<div class="scrollable-bottom">
<h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1><h1>Content</h1>
</div>
</div>
As your containers height is 400px and 70px height is covered by the header.
Just give max-height:330px to scrollable div. It will solve your problem. Here is the result http://jsfiddle.net/o2307qup/2/
There maybe some paddings, margins and borders that you might be playing with. So change the max-height according to them, if you are not seeing the perfect result. Also read the comment below of #Marc
I am currently making a website without using framework however I have run into a problem. My divs are not getting centered within the container even though the container itself is centered in the body.
Html
<body>
<div id="content">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
Css
#content{
width: 90%;
height: 100%;
margin: 0 auto;
}
.box{
width: 400px;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
The divs are perfectly centered when I have my window to full width, but once I resize it, they just reorganize without centering.
Before resizing:
http://cl.ly/image/241R2I24280w/Screen%20Shot%202014-09-26%20at%2021.49.23.png
After resizing the window: http://cl.ly/image/2y2g2W0n230g/Screen%20Shot%202014-09-26%20at%2021.50.21.png
I have tried different methods to solve it, such as doing margin: 0 -10%; and margin: 0 25%;
When it comes to positioning I get confused.
Thanks.
Just change your CSS like this, this way you can adapt your boxes in many ways and they will react to responsive layouts as expected:
#content {
width: 90%;
height: 100%;
margin: 0 auto;
text-align:center;
display:block;
}
.box {
width: 45%;
height: 150px;
display:inline-block;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px 2%;
}
See fiddle here
Explanation:
I have removed your floats, used block elements and replaced your fixed sizes by percentages. Then, I used a text-align:center property in your container box #content so everything is nicely aligned in the center of that container. Now, if you resize, columns will take 45% of the width of the screen, but you can obviously change the behavior via media queries and use something like .box{display:box} for small screens
There are multiple solutions to your problem. Depending on what you have inside those boxes this might be the simplest one: text-align:centerwith a display:inline-block combo; See here.Fiddle
2 solutions :
You can use a percentage for the width your boxes.
#content{
width: 90%;
height: 100%;
margin: 0 10%;
}
.box{
width: 30%;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
Boxes will resize with the content but the stuff in the boxes might look weird in small sizes.
Or
You can use a pixel value for the width of your content.
#content{
width: 1200px;
height: 100%;
margin: 0 10%;
}
.box{
width: 400px;
height: 150px;
float: left;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin: 13px;
}
Width of boxes will not change while resizing, nor the stuff in it, but that can be painful on small screens.
add auto margin for your box
.box{
width: 400px;
height: 150px;
border: 1px solid #c8c8c8;
border-radius: 3px;
margin-top: 13px;
margin-left:auto;
margin-right:auto;
}
I made your files on my machine and the divs are not centered so I assume your screen or resolution settings are different, or your content container is within one or more other divs?
Anyhow, try adding 'clear:left;' in your box class code and it should resolve your issue (put it just above the 'float:left' line. good luck!
I want to achieve the following effect: http://jsfiddle.net/3KJta/1/
However the solution I have uses a known width for the small div and the larger div. I need this to work with variable sized divs. The use case for this is a tooltip that appears above a smaller flexible sized element. The tooltip content isn't known and so the width could be anything.
So far I have:
<div class="small">
<div class="smaller"></div>
<div class="larger"></div>
</div>
and
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
div {
border: 2px solid black;
}
.small {
width: 50px;
height: 50px;
position: absolute;
left: 200px;
top: 50px;
text-align: center;
}
.smaller {
width: 20px;
height: 20px;
border-color: red;
display: inline-block;
}
.larger {
width: 200px;
height: 200px;
border-color: blue;
display: inline-block;
margin-left: -75px /* NOTE: in reality, .small has a variable width, and so does .larger, so i can't just take off this fixed margin */
}
If you are ok with using css3 and only support modern browsers you can use transform: translateX(-50%); to center the bigger box (currently supported browsers).
See this example: http://jsfiddle.net/2SQ4S/1/
If you use and extra element you can do it:
<div class="small">
<div class="smaller"></div>
<div class="larger">
<div>I'm extra</div>
</div>
</div>
CSS
.larger {
position:relative;
left:50%;
width:8000%;
margin-left:-4000%;
text-align:center;
border:none;
}
.larger div {
width: 200px;
height: 200px;
border-color: blue;
margin:auto;
}
http://jsfiddle.net/3KJta/4/
although that does cause some issues with content being wider than the page so you would need it all in a container with overflow:hidden:
http://jsfiddle.net/3KJta/7/
All a bit ugly though. Perhaps there's a solution where you can avoid doing this. Maybe a JS solution that measures the size of the content you're trying to show and offsets it.
I am sure that this question is already answered, but I find it hard to search for it.
I have the following html:
<div id='outerBox'>
<div id='leftBox'><div id='secondLevelBox'></div></div>
<div id='rightBox'></div>
</div>
and the following css:
#outerBox {
width: 300px;
height: 200px;
border: 1px solid black;
}
#leftBox {
height: 100%;
width: 55%;
background-color: blue;
float: left;
}
#rightBox {
height: 100%;
width: 45%;
background-color: yellow;
float: left;
}
#secondLevelBox {
height: 100%;
width: 100%;
}
(See http://jsfiddle.net/dsMdb/1/)
this displays ok. But if I now add a border: 1px solid red to one of the inner divs, they will grow 2 pixels and the layout will break: http://jsfiddle.net/dsMdb/5/
How can I wrokaround this? (solutions for IE >=8 and current FF are ok)
You can change the way the browser is supposed to calculate the offset for the border & layout.
Take a look at the Box Model properties in CSS3, this way you can define the offset etc.
The command you're looking for in CSS is box-sizing. By default this set to content-box, which adds the width, padding etc as different values on top of each other.
By setting it to border-box, you can force the browser to instead render the box with the specified width and height, and add the border and padding inside the box.
Should apply to your border as well normally.
Problem is that it adds a border on the outside of that inner div. Since your red border is 1px, then it adds total of 2px.
Quick way to fix this is to remove `2px` from the outer `div`s width.
#outerBox {
width: 298px;
height: 200px;
border: 1px solid black;
}
Also, I would like to add, that this fix is very browser compatible ;)
I would suggest to have pixel graduation in the width and accordingly give room for border, like
Since total width is 300 px,
#leftBox {
height: 100%;
width: 165px;
background-color: blue;
float: left;
}
#rightBox {
height: 100%;
width: 145px;
background-color: yellow;
float: left;
}
now reduce the width accordingly and this would work across browsers.