简体   繁体   中英

how to position heading in the center of the page

Hi I am trying to put my heading in the middle of the page, I have two logos which need to be placed either side of the heading. I've tried text-align:center and even margin:0px auto neither of these have worked? Is it my layout that needs changing? Or is something missing in my CSS?

My code is below or you can view a jsFiddle

Would love any input on how this can be fixed and anything I should bear in mind when doing this in the future

index.html

<div class="wrapper">


<header>
<h1>Website Service</h1>
<img src="imgs/logoa.png" id="sjplogo"/>
<img src="imgs/logob.png" id="sslogo"/>
</header> 
<div class="maincontent"></div>
<footer>
</footer>
</div>

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 {
       height:100%;

    line-height: 1;
    background-color:#EEEEEE;
        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                   *
*********************************/

#wrapper {
height:100% 
}

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;
    width:auto;

}

header h1 {
    text-align:center;
    margin:0px auto;
    font-size:42px;
    color:white;
    font-family:"ufonts com perpetua 2";
    display:inline-block;
}

.maincontent {

height:600px;

}
#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;
}

h2 is a default block level element, you are using display: inline-block; and hence it won't center your text, as it doesn't take up entire horizontal space anymore, so you need to get rid of display: inline-block;

Demo

Also, if you want to have two images besides your text, wrap all the three elements inside a container, assign some fixed width to your container element, and use margin: auto; , to position the images correctly, you can use position: relative; for the wrapper element, and than use position: absolute; to position each element inside the container element.

Positioning Demo

<div class="header">
    <div class="wrap">
        <span class="img_1"></span>
        <h2>Hello</h2>
        <span class="img_2"></span>
    </div>
</div>

.header .wrap {
    border: 1px solid #f00;
    width: 100px;
    margin: auto;
    text-align: center;
    position: relative;
}

.header .wrap .img_1, .header .wrap .img_2 {
    border: 1px solid #0f0;
    position: absolute;
    height: 20px;
    width: 20px;
    top: 25px;
}

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

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

您应该为header设置特定width以生成margin:0 auto工作。

can you please used this code Demo

<header>
    <center><h1>Website Service</h1></center>
    <img src="imgs/logoa.png" id="sjplogo"/>
    <img src="imgs/logob.png" id="sslogo"/>
</header> 

Remove display:inline-block; code from css and execute code.

demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM