Having difficulty aligning text in HTML5 elements

I'm experiencing some difficulty with the alignment of the text within the button element. When attempting to add padding to the top or bottom of the button in order to center the text, it ends up shifting the entire button instead. I have a feeling that my lack of understanding in positioning and display attributes is contributing to this issue.

<!DOCTYPE html>
<html>
<head>
    <title>WhiteGrid</title>
    <link type="text.css" rel="stylesheet" href= "stylesheet.css"/>
</head>
<body>
    <div>
        <div id="header"><img src="title.png"></div>
        <div id="navbar">
            <div class="button"><p>Home</p></div>
            <div class="button"><p>Gallery</p></div>
            <div class="button"><p>About</p></div>
            <div class="button"><p>Settings</p></div>

        </div>
        <div id="body"></div>
        <div id="footer"><p>Copyright&copy 2015 Hayden Shaw. All rights reserved.</p></div>
    </div>
</body>
</html>

CSS:

body {
    background-color: #C6C1C9;
}

#header {
    display: block;
    background-color: #856799;
    height: 60px;
    width: 100%;
    box-shadow: 0px 1px rgba(0,0,0,0.2);
}
#header > img {
    display: block;
    margin: auto;
}

#navbar {
    display: block;
    background-color: rgba(73,71,74,0.7);
    height: 40px;
    width: 100%;
    box-shadow: 0px 1px rgba(0,0,0,0.2);
}
#body {
    display: block;
    width: 100%;
    min-height:500px;
}
#footer {
    padding-top:24px;
    display: block;
    background-color: #7D7285;
    width: 100%;
    height: 60px;
    box-shadow: 0px 1px rgba(0,0,0,0.2);
}
#footer > p {
    position: relative;
    text-shadow: 0px -1px rgba(0,0,0,0.2);
    color: #A3A3A3;
    font-family: Verdana;
    font-size: 10px;
    text-align: center;

}
.button{
    margin:0px;
    text-shadow: 0px -1px rgba(0,0,0,0.2);
    font-family: Verdana;
    text-align: center;
    color: white;
    display: inline-block;
    height: 40px;
    width: 80px;
    background-color: rgba(73,71,74,0);
}
.button:hover{
    background-color: #353336;
    color: #856799;
}

Thank you for your help.

Answer №1

Instead of adding padding to the button, try applying it to the paragraph where the text is located. If you're struggling with formatting the text, consider using an image of the text for simplicity.

It appears that you're working on creating a menu bar. I recommend not using divs for this purpose. Utilizing either a table or list would be a more straightforward approach.

<table>
        <tr>
    <td>Home</td>
    </tr>
        </table>

    <ul>
    <li>Home</li>
</ul>

Styling will be much smoother with this method.

Answer №2

Avoid using positioning in this scenario, as it is unnecessary. Additionally, creating buttons using div tags is not recommended for various reasons.

Instead, consider utilizing the <button /> element or an anchor tag:

HTML:

<a href="#" class="paddedButton khaki noUnderline">Button</a>

CSS:

.paddedButton
{
    padding: 10px;
}

.khaki
{
    background-color: khaki;
}

.noUnderline
{
    text-decoration: none;
}

.noUnderline:hover
{
    text-decoration: underline;
}

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

What could be causing the nth-child selector to malfunction in CSS?

I am having an issue with my CSS code using the nth-child selector to assign border colors to different social media links within a list group. Can you help me find out what I'm doing wrong? .list-group-item:nth-child(1) { border-right: 3px ...

Two horizontal lines placed side by side on one line

Just getting started with CSS and HTML for a school project here. I currently have 2 vertical lines set up like this. <hr class="topline" width=58% size=3 NOSHADE> <hr class="verticalline" width=0.15% size=1000 NOSHADE> I've ...

Adjusting Image Size based on Window Width for Internet Explorer

Based on the provided code snippet <style> .x{ background: url('img.jpg') no-repeat; background-size: contain; height: 100%; } </style> <div class="x"></div> It is functioning correctly in Chrome and Firef ...

What are some recommendations for a great place to study CSS?

Can anyone recommend a great resource for mastering CSS? I'm looking to go beyond the basics of how rules are matched and understand how to integrate various skills to create more complex projects. While the W3School tutorials are helpful for understa ...

Ways to direct to a specific div upon clicking an anchor tag following a page reload?

<a href="#goto">Link 1</a> <div id="goto">DIV</div> Whenever I click on the anchor tag, my webpage reloads and displays a new div with the ID of goto. This div was previously hidden but is now visible at the bottom of the page. I ...

"Improve text visibility on your website with Google PageSpeed's 'Ensure text remains visible' feature, potentially saving you

Click here for the image reference showing the 0ms potential saving message I'm having trouble with a warning in Google PageSpeed and I've tried everything. This is the code I'm using to define the font-family: @font-face { font-family: ...

Basic AJAX request not functioning properly

I am encountering an issue with a very simple AJAX call on my localhost. I am using a Windows 10 Machine with XAMPP running. I have checked the packages, and it seems that the AJAX request is not being sent to handle.php. Can someone help me figure out wha ...

Swapping out an image using CSS

Previously, the most common method for "removing" text from elements was through text-indent: -9999px, however, it is now recommended to use text-indent: 100%; white-space: nowrap; overflow: hidden; This way, the browser does not need to create a huge 9 ...

Refining Content without the Need for a Submit Button in Django

To filter objects, I currently have to choose an option from the dropdown box and then click the submit button. However, I want the filtering process to happen immediately after selecting an alternative from the dropdown menu, without having to click the ...

Is it possible to change the text displayed when hovering over various images using just CSS and HTML?

I have created an image and text hover effect. There are four images and corresponding paragraphs for each image. When hovering over an image, the specific paragraph should replace the previous one using only HTML and CSS. Please note the following: The de ...

Having difficulty with printing a particular div

I need help with printing a specific div containing checkboxes using jQuery. The checkboxes are initially checked based on data from a database, but when I try to print the div, the checkboxes remain unchecked in the print view. Below is the code snippet ...

Include a hyperlink to a website that was created with the help of Photoshop

Is it feasible to incorporate a hyperlink into a website that was designed using Photoshop templates? In the past, creating templates in Photoshop and then adding links with tools like Dreamweaver was common practice. I believed that dynamically adding li ...

Adjust the width of columns by simply clicking in Angular

I am working on a page with two buttons split into two columns using col-md-6. I want to make it so that when one button is clicked, the first button becomes col-md-9 and the other becomes col-md-3, and vice versa. However, I am unsure of how to achieve t ...

What is causing my 100% height to be excessively large?

I'm facing an issue with my background containers, "background-overlay" and "background-gradient", as they are extending the height of the document beyond the actual content displayed. I am unsure of what could be causing this problem. It might be som ...

Adjust the placement of the div dynamically in response to the positioning of another div tag

I have an icon that resembles a chat bubble. If the position of the icon is not fixed, how can I display the chat bubble relative to the icon and adjust its position dynamically based on the icon's location? See image here #logowrap{ padding: 8px ...

The PHP loop is failing to show the file's content

My coding script is designed to extract specific parameters from a text file stored on the server. Here's an example of my source code: <?php $traw = file_get_contents("path/to/transactions.file"); $lns = explode("\n",$traw); $mode[&a ...

Complete and automatically submit a form in a view using AngularJS

I have developed a basic AngularJS application that is functioning smoothly. Currently, I am looking to populate certain fields and submit the form directly from the view without requiring any user input. Below, you'll find some simplified JavaScrip ...

Optimal Placement of CSS and index.html Files in ReactJS with Redux

Currently, my setup for the index.html file looks like this: <!doctype html> <html class="no-js" lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Pra ...

Insert a gap between the File Input button and the Placeholder text

https://i.sstatic.net/Gnm6u.png I'm currently attempting to create a separation between the "Choose File" button and the "No file chosen" area, similar to what is shown in the image above. Below is the HTML code I am using: <p class="mb-0 f- ...

Missing td data when using Beautiful Soup to parse HTML tables in Python

When attempting to extract a table using Beautiful Soup, I encounter an issue. from selenium import webdriver from bs4 import BeautifulSoup import pandas as pd import numpy as np import lxml import xlrd HorseNo = ["T421"] ...