表格标签继承P标签对齐

时间:2015-04-21 20:14:46

标签: html html5

表单标记是否有办法从其正上方的p标记继承对齐?

示例:

<p style="text-align: center;">
<form...

默认情况下,它左对齐。但是我很好奇,如果有一种方法,因为img标签确实继承了对齐:

<p style="text-align: center;">
<img...

编辑:在这里小提琴 - &gt; https://jsfiddle.net/nyqyxczh/

2 个答案:

答案 0 :(得分:2)

formblock-level element,这意味着它不会关注text-align。如果您将form设置为display: inline-block,则可以将其设置为p标记的对齐方式。

虽然在文体上注意:我建议不要在form标记内放置p标记,因为它不是真正的文字段落,是吗?除非您尝试使用内嵌式form:&#34;您好,我的名字是_______,我的电子邮件是______。&#34;在这种情况下,是的,肯定是display: inline,但我可能会将p放在form内。

编辑2以澄清上述内容:

块级元素将像其他任何元素一样继承其父级的属性(请参阅下面的re:为什么form不会继承p样式),但块本身将不会根据text-align因为text-align定义了内联事物的对齐方式。也就是说,div text-center内的div本身不会居中,除非它被标记为inline-blockinline。但内部div的内容将集中在内部div内。

修改

看起来form不是p的有效孩子,因此将被浏览器强制退出p,从而不会继承任何样式(或识别选择器)比如p > form)。答案是,您无法form继承p中的内容,因为它拒绝在p内嵌套。

答案 1 :(得分:-2)

Inheritance concerns,

transferring information from a style sheet to the elements of a web document.

Declarations are defined into CSS rules, concern specific HTML elements, which are known as selectors. Elements that are embedded (inline) into selectors-elements for whom the style has been declared through a CSS rule automatically get the same style.

In our example below, the paragraph element <p> contains elements that inherit its style properties.

The CSS rule for the paragraph element <p> is:     


  p  {font family: arial, san-serif;  color: blue}


We then write the HTML code as below:


 <p>Inheritance concerns
   <b>the information transition
      <i>from a style sheet</i>           
   </b>
   to the elements of a web document 
</p>


which gives us







The inner elements b and i give their contents the style of parent element p. This means that they inherit the style from their outer element which is the tag <p>.

Most of the CSS properties are inherited from the parent to its child elements.

This fact gives us the possibility when we design our CSS style sheet to start from the top level elements in the hierarchic structure of HTML document and pass the style properties to the embedded elements (inner) avoiding to rewrite them for these elements.

Properties which are not inherited such as display, vertical-align and the border properties if we do not define a declaration through a style rule that sets values to them, they will take the default values of the browser when it reads the web document.