.u-cover { position: relative; display: block; margin-bottom: 10px; } .u-cover-1 { width: 140px; height: 140px; } <div class="u-cover u-cover-1"> <img src="/static/artist/1656403897.jpg"> <div style="flex: 1;"> <h1>歌手姓名</h1> <p>歌手简介</p> </div> </div> 怎么才能让图片在左边,姓名和简介在右边
时间: 2024-02-14 15:06:34 浏览: 22
可以使用CSS中的Flexbox布局来实现将图片放在左边,姓名和简介放在右边,具体的代码如下:
HTML代码:
```
<div class="u-cover u-cover-1">
<img src="/static/artist/1656403897.jpg">
<div class="info">
<h1>歌手姓名</h1>
<p>歌手简介</p>
</div>
</div>
```
CSS代码:
```
.u-cover {
display: flex;
align-items: center;
}
.u-cover img {
flex: 0 0 auto;
margin-right: 10px;
}
.info {
flex: 1;
}
```
这里我们使用了`display: flex`来将`.u-cover`元素设置为Flex容器,使用`align-items: center`来使Flex容器内的项目垂直居中对齐。接着,我们给图片设置了`flex: 0 0 auto`来指定其不可伸缩,并且不要将其缩小或放大。同时,我们也给图片设置了一个`margin-right: 10px`来与姓名和简介之间留出一些空隙。最后,我们给`.info`元素设置了`flex: 1`,使其占据剩余的空间,从而将姓名和简介放在右边。
相关问题
.list-container li .img-list { width: 100%; margin-top: 10px; /* position: relative; / } ul{ list-style-type: none ; } .img-list .img { / float: left; / height: 300px; margin-right: 1%; / margin-bottom: 10px; / display: block; / background-position: center; */ background-repeat: no-repeat; background-size: contain; }这段css代码输出的图片被压缩了,旁边有留白
这段 CSS 代码中的问题在于 `.img-list .img` 的 `background-size` 属性被设置为 `contain`,这会导致图片被缩放并留白。如果您希望图片充满容器而不留白,您可以将 `background-size` 属性设置为 `cover`,这会将图片缩放到完全覆盖容器,并保持其纵横比例。另外,如果您希望图片之间没有间隙,可以将 `.img-list .img` 的 `margin-right` 属性设置为 0。最终的 CSS 代码可能看起来像这样:
```
.list-container li .img-list {
width: 100%;
margin-top: 10px;
}
ul {
list-style-type: none;
}
.img-list .img {
height: 300px;
margin-right: 0;
margin-bottom: 10px;
display: block;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
```
优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
Here are some suggestions to optimize the code:
1. Use shorthand properties whenever possible. For example, instead of writing `background-size: 100% 100%;`, you can write `background-size: cover;`.
2. Consolidate similar styles into classes, instead of repeating them for every element. For example, you can create a class for the font family and apply it to all elements that use that font.
3. Remove unnecessary styles that are not being used or overwritten by other styles.
4. Use more specific selectors to target elements, instead of relying on the order of elements in the HTML. This will make the code more robust and easier to maintain.
5. Consider using a CSS preprocessor like Sass or Less, which can help you write cleaner and more organized code.
Here's an example of how the code could be optimized:
```
.bg {
width: 100%;
height: 100vh;
background: url('../../assets/img/info-bg.png') no-repeat center center / cover;
position: relative;
font-family: AlibabaPuHuiTiR;
}
.goBack {
position: absolute;
top: 34px;
right: 65px;
cursor: pointer;
color: #fff;
width: 181px;
padding: 15px 10px;
background: rgba(24, 31, 30, 0.52);
border: 1px solid #4a524e;
border-radius: 5px;
font-size: 18px;
font-family: AlibabaPuHuiTiR;
z-index: 111;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.home-left {
position: absolute;
top: 18%;
left: 40px;
width: 41%;
height: 76%;
font-size: 24px;
color: #fff;
}
.unit {
font-size: 24px;
color: #636363;
}
.home-left-title {
font-size: 24px;
color: #fff;
line-height: 36px;
}
.home-right {
position: absolute;
top: 18%;
right: 88px;
width: 46%;
height: 78%;
}
.model {
display: flex;
justify-content: center;
align-items: center;
height: 90%;
}
#threeContained {
width: 100%;
height: 100%;
}
.model-qk-img {
width: 82%;
height: 90%;
background: url('../../assets/img/howo.png') no-repeat center center / cover;
}
.model-zk-img {
width: 56%;
height: 90%;
background: url('../../assets/img/heavyT.png') no-repeat center center / cover;
}
.model-gj-img {
width: 82%;
height: 90%;
background: url('../../assets/img/transit.png') no-repeat center center / cover;
}
.car-online {
margin-bottom: 50px;
}
.day-data {
display: flex;
justify-content: space-between;
align-items: center;
height: 29%;
margin-left: 30px;
}
.day-val {
width: 40%;
}
.prefix {
display: inline-block;
width: 6px;
height: 14px;
background: #fff;
margin-right: 20px;
}
.zh-title {
margin-left: 30px;
padding-top: 30px;
font-size: 30px;
font-weight: 700;
text-align: left;
color: #fff;
line-height: 32px;
letter-spacing: 0.3px;
font-family: AlibabaPuHuiTiB;
}
.en-title {
margin-left: 30px;
font-size: 14px;
font-weight: 400;
text-align: left;
color: #fff;
line-height: 32px;
letter-spacing: -0.91px;
font-family: AlibabaPuHuiTiR;
}
.font-alibaba {
font-family: AlibabaPuHuiTiR;
}
.font-alibaba-bold {
font-family: AlibabaPuHuiTiB;
}
```