英文:
CSS Centered Width Styling in Vaadin Theme - Java
问题
I have a weird behavior that I can not explain to myself. Could anyone explain this to me?
我有一个奇怪的行为,我无法解释给自己。有人能解释一下吗?
I would like to have a Horizontal Layout of width:80% and it contains buttons and they should be also centered. But It comes out weird. I decided to include the HTML code with .css - Let me know if you need the Java/Vaadin code as well. But I think that .CSS should dictate everything here.
我想要一个宽度为80%的水平布局,其中包含按钮,它们也应该居中。但是它看起来很奇怪。我决定将HTML代码和.css包括在内 - 如果您还需要Java/Vaadin代码,请告诉我。但我认为.css应该在这里决定一切。
And what I want to achieve is this:
而我想要实现的是这样的:
With the .HTML code:
使用.HTML代码:
<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>
<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>
英文:
I have a weird behavior that I can not explain to myself. Could anyone explain this to me?
I would like to have a Horizontal Layout of width:80% and it contains buttons and they should be also centered. But It comes out weird. I decided to include the HTML code with .css - Let me know if you need the Java/Vaadin code as well. But I think that .CSS should dictate everything here.
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);
}
}
hopefully the thing works, when you try it at your end?
(this one has even some extra orange space):
And probably you will also need the pom.xml:
<?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>
<!-- Project from https://start.vaadin.com/ -->
<groupId>com.example.application</groupId>
<artifactId>flowcrmtutorial</artifactId>
<name>flowcrmtutorial</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>17</java.version>
<vaadin.version>24.1.4</vaadin.version>
<selenium.version>4.10.0</selenium.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
</parent>
<repositories>
<repository>
<id>Vaadin Directory</id>
<url>https://maven.vaadin.com/vaadin-addons</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<!-- Replace artifactId with vaadin-core to use only free components -->
<artifactId>vaadin</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.parttio</groupId>
<artifactId>line-awesome</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-testbench-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>spring-boot:run</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5719</jvmArguments>
<wait>500</wait>
<maxAttempts>240</maxAttempts>
</configuration>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-frontend</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And what I want to achieve is this:
With the .HTML code:
<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>
答案1
得分: 2
以下是已翻译的内容:
空白区域(或如浏览器检查器所示的紫色区域)在布局中的原因是其内容被左对齐(默认),而不是水平居中。
如文档所示(https://vaadin.com/docs/latest/components/horizontal-layout#horizontal-alignment),这是通过 setJustifyContentMode(JustifyContentMode.CENTER)
配置的。
(在 HorizontalLayout 和 VerticalLayout 中设置水平和垂直对齐的 API 是“正交的”,因为它遵循与 FlexBox 模型相同的方式。我的意思是,在 HorizontalLayout 中,垂直对齐是使用 setAlignItems
设置的,而水平对齐称为“justification”,并使用 setJustifyContentMode
设置,而在 VerticalLayout 中则相反。背后的原理是,“justification” 表示布局的“主”或“方向性”轴,而“alignment” 表示“横跨”方向轴,跨越方向轴。)
英文:
The reason for the empty (or purple, as shown by the browser's inspector) area in the layout is that its contents are left-aligned (the default) rather than horizontally centered.
As shown in the documentation
(https://vaadin.com/docs/latest/components/horizontal-layout#horizontal-alignment), this is configured with setJustifyContentMode(JustifyContentMode.CENTER)
.
(The API for setting horizontal and vertical alignment in HorizontalLayout and VerticalLayout are "orthogonal" to each other, because it follows the FlexBox model that works the same way. What I mean by this is that in HorizontalLayout, vertical alignment is set with setAlignItems
and the horizontal alignment is called "justification" and set with setJustifyContentMode
, whereas in VerticalLayout it's the other way around. The rationale behind this is that "justification" denotes the layout's "main" or "directional" axis, while "alignment" denotes the "cross axis" that goes "across" the directional axis.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论