提问者:小点点

如何对标签和按钮进行内联和居中对齐


我正在自学前端开发,我正在尝试使用React和样式组件构建一个简单的MyAccount页面。我想创建一个简单的布局与标签和按钮像这样(但当然更漂亮):

布局

这是标签和按钮的当前情况

以下是myAccount代码:

import React, {useState} from 'react'
import styled, {createGlobalStyle} from 'styled-components'
import ClearIcon from '@material-ui/icons/Clear';
import css from '../components/css/MyAccount.css'

const GlobalStyle = createGlobalStyle`
@import url('https://fonts.googleapis.com/css?family=Raleway');
 body {
font-family: 'Raleway', sans-serif;
}`


function MyAccount(props) 
{

return(
    <>
       <GlobalStyle></GlobalStyle>
      <div className='MyAccount'>
        <div className='CloseButton1'> <ClearIcon fontSize='large' onClick={props.onClose}> 
 </ClearIcon> </div>
    <label>Email</label>
      <Button>Edit</Button>
    <label>Username</label>
        <Button>Edit</Button>
      </div>
    </>
)
}

const Button = styled.button`
color: #000;
background-color: #00FF60;
font-size: 1em;
border: 2px solid #00ff60;
border-radius: 8px;
height:10%;
width:10%;
margin: 0 auto;
padding: 0;
vertical-align: middle;

&:hover {
background-color: #16161b;
color: #f1f1f1;
border-color: #f1f1f1;
}
`;

export default MyAccount

标签在myAccount.css中有自己的css(现在它是空的,因为我不知道如何做我要做的事情),下面是它:

.MyAccount{
 display: flex;
 position: absolute;
 width: 800px;
 height: 400px;
 top:10%;
 justify-content: center;
 align-content: center;
 left:15%;
 border-radius: 12px;
 z-index: 1200;
 box-shadow: 0 0 0 9999px rgba(0,0,0,0.5);
 background: #0b0b0b;
 color: white;
 border: 1px solid green;
 }

.label{
 }


@media only screen and (max-width: 1200px) {

}

@media only screen and (max-width: 1680px) {
 .MyAccount{
   left: 25%;
}
}

@media only screen and (min-width: 1680px) {
 .MyAccount{
  left: 560px;
}
}

共1个答案

匿名用户

你需要把它想象成框,电子邮件框和用户框,框将是div,在每个div中你将有两个组件(标签和按钮)。div本身将有一个“display:block”属性,这将使它们占据整行,每个div中的组件需要是“display:inline”(当然是基本的)

所以:

<div>
    <label style="display:inline">Email</label>
    <button style="display:inline">make your thing</button>
</div>

这两种方式都会按你想要的显示它们,然而,你应该谷歌“flex-box”,这会让所有的事情都更有响应力,更容易;)