Use CSS to center-align the content

While attempting to create a Google search replica, I encountered an issue wherein the contents ended up superimposed rather than arranged at the center. Is there a method to organize them in a dropwise format?

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Search Engine</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="style.css">
        <style> 
            .center {
               height: 100vh;
               position: relative;
            } 

           .center > div {
                margin: 0;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-right: -50%;
                transform: translate(-50%, -50%) 
                }
         </style>

    </head>
    <body>
        <div class="container">
            <div class="links">
                <a href="googleImageSearch.html">Google Image Search</a>
                <a href="googleAdvancedSearch.html">Google Advanced Search</a>
            </div>
            <br>
            <div class="center">
                <div class="logo">
                    <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" alt="Google Logo" width="272" height="92">
                </div>
                <br>
                <div>
                    <form action="https://google.com/search">
                        <input type="text" name="q">
                        <div class="buttons">
                            <input type="submit" value="Google Search">
                            <input type="button" value="I'm Feeling Lucky">
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

Answer №1

I have a suggestion for you:

.center{
   margin:0 auto;
 }

By using this code, your div will be centered in the middle of the page. If you need more space between the top of the page and the div, simply adjust the margin settings. I noticed that you have nested div elements with different styling. In order to center all of your elements, it's best to place the margin:0 auto; inside the container class and remove the position relative. Alternatively, you can replace all existing code in the "center" class with my suggested code.

Answer №2

If you want to center content, consider using flexbox. The properties align-items and justify-content will help align your content in the center. You may need to make some small adjustments to other elements as well.

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  flex-direction: column;
  width: 100vw;
} 

form {
  diplay: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

input[type="text"] {
  border-radius: 2.5em;
  height: 2em;
  width: 500px;
  border: 1px solid black;
}

.buttons {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 1em;
}

.buttons > * {
  margin-right: 0.85em;
}

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 sets the Virtual DOM apart from the Shadow DOM?

During my exploration of Vue.js, I delved into the realm of shadow DOM to grasp the essence of a fundamental Vue.js Component. As I studied shadow DOM and its similarities to virtual DOM, I embarked on a quest for diverse information pertaining to both con ...

Exclusive carousel design for mobile users with bootstrap framework only

I need help with customizing the bootstrap4 carousel. Currently, I have a carousel that works on all devices, but I want it to only display on mobile devices. On desktops, I want the photos to be displayed side by side. I know I can create two container ...

Randomly positioning an image while the page is loading

I've been developing a portfolio website to showcase my design work, but I've encountered a minor issue. My goal is to have the images load in random positions and then make them draggable using Dragabilly. However, sometimes all the images end ...

Ways to distinguish jquery response apart from using $.post method?

Using jquery $.post method: $.post( 'includes/studiesAjax/addTryb.php', {inputVL: inputVL}, function(res){ $("#container").html(res); } ); The response contains a long html code. I am interested in extracting data from ...

How can I incorporate the "onClick()" function within an ajax call, while still utilizing the data returned in the success message?

After successfully making an Ajax call, I would like to implement an on-click function while still utilizing the original data retrieved. $.ajax({ URL: ......, type: 'GET', success: function (res) { var Ob ...

Create a way to allow users to download html2canvas with a customized filename

I have a div where I want to use html2canvas to save a PNG file of its content. The issue is that the name of the saved file is static in my code and does not change unless I manually edit it. Is there a way to dynamically set the filename based on user i ...

What is the best way to use JavaScript to fill in missing images with alternative HTML content?

As a provider of a service that helps users find the location of pictures, I face a challenge due to the fact that the pictures are stored on another internal site. Some users may not have access to this site as my service offers additional information. Th ...

Line up with miniature stature

How can I prevent the image in this row from getting a margin at the bottom when I resize the window? <div class="row-fluid"> <div class="span2"> <img src="https://s3.amazonaws.com/media.jetstrap.com/SLgRUEHSyGwQVfG4x6ed_curso_a ...

Situations where a C# method fails to execute upon the creation of runtime-generated input

After testing the following code, I achieved successful results: <asp:Content runat="server" ID="CNT1" ContentPlaceHolderID="CPH1"> <div id ="CNT" runat="server"> <input name="btnName" type="submit" runat="server" id="btnId" cli ...

Load elements beforehand without displaying them using a div

In order to efficiently manipulate my Elements using jQuery and other methods, I am exploring the idea of preloading them all first. One approach I have considered is creating a div with CSS display set to none, and placing all the elements I need for my w ...

The JSP page is unable to locate or access relative resources such as CSS stylesheets and images

I'm struggling to create a basic web page using JSP. The issue I'm facing is that no resources, such as images or CSS, are being found regardless of the URL I use. Here is my simple JSP code: <%@ page language="java" contentType="text/html; ...

What is the process of transforming a string into an angular binding?

I have a variable called message that contains the text "you are moving to {{output.number}}". I attempted to insert this into a div element using $("#message").html(message); However, it just displayed the entire string without replacing {{output.number ...

interactive navigation menu with countdown feature

Looking to create an HTML/jQuery dropdown menu, but encountering a problem. The goal is for the div to slide down when the mouse hovers over the "games" navigation button and only disappear after 5 seconds of the mouse being out, not instantly. Here's ...

Tips on eliminating the header section in SlickGrid?

Is there a way to remove the header row in SlickGrid using CSS? I know there isn't an API for it, but maybe some CSS tricks can do the job. Any suggestions? Check out this example: Calling all CSS experts! I'm looking for assistance in modifyin ...

The page only appears properly after clearing the cache

My floating list menu is behaving oddly in Firefox. It only displays correctly after clearing the cache, but as soon as I refresh the page, the menu breaks again. The links inside the list elements appear wider than the text, even though I haven't se ...

What are the best ways to format text conditionally depending on a form's status?

Is there a way to change the text color in an HTML form to be red when the form is invalid and green when it is valid using Angular 8? (HTML) <p class="status"> Form Status: {{ Form.status }} </p> (TS) Form = this.fb.group({ ...

What can I do to prevent my paragraph from extending down the entire page when adjusting the resolution?

I am facing an issue with my responsive website where the paragraph text appears as a long word tower when the screen size is reduced. How can I resolve this problem and reduce the space between the words? You can see the issue in the image here: https://i ...

What is the best way to center a text and a rectangle (containing some text) on a page using Bootstrap?

I'm in the process of creating a webpage using HTML and CSS that needs to look exactly like this image: https://i.sstatic.net/wYKZw.png Currently, I have a version of it on JSFiddle. The issue with the JSFiddle version is that the text and rectangle ...

Determine the initial left position of a div element in CSS prior to applying

Scenario : const display = document.querySelector('.display'); const container = document.querySelector('.outer-div'); document.addEventListener("click", (event) => { if (!event.target.closest("button")) return; if(event ...

The functionality of redirecting to the homepage after registration is not working as intended due to the issue of the uiD not being properly set in the session

I have created a registration form to register users and once the registration is complete, it should redirect to index.html (home page). There seems to be an issue where upon pressing the submit button, the page refreshes and the form resets without redi ...