0

我正在尝试将figure左对齐并将同一行上的文本向右对齐,这是我的 html:

<div class="container px-0">
  <div class="row">
    <h2 style="text-align:center;"><strong>Lorem Ipsum</strong></h2>
    <figure class="image image-style-align-left"><img src="https://c4.wallpaperflare.com/wallpaper/500/442/354/outrun-vaporwave-hd-wallpaper-preview.jpg"></figure>
    <p style="margin-left:0px">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

我尝试了不同的属性,例如:

figure { 
  float: right;
  text-align: right;    
}

figure仍然占据整个行宽。我该如何解决?

4

3 回答 3

1

您的figure块从用户代理样式表继承了 40px 的左边距(至少,这就是我在这里看到的)。要删除它,margin: 0请在您自己的样式表中应用,您应该会发现该图形已根据需要向左对齐。

要调试此类问题,浏览器的 Inspector 工具非常有用。右键单击您感兴趣的元素并选择“检查元素”,您将获得一系列信息,包括计算出的大小、边距和填充,以及正在使用的所有样式的详细视图在元素上。

===使用片段编辑===

<div class="container px-0">
  <div class="row">
    <h2 style="text-align:center;"><strong>Lorem Ipsum</strong></h2>
    <figure class="image image-style-align-left" style="margin:0;"><img src="https://c4.wallpaperflare.com/wallpaper/500/442/354/outrun-vaporwave-hd-wallpaper-preview.jpg"></figure>
    <p style="margin-left:0px">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

===编辑 2===

好的,所以您已经分享了有关您的问题的更多信息 - 特别是您无法更改 HTML,并且您希望文本块流向图像块的右侧。假设这是准确的,并且您只能对 CSS 文件进行更改,那么您能做的最好的事情是这样的:

.row .figure {
  float: left;
  width: 50%;
}

.row p {
  float: right;
  width: 50%;
}

如果你不喜欢使用浮动,而是使用不同的方案,比如 flexbox,原理是一样的。但是你会在选择器中遇到主要的精度问题:将样式应用于figureorp并且它会应用到任何地方,当然。因此,如果您无法将特定的类甚至id标签添加到相关块中,那么您可能就不走运了。

于 2021-10-19T10:50:53.623 回答
1

从片段中的 CSS 类中,我相信您使用的是 Bootstrap,所以我将 Boostrap 5.0.2 插入到片段中,并稍微修复了结构,max-width并对内部图像进行了调整figure

figure img {
  max-width: 100%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container px-0">
  <div class="row">
    <h2 style="text-align:center;"><strong>Lorem Ipsum</strong></h2>
    <div class="row">
      <div class="col-6">
        <figure class="image image-style-align-left"><img src="https://c4.wallpaperflare.com/wallpaper/500/442/354/outrun-vaporwave-hd-wallpaper-preview.jpg"></figure>
      </div>
      <div class="col-6">
        <p style="margin-left:0px">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
          up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
          Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
          a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
          form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
      </div>
    </div>

于 2021-10-19T10:55:00.130 回答
0

我会这样想 //添加这个样式

figure {  
display: inline;
float: left;
}
于 2021-10-19T11:04:12.623 回答