Css div toàn bộ chiều rộng của màn hình

Để chia tỷ lệ theo chiều rộng màn hình, chúng ta có thể sử dụng tỷ lệ phần trăm. Tuy nhiên, điều quan trọng là chúng tôi bắt đầu với một chiều rộng xác định, không chỉ đưa ra tỷ lệ phần trăm và mong đợi mọi trình duyệt diễn giải nó theo cùng một cách

Nếu chúng ta biết rằng đây là một bố cục cố định, nghĩa là nó sẽ không được thêm động với nhiều div hơn, thì chúng ta có thể viết tất cả các quy tắc trong CSS

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/

Ví dụ đã nhận xét tạo ra sự khác biệt giữa các trình duyệt

1% + 31% + 2% + 31% + 2% + 31% + 1% = 99% // 1% safety margin

Đối với tình huống động trong đó các cột đang được thêm hoặc xóa, tập lệnh sẽ ghi đè lên quy tắc trên bằng độ rộng tỷ lệ được tính toán

Nếu chúng ta muốn có độ rộng khác nhau trên ba cột trên, chúng ta cần viết các quy tắc riêng

#wrapper>div {
    display: inline-block;
    margin: auto;
    text-align: left;
}
#wrapper>div:first-child {
    width: 15%;
}
#wrapper>div:nth-child[2] {
    width: 56%; /* or less */
}
#wrapper>div:last-child {
    width: 25%;
}

Div trung tâm được đặt thành chiều rộng còn lại có sẵn, một số an toàn, chẳng hạn như 56% trở xuống để cho phép mức an toàn. Tôi gọi chúng là 'giới hạn an toàn' nhưng những gì chúng tôi đang cố gắng ngăn chặn là bao bọc. Những gì có thể trông ổn trong trình duyệt webkit có thể không hoạt động trong IE hoặc Opera hoặc các trình duyệt của nhà cung cấp khác. Thử nghiệm là cách duy nhất để biết chắc chắn và thậm chí điều đó còn trở nên lộn xộn vì có quá nhiều phiên bản của mỗi phiên bản.

Bây giờ khi chúng ta chuyển sang chiều cao, đây là lúc mọi thứ có thể trở nên rắc rối và không an toàn khi cho rằng phương pháp tương tự sẽ hiệu quả, vì vậy tôi sẽ không. Điều này sẽ mất một số điều tra cẩn thận. Một giải pháp tốt hơn là sử dụng tập lệnh DOM để quản lý tỷ lệ chiều cao. Người học sẽ được để lại để điều tra này

Thuộc tính chiều rộng được sử dụng để lấp đầy không gian ngang còn lại của div bằng CSS. Bằng cách đặt chiều rộng thành 100%, nó sẽ lấy toàn bộ chiều rộng có sẵn của cha mẹ.  

cú pháp

width: 100%;

ví dụ 1. Ví dụ này sử dụng thuộc tính chiều rộng để lấp đầy không gian ngang. Nó đặt chiều rộng thành 100% để lấp đầy hoàn toàn.  

html




 

div { display: inline-block; width: 32%; /* of wrapper width */ margin: auto; text-align: left; } /* alternately */ /* #wrapper>div { float: left; width: 31%; /* of wrapper width */ margin: 0 1%; text-align: left; } */0>

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2____6____50>

2____53

2____55

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2____57____50____8

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2div { display: inline-block; width: 32%; /* of wrapper width */ margin: auto; text-align: left; } /* alternately */ /* #wrapper>div { float: left; width: 31%; /* of wrapper width */ margin: 0 1%; text-align: left; } */20

2____82

2>4

html1>6

html1>8

html11

 

3 >4

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2______6>7 >8

2____6>7

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
22

_______71____124

27>7>

2

2____6>7

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
27>7>

7____83>

 

7______709

đầu ra

 

 

ví dụ 2. Ví dụ này sử dụng thuộc tính chiều rộng để lấp đầy không gian ngang. Nó đặt chiều rộng thành 100% để lấp đầy hoàn toàn.  

html




10

div { float: left; width: 31%; /* of wrapper width */ margin: 0 1%; text-align: left; } */0>

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2____6____50>

_______52____522

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2____57____50____8

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
20

2____82

2>4

html1>6

html1____551

html1div { float: left; width: 31%; /* of wrapper width */ margin: 0 1%; text-align: left; } */02

2____82

2div { float: left; width: 31%; /* of wrapper width */ margin: 0 1%; text-align: left; } */2______6>7 >8

2____6>7

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
22

_______71____124

27>7>

2

2____6>7

2

2____6>7 7>

body {
    margin: 0;
    padding: 0;
    width: 100%; /* full window width */
    text-align: center;
}
#wrapper {
    width: 95%; /* of body width */
}
#wrapper>div {
    display: inline-block;
    width: 32%; /* of wrapper width */
    margin: auto;
    text-align: left;
}
/* alternately */
/*
#wrapper>div {
    float: left;
    width: 31%; /* of wrapper width */
    margin: 0 1%;
    text-align: left;
}
*/
2____57____87

đầu ra

 

CSS là nền tảng của các trang web, được sử dụng để phát triển trang web bằng cách tạo kiểu trang web và ứng dụng web. Bạn có thể học CSS từ đầu bằng cách làm theo Hướng dẫn CSS và Ví dụ về CSS này

Bạn sẽ tạo div có chiều rộng màn hình 100% bên trong vùng chứa như thế nào?

Lý do tại sao div toàn chiều rộng của bạn không kéo dài 100% ra màn hình là do "vùng chứa" chính của nó chỉ chiếm khoảng 80% màn hình. Nếu bạn muốn làm cho nó kéo dài 100% ra màn hình, bạn có thể cố định vị trí "full-width-div" hoặc sử dụng lớp "container-fluid" thay vì "container".

Chiều rộng của div có phải là 100% theo mặc định không?

Vì vậy, về bản chất, phần tử chứa bao nhiêu nội dung không quan trọng bởi vì chiều rộng của phần tử luôn là 100% , nghĩa là, . Hãy nghĩ về các phần tử như

Chủ Đề