Computer >> 컴퓨터 >  >> 프로그램 작성 >> CSS

CSS를 사용하여 요소 주위에 여백 설정

<시간/>

CSS margin 속성은 테두리 주변의 선택된 요소 주변의 여백 영역을 설정하는 데 사용됩니다. margin 속성은 margin-top, margin-right, margin-bottom 및 margin-left의 약어입니다.

구문

CSS margin 속성의 구문은 다음과 같습니다 -

Selector {
   margin: /*value*/
}

다음 예는 CSS 여백 속성을 보여줍니다 -

예시

<!DOCTYPE html>
<html>
<head>
<style>
body * {
   display: flex;
   float: left;
   margin: 10px 15px;
   border: thin solid;
   text-align: center;
   width: 40%;
   border-radius: 5%;
   box-shadow: 0 0 4px 1px grey;
}
p {
   font-family: "Sim Sun", monospace;
   background-color: lightcyan;
}
div {
   font-family: Impact, cursive;
   background-color: lightgreen;
}
</style>
</head>
<body>
<p>First demo text</p>
<div>A demo line goes like this..</div>
</body>
</html>

출력

이것은 다음 출력을 제공합니다 -

CSS를 사용하여 요소 주위에 여백 설정

예시

<!DOCTYPE html>
<html>
<head>
<style>
table, table * {
   margin: 5% 30%;
   border: 12px double red;
   padding: 0.5rem;
   border-radius: 30px;
}
td:nth-child(even) {
   background-color: lightblue;
}
td:nth-child(odd) {
   background-color: lightgreen;
}
</style>
</head>
<body>
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
</html>

출력

이것은 다음 출력을 제공합니다 -

CSS를 사용하여 요소 주위에 여백 설정