Im creating a website for a guild I am apart of, and whilst creating this page, Im trying to have a div within a div, the dark div (contentBox) for holding the lighter divs (contentSection), which in turn, hold text. However, when altering padding of the contentSection div, it also moves the contentBox div. Ill provide screen shots so you can see what I mean.
Html is simply:
<div class="contentBox">
<div class="contentSection">
<h2> We have TeamSpeak!</h2>
</div>
</div>
CSS is:
.contentBox{
background-color: #1E1E1E;
border-color: #080808;
border-right-style: solid;
border-left-style: solid;
border-width: 1px;
height: 100vh;
width: 70%;
margin: 0 auto;
}
.contentSection{
background-color: #4B4B4B;
height: 30%;
width: 85%;
margin: 110px auto;
padding-top: 5px;
border-radius: 2px;
padding-left: 15px;
}
What I want it to look like:
What it ends up looking like:
I apologise if im too vague, just ask if you have questions.
Question has been Solved, adding overflow: auto fixed the problem!
Why this is happening
The effect you are seeing is due to collapsing margins. Adding overflow: auto creates a new block formatting context which keeps borders within the edges of the containing block.
Adding padding also prevents margins from collapsing. In your top image, you have padding, so the margin on h2 and the inner div are contained within the outer block. With no padding, the margin of inner elements collapse with the margins from the outer div's.
try this
is it ok?
.contentBox {
background-color: #1E1E1E;
border-color: #080808;
border-right-style: solid;
border-left-style: solid;
border-width: 1px;
height: 100vh;
width: 70%;
margin: 0 auto;
}
.contentSection {
background-color: #4B4B4B;
height: 30%;
width: 85%;
margin: 110px auto;
padding-top: 5px;
border-radius: 2px;
padding-left: 15px;
padding-top: 50px;
}
<div class="contentBox">
<div class="contentSection">
<h2> We have TeamSpeak!</h2>
</div>
</div>
Related
I have an svg img I got from thenounproject.com (don't worry, I have a place on my site where I give credit to the creators of the images) which I have inside a div. I have set the CSS of the div to have overflow: hidden; however the img is stickout out of the bottom, changing the height of the containing div above the div the svg img is contained it.
Here is the photo of the end result so far (the blue overlay is the <img> object being viewed with firebug so you can see how it is sticking out beyond the white div containing it)
the code I have is:
HTML
<div class="dropdown">
<div class="box edit"><img src="../media/gear.svg"/></div>
</div>
CSS
.dropdown .box{
width: 32px;
height: 32px;
padding: 5px 0px;
margin: 0px 4px;
display: inline-block;
background-color: white;
overflow: hidden;
font-size: 1em;
font-weight: bold;
text-align: center;
border: 1px solid transparent;
border-radius: 2px;
cursor: pointer;
}
.box.edit{
float: right;
padding: 0px;
}
I'm trying to get it so that the "blueish" overlay in the photo, which represents the svg img, does not extend beyond the white box
UPDATE
Thank you all for your answers. I though I would update this to narrow down my question now that I have gotten your feedback. I've tried removed float: right; and the other ideas (remove the border: 0px solid transparent;) but, while helpful, they did not solve the problem.
I currently have transform: rotate(90deg); applied to .box.edit so that way at least the overflow is inline with the rest of the .dropdown bar.
I've tried max-height: 100% and width: 100%; height: auto; etc. but that does not solve my problem. I do not need the entire svg in the box, only what you can see in the photo above (the gear). The part below that has copyright bit from thenounproject.com (see my above statement, I am still following their rules on using photos).
I don't know if I will need to edit the svg file or what, but I was trying to use overflow: hidden; to cut off the end bit (so it does not affect my spacing).
Thank you for your assistance so far.
try this remove border
.dropdown .box{
width: 32px;
height: 32px;
padding: 5px 0px;
margin: 0px 4px;
display: inline-block;
background-color: white;
overflow: hidden;
font-size: 1em;
font-weight: bold;
text-align: center;
/*border: 1px solid transparent;*/
border-radius: 2px;
cursor: pointer;
}
Remove the float from .box and use display: inline-block instead
Hi he is working and now you can define your img css width and height 100% as like this
.dropdown .box > img {
vertical-align: top;
width: 100%;
height: 100%;
}
.dropdown .box{
width: 32px;
height: 32px;
padding: 5px 0px;
margin: 0px 4px;
display: inline-block;
background-color: white;
overflow: hidden;
font-size: 1em;
font-weight: bold;
text-align: center;
border: 1px solid transparent;
border-radius: 2px;
cursor: pointer;
}
.box.edit{
float: right;
padding: 0px;
}
.dropdown .box > img {
vertical-align: top;
width: 100%;
height: 100%;
}
<div class="dropdown">
<div class="box edit"><img src="http://i.stack.imgur.com/8wc74.png"/></div>
</div>
So unless I am reading this wrong the height and width attribuite would work wouldn't it?
Code would be like this and then you would just adjust the height and width according to what you would need..
<div class="dropdown">
<div class="box edit"><img src="../media/gear.svg"/ height="42" width="42"></div>
</div>
As of now your image height is exceeding more than the height of its container due to which it is showing overlay going out of its container. Well applying max-height:100%; to image will make your image to stay within it's parent container, so give it a try.
I have finally found what the problem was with this:
Upon further research, I found that a <svg> has an attribute called "viewBox," which controls how much of the <svg> is shown. The <svg> I was using had a viewBox setting of "0 0 100 125," which basically means the width of the <svg> was 100 and the height 125. Upon finding this, and reducing the height to 100, the <svg> became a proper square and did not stick out further than it's containing div.
Thank you everyone for your answers, a lot of them were good and helpful.
I am having no luck trying to align some text.
I tested with the Overflow Hidden value removed but still couldn't get it looking how I would like.
Here is a codepen page example:
http://codepen.io/anon/pen/oXrPGJ
HTML
<div>
<article>
<section>
<h2 class="colourtitle">
<img class="sideimage" src="http://placehold.it/256x256">
I'm glad I wrap, wish I was vert mid
</h2>
</section>
<article>
</div>
And the CSS
html {
color: #222;
font-size: 1em;
line-height: 1.4;
}
body {
font: 16px/26px Helvetica, Helvetica Neue, Arial;
}
h2 {
padding-top: 1.5em !important;
overflow: hidden !important;
}
.colourtitle {
border-bottom-style: solid;
border-bottom-color: red;
border-bottom-width: 10px;
padding: 5px 0 0 0;
}
.sideimage {
display: inline-block;
margin-right: 0.5em;
max-width: 70px;
padding-top: 10px;
border-right-style: solid;
border-right-color: red;
border-right-width: 10px;
float: left;
}
I have Top Padding on my header tag, because on my actual page there will be many of these stacked, so just creates a nice white space.
I have set the Overflow Hidden, so that when viewed on a mobile screen the text wraps as I wish, and does not sit beneath the image.
h2 {
padding-top: 1.5em !important;
overflow: hidden !important;
}
You can see I'm using a thumb image, which has a right side border to it.
Along with an H2 tag, and that's all using a bottom side border.
But, I would like the text to be a little bit lower, so that it positions in the middle vertically, but still does the wrapping when needed on small screens.
I hope that is clear, and that someone can help me out with a snipped of code required. Thanks
Depending on your browser support requirements, you could look into flexbox.
Looking at the provided link and code, you could get the text to position in the middle vertically by using display: flex and align-items: center.
.colourtitle {
border-bottom-style: solid;
border-bottom-color: red;
border-bottom-width: 10px;
padding: 5px 0 0 0;
display: flex;
align-items:center;
}
I'm using this method to get my footer at the bottom of the page properly.
However, when I add a border to my footer, I end up with a scroll bar regardless of the content on the screen. My confusion is that:
I thought borders functioned outside padding but inside margins, such that like padding they do not effect any layout external to the div.
Is this wrong?
Here is my skeleton html:
<body>
<div class="wrapper">
<div id="top"></div>
<div id="body">
<div id="box1"></div>
<div id="box2"></div>
</div>
<div class="push"></div>
</div>
<div class="footer"></div>
</body>
And here is the relevant CSS:
#top
{
height: 105px;
border-bottom-style: solid;
border-bottom-color: #044E97;
border-bottom-width: 7px;
}
#body
{
margin-top: 25px;
width: 100%;
background-color: white;
color: #282828;
font-size: 85%;
}
#box1
{
width: 460px;
float: left;
margin-left: 25px;
margin-right:75px;
}
#box2
{
margin-left: 25px;
margin-top: 15px;
padding-top: 0%;
padding-bottom:0%;
margin-bottom:45px;
width: 350px;
height: 320px;
float:left;
border-top-style: solid;
border-top-color: #FFFFFF;
border-top-width: 10px;
}
html
{
height: 100%;
}
body
{
min-height: 100%;
background-color: white;
margin: 0;
}
html, body
{
min-height:100%;
position:relative;
}
.wrapper
{
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -3em;
}
.footer, .push
{
height: 3em;
clear: both;
}
.footer
{
width:100%;
background-color: #0563A1;
border-top-style: solid;
border-top-color: #044E91;
border-top-width: 8px;
color: white;
font-size: 77%;
padding-top:.3em;
padding-bottom:.3em;
}
If I change the footer div to not have padding, the scroll bar clears.
This assumption is incorrect:
I thought borders functioned outside padding but inside margins, such that like margins they do not effect any layout
Margins and borders do affect layout—it is just that they are positioned outside the padding. The hierarchy of spacing starts from explicitly defined dimensions (width and height), followed by paddings, then borders, then margins.
If borders and margins did not affect layout, it would then be impossible to create spacing between elements (no margins) or that borders of adjacent elements will overlap (borders taking up no additional space).
The issue you are facing is that borders are computed not as part of the width or height—when you leave a 3em space at the bottom of your body, the footer that is 3em high will fill the space. But when you add borders and/or padding to it, it will add an additional vertical height (sum of top padding of 8px, and top and bottom borders of 0.3em each) to the defined height, causing it to exceed 3em and hence trigger an overflow.
To force your footer to stick to 3em, you can either use box-sizing: border-box to force the height attribute to take into account border widths and padding, or height: calc(3em - 0.6em - 8px) to manually reduce the height of the footer so the sum of height, top padding and top+bottom border widths remains at 3em total.
Change your box-model to border-box, like this:
html{box-sizing: border-box;}
Let me know if it helps.
I'm on Win XP but this problem occurs on Firefox, IE, and Google Chrome. I want to align two DIVs on the same horizontal plane. I want the div on the left to occupy 24% of the screen and the div on the right to occupy the rest. Everything looks fine when my browser is maximized, but when I click the resize button to make the window smaller, the two DIVs are no longer on the same plane. The top of the right DIV appears beneath the bottom edge of the left DIV (the left boundary of the right DIV is still correctly aligned just after the right boundary of the left div).
How can I make both DIVs appear on the same horizontal plane, even when the window is not maximized? Here is the HTML I'm using ...
<div class="header">
<img src="logo.gif"/>
</div>
<div class="container">
<div id="contents">
<div class="categoryPanel"></div>
<div class="productDetailsPanel"></div>
</div>
</div>
and here is the CSS ...
.header {
width: 100%;
height: 63px;
background-color: #333366;
}
.container {
width: 100%;
}
.categoryPanel {
height: 600px;
width: 24%;
float: left;
margin: 10px 5px 0px 0px;
background-color: green;
}
.productDetailsPanel {
height: 600px;
border-color: #BBBBBB;
border-style: solid;
border-width: 1px;
float: right;
margin: 10px 10px 0 5px;
}
Thanks, - Dave
One way to kind of achieve the layout you want is to stop floating .productDetailsPanel, and give it a left margin of 25% (i.e. the width of .categoryPanel, plus spacing between them). (You’ll also want to remove the top margin on .categoryPanel.)
http://jsfiddle.net/uSbAs/
But that does mean the spacing between your columns will be defined as a percentage of the .container, rather than a fixed number of pixels.
On .productDetailsPanel, remove float: right and add overflow: hidden, job done; it's exactly what you asked for.
http://jsfiddle.net/thirtydot/KjZ8Q/
The reason overflow: hidden helps in this case is not obvious, read this.
In order for it to take up the entire space you would need to either fill it with something or provide a width. I've create a jsfiddle to show the results. Essentially I modified the .productsDetailsPanel by adding a width: 75%; removing the float:right; and modifying your margin: 10px 0 0 0;
Here is the new css for .productsDetailsPanel
.productDetailsPanel {
height: 600px;
width: 75%;
border-color: #BBBBBB;
border-style: solid;
border-width: 1px;
margin: 10px 0px 0 0px;
float: left;
background-color: red;
}
http://jsfiddle.net/rhoenig/qXvag/
This could work (float: left):
.productDetailsPanel {
height: 600px;
border-color: #BBBBBB;
border-style: solid;
border-width: 1px;
float: left;
margin: 10px 10px 0 5px;
}
SO designing a website, I ran into a particular problem.
I want two columns of text next to each other. So I floated (left) the first set of text left, and set a margin of 50% to keep it to one side. But when I did the next column of text, and floated it right (I tried left too), but it appears under the former column of text. I also tried using position:relative and moving it, but it refused to move.
I am guessing this is becuase elements cannot be placed within another elements margin.
Please help, with no Javascript. It is evil.
html
<h1 id="firstcolumn">blahblahblahblahblahblah</h1>
<h2 id="secondthingy">foobarfoobarfoobar</h2>
css
#firstcolumn {
float: left;
margin: 20;
margin-left: 15px;
margin-right: 50%;
border-right-style: solid;
border-right-color: black;
border-right-width: 3px;
font-size: 200%;
}
#secondthingy {
float: right;
}
The following should get you two columns of left aligned text:
#firstcolumn {
float: left;
margin: 20;
max-width: 50%;
margin-left: 15px;
border-right-style: solid;
border-right-color: black;
border-right-width: 3px;
font-size: 200%;
}
#secondthingy {
float: right;
text-align:left;
width:45%;
}
use a <div class="wrapper"></div> and fix a width to it.
then put two <div class="column"></div> into that that are float: left and have those two columns set to 50% of the wrapper.
then put your text into each of the columns.
I believe this is what you were attempting to do. if not, please elaborate.
Absolutely.
#secondthingy {
left:50%;
position:absolute;
}
You can remove the 2 extra margin values on #firstcolumn to get the desired result:
#firstcolumn {
float: left;
margin-left: 15px;
border-right-style: solid;
border-right-color: black;
border-right-width: 3px;
font-size: 200%;
}
#secondthingy {
float: right;
}
I think the margining was taking up too much space and causing the h2 to be pushed down a line.