html - 列之间的CSS空间

标签 html css

我想在两列之间添加一个空格。我试过添加边距,但这会将第二列移动到第一列下方。我看到的任何示例都是为了引导。

这是我要复制的内容

enter image description here

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}
#heading {
    border: 1px double black;
}

.column {
  float: left;
  width: 50%;
  border: 1px solid black;
}

.row {
  padding: 5px;
  border: 1px solid black;
}
.row:after {
  content: "";
  display: table;
  clear: both;
  padding: 5px;
}

h2, h3 {
    text-align: center;
}
</style>
</head>
<body>

<h2 id="heading">Two Equal Columns</h2>

<div class="row">
  <div class="column">
    <iframe width="100%" src="https://www.youtube.com/embed/bjmYkPkjnVo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    <h3>Ingredients</h3>
    <ol>
      <li>1 pound dry spaghetti</li>
      <li>salt and freshly ground black pepper to taste</li>
      <li>6 cloves garlic, sliced thin</li>
      <li>1/2 cup olive oil (note: I prefer a regular olive oil for this recipe, as opposed to a strongly flavored extra virgin olive oil)</li>
      <li>1/4 teaspoon red pepper flakes, or to taste</li>
      <li>1/4 cup chopped Italian parsley</li>
      <li>1 cup finely grated Parmesan cheese (highly recommend Parmigiano-Reggiano)</li>
      <li>1 tablespoon of butter</li>
    </ol>
  </div>
  <div class="column">
    <iframe width="100%" src="https://www.youtube.com/embed/sAhUTzTJNaE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    <h3>Ingredients</h3>
    <ol>
      <li>500g of Pasta (cooked as per packet directions)</li>
      <li>30g of Butter</li>
      <li>4 Cloves of Minced Garlic (about 3-4 teaspoons)</li>
      <li>1 1/2 Cups of Cream (about 400ml)</li>
      <li>Salt and Pepper</li>
      <li>1/2 Cup of Freshly Grated Parmesan Cheese</li>
      <li>2 Teaspoons of Freshly Chopped Parsley (plus extra to garnish)</li>
    </ol>
  </div>
</div>

</body>
</html>

最佳答案

问题是您的两列都占用了 50% width。您需要缩小它,以便您可以使用新创建的 margin

在下文中,我将此width 更改为45%,然后添加了10% 的margin-right 到第一列(使用选择器 .column:first-of-type)。

请注意,具有额外边距的列仍然占据整个宽度,因为两列现在总计 90%,为 边距留下 10% 。如果要对此进行调整,只需确保 margin 等于 100% 减去列的总 width

* {
  box-sizing: border-box;
}

#heading {
  border: 1px double black;
}

.column {
  float: left;
  width: 45%;
  border: 1px solid black;
}

.column:first-of-type {
  margin-right: 10%;
}

.row {
  padding: 5px;
  border: 1px solid black;
}

.row:after {
  content: "";
  display: table;
  clear: both;
  padding: 5px;
}

h2,
h3 {
  text-align: center;
}
<body>
  <h2 id="heading">Two Equal Columns</h2>
  <div class="row">
    <div class="column">
      <iframe width="100%" src="https://www.youtube.com/embed/bjmYkPkjnVo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
      <h3>Ingredients</h3>
      <ol>
        <li>1 pound dry spaghetti</li>
        <li>salt and freshly ground black pepper to taste</li>
        <li>6 cloves garlic, sliced thin</li>
        <li>1/2 cup olive oil (note: I prefer a regular olive oil for this recipe, as opposed to a strongly flavored extra virgin olive oil)</li>
        <li>1/4 teaspoon red pepper flakes, or to taste</li>
        <li>1/4 cup chopped Italian parsley</li>
        <li>1 cup finely grated Parmesan cheese (highly recommend Parmigiano-Reggiano)</li>
        <li>1 tablespoon of butter</li>
      </ol>
    </div>
    <div class="column">
      <iframe width="100%" src="https://www.youtube.com/embed/sAhUTzTJNaE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
      <h3>Ingredients</h3>
      <ol>
        <li>500g of Pasta (cooked as per packet directions)</li>
        <li>30g of Butter</li>
        <li>4 Cloves of Minced Garlic (about 3-4 teaspoons)</li>
        <li>1 1/2 Cups of Cream (about 400ml)</li>
        <li>Salt and Pepper</li>
        <li>1/2 Cup of Freshly Grated Parmesan Cheese</li>
        <li>2 Teaspoons of Freshly Chopped Parsley (plus extra to garnish)</li>
      </ol>
    </div>
  </div>
</body>

关于html - 列之间的CSS空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317490/

相关文章:

php - 如何通过 HTML/CSS 页面创建 Wordpress 模板?

php - 如何从 HTML 表和数据库中删除查询?

javascript - HTML5 每隔几秒保存一次 Canvas 图像

javascript - 如何手动拖动和调整图像直到它适合css中的特定框

javascript - CSS 类别名

javascript - 在文本框上方添加div而不更改文本框的位置

javascript - 无法使用 Javascript 函数从 HTML 5 表中获取字符串

html - 您可以更改 View 中局部的特定 CSS 属性吗?

jquery - 将侧边栏高度设置为与滚动内容相等

html - 多页粘性页脚