Javascript 如何在所有浏览器和设备上集中网页上的某些元素?
我有一些元素,div或其他id,我想把它放在网页的中心。 但当它在电脑屏幕上看起来不错时,在手机上看起来就不好了。 如何正确地将其居中,使用任何框架(如bootstrap或其他),或者不使用任何方法,jquery也是可能的。 它在所有手机和电脑浏览器上都很好看。 谢谢。这很简单。 HTMLJavascript 如何在所有浏览器和设备上集中网页上的某些元素?,javascript,jquery,html,css,cross-browser,Javascript,Jquery,Html,Css,Cross Browser,我有一些元素,div或其他id,我想把它放在网页的中心。 但当它在电脑屏幕上看起来不错时,在手机上看起来就不好了。 如何正确地将其居中,使用任何框架(如bootstrap或其他),或者不使用任何方法,jquery也是可能的。 它在所有手机和电脑浏览器上都很好看。 谢谢。这很简单。 HTML <div id="container"> <div id="content"> Your content here. It can be text, image or
<div id="container">
<div id="content">
Your content here. It can be text, image or anything.
</div>
</div>
<div id="centered">
<!-- Place your content here -->
</div>
注意:
边距自动实际居中对齐有时取决于场景。如果有固定的高度和宽度,则更容易实现。祝你好运
提示
如果要查看的对齐方式,应使用#选择器{border:solid}在设计过程中使用CSS中的code>,然后您可以将其删除。这非常简单。
HTML
<div id="container">
<div id="content">
Your content here. It can be text, image or anything.
</div>
</div>
<div id="centered">
<!-- Place your content here -->
</div>
注意:
边距自动实际居中对齐有时取决于场景。如果有固定的高度和宽度,则更容易实现。祝你好运
提示
如果要查看的对齐方式,应使用#选择器{border:solid}在设计过程中使用CSS中的code>,然后可以将其删除。尝试以下操作:
<div class="center">
<p>.........content...........</p>
</div>
试试这个:
<div class="center">
<p>.........content...........</p>
</div>
我会给你另一个解决方案,定位在绝对中心(水平和垂直),永远不会失败,并在我测试过的每个浏览器(ie 8+)上工作
您有HTML
<div id="container">
<div id="content">
Your content here. It can be text, image or anything.
</div>
</div>
<div id="centered">
<!-- Place your content here -->
</div>
如果您只想垂直,下面是:
#centered{
width: 500px;
height: 500px;
position:absolute;
right:0;
left:0;
margin:auto;
}
#centered{
width: 500px;
height: 500px;
position:absolute;
top:0;
bottom:0;
margin:auto;
}
并且只有水平的:
#centered{
width: 500px;
height: 500px;
position:absolute;
right:0;
left:0;
margin:auto;
}
#centered{
width: 500px;
height: 500px;
position:absolute;
top:0;
bottom:0;
margin:auto;
}
我会给你另一个解决方案,定位在绝对中心(水平和垂直),永远不会失败,并在我测试过的每个浏览器(ie 8+)上工作
您有HTML
<div id="container">
<div id="content">
Your content here. It can be text, image or anything.
</div>
</div>
<div id="centered">
<!-- Place your content here -->
</div>
如果您只想垂直,下面是:
#centered{
width: 500px;
height: 500px;
position:absolute;
right:0;
left:0;
margin:auto;
}
#centered{
width: 500px;
height: 500px;
position:absolute;
top:0;
bottom:0;
margin:auto;
}
并且只有水平的:
#centered{
width: 500px;
height: 500px;
position:absolute;
right:0;
left:0;
margin:auto;
}
#centered{
width: 500px;
height: 500px;
position:absolute;
top:0;
bottom:0;
margin:auto;
}
CSS
HTML
这将居中进行
退房
第一个div占据整个屏幕,每个浏览器都有一个display:flex设置。第二个div(居中div)利用display:flex div,其中margin:auto工作得非常出色
注意IE11+(IE10带前缀)CSS
HTML
这将居中进行
退房
第一个div占据整个屏幕,每个浏览器都有一个display:flex设置。第二个div(居中div)利用display:flex div,其中margin:auto工作得非常出色
注意IE11+(IE10 w/前缀)您是否尝试过文本对齐:居中
?是否可能重复您尝试过文本对齐:居中
?是否可能重复