简体   繁体   English

内联CSS图像和段落内联

[英]Inline CSS Image and Paragraph inline

I am having trouble getting my div to layout an image and paragraph on the same line, I have search numerous topics on stackoverflow and on google but the no soultions are working. 我无法让我的div在同一行上布置图像和段落,我在stackoverflow和google上搜索了很多主题,但是没有任何解决方案。

Due to the web host I am having to use inline css to style the site. 由于网络托管,我不得不使用内联CSS来样式化网站。 (trust me I would much rather use css files but the ability to do so is not there, due to the chosen web host) (相信我,我宁愿使用css文件,但是由于选择了Web主机,所以没有使用css文件的能力)

Normally I would use bootstrap to achieve this but as noted that is not an option. 通常,我会使用引导程序来实现此目的,但是如上所述,这不是一个选择。

 <div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 30px solid #ffd800;"> <h1 style="color:black; margin-top: -27px; padding-left: 5px; font-weight:bold;"> This is a title </h1> <div id="image" style="margin-left: 10px;"> <img src="https://placehold.it/100x200" width="100" height="200" alt="Image"> </div> <div id="texts" style="float:right;"> <p style="color:black; margin-top: 10px;margin-left: 10px; margin-bottom: 10px; font-size: 120%;"> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. </div> </p> </div> 

What I'm getting: 我得到的是: 在此处输入图片说明

What I want: 我想要的是: 在此处输入图片说明

All you need to do is apply float: left to image 您需要做的就是应用float: left to image

 <div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 30px solid #ffd800;"> <h1 style="color:black; margin-top: -27px; padding-left: 5px; font-weight:bold;"> This is a title </h1> <div id="texts"> <p style="color:black; margin-top: 10px;margin-left: 10px; margin-bottom: 10px; font-size: 120%;"> <img src="http://placehold.it/100x200" width="100" height="200" alt="Image" style="float: left; margin-right: 10px;" /> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. </p> </div> </div> 

You can remove float: right from the #texts div and add float: left to the #image div: 您可以删除float: right#texts DIV并添加float: left#image DIV:

 <div style="display:inline-block; color:black; border:3px solid #ffd800; margin-bottom:25px; border-radius:10px; background-color: #FFF1AD; box-shadow:13px 15px 6px #2b2626; border-top:30px solid #ffd800;"> <h1 style="color:black; margin-top:-27px; padding-left:5px; font-weight:bold"> This is a title </h1> <div id="image" style="margin-left:10px; float:left"> <img src="https://placehold.it/100x200" width="100" height="200" alt="Image"> </div> <div id="texts"> <p style="color:black; margin-top:10px; margin-left:10px; margin-bottom:10px; font-size: 120%"> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. </p> </div> </div> 

1. Apply a maximum width to sibling element: 1.对同级元素应用最大宽度:

Apply a maximum width to the sibling element ( #texts ) of #image , eg: 的最大宽度施加到同级元素( #texts的) #image ,例如:

<div id="texts" style="float:right; max-width: 80%;">

Note: max-width property value given only as a demonstration. 注意: max-width属性值仅作为示例给出。 Adjust accordingly and as per requirements. 根据要求进行相应调整。

As it is now, it contains enough text/content to occupy the whole horizontal width. 现在,它包含足够的文本/内容来占据整个水平宽度。

2. Declare the display type of first nested element or float left 2.声明第一个嵌套元素的显示类型或向左浮动

Declare the first nested element ( #image ) as either an inline-block , eg: 将第一个嵌套元素( #image )声明为一个内联块 ,例如:

<div id="image" style="margin-left: 10px;display: inline-block;">

Or float it left , eg: 或将其left float ,例如:

<div id="image" style="margin-left: 10px;float: left;">

Note: this float may need to be cleared on the containing parent element. 注意:此浮点数可能需要在包含的父元素上清除。

As it is now, this element ( div ) is a block element by default, block elements will occupy the full available width of a containing element. 现在,此元素( div )默认是一个元素, 元素将占据包含元素的全部可用宽度。

Code Snippet Demonstration: 代码段演示:

 <div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 30px solid #ffd800;"> <h1 style="color:black; margin-top: -27px; padding-left: 5px; font-weight:bold;"> This is a title </h1> <div id="image" style="margin-left: 10px; display: inline-block;"> <img src="https://placehold.it/100x200" width="100" height="200" alt="Image"> </div> <div id="texts" style="float:right; max-width: 80%;"> <p style="color:black; margin-top: 10px;margin-left: 10px; margin-bottom: 10px; font-size: 120%;"> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's.</p> </div> 

Using inline isn't normally the best idea, but if that's what you need to do in this case then you can still use Flexbox: 使用内联通常不是最好的主意,但是如果在这种情况下需要这样做,则仍然可以使用Flexbox:

 <div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 30px solid #ffd800;"> <h1 style="color:black; margin-top: -27px; padding-left: 5px; font-weight:bold;">This is a title</h1> <div style="display: flex;"> <div id="image" style="margin-left: 10px;"> <img src="http://placehold.it/100x200" width="100" height="200" alt="Image"> </div> <div id="texts"> <p style="color:black; margin-top: 10px;margin-left: 10px; margin-bottom: 10px; font-size: 120%;"> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's.</p> </div> <div> </div> 

With this you need to add another div around the image and text content and set it to display: flex; 这样,您需要在图像和文本内容周围添加另一个div并将其设置为display: flex;

 <div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 50px solid #ffd800;"> <h1 style="color:black; margin-top: -40px; padding-left: 5px; font-weight:bold;"> This is a title </h1> <div id="image" style="margin-left: 10px; margin-right: 15px; float:left;"> <img src="https://placehold.it/100x200" width="100" height="200" alt="Image"> </div> <div id="texts" style=""> <p> Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's.Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's.Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. Here is my content, I am writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. </div> </p> </div> 

<div style="display:inline-block; color:black;border: 3px solid #ffd800; margin-bottom: 25px; border-radius: 10px; background-color: #FFF1AD; box-shadow: 13px 15px 6px #2b2626; border-top: 30px solid #ffd800;">
<h1 style="color:black; margin-top: -27px; padding-left: 5px; font-weight:bold;">
          This is a title
  </h1>

  <div id="image" style="margin-left: 10px;float:left;">
          <img src="http://placehold.it/100x200" width="100" height="200" alt="Image"> 
      </div>

      <div id="texts" style="float:right;width:90%;"> 
<p style="color:black; margin-top: 10px;margin-left: 10px; margin-bottom: 10px;  font-size: 120%;">

          Here is my content, I am  writting more than I need to show how the paragraph looks when it takes more than one line. Wouldn't it be nice if the text stayed together when an image was included. Here is some more text purely for testing nothing interesting to read here other than some typo's. 
    </div>
  </p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM