私は自分のサイト用にいくつかのCSSをデザインしています。私はのセクションを単一の背景のヘッダーと段落を持つ均一なテキストでフォーマット(ヘッダーと段落の間に適切な間隔を追加する)する必要があります。これは、margin
またはpadding
プロパティを使用して行うことができます。私はCSSのこれらの2つの違いを理解しています。また、これらのCSSプロパティのSOに関する利用に関する質問の多くがありますテキストの書式設定時に余白やパディングを使用する必要がありますか?
- Difference between margin and padding?
- When to use margin vs padding in CSS
- Margin or padding what is recommended?
しかし、疑いの色合いは、どのような性質の光があってはなりません適切なテキストフォーマットに使用されます。
私はテキスト要素(ヘッダや段落)の間の間隔を制御するためにmargin
またはpadding
を使用する必要がありますのは、私はこのようなテキストの書式を設定する必要があるとしましょうか? <p>
と<h..>
タグで使用することをお勧めしますか?一般的なプラクティスは何ですか?ここで
は、私は今のところ思い付いたものです:
/* tiny reset */
html { font-size: 10px; }
html, body { margin: 0; padding: 0; }
* { box-sizing: border-box; }
p {
font-family: 'Roboto', sans-serif;
font-size: 1.6rem;
line-height: 2.5rem;
margin: 0.6rem 0 2.0rem 0;
padding: 0 2px 4px 2px;
width: 100%; /* Opera needs this */
text-rendering: optimizeLegibility;
}
h1, h2 {
font-family: 'Roboto', sans-serif;
font-weight: 700;
text-align: center;
text-transform: uppercase;
padding: 0 2px 4px 2px;
text-rendering: optimizeLegibility;
}
h1 {
font-size: 4.0rem;
line-height: 4.0rem;
margin: 2.6rem 0 2.0rem 0;
}
h2 {
font-size: 3.2rem;
line-height: 3.2rem;
margin: 2.4rem 0 2.0rem 0;
}
.wrap {
margin: 20px;
padding: 20px;
-webkit-box-shadow: 0px 0px 12px 4px rgba(0,0,0,0.3);
-moz-box-shadow: 0px 0px 12px 4px rgba(0,0,0,0.3);
box-shadow: 0px 0px 12px 4px rgba(0,0,0,0.3);
}
<div class="wrap">
<p>The last declaration of a block doesn't need to be terminated by a semi-colon, though it is often considered good style to do it as it prevents forgetting to add it when extending the block with another declaration.</p>
<h2><strong>CSS statements</strong></h2>
<p>If style sheets could only apply a declaration to each element of a Web page, they would be pretty useless. The real goal is to apply different declarations to different parts of the document.</p>
<p>CSS allows this by associating conditions with declarations blocks. Each (valid) declaration block is preceded by a selector which is a condition selecting some elements of the page. The pair selector-declarations block is called a ruleset, or often simply a rule.</p>
</div> <!-- /wrap -->
あなたの質問には答えませんが、次の2つのリンクを調べると役立ちます。http://www.w3schools.com/css/css_margin.aspおよびhttp://www.w3schools.com/css/ css_padding.asp。私の経験から、1つの主な違いは、マージンを使用すると、autoを使用してコンテナ内の要素を水平方向に中央に配置できることです。ベストプラクティスの観点から、何を言いたいのか分からない。 – Erick
誰かがダウン投票した人と、近くに投票した人 - 私に質問を改善する方法を教えてください! – ezpresso
この質問には、1つの答えが1つしかないことは明らかです。もう少し考えてみてください。ある時点で、あなたはf。これらの段落の1つを背景色でハイライトしたい。今では、段落の幅をカバーするか、またはテキストの左右の白い「余白」に伸ばしたいのですか?そのような_actual_要件に基づいて、_specific_状況に最適なものを決定します。 – CBroe