如何在页面中间居中放置3个div [英] How to center 3 divs in the middle of the page

查看:211
本文介绍了如何在页面中间居中放置3个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将3个div居中到页面中间,我让每个div都向左浮动,以便它们并排,但是我希望它们在页面的中央。

Hi I want to center 3 divs to the middle of the page, I have each of them floating left so that they are side by side however I want these to be central to the page.

我将它们放在宽度为 100%的包装纸中,并尝试使用 margin:0px auto 以使其居中,但这不起作用,但是 width:100% width:auto ,但是,如果我指定宽度,这似乎居中,但是我不想为包装器指定宽度,我希望它是页面的大小。

I have them in wrappers with a width of 100% and have tried to use margin:0px auto in order to center this this doesn't work but width:100% or width:auto, however if I specify a width this seems to center however I don't want to specify a width to my wrapper I want it to be the size of the page.

我不确定自己做错了什么。因此,任何输入和一双新鲜的眼睛都是很棒的!

I'm not entirely sure in regards to what I've done wrong. So any input and a fresh pair of eyes would be fantastic!

我的代码在下面,或者您可以查看 jsFiddle

My code is below or you can view a jsFiddle

index.html

index.html

<div class="wrapper">

<div class="header">
    <div class="wrap">
        <span class="img_1"><img src="imgs/sjplogo.png" id="sjplogo"/></span>
        <h2>Website Service</h2>
        <span class="img_2"><img src="imgs/sslogo.png" id="sslogo"/></span>
</div></div>

<div class="maincontent">
    <div class="maincontentWrapper">
    <div class="topWrapper">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
    </div><!--End topWrapper-->

    <div class="bottomWrapper">
    <div class="leftBottom"></div>
    <div class="rightBottom"></div>
    </div><!--End bottomWrapper-->

    </div><!-- End maincontentWrapper-->

</div>
<footer>

</footer>
</div>

style.css

style.css

@charset "utf-8";
/* CSS Document */

/********************************
* CSS Reset                     *
*********************************/
html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section {
    display: block;
}

body {

    line-height: 1;
    background-color:#FFF;
        font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;

}

ol, ul {
    list-style: none;
}

blockquote, q {
    quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

/********************************
* Main Styles                   *
*********************************/
.header {
background: #062141; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzA2MjE0MSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjI1JSIgc3RvcC1jb2xvcj0iIzBmMmY0ZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjYyJSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
background: -moz-linear-gradient(top,  #062141 0%, #0f2f4e 25%, #2b5878 62%, #2b5878 99%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#062141), color-stop(25%,#0f2f4e), color-stop(62%,#2b5878), color-stop(99%,#2b5878)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* IE10+ */
background: linear-gradient(to bottom,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#062141', endColorstr='#2b5878',GradientType=0 ); /* IE6-8 */
    height:100px;
}


.header .wrap {
    width: 100%;
    margin: auto;
    text-align: center;
    position: relative;
}

.header .wrap .img_1, .header .wrap .img_2 {
    position: absolute;
    width: 100%;
    top: 5px;
}

.header .wrap .img_1 {
    left: 0;
}

.header .wrap .img_2 {
    right: 0;
}

.header h2 {
    text-align:center;
    font-size:40px;
    color:white;
    font-family:"ufonts com perpetua 2";
    line-height:100px;
}


#sjplogo {
float:left; 
}
#sslogo {
float:right;    
}

footer {
background: #062141; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzA2MjE0MSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjI1JSIgc3RvcC1jb2xvcj0iIzBmMmY0ZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjYyJSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
background: -moz-linear-gradient(top,  #062141 0%, #0f2f4e 25%, #2b5878 62%, #2b5878 99%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#062141), color-stop(25%,#0f2f4e), color-stop(62%,#2b5878), color-stop(99%,#2b5878)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* IE10+ */
background: linear-gradient(to bottom,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#062141', endColorstr='#2b5878',GradientType=0 ); /* IE6-8 */
height:100px;
postion:absoutle;
bottom:0;
}

.maincontent p {
    font-family:"ufonts com perpetua 2";
    font-size:16px;
    color:#001838;
    margin:10px;    
}


/**STICKY FOOTER**/

.fixedfooter {
position:fixed;
   left:0px;
   bottom:0px;
   height:30px;
    width:100%;
    height:50px;
        text-align:center;
    font-size:20px;
    font-weight:bold;
    line-height:40px;

    /*gradient*/
    background: #062141; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzA2MjE0MSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjI1JSIgc3RvcC1jb2xvcj0iIzBmMmY0ZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjYyJSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzJiNTg3OCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
background: -moz-linear-gradient(top,  #062141 0%, #0f2f4e 25%, #2b5878 62%, #2b5878 99%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#062141), color-stop(25%,#0f2f4e), color-stop(62%,#2b5878), color-stop(99%,#2b5878)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* IE10+ */
background: linear-gradient(to bottom,  #062141 0%,#0f2f4e 25%,#2b5878 62%,#2b5878 99%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#062141', endColorstr='#2b5878',GradientType=0 ); /* IE6-8 */
    }


.maincontent {
height:1000px;
padding-bottom:20px;
}

.maincontentWrapper {
    margin:0px auto;
    width:100%;
    height:1000px;
}

.topWrapper {
    width:100%;
    margin:0px auto;
}

.left{
    width:200px;
    height:200px;
    background-color:red;
    float:left;
    margin-right:20px;
    }

.center {
width:200px;
height:200px;
float:left;
background-color:blue;  
margin-right:20px;
}

.right {
    width:200px;
    height:200px;
    background-color:black; 
    float:left;
    margin-right:20px;
}


推荐答案

更改 .topWrapper div设置 text-align:center 规则,然后删除 float:left 从这三个div(.left,.center和.right)中提取,并替换为 display:inline-block

Change the .topWrapper div to have a text-align:center rule, then remove the float:left from those three divs (.left, .center, and .right) and replace them with display:inline-block.

jsFiddle示例

.topWrapper {
    width:100%;
    text-align:center;
}
.left {
    width:200px;
    height:200px;
    background-color:red;
    display:inline-block;
    margin-right:20px;
}
.center {
    width:200px;
    height:200px;
    display:inline-block;
    background-color:blue;
    margin-right:20px;
}
.right {
    width:200px;
    height:200px;
    background-color:black;
    display:inline-block;
    margin-right:20px;
}

这篇关于如何在页面中间居中放置3个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆