根据屏幕尺寸使用 CSS 更改列宽
要根据屏幕尺寸更改列宽,请使用媒体查询。媒体查询适用于需要为平板电脑、手机、电脑等不同设备设置样式的情况。
首先,设置 div -
<div class="sample">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quod, maiores!</div>
设置初始宽度
要设置上述 div 的宽度,请在 CSS 中使用 width 属性 -
.sample {
width: 50%;
background-color: lightblue;
height: 200px;
font-size: 18px;
}
更改列宽
现在,要根据屏幕尺寸更改列宽,请将 width 设置为 100% -
.sample {
width: 100%;
}
屏幕尺寸小于 700 像素时,宽度为 100% -
@media only screen and (max-width: 700px) {
body {
margin: 0;
padding: 0;
}
.sample {
width: 100%;
}
}
示例
以下代码可根据屏幕尺寸更改列宽 -
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample {
width: 50%;
background-color: lightblue;
height: 200px;
font-size: 18px;
}
@media only screen and (max-width: 700px) {
body {
margin: 0;
padding: 0;
}
.sample {
width: 100%;
}
}
</style>
</head>
<body>
<h1>Changing column width based on screen size</h1>
<div class="sample">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quod, maiores!</div>
<p>Resize the browser window to 700px and below to see the above div width change to 100%</p>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP