更多efficient/appropriate 这段代码的写法?
More efficient/appropriate way to write this code?
所以我最近才开始学习响应式网页设计并在 CSS 中使用媒体查询。我已经明白了如何为手机做出适当的响应和优化。
如果有人只是觉得无聊或者有兴趣看这个,你能不能告诉我我是否在正确的轨道上,或者我是否违反了任何严肃的编码规则?哈。
这是我正在做的部分工作:(这是指移动的小盒子,忽略背景即可)
<style>
.three img {
position: relative;
top: 0;
}
#shoutout{
position: absolute;
margin: 40% 45%;
border: 2px solid black;
background-color: white;
padding: 7% 14%;
z-index: 1;
}
.sotext p{
font-size: 120%;
font-weight: bold;
text-align: center;
}
@media screen and (max-width: 1023px){
#shoutout{
text-align: center;
font-size: 75%;
margin-top: 40%;
}
.sotext p{
text-align: center;
font-size: 100%;
}
}
@media screen and (max-width: 740px){
#shoutout{
text-align: center;
margin-top: 35%;
margin-left: 45%;
}
.sotext p{
text-align: center;
font-size: 60%;
}
}
</style>
<head>
<!-- Header Placeholder-->
</head>
<body>
<div>
<section id="shoutout">
<!-- shoutut box -->
<div class="sotext">
<p>Placeholder Text</p>
</div>
</section>
<section class="three">
<img src="http://static1.squarespace.com/static/55bed56ee4b04fdc6e0dd0d8/t/575d8fed4d088eb0ab810e52/1465749487419/mother_daughter.png" alt="shoutout image" />
</section>
</div>
<body>
小调整
html, body {
height: 100%;
width: 100%;
}
.background {
background-image:url(http://static1.squarespace.com/static/55bed56ee4b04fdc6e0dd0d8/t/575d8fed4d088eb0ab810e52/1465749487419/mother_daughter.png);
background-position: center left;
background-size: cover;
width: 100%;
height: 100%;
}
#shoutout{
position: absolute;
margin: 40% 45%;
border: 2px solid black;
background-color: white;
padding: 7% 14%;
z-index: 1;
}
.sotext p{
font-size: 1.2em;
font-weight: bold;
text-align: center;
}
@media screen and (max-width: 1023px){
#shoutout{
text-align: center;
font-size: 0.75em;
margin-top: 40%;
}
.sotext p{
font-size: 1em;
}
}
@media screen and (max-width: 740px){
#shoutout{
margin-top: 35%;
margin-left: 45%;
}
.sotext p{
font-size: 0.6em;
}
}
<div class="background">
<section id="shoutout">
<!-- shoutut box -->
<div class="sotext">
<p>Placeholder Text</p>
</div>
</section>
</div>
我个人会使用 background-image 而不是 img 标签,这样您可以更好地控制显示。我还会使用 ems 而不是字体大小的百分比。正如我在评论中所说,删除重复项。
所以我最近才开始学习响应式网页设计并在 CSS 中使用媒体查询。我已经明白了如何为手机做出适当的响应和优化。
如果有人只是觉得无聊或者有兴趣看这个,你能不能告诉我我是否在正确的轨道上,或者我是否违反了任何严肃的编码规则?哈。
这是我正在做的部分工作:(这是指移动的小盒子,忽略背景即可)
<style>
.three img {
position: relative;
top: 0;
}
#shoutout{
position: absolute;
margin: 40% 45%;
border: 2px solid black;
background-color: white;
padding: 7% 14%;
z-index: 1;
}
.sotext p{
font-size: 120%;
font-weight: bold;
text-align: center;
}
@media screen and (max-width: 1023px){
#shoutout{
text-align: center;
font-size: 75%;
margin-top: 40%;
}
.sotext p{
text-align: center;
font-size: 100%;
}
}
@media screen and (max-width: 740px){
#shoutout{
text-align: center;
margin-top: 35%;
margin-left: 45%;
}
.sotext p{
text-align: center;
font-size: 60%;
}
}
</style>
<head>
<!-- Header Placeholder-->
</head>
<body>
<div>
<section id="shoutout">
<!-- shoutut box -->
<div class="sotext">
<p>Placeholder Text</p>
</div>
</section>
<section class="three">
<img src="http://static1.squarespace.com/static/55bed56ee4b04fdc6e0dd0d8/t/575d8fed4d088eb0ab810e52/1465749487419/mother_daughter.png" alt="shoutout image" />
</section>
</div>
<body>
小调整
html, body {
height: 100%;
width: 100%;
}
.background {
background-image:url(http://static1.squarespace.com/static/55bed56ee4b04fdc6e0dd0d8/t/575d8fed4d088eb0ab810e52/1465749487419/mother_daughter.png);
background-position: center left;
background-size: cover;
width: 100%;
height: 100%;
}
#shoutout{
position: absolute;
margin: 40% 45%;
border: 2px solid black;
background-color: white;
padding: 7% 14%;
z-index: 1;
}
.sotext p{
font-size: 1.2em;
font-weight: bold;
text-align: center;
}
@media screen and (max-width: 1023px){
#shoutout{
text-align: center;
font-size: 0.75em;
margin-top: 40%;
}
.sotext p{
font-size: 1em;
}
}
@media screen and (max-width: 740px){
#shoutout{
margin-top: 35%;
margin-left: 45%;
}
.sotext p{
font-size: 0.6em;
}
}
<div class="background">
<section id="shoutout">
<!-- shoutut box -->
<div class="sotext">
<p>Placeholder Text</p>
</div>
</section>
</div>
我个人会使用 background-image 而不是 img 标签,这样您可以更好地控制显示。我还会使用 ems 而不是字体大小的百分比。正如我在评论中所说,删除重复项。