简体   繁体   English

CSS格式问题 - 网页设计的新手

[英]CSS formatting problems - new to web design

I've followed various online tutorials (codeacademy, some reading, dash), and thought I was finally getting to grips with things. 我已经按照各种在线教程(codeacademy,一些阅读,破折号),并认为我终于掌握了一些东西。

I've made a couple of web pages of different styles, no backend functionality yet I'm talking about just the front end stuff. 我制作了几个不同风格的网页,没有后端功能,但我只是谈论前端的东西。

I keep running into strange problems with regards to positioning things on the page and I'm really not sure where it is I'm going wrong. 关于在页面上放置东西的问题,我一直遇到奇怪的问题,我真的不确定它在哪里,我出错了。 I know you cant answer in general where I'm going wrong as obviously each case will differ. 我知道你一般都不能回答我的错误,因为显然每个案例都会有所不同。

Basically I cant seem to manipulate the menu section in the way I want...I wanted the menu items to all be next to each other, and have a slightly different colour to the background of the ul. 基本上我似乎无法按照我想要的方式操作菜单部分...我希望菜单项全部彼此相邻,并且与ul的背景颜色略有不同。 For some reason the background colour kept moving when I tried to have the li's as a different colour. 出于某种原因,当我试图将li作为不同的颜色时,背景颜色一直在移动。 Now that I have the whole thing as one colour, I cant change the margin of the li's to move them closer to each other. 既然我将整个事物作为一种颜色,我就不能改变它的边缘以使它们彼此靠近。

Getting rather stressed with all this at the moment as it seems like easy things are very difficult so I must be making a major error somewhere. 此刻所有这些都变得相当紧张,因为看似简单的事情非常困难所以我必须在某个地方犯一个重大错误。 Are there any other tutorials anyone can suggest? 有没有其他人可以建议的教程?

But in this specific case, what am I doing wrong? 但在这个具体案例中,我做错了什么? Is it the way I have arranged the HTML? 这是我安排HTML的方式吗? The CSS? CSS? If I'm doing something majorly wrong then please point it out as I don't want to keep practicing the wrong thing. 如果我做了一件非常重要的事情,那么请指出,因为我不想继续做错事。

 .banner .container{ background: #1D2926; width: cover; height: 50px; margin: -10px -30px 0 -20px; } .banner .strip { background: #E3C109; height: 5px; width: cover; margin: 0px -30px 0px -20px; } body { text-align: center; } .logo { font: ; color: #000; background: url(""); margin-top: 150px; margin-bottom: 80px; height: 20px; } h1 { font-size: 60px; font-family: 'Niconne', cursive; } ul { background: rgba(0, 0, 0, 0.1); padding: 2px 0 2px 0; } li { margin-left: 0 -2px 0 -2px; display: inline; font-size: 25px; padding: 0 20px 0 20px; font-family: 'Londrina Solid', cursive; border: 1px solid #d3c5ba; } 
 <div class="banner"> <div class="container"> &nbsp; &nbsp; &nbsp; </div> <div class="strip"> &nbsp; </div> </div> <div class="logo"> <h1>Name of business</h1> </div> <div class="menu"> <div class="container"> <ul> <li><a href="#">Home</a></li> <li><a href="#">Food</a></li> <li><a href="#">Drink</a></li> <li><a href="#">Events</a></li> <li><a href="#">Pennine Trail</a> </li> <li><a href="#">Contact</a></li> </ul> </div> </div> 

Your markup looks fine, one of the best ways of learning HTML/CSS is trial and error. 您的标记看起来很好,学习HTML / CSS的最佳方法之一是反复试验。

The first issue I see on why your margin's on the li are not responding to your changes is because, you have your styles defined incorrectly on the margin-left style: 第一个问题我看到为什么你的李的余量没有响应你的更改是因为,你在左边缘样式上错误地定义了你的样式:

margin-left: 0 -2px 0 -2px;

It should be either: 它应该是:

margin-left: -2px;
margin-right: -2px;

or 要么

margin: 0 -2px 0 -2px;

I have made some css changes to achieve whatever you had mentioned in your question. 我做了一些css更改,以实现你在问题中提到的任何内容。 Check once if you wanted to have this. 如果你想要这个,请检查一次。

.banner .container{
background: #1D2926;
width: cover;
height: 50px;
margin: -10px -30px 0 -20px;
}

.banner .strip {
background: #E3C109;
height: 5px;
width: cover;
margin: 0px -30px 0px -20px;
}

body {
text-align: center;
}

.logo {
font: 2em;
color: #000;
background: url("");
margin-bottom: 0px;
}

h1 {
font-size: 60px;
font-family: 'Niconne', cursive;
}

ul {
background: rgba(0, 0, 0, 0.1);
padding: 2px 0 2px 0;
}

li {
margin-left: 0 -2px 0 -2px;
display: inline;
font-size: 100%;
padding: 0 3% 0 3%;
font-family: 'Londrina Solid', cursive;
border: 1px solid #d3c5ba;
  background: red;
}

CODEPEN CODEPEN

Add overflow: hidden and width: 100% to the ul 添加overflow: hiddenwidth: 100%ul

ul {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 0 2px 0;
    width: 100%;
    overflow: hidden;
} 

Add float: left and width: 16.5% to the li's 添加float: leftwidth: 16.5%li's

li {
    display: inline-block;
    font-size: 25px;
    font-family:'Londrina Solid', cursive;
    border: 1px solid #d3c5ba;
    width: 16.5%;
    float: left;
}

How I got the width of 16.5% is 100/6 . 我如何得到16.5%的宽度是100/6 6 is the number of links that you have on page and 100 is the width of the parent. 6是页面上的链接数,100是父级的宽度。 So, if you add more or get rid of some links just do 100 divided by the number of links. 所以,如果你添加更多或删除一些链接,只需要100除以链接数。

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

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