Centering text and ensuring it is the perfect size on a webpage using HTML/CSS

I am currently constructing a website as part of a class project at my school. After finalizing the design for the site, I have now moved on to the development phase.

Among the various stylesheets available, I have settled on "Roboto Slab" which gives the desired look and feel to the website. Most of the main page has already been crafted by me.

However, I am encountering one persistent issue while attempting to format the text. Despite my efforts to position the text exactly in the center of the webpage, it appears slightly lower than expected. Additionally, there seems to be an unnecessary horizontal scrolling option present.

Below is the code snippet for the website:

<html>
<head>
    <title>MySite</title>
    <meta name="description" content="My Site.">
    <link rel="stylesheet" href="core.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style> 
        css code here
    </style>;
</head>
<body>
    body content here
</body>
</html>

I am puzzled as to why this issue persists despite trying different methods such as align: center. Can anyone provide some insight into this matter?

!! EDIT !! Successfully resolved font size problem. Hooray for me! Added a new section in the CSS script for H2 elements.

Answer №1

To center the content, you should add a new div in your HTML code.

<i class="do-you-even-flexbox"></i>

<div class="container">
    <div class="box">
         <h1>Hi.</h1>

         <h2>This is the website for my independent study.</h2>

    </div>
</div>
<div class="links"> <a href="some_page_here">Example Page.</a>
 <a href="some_page_here">Another Page.</a>
 <a href="some_page_here">Another page! :D</a>
 <a href="derp">Blah blah</a>

</div>

After that, use CSS with flex-box to center the newly added "box" div.

html, body, .do-you-even-flexbox {
    height:100%;
width:100%;
overflow-x:hidden;
}
body {
    margin:0;
    font:normal 14px/1.2"Roboto", Helvetica, Arial, sans-serif;
    text-align:center;
    background:#111 url('http://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Alpamayo.jpg/1280px-Alpamayo.jpg') center top/ cover;
}
::-moz-selection {
    background:#2fdd11;
    color:#fff;
}
::selection {
    background:#2fdd11;
    color:#fff;
}
a {
    text-decoration:none;
}
a:hover, a:active {
    outline:none;
    text-decoration:underline;
}
a:focus {
    outline:thin dotted;
}
.do-you-even-flexbox, .container {
    position:relative;
    top:0;
    width:100%;
    height:100%;
}
.container {
    padding:8px 20px 15px;
    display:flex;
    align-content:center;
}
.box {
    height:20%;
    margin:auto
}
h1, h2 {
    font-family:"Roboto Slab", Helvetica, Arial, sans-serif;
    margin:0;
    color:#000;
    font-weight:400;
    font-size:24px;
}
.social {
    margin-top:30px;
}
.social a {
    background-color:#F5F7FA;
    border-radius:35px;
    margin:0 3px;
    width:26px;
    height:26px;
    padding:16px;
    display:inline-block;
    -webkit-transition:background-color .6s ease;
    transition:background-color .6s ease;
}
.social a:hover, .social a:focus {
    outline:none;
    background-color:#FC6E51;
}
.icon {
    width:26px;
    height:26px;
}
.icon>path {
    fill:#111;
}
.links {
    color:#333;
    margin-top:30px;
}
.links a {
    display:inline-block;
    padding:0 5px;
    color:#000;
}
.links {
    color:#000;
}
.links a:hover {
    color:#000;
}
@media (max-width: 660px) {
    h1 {
        font-size:64px;
    }
    h2 {
        font-size:24px;
    }
}
@media (max-width: 460px) {
    .do-you-even-flexbox {
        display:none;
    }
    .container {
        margin:0 auto;
        margin-top:4%;
    }
    .links a {
        display:block;
    }
    h1 {
        font-size:48px;
    }
    h2 {
        font-size:16px;
    }
}
@media (max-width: 350px) {
    h1 {
        font-size:40px;
    }
}

I adjusted the heading tag sizes for demonstration purposes, but feel free to customize them according to your needs. You may also need to modify the box height for optimal centering. Check out the fiddle here

Answer №2

By placing the CSS file link before the style tag that sets h1 and h2 to have a font size of 96px, both headings end up having the same size. Simply move the line

<link rel="stylesheet" href="core.css">

below the <style> ... </style> section to differentiate the sizes of h1 and h2.

Answer №3

Your code has been modified in a fiddle, with unnecessary sections removed for clarity. You can easily add them back as needed to achieve your desired result.

JSFiddle Link

Modified HTML:

<div id="wrapper">
    <div id="container">
        <div id="centerText">
            <h1>Hi.</h1>
            <h1>This is the website for my independent study.</h1>
        </div>
    </div>
</div>

<footer>
    <div class="footer">
        <a href="#">Example Page.</a>
        <a href="#">Another Page.</a>
        <a href="#">Another page! :D</a>
        <a href="#">Blah blah</a>
    </div>
</footer>

Modified CSS:

html {
    background: url('http://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Alpamayo.jpg/1280px-Alpamayo.jpg') no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

h1 {
    padding: 0;
    margin: 0;
}

body {
    overflow-x: hidden;
}

footer {
    bottom: 0;
    position: fixed;
    width: 100%;
}
/* Rest of the CSS styles remain same */

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Web browsers are displaying code rather than the page itself

I recently hosted a .Net application on my Local IIS Server. Unfortunately, I neglected to handle error cases in MVC, opting instead to utilize the IIS .NET Error for page redirection to a specific page. When errors occur, the ASP.Net error page shows up ...

Effortlessly move and release Internet Explorer

Encountering drag and drop issues in Internet Explorer and Safari, while functioning correctly in Firefox 15 (untested on other versions). Dragging and dropping items between dropzones works in Safari, but sorting does not. In Internet Explorer, nothing wo ...

Develop a JavaScript function to generate a multiple selection form

Hey there! I have a question... Can I use JavaScript to create a multiple select form? Here's an example: <select name="item[]"> <option value="0">Option 1</option> <option value="1">Option 2</option> </select> &l ...

Unexpected UTF-8 Encoding Issue Found on Website Featuring French Content

I am currently developing a NodeJS Web App with 5 different home pages for 5 different languages. Everything seems to be working smoothly except for the French version, which is displaying broken characters as shown in this image: https://i.sstatic.net/B6f ...

At a specific point in the route, all CSS and JS links in Node.js vanish

Hey everyone, I'm facing a strange issue where the CSS styling and JS code are not loading when I access the route http://localhost:5000/posts/edit/<%=idofblog%>. As a result, my webpage looks very unattractive, and I'm not sure what's ...

Ensure that the child div fills the entire parent area when resizing the page

I am facing an issue with a container div that contains two elements. When I resize the window, one inner div moves under the other. However, my requirement is that when one div moves under another on resizing, the first div should occupy the full width o ...

`How can I relocate an IFRAME within a DIV?`

Users have the ability to freely edit HTML content, which is fine. However, I need to relocate all IFRAME elements into a DIV. I attempted the following code: $('#rolunk_content iframe').html ('<div>1' + $('#rolunk_content i ...

Retrieve properly formatted text from the editor.document using the VSCode API

I have been working on creating a personalized VSCode extension that can export the current selected file contents as a PDF. Although PrintCode exists, it does not fit my specific needs. The snippet of code I am currently using is: const editor = vscode.w ...

Rejuvenate a just-launched window.open starting from the about:blank

After receiving data from an ajax result, I am trying to open a pdf in a new window. However, the pdf viewer is only displayed if I manually resize the window (using manual hotspot resizing). How can I ensure that the contents display properly in its popu ...

What methods are available for eliminating special characters from submitted data?

Looking for a way to eliminate special characters from incoming data. Regular Expressions could be an option, but wondering if there are other methods available. Any suggestions on how to remove these special characters? ...

Tips for preventing words from being split between lines in an error message

I am encountering a problem in Angular 2 where error messages are being split onto two lines when there isn't enough space for the entire word. Check out this screenshot for reference Is it possible to prevent words from being divided across lines? ...

Discover and locate inactive element using Selenium in Python

Struggling with Automation: Unable to Locate Button on Website I'm currently facing a challenge in automating a website as I am unable to locate a specific button using Selenium. The problem lies in the fact that the button is disabled by default, ma ...

I'm attempting to make this script function correctly when an image is loaded

Can anyone help me figure out how to make this script function on image load instead of onclick? I came across the script on stackoverflow, but it was originally set up for onclick. I want it to work for onload or .load HTML====== <div id="contai ...

Find and choose the main div instead of the sub div in Robot Framework

I'm new to Robot Framework and working on test automation. I need to select the parent div, but the child div is always being clicked instead. Here's my current RF code: Wait and click element xpath: //div[contains(@class,'parent&a ...

Attach functions to elements added dynamically

I've been struggling with trying to attach functions to newly added elements using jQuery. Despite experimenting with various online examples, I haven't been able to get anything to work properly. My setup involves a form with an ID and a button. ...

Ways to modify the look of a checkbox when it is checked, without using extra elements

Is there a way to change the color of a checkbox, label, or any other element using only CSS based on the :checked selector? The HTML code is provided below and cannot be changed. <div class="chk"> <label> CheckMe <inpu ...

Using jQuery to select ASP control based on its dynamically generated Name attribute

When attempting to reference an ASP control in JavaScript, I initially used the generated ID but encountered issues. Surprisingly, referencing the control by its generated "Name" proved to be successful. ASP <asp:CheckBox ID="chkMyself" runat="server" ...

Is there a way for me to set a different color for the background below the svg and a separate color above the svg? I am hoping to achieve this unique design effect

I am looking to create a distinctive background design with different colors above and below an svg image. I have set a background image for the body element and will be using a grid layout with a transparent background color above it. My challenge is fin ...

Refresh the HTML content as soon as a modification is detected in the MySQL Database

Apologies if this sounds like a basic question, but I've hit a roadblock in my search for answers on the vast expanse of the internet... I am in the process of constructing a website that requires certain data on the page to be updated automatically. ...

Sliding right is done by activating the Switch Function, while sliding to the left can be achieved by deactivating it with the help

My objective is to create a functionality where flipping the switch button will cause it to slide automatically to the right, revealing a red background div. When switched off, it should return to its original position with a yellow background using Jquery ...