Creating a versatile button that adjusts seamlessly across different screen sizes, from smartphones to larger

I need assistance with a straightforward task - creating a button that remains centered at all times. What would be the most effective method to achieve this, and is it possible to do so easily?

Currently, I have managed to create a blue button; however, when I set both height and width to 100%, it does not occupy the entire screen for some unknown reason. You can view the image here. Why isn't the body element automatically filling up 100% of the page as expected?

Here's the code I have written so far:

In my Style.css file within an Angular project:

.body {
    height: 100%;
    width: 100%;
}

In app.component.ts:

<button class="button button5"></button>

In app.component.css:

.button {
    background-color: #0066ff;
    border: 1px solid blue;
    color: white;
    height: 100%;
    width: 100%;
}

.button5 {border-radius: 50%;}

I anticipated that my button would fill the entire page since its parent (the body) should inherently occupy 100% of the page.

Answer №1

Aligning a button in the center of a webpage is a breeze with the help of flex-box

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
.button {
  background-color: #838383;
  color: #fefefe;
  padding: 16px;
  /* ...if you really want it to be 100% of the screen un-comment below */
/*   height: 100%; 
  width: 100%; */
}
<button class="button"> I am a centered button </button>

Answer №2

Are you familiar with the straightforward (though not officially recommended) <center> tag? I find it gets the job done without issues.

If not, there's always the option of using margin-left: calc(50% - 50px); when dealing with an image that is 100px wide. You could also opt for text-align:center; or even align:center;.

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

Experiencing difficulties with setting the width of owl carousel

Here is the HTML code snippet: <div class="row"> <div class="col-lg-7 col-sm-6"> <div class="owl-carousel" id="homeCarousel"> <div class="item"> <img src="images/brela.jpg" alt="brela makarska"> </d ...

What is the most efficient way to retrieve an image from a database using its unique ID while incorporating CSS

My database table consists of the following columns: ID Nome Country Imagem 1 John USA images/######.jpg 2 Ana USA images/######.jpg 3 ## ## images/######.jpg How should I begin my code? I am looking to retrieve the image based on the ...

chosen problem concerning jquery

My objective is to transfer li elements from one container to another container when the user clicks on the li span. I have a choices container and a choices display, so when a user clicks on the li span with the id of "add" in the first container, the r ...

Determine whether a click event originated from within a child window

Currently, I am utilizing window.open to initiate a new window in javascript and my goal is to identify clicks made within the child window. Essentially, if a click event occurs in the child window, I aim to modify the parent window accordingly. I have a ...

Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home p ...

Conceal a Tag with CSS

Looking to conceal a label using CSS <p class="half_form "> <label for="property_bathrooms">Bathrooms (*only numbers)</label> <input type="text" id="property_bathrooms" size="40" class= ...

Tips on how to ensure the navigation menu is the same size as its child elements in the navigation menu

I'm currently designing a navigation menu that includes a child navigation menu. My goal is to have the size of the main navigation menu match that of the child navigation menu. To achieve this, I've created three hyperlink tags followed by a di ...

How can I utilize Bootstrap to organize multiple div layers on top of each other?

I am trying to accomplish the following: layered div The rows are displaying correctly, but I'm having trouble positioning the top layer div (the one with the class map) in the correct place. It keeps pushing some of the rows to the side. How can I a ...

Learn the step-by-step process of converting JSON functionality into HTML/text output using Golang

I need to modify the API responses to display as HTML or text, as the current script only outputs JSON format. func writeJSONResponse(w http.ResponseWriter, statusCode int, data interface{}) { w.Header().Set("Content-Type", "applicatio ...

What is the best way to create pages that showcase 10 posts each?

Currently, I am facing a challenge with displaying 100 posts using the Fetch API on one single page. I am looking for a way to create separate pages where each page would showcase only 10 posts. Below is my existing JavaScript code: fetch('htt ...

Can someone explain the purpose of `svg:not(:root) {overflow: hidden}` in simple

I am facing an issue with displaying my .svg file in my next-js project. It seems that the CSS code svg:not(:root) { overflow: hidden; } is causing the problem. Could someone explain what this particular CSS rule is for? After changing the code to svg:no ...

What is the process for storing form data into a text file?

Despite seeing similar questions in the past, I am determined to get to the bottom of why this code isn't functioning as expected. My goal is to input form data into a .txt file using a post request. While I lack extensive knowledge of PHP, I am pieci ...

Upon submission of the form, trigger an email to be sent and simultaneously open a

I need my form to simultaneously open a window and send the form data via email when submitted. <form action="" method="post" id="form" onsubmit="paymentfunc();return false;"> Submit button: <input type="submit" value="Purchase" class="btn" id= ...

Include a clickable jQuery icon in an HTML document for testing purposes

Is there a way to include an icon [+] at the end of this statement, so that when clicked it triggers an alert? <div id="catalog" style="width: 518px;"> <ul id="catalog111" style="left: -37px;top: -140px;width=500%;"> < ...

Getting the URL of a CSS background image in NodeJS and Express: A step-by-step guide

I've recently learned that using getComputedStyle in a Node environment is not possible due to it being a browser-specific behavior. Despite this, I am still interested in retrieving all background image URLs within Node and downloading them as shown ...

The CSS border on a div has inexplicably ceased functioning in Chrome, yet continues to work perfectly in

Just a moment ago, something strange happened while I was working on my memory game. After reopening the page, the borders around the divs stopped showing up when navigating with the keyboard arrows. (Click in the window to activate the arrow keys.) Normal ...

Switch the video source using JavaScript

I've been experimenting with creating a custom dropdown (ul li list) that allows users to select a different video to play, which is then loaded into the background of the page. Once the user clicks on the dropdown, it should switch to a different vi ...

Fullscreen overlay or pop-up display

I have a website with images and iframes displayed within a fancybox. Everything is functioning properly, but now the website's requirement is to have a fullscreen overlay like on the usatoday website. Take a look at for reference. When clicking on ...

Unveiling the method to retrieve XML tag based on the attribute of its parent element

This snippet is a segment of XML code: <?xml version="1.0"?> <menu> <pizzas> <pizza number="0"> <title>Tomato &amp; Cheese</title> <small>550</small> <medium></medium> <large> ...

How come this bootstrap text starts overlapping the image when I resize the screen?

In my Bootstrap 5.1 project with a fluid container, I have a row with two columns - one with a width of 3 and the other with a width of 9. Everything looks fine when the screen is fullscreen: https://example.com/image1.png However, when I resize the win ...