CSS: center elements inside side by side 本文转载自  Ali Almoullim  查看原文  2015-03-17  517    css/

时间:2022-08-08 06:06:13

i've been trying all night to center this elements without having them stacked on each other:

我一直在努力整夜把这些元素集中在一起,而不是把它们叠在一起:

CSS: center elements inside  side by side
			      
			      
			      
			      
			      
			      
			         本文转载自
			      
			       Ali Almoullim
			       查看原文
			      
			       2015-03-17
			       517 
                  
                      
                     
                     
                     
                     css/
                                          
                     
                     
                     html                     
                     
                  
			        
			          
				        
				      
			      
			      
			          
						
						     (adsbygoogle = window.adsbygoogle || []).push({});
 						
                 
                        
                        
                            i've been trying all night to center this elements without having them stacked on each other: 
我一直在努力整夜把这些元素集中在一起,而不是把它们叠在一起: 
 
I've tried floating them to the left it works, but, they stick to the left side no matter what i did. 
我已经尝试将它们漂浮在左侧,但是,无论我做了什么,它们都会粘在左侧。 
 
HTML: 
<div class="center pages clearfix">
    <buuton class="center page-number">1</buuton>
    <buuton class="center page-number">2</buuton>
    <buuton class="center page-number">3</buuton>
</div>
 
CSS: 
.center {
    margin-left: auto;
    margin-right: auto; 
}

.page-number {
    display: block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}


     (adsbygoogle = window.adsbygoogle || []).push({});3 个解决方案
                           
                           
							  
							    #1
							    
							      1  
just update "display:block" to "display:inline-block" like the below updated css 
只需将“display:block”更新为“display:inline-block”,如下面更新的css 
updated css 
.center { margin-left: auto; margin-right: auto; text-align:center; } 
.center {margin-left:auto; margin-right:auto;文本对齐:中心; } 
.page-number {
    display: inline-block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}
 
demo
							     
							                          
                           
							  
							    #2
							    
							      
1  
Try this: https://jsfiddle.net/uo23L4n4/ 
试试这个:https://jsfiddle.net/uo23L4n4/ 
use inline-block instead of block for the buttons. 
使用内联块而不是按钮块。 
use text-align:center 
<button class=" page-number">1</button>

.center {
margin-left: auto;
margin-right: auto; 
text-align: center;
}

.page-number {
display: inline-block;
float: none;
width: 28px;
height: auto;
border-radius: 3px;
background-color: rgba(0, 0, 0, 0.47);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
font-weight: 400;
line-height: 1.5;
text-align: center;
color: rgba(255, 255, 255, 0.47);
}

							     
							                          
                           
							  
							    #3
							    
							      
0  
Change your mark up to a list, makes it very easy to do this.  
将标记更改为列表,使其非常容易。 
 <ul class="center pages clearfix">
    <li><buuton class="center page-number">1</buuton></li>
    <li><buuton class="center page-number">2</buuton></li>
    <li><buuton class="center page-number">3</buuton></li>
 </ul>
 
Use this css 
使用这个CSS 
 ul{
    list-style:none;
 }
 li{
   display:inline-block;
 }

							     
							                          
                           
                        
                 
               
				
				     (adsbygoogle = window.adsbygoogle || []).push({});
				
			     
			     		     
			     
			         
		              ×
		              注意!
		              
		              
		                 
		                 
		                 本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。

I've tried floating them to the left it works, but, they stick to the left side no matter what i did.

我已经尝试将它们漂浮在左侧,但是,无论我做了什么,它们都会粘在左侧。

CSS: center elements inside  side by side
			      
			      
			      
			      
			      
			      
			         本文转载自
			      
			       Ali Almoullim
			       查看原文
			      
			       2015-03-17
			       517 
                  
                      
                     
                     
                     
                     css/
                                          
                     
                     
                     html                     
                     
                  
			        
			          
				        
				      
			      
			      
			          
						
						     (adsbygoogle = window.adsbygoogle || []).push({});
 						
                 
                        
                        
                            i've been trying all night to center this elements without having them stacked on each other: 
我一直在努力整夜把这些元素集中在一起,而不是把它们叠在一起: 
 
I've tried floating them to the left it works, but, they stick to the left side no matter what i did. 
我已经尝试将它们漂浮在左侧,但是,无论我做了什么,它们都会粘在左侧。 
 
HTML: 
<div class="center pages clearfix">
    <buuton class="center page-number">1</buuton>
    <buuton class="center page-number">2</buuton>
    <buuton class="center page-number">3</buuton>
</div>
 
CSS: 
.center {
    margin-left: auto;
    margin-right: auto; 
}

.page-number {
    display: block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}


     (adsbygoogle = window.adsbygoogle || []).push({});3 个解决方案
                           
                           
							  
							    #1
							    
							      1  
just update "display:block" to "display:inline-block" like the below updated css 
只需将“display:block”更新为“display:inline-block”,如下面更新的css 
updated css 
.center { margin-left: auto; margin-right: auto; text-align:center; } 
.center {margin-left:auto; margin-right:auto;文本对齐:中心; } 
.page-number {
    display: inline-block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}
 
demo
							     
							                          
                           
							  
							    #2
							    
							      
1  
Try this: https://jsfiddle.net/uo23L4n4/ 
试试这个:https://jsfiddle.net/uo23L4n4/ 
use inline-block instead of block for the buttons. 
使用内联块而不是按钮块。 
use text-align:center 
<button class=" page-number">1</button>

.center {
margin-left: auto;
margin-right: auto; 
text-align: center;
}

.page-number {
display: inline-block;
float: none;
width: 28px;
height: auto;
border-radius: 3px;
background-color: rgba(0, 0, 0, 0.47);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
font-weight: 400;
line-height: 1.5;
text-align: center;
color: rgba(255, 255, 255, 0.47);
}

							     
							                          
                           
							  
							    #3
							    
							      
0  
Change your mark up to a list, makes it very easy to do this.  
将标记更改为列表,使其非常容易。 
 <ul class="center pages clearfix">
    <li><buuton class="center page-number">1</buuton></li>
    <li><buuton class="center page-number">2</buuton></li>
    <li><buuton class="center page-number">3</buuton></li>
 </ul>
 
Use this css 
使用这个CSS 
 ul{
    list-style:none;
 }
 li{
   display:inline-block;
 }

							     
							                          
                           
                        
                 
               
				
				     (adsbygoogle = window.adsbygoogle || []).push({});
				
			     
			     		     
			     
			         
		              ×
		              注意!
		              
		              
		                 
		                 
		                 本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。

HTML:

<div class="center pages clearfix">
    <buuton class="center page-number">1</buuton>
    <buuton class="center page-number">2</buuton>
    <buuton class="center page-number">3</buuton>
</div>

CSS:

.center {
    margin-left: auto;
    margin-right: auto; 
}

.page-number {
    display: block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}

3 个解决方案

#1


1  

just update "display:block" to "display:inline-block" like the below updated css

只需将“display:block”更新为“display:inline-block”,如下面更新的css

updated css

.center { margin-left: auto; margin-right: auto; text-align:center; }

.center {margin-left:auto; margin-right:auto;文本对齐:中心; }

.page-number {
    display: inline-block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}

demo

#2


1  

Try this: https://jsfiddle.net/uo23L4n4/

试试这个:https://jsfiddle.net/uo23L4n4/

use inline-block instead of block for the buttons.

使用内联块而不是按钮块。

use text-align:center

<button class=" page-number">1</button>

.center {
margin-left: auto;
margin-right: auto; 
text-align: center;
}

.page-number {
display: inline-block;
float: none;
width: 28px;
height: auto;
border-radius: 3px;
background-color: rgba(0, 0, 0, 0.47);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
font-weight: 400;
line-height: 1.5;
text-align: center;
color: rgba(255, 255, 255, 0.47);
}

#3


0  

Change your mark up to a list, makes it very easy to do this.

将标记更改为列表,使其非常容易。

 <ul class="center pages clearfix">
    <li><buuton class="center page-number">1</buuton></li>
    <li><buuton class="center page-number">2</buuton></li>
    <li><buuton class="center page-number">3</buuton></li>
 </ul>

Use this css

使用这个CSS

 ul{
    list-style:none;
 }
 li{
   display:inline-block;
 }

#1


1  

just update "display:block" to "display:inline-block" like the below updated css

只需将“display:block”更新为“display:inline-block”,如下面更新的css

updated css

.center { margin-left: auto; margin-right: auto; text-align:center; }

.center {margin-left:auto; margin-right:auto;文本对齐:中心; }

.page-number {
    display: inline-block;
    float: none;
    width: 28px;
    height: auto;
    border-radius: 3px;
    background-color: rgba(0, 0, 0, 0.47);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
    text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    font-weight: 400;
    line-height: 1.5;
    text-align: center;
    color: rgba(255, 255, 255, 0.47);
}

demo

#2


1  

Try this: https://jsfiddle.net/uo23L4n4/

试试这个:https://jsfiddle.net/uo23L4n4/

use inline-block instead of block for the buttons.

使用内联块而不是按钮块。

use text-align:center

<button class=" page-number">1</button>

.center {
margin-left: auto;
margin-right: auto; 
text-align: center;
}

.page-number {
display: inline-block;
float: none;
width: 28px;
height: auto;
border-radius: 3px;
background-color: rgba(0, 0, 0, 0.47);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.34);
text-shadow: 0 2px 5px rgba(0, 0, 0, .5);
font-weight: 400;
line-height: 1.5;
text-align: center;
color: rgba(255, 255, 255, 0.47);
}

#3


0  

Change your mark up to a list, makes it very easy to do this.

将标记更改为列表,使其非常容易。

 <ul class="center pages clearfix">
    <li><buuton class="center page-number">1</buuton></li>
    <li><buuton class="center page-number">2</buuton></li>
    <li><buuton class="center page-number">3</buuton></li>
 </ul>

Use this css

使用这个CSS

 ul{
    list-style:none;
 }
 li{
   display:inline-block;
 }