如何自定义对齐内容的列,但保持文本对齐?

时间:2019-01-22 15:06:50

标签: html css twitter-bootstrap

我正在尝试对齐列中的文本内容。我不知道如何将整个内容对齐到列的中心或右侧,并使文本内容保持对齐。

我在jsfiddle中添加了一个示例:https://jsfiddle.net/k58vr4nq/

当前的列看起来像这样:

|-----------------------------------------------|
|Lorem ipsum    |Lorem ipsum    |Lorem ipsum    |
|dolor sit      |dolor sit      |dolor sit      |
|amet           |amet           |amet           |
|consetetur     |consetetur     |consetetur     |
|sadipscing     |sadipscing     |sadipscing     |
|-----------------------------------------------|

我期望这样的事情:

|-----------------------------------------------|
|Lorem ipsum    |  Lorem ipsum  |    Lorem ipsum|
|dolor sit      |  dolor sit    |    dolor sit  |
|amet           |  amet         |    amet       |
|consetetur     |  consetetur   |    consetetur |
|sadipscing     |  sadipscing   |    sadipscing |
|-----------------------------------------------|

4 个答案:

答案 0 :(得分:1)

首先,为div中的每一列添加文本,这样它就会成为具有最宽文本行宽度的元素。然后,在父级(col元素)上使用flexbox并根据需要对齐div。

HTML:

<div class="container">
  <div class="row">
    <div class="col text-container left">
      <div>
        Lorem ipsum<br/>
        dolor sit<br/>
        amet<br/>
        consetetur<br/>
        sadipscing<br/>
      </div>
    </div>
    <div class="col text-container middle">
      <div>
        Lorem ipsum<br/>
        dolor sit<br/>
        amet<br/>
        consetetur<br/>
        sadipscing<br/>
      </div>
    </div>
    <div class="col text-container right">
      <div>
        Lorem ipsum<br/>
        dolor sit<br/>
        amet<br/>
        consetetur<br/>
        sadipscing<br/>
      </div>
    </div>
  </div>
</div>

和CSS:

.col {
  border: solid 1px #6c757d;
}

.text-container {
  display: flex;
}

.middle {
  justify-content: center;
}

.right {
  justify-content: flex-end;
}

以下是完整解决方案的内容:https://jsfiddle.net/7vetqkrz/

答案 1 :(得分:0)

为此,您必须将col内容包装在元素内:

<div class="col">
  <div class="content">
     Lorem ipsum<br/>
      dolor sit<br/>
      amet<br/>
      consetetur<br/>
      sadipscing<br/>
  </div>
</div>

然后,您必须使用一些content为该margin类设置样式:

  .content {
    margin-left: 1rem;
  }

提琴:https://jsfiddle.net/2ukyqo7m/4/

答案 2 :(得分:0)

使用display: inline-block将内容添加到块中,使其适合大小,在该块内对齐,然后对齐该块。

请参见下文(我不会内联地进行,它可以显示正在发生的事情):

Here's your fiddle updated

<div class="container">
    <div class="row">
        <div class="col">
            <div style="display: inline-block; text-align: left">
                Lorem ipsum<br/> dolor sit<br/> amet
                <br/> consetetur
                <br/> sadipscing
                <br/>
            </div>
        </div>
        <div class="col text-center">
             <div style="display: inline-block; text-align: left">
                Lorem ipsum<br/> dolor sit<br/> amet
                <br/> consetetur
                <br/> sadipscing
                <br/>
            </div>
        </div>
        <div class="col text-right">
             <div style="display: inline-block; text-align: left">
                Lorem ipsum<br/> dolor sit<br/> amet
                <br/> consetetur
                <br/> sadipscing
                <br/>
            </div>
        </div>
    </div>
</div>

答案 3 :(得分:0)

添加到CSS

final class CameraViewController: UIViewController {
   ...
   @IBAction func captureButtonDidTap(_ sender: UIButton) {
        guard isSessionRunning else { return }

        guard let photoOutput = self.photoOutput else { return }

        let settings = AVCapturePhotoSettings()
        let processor = CameraCaptureProcessor(photoOutput: photoOutput, settings: settings)
        self.capturedDelegate = processor

        processor.capturePhoto()
    }
   ...
}

final class CameraCaptureProcessor: NSObject, AVCapturePhotoCaptureDelegate {
    private var photoOutput: AVCapturePhotoOutput!
    private(set) var settings: AVCapturePhotoSettings!

    convenience init(photoOutput: AVCapturePhotoOutput, settings: AVCapturePhotoSettings) {
        self.init()
        self.photoOutput = photoOutput
        self.settings = settings
    }

    func capturePhoto() {
        photoOutput.capturePhoto(with: settings, delegate: self)
    }

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        if let error = error {
            print(error)
            return
        }
    }
}