Styling the width of elements in a Vaadin theme with CSS centering and Java programming

Can someone help me with an odd issue I'm experiencing? I am trying to create a Horizontal Layout with buttons centered within 80% width, but it's not turning out as expected. Below is the HTML code along with the .css - I can provide the Java/Vaadin code if needed, although I believe the .CSS should be sufficient.

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

@Route("")
public class TestView extends VerticalLayout {

    public TestView() {
        Button test = new Button("Test");
        test.setWidth("30%");
        Button test1 = new Button("Test");
        test1.setWidth("30%");
        Button test2 = new Button("Test");
        test2.setWidth("30%");
        
        HorizontalLayout horizontalLayout = new HorizontalLayout(test, test1, test2);
        horizontalLayout.setAlignItems(Alignment.CENTER);
        horizontalLayout.setWidth("80%");
        add(horizontalLayout);
        
        // Center the view content on the page
        setSizeFull();
        // setJustifyContentMode(JustifyContentMode.CENTER);
        setAlignItems(Alignment.CENTER);
    }
}

https://i.sstatic.net/nO482.png

Please check if this works correctly on your end?

https://i.sstatic.net/zS5Cj.png

(Note: there is some extra orange space in this one):

https://i.sstatic.net/AFSyr.png

Additionally, the following pom.xml file may also be required:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   ...
   (Pom.xml contents omitted for brevity)
   ...
</project>

The desired layout outcome is shown here:

https://i.sstatic.net/Ci7w5.png

Using the following .HTML code snippet:

<html>
<head>
</head>
<body>
<div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
 <div style="width: 80%;">
     <div style="display: flex; justify-content: center;">
         <button style="width: 30%;">Button 1</button>
         <button style="width: 30%;">Button 2</button>
         <button style="width: 30%;">Button 3</button>
     </div>
 </div>
</div>
</body>
</html>

Answer №1

The reason for the empty (or purple, as indicated by the browser's inspector) space in the design is due to its content being aligned to the left (the default) instead of being centered horizontally.

According to the documentation (), this can be adjusted using

setJustifyContentMode(JustifyContentMode.CENTER)
.

(The method for setting horizontal and vertical alignment in HorizontalLayout and VerticalLayout are deliberately different from each other, following the FlexBox model. In HorizontalLayout, vertical alignment is controlled with setAlignItems, while horizontal alignment is referred to as "justification" and modified with setJustifyContentMode. Conversely, in VerticalLayout, the roles are reversed. This distinction is made based on the understanding that "justification" pertains to the layout's primary or directional axis, whereas "alignment" relates to the cross axis that intersects the directional axis.)

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

The HTML button functions correctly on Windows browsers but experiences issues on Android devices

Explore some delicious minty black tea blends here! After clicking on the "Add to Cart" buttons for various quantities, a confirmation message pops up indicating that the product has been successfully added to the cart. An issue arises when attempting to ...

What is the best approach for testing an HTML element that does not have distinct attributes?

Currently, I am utilizing Coded UI Test to conduct tests on a web application. In my workflow, I rely on a class called Locator to store specific information necessary for CUIT to locate a control. When it comes to interacting with a control, a page objec ...

Combining labels over multiple lines using lower division

I have a setup where I am using two DIVs, one positioned below the other. The top DIV contains a Multiline Label that dynamically generates data. However, when the data in the label exceeds a certain length, it does not create space and instead overlaps w ...

Arrays store values by reference, so when comparing their pointers, they should point to the same values in order to evaluate as true

In my understanding, arrays and objects act as pointers, pointing to a specific memory location. In the given example, when var a is updated, b should also be updated since it points to the same memory location as a. The expected final result would be tru ...

Unable to show the input's value

Need help in taking user input to display calculated values //html <div class="empty"> <h5> Enter Empty Seats </h5> <ion-item> <ion-input placeholder="Enter Number of Empties.." type="number" name="emptySeats" [( ...

A Java application designed to organize arrays and then return them in sorted order

Here is the code I have been working on: import java.util.Scanner; public class ReadStrings{ public static String main(String[] args) { Scanner in = new Scanner(new File("input3.txt")); String [] array = new String[100]; int nextSpot = 0; while( in.hasNe ...

You are unable to choose more than one file at a time for

Currently, I am in the process of developing an application using HTML, CSS, and JS by following a tutorial. The main purpose of this app is to merge multiple PDF files together. However, I have encountered an issue where I am unable to select multiple fil ...

Swapping mouse cursor using JavaScript

Currently, I am working on a painting application in JavaScript that utilizes the Canvas Object. I would like to customize the mouse cursor when it hovers over the Canvas object. Can anyone advise me on how to accomplish this? ...

Uploading Images Using the Spring REST Framework

Seeking guidance on uploading an image using RestTemplate client, sending a POST request to a Spring-based REST server, and saving it on the server. Could someone assist me with how to achieve this setup for both my Spring client and server? Thank you. Li ...

JavaScript Cursor-Based Auto Typing Tool

I want to develop a straightforward Javascript auto writer within an HTML page. Below is the code I have written: <html> <head> <style> #type { display: inline-block; } #cur { display: inline-block; ...

The issue with the left border color not functioning in JavaScript

Attempting to alter the border-left-color property using this script is resulting in a Uncaught SyntaxError: Unexpected token -. Is border-left-color actually supported? Javascript function logoChange() { var description = new Array (); description[0] ...

What is the best way to set up a side-opening feature in the UI design?

I am having trouble with the Amoclan shape transitioning to the next side. I need the transition to occur exactly as specified in the documentation, but for some reason it is still moving on to the next side. <html> <head> <script src=" ...

utilizing hibernate to define a composite key mapping

I'm currently working on establishing a one-to-many relationship using Hibernate: Within my project, I have a profile object that consists of various properties: Profile.java public class Profile { private String profileId; private String name ...

Updating certain fields in AngularJS with fresh data

I am facing a challenge with the following code snippet: <div ng-controller="postsController"> <div id = "post_id_{{post.postid}}" class = "post" ng-repeat="post in posts"> ...... <div class="comments"> < ...

Translating a string into JSON format on an Android device

client.post("http://10.0.2.2/project/process/selectalluser.php", new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { Integer contacts = controller.getContactsCount(); Log.d("Reading c ...

What strategies can be implemented to enhance accessibility for custom table behavior?

Our team is currently working on a customized web application for a client's specific needs regarding data tables. The requested functionality includes the ability to sort table columns by clicking on the header, which has already been successfully im ...

Safari browser experiencing issues with JQuery Slider functionality

I am facing an issue with a slider that works perfectly in all browsers except Safari. I have been struggling to figure out the root cause of this problem. You can view the fiddler code here: http://jsfiddle.net/sjramsay/UyvvL/ It appears that when the co ...

Set the background-color of each <td> element to be equal to a value in the array, with each group of three elements having the same

I am trying to color every <td> element in a row of three columns with the same color using the following code: for (var i = 0; itr < $("td").length; i++) { $("td").eq(i).css("background-color", Colors[i]); } However, this code colors each i ...

Guide to filling out select dropdowns in HTML with information from a database

I'm new to PHP and HTML, so please be patient with me. I've been attempting to populate a select box using data from a MySQL database, but all I see is an empty textbox. Here's my current code: <select name="cargo"> <?php ...

VueJS encountered an issue while displaying the image

I am facing an issue with VueJS while trying to populate my index.html with images fetched from the iTunes API. The API returns a URL to an image, but I am struggling to render them on my webpage. The 'item' variable holds the object data. Initi ...