Aligning three hyperlinks evenly to spread across the webpage

My professor provided the class with an image that may resemble something he could ask us to recreate on an upcoming exam. He suggested we try replicating it at home to study. I have most of it figured out, but there are three links positioned perfectly across the top of the page that I can't seem to get right.

I've spent hours tweaking the numbers, trying different widths and margins, like setting the link width to 33% and margin to 1.66%. However, my attempts led to unexpected outcomes where one of the links ended up below the others in the browser. It's becoming more confusing than helpful. The perfection of his image suggests a mathematical solution rather than arbitrary number adjustments.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="practiceTest.css">
    <title>Document</title>
</head>
<body>
    <div id="box1">
        <a href="https://www.google.com" id="googLink">Google</a>
        <a href="https://www.microsoft.com" id="microLink">Microsoft</a>
        <a href="https://www.yahoo.com" id="yahooLink">Yahoo</a>
    </div>

    <hr>
    <div id="box2">
        <h1 id="header">Guest Book</h1>

        <form action="post">
            First Name:
            <input type="text" id="firstName">
            Last Name: 
            <input type="text" id="lastName">
            College:
            <input type="checkbox" name="Business" id="business">
            Business
            <input type="checkbox" name="Education" id="education">
            Education
            <input type="checkbox" name="arts" id="arts">
            <br>
            Current Profession:
            <select>
                <option value="idk">unemployed</option>
                <option value="idk1">idk1</option>
            </select>
            <br>
            Comments:
            <br>
            <textarea name="comments" id="comments" cols="30" rows="2"></textarea>
            Would you like to share your comments with others?
            <br>
            <input type="radio" name="yes" id="yes">
            <input type="radio" name="no" id="no">
            <hr>
            <button type="submit">Send</button>
            <button type="reset">Clear</button>
        </form>
    </div>
    <hr>
    <div id="box3">
        Copyright - Made up Name
    </div>
</body>
</html>

CSS

#box1{
    background-color: black;
    align-self: center;
    padding: 20px;

}

a{
    align-content: center;
    width: 20%;
    margin: 0 5.33%;
    padding: 10px 0;
    color: azure;
    background-color: red;
    display: inline-block;
    text-decoration-line: none;
    color: white;
    text-align: center;

}

body{
    background-color: whitesmoke;
}

#box2{
    padding: 20px 20px;
    margin: auto 10px;
    background-color: white;
    color: darkred;
}

#box3{
    color: white;
    background-color: black;
    padding: 20px;
    text-align: center;
}

Answer №1

If you set a margin of 1.66% on the left and right sides, remember to subtract this from your width, resulting in 33.33% - 1.66% - 1.66%. This means your width should be 30.01% in this scenario. Make sure to use float:left instead of display:inline-block to avoid extra space around the element.

If you plan on adding padding inside the a tags, remember to apply box-sizing:border-box so that the padding is included within the element's width.

New Advice:

Alternatively, consider using display:flex to evenly distribute spacing. You can easily find guides on how to utilize display:flex (or the css flexbox layout model) through Google.

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

Drop and drag to complete the missing pieces

Here is a drag and drop fill in the blanks code I created. The challenge I'm facing is that I want users to be able to correct their mistakes by moving words to the right place after receiving points. <!DOCTYPE HTML> <html> <head&g ...

Guide to positioning elements in CSS

I'm struggling to get all the elements to align in a single row. I've tried using display: inline-block, but it's not working as expected. I'm working with DataTables and I want the button images to be aligned with the page number box. ...

Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs. Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote. I&ap ...

How to send the chosen option value from a select tag in Python Flask

My user registration form features a select tag, and I'm trying to assign the value of the selected option to a variable using the code below: registration.html <form method="POST" action="/register"> <div class="form-group"> ...

Accessing iframe's HTML using a server-side solution by circumventing the Cross-Domain Policy

Our current challenge involves accessing dynamically generated HTML elements using JavaScript, particularly when trying to extract image URLs from these elements. When the HTML elements are generated solely through JavaScript, extracting the URL is straigh ...

Convert JavaScript back into an HTML attribute

Is there a way to include a JavaScript code in an HTML attribute like shown below? <div class="xx" animation="yy" animate-duration="<script>Code goes here<script>" ...> </div> I'm struggling to figure it out, is there a solut ...

"The JQuery .validate method is not compatible with this property or method" - error message displayed

I seem to be facing a problem that I can't seem to solve. I keep getting an error message that says "Object doesn't support this property or method" while using jquery-1.10.4. The .validate method is not functioning properly for me. Any assistanc ...

Table for maximizing space within a cell (Internet Explorer 8)

I am attempting to make the "innerTable" fill up all available space within another table. The issue is with IE8 (no compatibility mode, IE8 browser mode, Doc mode IE8 Standards). The table does not expand to fit the containing TD. I tried enclosing the ta ...

Retrieving Data Attribute From Form Using Python Flask

Is there a way to retrieve data from a POST request in Flask that is not the standard text field value? I am looking for a solution that does not involve using JavaScript, but rather only Python. Let's consider a simplified example: I need to extrac ...

Scrape data from websites where the main URL remains constant but the information in the table varies

If you take a look at this link, you'll notice that when the next page is selected, the table on the website gets reloaded with new content without any change in the URL. Even after inspecting the developer tools > Network > XHR, it's difficult t ...

Is it possible to use getJSON to retrieve a value from an HTML tag ID that has already been displayed on a webpage? How about using json_decode

Is there a way to retrieve the value using the HTML tag ID after an HTML page has been rendered? For example, how can I access the date value using the td_date in my JavaScript function? Below is the code snippet that populates the data on the page: listS ...

The browser encountered an issue trying to load a resource from a directory with multiple nested levels

I've stumbled upon an unusual issue. Here is a snapshot from the inspector tools. The browser is unable to load certain resources even though they do exist. When I try to access the URL in another tab, the resource loads successfully. URLs in the i ...

Is there a quicker method than using document.getElementById('element-id').innerHTML to retrieve content?

Currently, I am developing a user-friendly step-by-step wizard for my website to gather information about custom orders. Utilizing JavaScript, I have been tasked with updating the content of each "page" using the document.getElementById('element-id&ap ...

What could be causing my fixed element to disappear when the webpage is not long enough?

OBJECTIVE: a div is { position:fixed; bottom:0% } it has a genuine height and width; the z-index is the highest in the document = so, it should always be visible! ISSUE: mobile browsers 'cut' the bottom end of the page when zooming out. ...

Is there a way to divide text in half while preserving the HTML structure?

I am in need of setting up an RSS feed where only a portion (or a specified number of characters) of the article's text is displayed, while keeping all HTML tags intact (such as images or YouTube videos). For instance, what should happen if the chara ...

Instructions for user to upload and play an MP4 file on their PC

For my web project that utilizes node.js, HTML, and JavaScript, I am looking to incorporate a video player element. I want users to have the ability to click a button and play an MP4 file directly on the webpage. How can I achieve this? I currently have s ...

The issue of flickering during the combination of slideToggle and animate in jQuery

I want to incorporate a hidden div that, when triggered by a button click, will smoothly reveal itself and "push" away part of another div without changing the overall size of the containing div. However, I have encountered an issue where the div below th ...

Validation of forms using Javascript

I currently have an HTML form with JavaScript validation. Instead of displaying error messages in a popup using the alert command, how can I show them next to the text boxes? Here is my current code: if (document.gfiDownloadForm.txtFirstName.value == &ap ...

Learn how to assign an ID to a React component in order to access it using the document.getElementById() method

After going through multiple inquiries, my understanding led me to believe that simply setting the id as shown below would suffice: <MyComponent id="myId"/> However, upon invoking document.getElementById() on the specified id, I receive a null resu ...

Refresh your webpage automatically without the need to manually refresh after clicking a button using AJAX in HTML and PHP!

One issue I'm facing is that the page doesn't auto-refresh, although it loads when I manually refresh it. Below you can find my HTML and AJAX code along with its database details. The Trigger Button <?php $data = mysqli_ ...