Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Căn giữa thẻ div bằng CSS

Cách 1: Thuộc tính align (HTML cũ)

<html>
<head>
    <title>div in center without css</title>
</head>
<body>
<div>
    THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT.
</div>
<div align="center" style="border: 2px solid blue">
    THIS PARAGRAPH IS IN CENTER
</div>
</body>
</html>

Cách 2: CSS class (khuyến nghị)

<html>
<head>
    <title>div in center with css</title>
    <link href="../../Content/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
    THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT.
</div>
<div class="mydiv">
    THIS PARAGRAPH IS IN CENTER
</div>
</body>
</html>

style.css

body {
    background-color: Gray;
}
.mydiv {
    padding: 10px;
    margin: auto;
    width: 30%;
    border: 4px solid black;
}

Để chữ cũng căn giữa trong box, thêm text-align: center vào class .mydiv.