将<span>标签设为圆形

这篇博客介绍了如何使用CSS样式来创建圆形元素,通过设置`border-radius`属性达到目的,并展示了蓝色和红色两种颜色的示例。此外,还提供了一个前端头像的实现方式,利用`text-align`, `line-height`和`border-radius`等属性,创建了一个带有天空蓝背景和白色文字的头像展示效果。

这个是当时工作时在网上找到的,不知道是哪篇博客了

<style>
	/*设置圆形*/
	.circle {
	    /*宽高相等*/
	    height: 50px;
	    width: 50px;
	    /*设置圆角边框,尺寸为宽高的一半,就是正圆*/
	    border-radius: 25px;
	    /*背景色*/
	    background-color: blue;
	    /*设置block才能显示出来*/
	    display: inline-block;
	}
	.blue {
	    background-color: blue;
	}
	.red {
	    background-color: red;
	}
</style>
<body>
    <span class="circle blue"></span>
    <span class="circle red"></span>
</body>

结果

前端需求设置头像

<span style="height: 50px; width: 50px; text-align: center; line-height: 50px; border-radius: 25px; background: skyblue; color: white; display: inline-block;">yu</span>

结果

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>移动端响应式图像选择器</title> <style> /* 全局样式重置 */ * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; padding: 15px; background-color: #f8f9fa; } /* 响应式计数器 */ #selectionCounter { position: sticky; top: 0; background: white; padding: 12px 15px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); z-index: 100; font-size: 16px; text-align: center; border-radius: 12px; margin-bottom: 15px; backdrop-filter: blur(10px); background-color: rgba(255, 255, 255, 0.85); } /* 响应式网格布局 - 移动设备优先 */ #imageGallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 15px; } /* 平板设备 */ @media (min-width: 600px) { #imageGallery { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 20px; } } /* 桌面设备 */ @media (min-width: 900px) { #imageGallery { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 25px; } } /* 用户卡片容器 */ .user-container { position: relative; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 12px rgba(0,0,0,0.08); transition: all 0.25s ease; background: white; aspect-ratio: 3/4; /* 保持统一高度比例 */ } .user-container:hover { transform: translateY(-3px); box-shadow: 0 6px 16px rgba(0,0,0,0.12); } /* 图像容器 */ .image-wrapper { position: relative; width: 100%; height: 0; padding-top: 140%; /* 保持原始比例 */ } /* 完整显示图片 */ .user-image { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: contain; background: #f0f2f5; } /* 移动端触摸优化 */ .user-container:active { transform: scale(0.98); } /* 自定义复选框 - 移动端友好 */ .custom-checkbox { position: absolute; top: 10px; right: 10px; z-index: 10; width: 28px; height: 28px; border-radius: 50%; background: rgba(255, 255, 255, 0.9); box-shadow: 0 2px 8px rgba(0,0,0,0.15); display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s; } .custom-checkbox::after { content: ""; width: 14px; height: 14px; border-radius: 50%; background: #4CAF50; opacity: 0; transform: scale(0.8); transition: all 0.2s; } input[type="checkbox"]:checked + .custom-checkbox::after { opacity: 1; transform: scale(1); } /* 用户标签 */ .user-label { position: absolute; bottom: 0; left: 0; right: 0; background: linear-gradient(transparent, rgba(0,0,0,0.7)); color: white; padding: 12px 8px 8px; font-size: 14px; font-weight: 500; text-align: center; } /* 选中状态 */ .user-container.selected { box-shadow: 0 0 0 3px #4CAF50; } /* 底部操作栏 - 移动端优化 */ #actionBar { position: fixed; bottom: 0; left: 0; right: 0; background: white; padding: 15px; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); display: flex; justify-content: space-around; z-index: 100; } .action-btn { padding: 12px 25px; border-radius: 50px; border: none; background: #4CAF50; color: white; font-weight: 600; font-size: 16px; cursor: pointer; box-shadow: 0 4px 6px rgba(0,0,0,0.1); transition: all 0.2s; } .action-btn:active { transform: translateY(2px); box-shadow: 0 2px 3px rgba(0,0,0,0.1); } #resetBtn { background: #f44336; } </style> </head> <body> <div id="selectionCounter">Selected: <span id="count">0</span>/2</div> <div id="imageGallery"></div> <div id="actionBar"> <button id="confirmBtn" class="action-btn">Confirm selection</button> <button id="resetBtn" class="action-btn">Reset</button> </div> <script> // 示例用户数据(不同比例图像) const users = [ { id: "ID30", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID53.jpg" }, { id: "ID27", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID2.jpg" }, { id: "ID45", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID3.jpg" }, { id: "ID12", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID4.jpg" }, { id: "ID88", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID5.jpg" }, { id: "ID76", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID6.jpg" }, { id: "ID15", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID7.jpg" }, { id: "ID22", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID8.jpg" } ]; const MAX_SELECTION = 2; // 创建图像卡片 function createImageCards() { const gallery = document.getElementById('imageGallery'); users.forEach(user => { // 创建卡片容器 const container = document.createElement('div'); container.className = 'user-container'; container.dataset.userId = user.id; // 图像包装容器 const imageWrapper = document.createElement('div'); imageWrapper.className = 'image-wrapper'; // 创建图像 const image = document.createElement('img'); image.className = 'user-image'; image.src = user.image; image.alt = `${user.id} 的照片`; image.loading = "lazy"; // 延迟加载优化 // 隐藏的复选框 const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.id = `checkbox-${user.id}`; checkbox.className = 'user-checkbox'; checkbox.style.display = 'none'; // 自定义可视化复选框 const customCheckbox = document.createElement('div'); customCheckbox.className = 'custom-checkbox'; customCheckbox.setAttribute('for', checkbox.id); // 用户ID标签 const label = document.createElement('div'); label.className = 'user-label'; label.textContent = user.id; // 组装组件 imageWrapper.appendChild(image); container.appendChild(checkbox); container.appendChild(customCheckbox); container.appendChild(imageWrapper); container.appendChild(label); gallery.appendChild(container); // 触摸/点击事件 container.addEventListener('click', function(e) { if (e.target !== checkbox) { const currentChecked = checkbox.checked; const selectedCount = document.querySelectorAll('.user-checkbox:checked').length; // 移动端触摸反馈 this.classList.add('touched'); setTimeout(() => this.classList.remove('touched'), 200); // 检查选择限制 if (!currentChecked && selectedCount >= MAX_SELECTION) { alert(`You can only select up to ${MAX_SELECTION} users`); return; } checkbox.checked = !currentChecked; updateSelectionState(checkbox); updateSelectionCount(); } }); }); } // 更新选择状态 function updateSelectionState(checkbox) { const container = checkbox.closest('.user-container'); if (checkbox.checked) { container.classList.add('selected'); } else { container.classList.remove('selected'); } } // 更新选择计数 function updateSelectionCount() { const selected = document.querySelectorAll('.user-checkbox:checked').length; document.getElementById('count').textContent = selected; } // 确认选择 document.getElementById('confirmBtn').addEventListener('click', () => { const selected = [...document.querySelectorAll('.user-checkbox:checked')] .map(cb => cb.id.replace('checkbox-', '')); if(selected.length === 0) { alert('Please select at least 1 user'); } else { alert(`Confirmed user selection: ${selected.join(', ')}`); // 实际应用中这里可以处理后续操作 } }); // 重置选择 document.getElementById('resetBtn').addEventListener('click', () => { document.querySelectorAll('.user-checkbox').forEach(cb => { cb.checked = false; updateSelectionState(cb); }); updateSelectionCount(); }); // 初始化 document.addEventListener('DOMContentLoaded', () => { createImageCards(); updateSelectionCount(); // 移动端手势检测 document.querySelectorAll('.user-container').forEach(container => { container.addEventListener('touchstart', function() { this.classList.add('touched'); }); container.addEventListener('touchend', function() { this.classList.remove('touched'); }); }); }); </script> </body> </html> 根據以上代碼,調整image checkbox 根據圖片的高度調整大小,不用每個一樣高度
最新发布
10-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值