css - 如何使用 flexbox 将两个按钮对齐到 'position:absolute' 的 div 的中心?

标签 css reactjs flexbox

到目前为止,这是我的代码的结果:

enter image description here

我实际上想要实现的是将两个按钮水平移动到中心。但目前它们是左对齐的。

这是我想要达到的结果:

enter image description here

我试过 alignItems 但没有效果。我不想使用任何 margin,因为容器大小可能会有所不同。

这是我的代码:

const H = "540px";
const W = "720px";

const ContentView = ({ width, height }) => (
  <div style={{ width, height, border: "1 solid red" }}>Hello! Alt View!</div>
);

const ControlView = ({ width, height, onReveal, onCopy }) => {
  const container = {
    width,
    height,
    position: "relative"
  };
  const controlsContainer = {
    width,
    height,
    position: "absolute",
    left: 0,
    top: 0,
    border: "5px solid green",
    display: "flex"
  };
  return (
    <div style={container}>
      <img style={{ width, height }} src="https://i.imgur.com/MQcuk3n.jpg" />
      <div style={controlsContainer}>
        <div
          style={{
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            justifyContent: "center"
          }}
        >
          <div>
            <button
              style={{
                width: "400px",
                height: "60px",
                minWidth: "200px",
                display: "block"
              }}
              onClick={onReveal}
            >
              Reveal
            </button>
          </div>
          <div>
            <button
              style={{ width: "400px", height: "60px", minWidth: "200px" }}
              onClick={onCopy}
            >
              Copy Meta
            </button>
          </div>
        </div>
      </div>
    </div>
  );
};

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { playing: false };
  }

  _onReveal() {
    this.setState({ playing: true });
  }

  _onCopy() {
    window.alert("Meta data copied");
  }

  renderAltView() {
    return <AltView />;
  }
  render() {
    const { width, height } = this.props;
    if (!this.state.playing) {
      return (
        <div>
          <h2>Controls</h2>
          <ControlView
            width={width}
            height={height}
            onReveal={() => this._onReveal()}
            onCopy={() => this._onCopy()}
          />
        </div>
      );
    }
    return (
      <div>
        <ContentView />
      </div>
    );
  }
}
ReactDOM.render(<App width={W} height={H} />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

不幸的是,我无法让它在 SO 代码片段中工作。工作版本位于 codepen https://codepen.io/kongakong/pen/EpRKPx

我可以使用什么 flex 指令来根据需要放置按钮?

最佳答案

您还需要使父 div 中的所有子元素居中。因此,在您的情况下,您需要将 flexbox 属性设置为 controlsContainer。

const controlsContainer = {
    width,
    height,
    position: 'absolute',
    left: 0,
    top: 0,
    border: '5px solid green',
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    justifyContent: 'center'
};

工作示例:

const H="540px";
const W="720px";

const ContentView = ({width, height}) => (
  <div style={{width, height, border: '1 solid red'}}>Hello! Alt View!</div>
)

const ControlView =  ({width, height, onReveal, onCopy}) => {
  const container = {
    width,
    height,
    position: 'relative',
  };
  const controlsContainer = {
    width,
    height,
    position: 'absolute',
    left: 0,
    top: 0,
    border: '5px solid green',
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    justifyContent: 'center'
  };
  return (
    <div style={container} >
      <img style={{width, height}} src="https://i.imgur.com/MQcuk3n.jpg" ></img>
      <div style={controlsContainer}>
        <div style={{display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center'}}>
          <div>
            <button style={{ width: '400px', height: '60px', minWidth: '200px', display:'block'}} 
               onClick={onReveal}>Reveal</button>
          </div>
          <div >
              <button style={{width: '400px', height: '60px', minWidth: '200px'}} 
                onClick={onCopy}>Copy Meta</button>
          </div>
        </div>
      </div>
    </div>
  )
}

class App extends React.Component{
  constructor(props){
   super(props);
   this.state = {playing: false};
  }
  
  _onReveal() {
    this.setState({playing: true})
  }
  
  _onCopy() {
    window.alert('Meta data copied')
  }
  
  renderAltView() {
    return (
      <AltView />
    )
  }
  render(){
    const { width, height } = this.props;
    if (!this.state.playing){
      return (
        <div>
          <h2>Controls</h2>
          <ControlView width={width} height={height} 
                onReveal={() => this._onReveal()}
                onCopy={() => this._onCopy()}
                />
          </div>);
    }
    return (<div><ContentView /></div>);
  }
}
ReactDOM.render(<App width={W} height={H}/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

关于css - 如何使用 flexbox 将两个按钮对齐到 'position:absolute' 的 div 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51672177/

上一篇:javascript - 如何将 NGBootstrap 工具提示附加到 Font Awesome 图标

下一篇:html - CSS - 在文本框旁边对齐图片的最佳方式?

相关文章:

html - 我可以在标题内正确对齐 CSS3 注释气泡吗?

javascript - 如何在快速工作区中转换简单的 react 组件

html - CSS:Flex 正在调整下一行的 div 大小

css - 在尊重图像宽高比和填充的同时设置复杂的网格布局

html - CSS3 Flex - 拉伸(stretch)宽度和高度

html - 将刻度线制作成列表标记

html - 如何绘制带有对 Angular 线和对 Angular 线文本的html表格?

html - 水平列表 - 列表项之间的元素符号等距/居中

javascript - 如何在我的 React 组件中重构此 ASYNC 调用以使其更具可读性?

javascript - 预填充受控组件