【发布时间】:2017-06-13 00:11:37
【问题描述】:
我已经完成了框架设计,现在我想在#content div 中进行实验。我想使文本居中,但无法正常工作。
可以在我的页面上看到。
index.HTML 最重要的部分(除去 script 和其他链接标签后)可以在这里看到:
body {
font-family: "PT Sans";
background-color: #dfc;
color: #000;
}
.top {
width: 100%;
height: 50px;
position: fixed;
left: 0;
top: 0;
background-color: #5dbf61;
z-index: 100;
}
.stickyTop {
width: 100%;
position: fixed;
left: 0;
top: 0;
z-index: 100;
}
.logo {
display: block;
font-size: 26px;
padding: 5px;
padding-left: 10px;
text-align: center;
letter-spacing: 1px;
}
.logo a {
text-decoration: none;
color: #fff;
cursor: pointer;
}
.sidebar {
width: 250px;
height: 100%;
position: fixed;
left: 0;
top: 50px;
background-color: #4caf50;
z-index: 99;
overflow-y: auto;
overflow-x: none;
}
.stickySidebar {
height: 100%;
position: fixed;
left: 0;
top: 50px;
z-index: 99;
}
.sidebar>.option {
height: 42px;
line-height: 40px;
width: 100%;
left: 0;
display: inline-block;
text-indent: 12px;
color: #dfc;
}
.sidebar a {
display: block;
text-decoration: none;
color: #dfc;
}
.sidebar>.option:hover {
background-color: #8bc34a;
}
.sidebar>.selected {
height: 42px;
line-height: 40px;
width: 100%;
left: 0;
display: inline-block;
background-color: #8bc34a;
text-indent: 12px;
color: #dfc;
}
.sidebar>.panel {
width: 226px;
/*250 - 12 - 12*/
left: 0;
display: inline-block;
padding: 12px;
color: #dfc;
font-size: 14px;
}
.sidebar>.empty {
height: 42px;
width: 100%;
left: 0;
display: inline-block;
text-indent: 12px;
color: #dfc;
}
.sidebar>.empty-border {
height: 42px;
width: 100%;
left: 0;
display: inline-block;
text-indent: 12px;
color: #dfc;
}
.navbar {
width: 100%;
height: 26px;
left: 250px;
top: 50px;
position: fixed;
display: block;
z-index: 99;
padding-left: 9px;
padding-top: 4px;
border-bottom: 2px solid #9b8;
background-color: #bda;
color: #796;
font-size: 14px;
}
.stickyNavbar {
height: 100%;
position: fixed;
left: 250px;
top: 50px;
z-index: 99;
}
.navbar a {
color: #796;
}
.content {
width: 100%;
height: 100%;
left: 250px;
top: 76px;
position: fixed;
display: block;
padding: 15px;
}
.spaced-text {
letter-spacing: 1px;
}
.fixed-center {
width: 100%;
display: block;
text-align: center;
}
<div id="container">
<div class="top">
<div class="logo">
<a href="#"><b>Nieposortowane</b></a>
</div>
</div>
<div class="sidebar">
<div class="option"><a href="#">Strona Główna</a></div>
<div class="option"><a href="#">Przyklad 1</a></div>
<div class="selected"><a href="#">Może coś tu będzie</a></div>
<div class="option"><a href="#">Strona Niegłówna</a></div>
<div class="empty-border"></div>
<div class="panel" id="info">
<b class="spaced-text">Informacje o stronie</b><br/><br/> Ta strona jeszcze nie jest stroną, bo trwa robienie designu. Lorem ipsum dolor sit amet jakoś tak cośtam dalej nie pamiętam a nie chce mi się kopiować.
</div>
<div class="panel" id="style-switcher">
<b class="spaced-text">Wybieranie stylu strony</b><br /><br />
<b>Styl strony:</b><br />
<input type="radio" id="style_1" name="design" onclick="update();" checked>Normalny</input><br />
<input type="radio" id="style_2" name="design" onclick="update();">Dark</input><br />
</div>
<div class="panel" id="footer">
<b>Nieposortowane.pl © 2017<br />
Karol Turbiarz</b><br /><br /> Wszelkie prawa zastrzeżone<br /> All rights reserved
</div>
<br /><br />
</div>
<div class="navbar">
<a href="#">Nieposortowane.pl</a> > <a href="#">Strona główna</a>
</div>
<div class="content">
<center>a</center>
</div>
</div>
你可以看到中心是如何偏向右侧的。怎么解决?
【问题讨论】:
-
文本在 div 中正确居中 - 问题是它居中的
contentdiv 比页面本身宽得多。此外,完全删除<center>,只需将text-align: center;放在contentdiv 上。 -
<center/>是一个已弃用的 html 元素:developer.mozilla.org/en-US/docs/Web/HTML/Element/center。使用 css 使文本居中。 -
@LuudJacobs 当我使用 css 时,同样的事情会发生
-
同样,您的
contentdiv 比页面大得多。您可能需要使用vw和calc()的一些变体来获得正确的值。在contentdiv 上尝试width: calc(100vw - 250px)。 -
你的标记是一团糟。为什么一切都是
position:fixed?您的内容具有 100% 的宽度(父容器的),但向右移动了 250 像素。那里有大量关于如何建立现代网站的文章,即使w3schools 现在也有相当高质量的材料。