▲三种方式实现元素水平居中显示

本文介绍了三种常见的CSS元素水平居中方法:自动外边距法、文本居中与自动外边距结合使用及负外边距法。每种方法均有详细的代码示例,适用于不同浏览器环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

css中让文本居中的属性很简单就可以实现,那就是设置text-align:center即可。而我这里所说的“元素”实际上是指容器,如果要有一个贴切点的标签,那应该可以用div来表示。

让元素水平居中,相信对于许多网页设计师而言都不会陌生。但是有的时候,自己就在想,为什么要让元素水平居中?是出于什么原因呢?都是一点自己的见解,蛮写下来...

首先,要 让元素水平居中,就必须得了解css设计中固定布局和流式布局两者的概念。它们之间的直观区别就看是否给元素设置了宽度。下面是两段代码,用来简单地说明固定布局和流式布局的区别。

1、固定布局demo: 

<html xmlns="http://www.w3.org/1999/xhtml">
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
<title>position-layout</title>
        
<style type="text/css">
         .wrapper{width:750px;position:relative;margin:
0 auto;text-align:left;}
         .contentArea{width:450px;position:absolute;top:
0;left:150px;height:500px;background:#96c;}
         .leftPanel{width:150px;position:absolute;top:
0;left:0;height:500px;background:#999;}
         .rightPanel{width:150px;position:absolute;top:
0;left:600px;height:500px;background:#06C;}
    
</style>
    
</head>
    
<body>
        
<div class="wrapper">
            
<div class="contentArea"></div>
            
<div class="leftPanel"></div>
            
<div class="rightPanel"></div>
        
</div>
    
</body>
</html>
 

  2、流式布局demo:

<html xmlns="http://www.w3.org/1999/xhtml">
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
<title>col3-margin-layout</title>
        
<style type="text/css">
            .contentArea{margin:
0 160px;height:500px;background:#96c;}
            .leftPanel{width:150px;
float:left;height:500px;background:#999;}
            .rightPanel{width:150px;
float:right;height:500px;background:#06C;}
        
</style>
    
</head> 
    
<body>
        
<div class="wrapper">
            
<div class="leftPanel"></div>
            
<div class="rightPanel"></div>
            
<div class="contentArea"></div>
        
</div>
    
</body>
</html>

  通过上面两个例子,可以得出:流式布局不存在元素水平居中的可能,因为它都是满屏显示的。只有固定布局,因为限宽,所以就有了让元素水平居中的可能。

其次,固定布局的实现也不一定要让元素水平居中,之所以这么做,是为了让浏览器的两边能够留出平均的旁白,而不是只有一边是一大片空白,影响美观。 

  都是些浅显的知识,下面进入主题。

============================================================================

  让元素水平居中的三种方式,我将分别进行介绍。如下:

1、自动外边距法

这是目前网页设计人员最熟悉的一种方法,它需要给容器设置宽度,并设置margin:auto样式。下面是一段代码:

<html xmlns="http://www.w3.org/1999/xhtml">
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
<title>position-layout</title>
        
<style type="text/css">
         .wrapper{width:750px;margin:
0 auto;position:relative;}
         .contentArea{width:450px;position:absolute;top:
0;left:150px;height:500px;background:#96c;}
         .leftPanel{width:150px;position:absolute;top:
0;left:0;height:500px;background:#999;}
         .rightPanel{width:150px;position:absolute;top:
0;left:600px;height:500px;background:#06C;}
    
</style>
    
</head>
    
<body>
        
<div class="wrapper">
            
<div class="contentArea"></div>
            
<div class="leftPanel"></div>
            
<div class="rightPanel"></div>
        
</div>
    
</body>

</html>
 

  通过这段代码,可以发现,这种方式在在目前各种主流浏览器下(包括ie6)都能很好的显示,只有在ie6以下的版本不生效,元素依然向左排列。如果不考虑低版本浏览器的问题,那么它将是最便捷的。

2、文本居中和自动外边距的结合使用。 

这种方式可以解决ie6以下版本不支持margin:0 auto的 问题,它的用法就是在body里设置text-align:center样式。具体代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
<title>position-layout</title>
        
<style type="text/css">
            body{text
-align:center;}
            .wrapper{width:750px;position:relative;margin:
0 auto;text-align:left;}
            .contentArea{width:450px;position:absolute;top:
0;left:150px;height:500px;background:#96c;}
            .leftPanel{width:150px;position:absolute;top:
0;left:0;height:500px;background:#999;}
            .rightPanel{width:150px;position:absolute;top:
0;left:600px;height:500px;background:#06C;}
        
</style>
    
</head>
    
<body>
        
<div class="wrapper">
            
<div class="contentArea"></div>
            
<div class="leftPanel"></div>
            
<div class="rightPanel"></div>
        
</div>
    
</body>
</html>
 

     在这里,text-align:center被作为css hack来使用,因为它本属于文本的样式,用在body里来实现元素居中的样式,做了本不属于自己该做的事...

3、负外边距法。

这种方式的实现方式比前两种复杂。它得结合定位来使用。具体代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
<title>negative-margin-element-center</title>
        
<style type="text/css">
            .wrapper{width:750px;position:relative;left:
50%;margin-left:-375px;}
            .contentArea{width:450px;position:absolute;top:
0;left:150px;height:500px;background:#96c;}
            .leftPanel{width:150px;position:absolute;top:
0;left:0;height:500px;background:#999;}
            .rightPanel{width:150px;position:absolute;top:
0;left:600px;height:500px;background:#06C;}
        
</style>
    
</head>
    
<body>
        
<div class="wrapper">
            
<div class="contentArea"></div>
            
<div class="leftPanel"></div>
            
<div class="rightPanel"></div>
        
</div>
    
</body>
</html>

  首先,让容器相对文档向右偏移50%,然后,将容器的左外边距设置为负的容器宽度的一半,即可实现元素的水平居中显示。这种方式没有hack,且兼容性很好,能在最广泛的浏览器下表现一致。

  以上就是我所知道的三种实现元素水平居中的方法,都比较简单,写下来就当是一次知识的回顾总结吧。

 

转载于:https://www.cnblogs.com/balaixianren/archive/2011/07/17/2108758.html

<ComboBox Grid.Row="1" x:Name="Combox_Role" SelectedIndex="1" Margin="40,10,40,10" FontSize="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Style="{StaticResource RoundedComboBoxStyle}"> <ComboBoxItem Content="管理员" /> <ComboBoxItem Content="操作员" /> </ComboBox> c#: <Style x:Key="RoundedTextBoxStyle" TargetType="TextBox"> <Setter Property="Background" Value="#FFF0F0F0"/> <Setter Property="Foreground" Value="#333333"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Border x:Name="border" CornerRadius="5" Background="{TemplateBinding Background}" BorderBrush="#FFABADB3" BorderThickness="1"> <ScrollViewer x:Name="PART_ContentHost" Margin="{TemplateBinding Padding}" VerticalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <!-- 获得焦点时改变边框颜色 --> <Trigger Property="IsFocused" Value="True"> <Setter TargetName="border" Property="BorderBrush" Value="#FF3C8CE9"/> <Setter TargetName="border" Property="BorderThickness" Value="1.5"/> </Trigger> <!-- 鼠标悬停效果 --> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="BorderBrush" Value="#FF7FADEA"/> </Trigger> <!-- 禁用状态 --> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="border" Property="Background" Value="#FFF5F5F5"/> <Setter TargetName="border" Property="BorderBrush" Value="#FFD9D9D9"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="RoundedComboBoxStyle" TargetType="ComboBox"> <Setter Property="Padding" Value="6,2"/> <Setter Property="BorderBrush" Value="LightGray"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Background" Value="White"/> <Setter Property="MinWidth" Value="80"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ComboBox"> <Grid> <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5" SnapsToDevicePixels="True"> <Grid> <ToggleButton x:Name="DropDownToggle" Focusable="False" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <ToggleButton.Template> <ControlTemplate TargetType="ToggleButton"> <Grid> <Rectangle x:Name="HoverBackground" Fill="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="5" RadiusY="5"/> <TextBlock x:Name="DropDownText" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0" FontSize="12" Foreground="#FFC0CB" Text="▼"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="HoverBackground" Property="Fill" Value="#9370DB"/> </Trigger> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="DropDownText" Property="Text" Value=""/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </ToggleButton.Template> </ToggleButton> <!-- 内容展示区域 --> <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}"/> </Grid> </Border> <Popup x:Name="PART_Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide" MinWidth="{TemplateBinding ActualWidth}"> <Border Background="White" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" CornerRadius="5"> <ScrollViewer> <StackPanel IsItemsHost="True"/> </ScrollViewer> </Border> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#FFF5F5F5"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFD9D9D9"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Border" Property="BorderBrush" Value="#FF7FADEA"/> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter TargetName="Border" Property="BorderBrush" Value="#FF3C8CE9"/> <Setter TargetName="Border" Property="BorderThickness" Value="1.5"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 下拉选项框大小跟主框一样,鼠标在主框文本上点击也可触发下拉,下拉选项文本居中
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值