我正在尝试将一个<ul>
内部居中放置<div>
。我尝试了以下
text-align: center;
和
left: 50%;
这是行不通的。
CSS :
.container {
clear: both;
width: 800px;
height: 70px;
margin-bottom: 10px;
text-align: center;
}
.container ul {
padding: 0 0 0 20px;
margin: 0;
list-style: none;
}
.container ul li {
margin: 0;
padding: 0;
}
我希望将ul
其放置在容器的中央。
以下是在CSS中将内容水平居中的解决方案列表。该片段包括所有这些。
html {
font: 1.25em/1.5 Georgia, Times, serif;
}
pre {
color: #fff;
background-color: #333;
padding: 10px;
}
blockquote {
max-width: 400px;
background-color: #e0f0d1;
}
blockquote > p {
font-style: italic;
}
blockquote > p:first-of-type::before {
content: open-quote;
}
blockquote > p:last-of-type::after {
content: close-quote;
}
blockquote > footer::before {
content: "\2014";
}
.container,
blockquote {
position: relative;
padding: 20px;
}
.container {
background-color: tomato;
}
.container::after,
blockquote::after {
position: absolute;
right: 0;
bottom: 0;
padding: 2px 10px;
border: 1px dotted #000;
background-color: #fff;
}
.container::after {
content: ".container-" attr(data-num);
z-index: 1;
}
blockquote::after {
content: ".quote-" attr(data-num);
z-index: 2;
}
.container-4 {
margin-bottom: 200px;
}
/**
* Solution 1
*/
.quote-1 {
max-width: 400px;
margin-right: auto;
margin-left: auto;
}
/**
* Solution 2
*/
.container-2 {
text-align: center;
}
.quote-2 {
display: inline-block;
text-align: left;
}
/**
* Solution 3
*/
.quote-3 {
display: table;
margin-right: auto;
margin-left: auto;
}
/**
* Solution 4
*/
.container-4 {
position: relative;
}
.quote-4 {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
/**
* Solution 5
*/
.container-5 {
display: flex;
justify-content: center;
}
<main>
<h1>CSS: Horizontal Centering</h1>
<h2>Uncentered Example</h2>
<p>This is the scenario: We have a container with an element inside of it that we want to center. I just added a little padding and background colors so both elements are distinquishable.</p>
<div class="container container-0" data-num="0">
<blockquote class="quote-0" data-num="0">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
<h2>Solution 1: Using <code>max-width</code> & <code>margin</code> (IE7)</h2>
<p>This method is widely used. The upside here is that only the element which one wants to center needs rules.</p>
<pre><code>.quote-1 {
max-width: 400px;
margin-right: auto;
margin-left: auto;
}</code></pre>
<div class="container container-1" data-num="1">
<blockquote class="quote quote-1" data-num="1">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
<h2>Solution 2: Using <code>display: inline-block</code> and <code>text-align</code> (IE8)</h2>
<p>This method utilizes that <code>inline-block</code> elements are treated as text and as such they are affected by the <code>text-align</code> property. This does not rely on a fixed width which is an upside. This is helpful for when you don’t know the number of elements in a container for example.</p>
<pre><code>.container-2 {
text-align: center;
}
.quote-2 {
display: inline-block;
text-align: left;
}</code></pre>
<div class="container container-2" data-num="2">
<blockquote class="quote quote-2" data-num="2">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
<h2>Solution 3: Using <code>display: table</code> and <code>margin</code> (IE8)</h2>
<p>Very similar to the second solution but only requires to apply rules on the element that is to be centered.</p>
<pre><code>.quote-3 {
display: table;
margin-right: auto;
margin-left: auto;
}</code></pre>
<div class="container container-3" data-num="3">
<blockquote class="quote quote-3" data-num="3">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
<h2>Solution 4: Using <code>translate()</code> and <code>position</code> (IE9)</h2>
<p>Don’t use as a general approach for horizontal centering elements. The downside here is that the centered element will be removed from the document flow. Notice the container shrinking to zero height with only the padding keeping it visible. This is what <i>removing an element from the document flow</i> means.</p>
<p>There are however applications for this technique. For example, it works for <b>vertically</b> centering by using <code>top</code> or <code>bottom</code> together with <code>translateY()</code>.</p>
<pre><code>.container-4 {
position: relative;
}
.quote-4 {
position: absolute;
left: 50%;
transform: translateX(-50%);
}</code></pre>
<div class="container container-4" data-num="4">
<blockquote class="quote quote-4" data-num="4">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
<h2>Solution 5: Using Flexible Box Layout Module (IE10+ with vendor prefix)</h2>
<p></p>
<pre><code>.container-5 {
display: flex;
justify-content: center;
}</code></pre>
<div class="container container-5" data-num="5">
<blockquote class="quote quote-5" data-num="5">
<p>My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.</p>
<footer>Tasha Yar about Data</footer>
</blockquote>
</div>
</main>
display: flex
.container {
display: flex;
justify-content: center;
}
注意事项 :
max-width
和 margin
可以通过分配一个固定的宽度和设定水平居中一个块级元素margin-right
和margin-left
到auto
。
.container ul {
/* for IE below version 7 use `width` instead of `max-width` */
max-width: 800px;
margin-right: auto;
margin-left: auto;
}
注意事项 :
transform: translatex(-50%)
&left: 50%
这类似于使用绝对定位和负边距的古怪居中方法。
.container {
position: relative;
}
.container ul {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
注意事项 :
top``left``translateY()``translateX()
transform2d
display: table
&margin
就像第一个解决方案一样,您将自动值用于左右边距,但不指定宽度。如果您不需要支持IE7或更低版本,则此方法更合适,尽管使用table
属性值作为有点不明智display
。
.container ul {
display: table;
margin-right: auto;
margin-left: auto;
}
display: inline-block
&text-align
也可以像对待常规文本一样对元素居中。缺点:您需要为容器和元素本身分配值。
.container {
text-align: center;
}
.container ul {
display: inline-block;
/* One most likely needs to realign flow content */
text-align: initial;
}
笔记:
问题内容: 由于某些原因,我的div不会在包含div的水平居中: 有时有一个行div,其中只有一个块div。我究竟做错了什么? 问题答案: 要实现您想要做的事情: 考虑使用代替。
本文向大家介绍如何将水平滚动条放置在DIV的中心?,包括了如何将水平滚动条放置在DIV的中心?的使用技巧和注意事项,需要的朋友参考一下 要将水平滚动条放置在div的中心,请使用向左滚动键。您可以尝试运行以下代码以将水平滚动条放置在div中。 示例 滚动条位于div的中心:
问题内容: 我需要将两个div彼此对齐,以便每个都包含一个标题和一个项目列表,类似于: 使用表非常容易,但是我不想使用表。我该如何实现? 问题答案: 将div浮动到父容器中,并设置其样式如下:
问题内容: 我们可以将图片设置为like的背景图片: 我需要在水平和垂直中心放置一张桌子。是否有使用的跨浏览器解决方案? 问题答案: 居中是CSS中最大的问题之一。但是,存在一些技巧: 要水平放置表格,您可以将左右边距设置为自动: 要垂直居中,唯一的方法是使用javascript: 否是可能的,因为表是一个块而不是一个内联元素。 编辑
问题内容: 对于我网站的主导航,主导航链接的宽度为980像素,带ul。我正在尝试使导航链接拉伸以均匀地适合div的宽度。 我正在做一些典型的CSS来水平制作ul列表(浮动:左,显示:块)。我可以调整li的填充以使其非常接近,但是我真正需要的是一种使其自动伸展以适应的方法。可能? 编辑 难度1:无法使用表格。困难2:每个导航项的宽度都不同,以容纳更长或更短的链接名称。 问题答案: 这是最简单的方法:
问题内容: 我唯一的问题是使它们三排成一直线并具有相等的间距。显然,跨度不能具有宽度,并且div(以及具有display:block的跨度)不能水平并排出现。有什么建议吗? 是我现在所拥有的。 问题答案: 您可以将divs与该属性一起使用,该属性将使它们彼此水平并排显示,但是随后您可能需要对以下元素使用清除功能,以确保它们不重叠。