简体   繁体   English

在div中添加填充,背景图片和颜色消失

[英]Adding padding within a div and the background image and color go away

First off let me say how much of a big help this website has been with my little project. 首先,我要说一下这个网站对我的小项目有多大帮助。 Anyway on to my question. 无论如何我的问题。

So right now I'm creating a personal website for myself and I want to add some padding to something, but when I add padding, it ends up creating space between some stuff and making it completely white without using the background image or color. 因此,现在我正在为自己创建一个个人网站,我想在某些内容上添加一些填充,但是当我添加填充时,它最终会在某些内容之间创建空间并使其完全变为白色,而无需使用背景图片或颜色。 This is what I mean. 这就是我的意思。

When I open up the "About" tab (I added padding to that one but not the one above), do you see how there's a lot of white space? 当我打开“关于”标签时(我在上面的那个中添加了填充,但没有添加上面的填充),您是否看到空白空间很大? How can I have that colored in grey and use the background image aswell? 如何将其设置为灰色并同时使用背景图像?

And when I close the tab, there is a big white gap, which is (I'm pretty sure) the padding not closing 当我关闭标签时,有一个很大的空白,这是(我很确定)填充没有关闭

I only added padding to the "About" tab and as you can see, it is messed up compared to the others (This is the jsfiddle) 我仅将填充添加到“关于”标签中,如您所见,与其他填充填充相比,它是一团糟(这是jsfiddle)

.small2{
font-family: 'Ubuntu', serif;
font-size: 30px;
padding: 15px;
margin: auto;
}

This is the css code for the class holding the info. 这是保存信息的类的CSS代码。

Take the padding off .two , and if you want the white border around the content in .four , you can use a margin: 取消.two的填充,如果您想要.four内容周围的白色边框,则可以使用边距:

 $(document).ready(function(event) { $('.one').click(function() { $('.two').slideToggle(); }) $('.three').click(function() { $('.four').slideToggle(); }) $('.five').click(function() { $('.six').slideToggle(); }) $('.seven').click(function() { $('.eight').slideToggle(); }) setTimeout(function() { $('.two').slideToggle(); }, 500); }); 
 body { margin: 0; } .container { overflow: hidden; } .one { position: relative; top: 0; background-color: #605F5F; z-index: 1; cursor: pointer; text-align: center; background-image: url("noise.png"); color: white; } .two { background-color: #333333; display: none; text-align: center; color: white; background-image: url("noise.png"); } .three { position: relative; top: 0; background-color: #605F5F; z-index: 1; cursor: pointer; text-align: center; background-image: url("noise.png"); color: white; } .four { background-color: #333333; display: none; text-align: center; background-image: url("noise.png"); color: white; margin: 15px; } .five { position: relative; top: 0; background-color: #605F5F; z-index: 1; cursor: pointer; text-align: center; background-image: url("noise.png"); color: white; } .six { background-color: #333333; display: none; text-align: center; background-image: url("noise.png"); color: white; } .seven { position: relative; top: 0; background-color: #605F5F; z-index: 1; cursor: pointer; text-align: center; background-image: url("noise.png"); color: white; } .eight { background-color: #333333; display: none; text-align: center; background-image: url("noise.png"); color: white; } .botbar { background-color: #2b2a2a; text-align: center; color: white; background-image: url("noise.png"); } .big1 { font-family: 'Megrim', serif; font-size: 210px; } .small1 { font-family: 'Ubuntu', serif; font-size: 80px; } .small2 { font-family: 'Ubuntu', serif; font-size: 30px; background-clip: initial; margin: auto; } .botinfo { font-family: 'Ubuntu', serif; font-size: 25px; } 
 <div class="container"> <div class="big1"> <div class="one">Main</div> </div> <div class="small1"> <div class="two">Welcome to my page! <br>Click around <br>Learn a thing or two about me</div> </div> <div class="big1"> <div class="three">About</div> </div> <div class="small2"> <div class="four">My name is Bob and I currently attend University of Bob. <br>If a passionate student that is always trying to learn new and exciting things and broaden their knowledge in the field of programming is someone you need, then I'm your guy. <br>I have worked on projects ranging from this website that was created by yours truly, a game of solitaire with my own personal twist, and much more. <br>All my projects can be found in the tab below!</div> </div> <div class="big1"> <div class="five">Projects</div> </div> <div class="small1"> <div class="six">My projects can be found here</div> </div> <div class="big1"> <div class="seven">Contact</div> </div> <div class="small1"> <div class="eight">You can contact me here</div> </div> <div class="botinfo"> <div class="botbar">Made by Bob | Copyright 2016</div> </div> </div> 

尝试将填充移至.four类,我认为它将为您提供所需的结果。

You could do some restructuring of the code to simplify it like 您可以对代码进行一些重组以简化它,例如

 $(document).ready(function(event) { $('.header').click(function() { $(this).next('.content').slideToggle(); }) setTimeout(function() { $('.content').first().slideToggle(); }, 500); }); 
 body { margin: 0; } .container { overflow: hidden; } .header { position: relative; top: 0; background-color: #605F5F; z-index: 1; cursor: pointer; text-align: center; background-image: url("noise.png"); color: white; } .content { background-color: #333333; display: none; text-align: center; color: white; background-image: url("noise.png"); } .botbar { background-color: #2b2a2a; text-align: center; color: white; background-image: url("noise.png"); } .big1 { font-family: 'Megrim', serif; font-size: 210px; } .small1 { font-family: 'Ubuntu', serif; font-size: 80px; } .small2 { font-family: 'Ubuntu', serif; font-size: 30px; background-clip: initial; padding: 15px; margin: auto; } .botinfo { font-family: 'Ubuntu', serif; font-size: 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="big1 header"> Main </div> <div class="small1 content"> Welcome to my page! <br>Click around <br>Learn a thing or two about me </div> <div class="big1 header"> About </div> <div class="small2 content"> My name is Bob and I currently attend University of Bob. <br>If a passionate student that is always trying to learn new and exciting things and broaden their knowledge in the field of programming is someone you need, then I'm your guy. <br>I have worked on projects ranging from this website that was created by yours truly, a game of solitaire with my own personal twist, and much more. <br>All my projects can be found in the tab below! </div> <div class="big1 header"> Projects </div> <div class="small1 content"> My projects can be found here </div> <div class="big1 header"> Contact </div> <div class="small1 content"> You can contact me here </div> <div class="botinfo"> <div class="botbar">Made by Bob | Copyright 2016</div> </div> </div> 

Use display : none; 使用display : none; for .small2 in css. 对于CSS中的.small2 Even though it is hidden it is taking space on UI. 即使它是隐藏的,它也在UI上占用了空间。 (if you need padding when visible) (如果您需要在可见时进行填充)

.small2{
font-family: 'Ubuntu', serif;
font-size: 30px;
background-clip: initial;
padding: 15px;
margin: auto;
display : none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM