英文:
How can I achieve aligned word wrap with image inline?
问题
有可能创建响应式布局并正确设置自动换行以避免目前出现的不美观的换行吗?
图片是一个带有背景图像的 SPAN 元素,但我可以接受任何可以设置背景图像的元素。
唯一要保留的是 UL 和 LI 用于子类别。其他所有内容都可以使用适当的 HTML 元素。
.MainCategories {
list-style: none;
margin: 0;
padding: 0;
text-align: left;
}
.MainCategories>li {
display: inline-block;
max-width: 837px;
max-width: 650px;
vertical-align: top;
margin: 20px;
padding: 20px;
border-radius: 20px;
border: 1px solid rgba(167, 204, 63, 0.5);
}
.MainCategories>li>div.CatHead {
white-space: nowrap;
}
.MainCategories>li>div.CatHead>a.PhotoFrame .Photo {
display: inline-block;
width: 100px;
height: 75px;
margin-right: 20px;
border-radius: 10px;
padding: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border: 1px solid #ddd;
vertical-align: middle;
background-size: 100px 75px;
}
.MainCategories>li>div.CatHead>a.PhotoFrame:hover .Photo {
opacity: 0.5;
}
.MainCategories>li>div.CatHead>a.ParentCat {
vertical-align: middle;
font-size: xx-large;
color: #888;
white-space: normal;
}
.MainCategories>li>div.CatHead>a.ParentCat:hover {
text-decoration-thickness: 1px;
text-underline-offset: 4px;
}
.MainCategories>li>ul {
list-style: none;
margin-top: 20px;
}
.MainCategories>li>ul>li {
display: inline-block;
padding: 5px 0;
vertical-align: middle;
}
.MainCategories>li>ul>li:before {
display: inline-block;
padding: 0 10px;
vertical-align: bottom;
line-height: 60%;
content: "•";
font-size: xx-large;
color: #888;
}
.MainCategories>li>ul>li:first-child:before {
content: "";
padding: 0;
font-size: 0;
line-height: 0;
}
.MainCategories>li>ul>li>a {
font-size: large;
text-underline-offset: 3px;
}
<ul class="MainCategories">
<li>
<div class="CatHead">
<a class="PhotoFrame" href="/en-us/categories/food"><span class="Photo" style="background-image:url(https://thevegcat.com/img/86460ca1-f14b-44ec-a96a-1aa47a3e39e8/small)"></span></a>
<a class="ParentCat" href="/en-us/categories/food">Food (additional words to break)</a>
</div>
<ul>
<li><a href="/en-us/categories/food/sweets">Sweets</a></li>
<li><a href="/en-us/categories/food/sauces-spices">Sauces & Spices</a></li>
<li><a href="/en-us/categories/food/snacks-nibbles">Snacks & Nibbles</a></li>
<li><a href="/en-us/categories/food/ready-made-meals">Ready-made meals</a></li>
<li><a href="/en-us/categories/food/spreads">Spreads</a></li>
<li><a href="/en-us/categories/food/milk-cream-yogurt">Milk, cream, yogurt</a></li>
<li><a href="/en-us/categories/food/semi-prepared-meals-and-mixtures">Semi-prepared meals and mixtures</a></li>
<li><a href="/en-us/categories/food/bread-and-bakery-products">Bread and bakery products</a></li>
<li><a href="/en-us/categories/food/cereal-flakes">Cereal Flakes</a></li>
<li><a href="/en-us/categories/food/beverage">Beverage</a></li>
<li><a href="/en-us/categories/food/baby-food">Baby Food</a></li>
</ul>
</li>
</ul>
这是我现在拥有的:
这是我想要的:
英文:
Is it possible to create responsive layout and have word wrap properly set to avoid these ugly line breaks that I have now?
Photo is SPAN element with background image but I'd accept any element that could have background image.
The only thing I want to keep is UL and LIs for subcategories. Everything else could be any of suitable HTML elements.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.MainCategories {
list-style: none;
margin: 0;
padding: 0;
text-align: left;
}
.MainCategories>li {
display: inline-block;
max-width: 837px;
max-width: 650px;
vertical-align: top;
margin: 20px;
padding: 20px;
border-radius: 20px;
border: 1px solid rgba(167, 204, 63, 0.5);
}
.MainCategories>li>div.CatHead {
white-space: nowrap;
}
.MainCategories>li>div.CatHead>a.PhotoFrame .Photo {
display: inline-block;
width: 100px;
height: 75px;
margin-right: 20px;
border-radius: 10px;
padding: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border: 1px solid #ddd;
vertical-align: middle;
background-size: 100px 75px;
}
.MainCategories>li>div.CatHead>a.PhotoFrame:hover .Photo {
opacity: 0.5;
}
.MainCategories>li>div.CatHead>a.ParentCat {
vertical-align: middle;
font-size: xx-large;
color: #888;
white-space: normal;
}
.MainCategories>li>div.CatHead>a.ParentCat:hover {
text-decoration-thickness: 1px;
text-underline-offset: 4px;
}
.MainCategories>li>ul {
list-style: none;
margin-top: 20px;
}
.MainCategories>li>ul>li {
display: inline-block;
padding: 5px 0;
vertical-align: middle;
}
.MainCategories>li>ul>li:before {
display: inline-block;
padding: 0 10px;
vertical-align: bottom;
line-height: 60%;
content: "•";
font-size: xx-large;
color: #888;
}
.MainCategories>li>ul>li:first-child:before {
content: "";
padding: 0;
font-size: 0;
line-height: 0;
}
.MainCategories>li>ul>li>a {
font-size: large;
text-underline-offset: 3px;
}
<!-- language: lang-html -->
<ul class="MainCategories">
<li>
<div class="CatHead">
<a class="PhotoFrame" href="/en-us/categories/food"><span class="Photo" style="background-image:url(https://thevegcat.com/img/86460ca1-f14b-44ec-a96a-1aa47a3e39e8/small)"></span></a>
<a class="ParentCat" href="/en-us/categories/food">Food (additional words to break)</a>
</div>
<ul>
<li><a href="/en-us/categories/food/sweets">Sweets</a></li>
<li><a href="/en-us/categories/food/sauces-spices">Sauces &amp; Spices</a></li>
<li><a href="/en-us/categories/food/snacks-nibbles">Snacks &amp; Nibbles</a></li>
<li><a href="/en-us/categories/food/ready-made-meals">Ready-made meals</a></li>
<li><a href="/en-us/categories/food/spreads">Spreads</a></li>
<li><a href="/en-us/categories/food/milk-cream-yoghurt">Milk, cream, yoghurt</a></li>
<li><a href="/en-us/categories/food/semi-prepared-meals-and-mixtures">Semi-prepared meals and mixtures</a></li>
<li><a href="/en-us/categories/food/bread-and-bakery-products">Bread and bakery products</a></li>
<li><a href="/en-us/categories/food/cereal-flakes">Cereal Flakes</a></li>
<li><a href="/en-us/categories/food/beverage">Beverage</a></li>
<li><a href="/en-us/categories/food/baby-food">Baby Food</a>
</li>
</ul>
</li>
</ul>
<!-- end snippet -->
This is what I have now
This is what I want
答案1
得分: 1
这似乎是一行代码。
.MainCategories {
list-style: none;
margin: 0;
padding: 0;
text-align: left;
max-width: 300px; /* 仅演示 */
}
.MainCategories>li {
display: inline-block;
max-width: 837px;
max-width: 650px;
vertical-align: top;
margin: 20px;
padding: 20px;
border-radius: 20px;
border: 1px solid rgba(167, 204, 63, 0.5);
}
.MainCategories>li>div.CatHead {
display: flex; /* <---------------------------- 这里 */
}
.MainCategories>li>div.CatHead>a.PhotoFrame .Photo {
display: inline-block;
width: 100px;
height: 75px;
margin-right: 20px;
border-radius: 10px;
padding: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border: 1px solid #ddd;
vertical-align: middle;
background-size: 100px 75px;
}
.MainCategories>li>div.CatHead>a.PhotoFrame:hover .Photo {
opacity: 0.5;
}
.MainCategories>li>div.CatHead>a.ParentCat {
vertical-align: middle;
font-size: xx-large;
color: #888;
white-space: normal;
}
.MainCategories>li>div.CatHead>a.ParentCat:hover {
text-decoration-thickness: 1px;
text-underline-offset: 4px;
}
.MainCategories>li>ul {
list-style: none;
margin-top: 20px;
}
.MainCategories>li>ul>li {
display: inline-block;
padding: 5px 0;
vertical-align: middle;
}
.MainCategories>li>ul>li:before {
display: inline-block;
padding: 0 10px;
vertical-align: bottom;
line-height: 60%;
content: "•";
font-size: xx-large;
color: #888;
}
.MainCategories>li>ul>li:first-child:before {
content: "";
padding: 0;
font-size: 0;
line-height: 0;
}
.MainCategories>li>ul>li>a {
font-size: large;
text-underline-offset: 3px;
}
<ul class="MainCategories">
<li>
<div class="CatHead">
<a class="PhotoFrame" href="/en-us/categories/food"><span class="Photo" style="background-image:url(https://thevegcat.com/img/86460ca1-f14b-44ec-a96a-1aa47a3e39e8/small)"></span></a>
<a class="ParentCat" href="/en-us/categories/food">Food (additional words to break)</a>
</div>
<ul>
<li><a href="/en-us/categories/food/sweets">Sweets</a></li>
<li><a href="/en-us/categories/food/sauces-spices">Sauces & Spices</a></li>
<li><a href="/en-us/categories/food/snacks-nibbles">Snacks & Nibbles</a></li>
<li><a href="/en-us/categories/food/ready-made-meals">Ready-made meals</a></li>
<li><a href="/en-us/categories/food/spreads">Spreads</a></li>
<li><a href="/en-us/categories/food/milk-cream-yoghurt">Milk, cream, yoghurt</a></li>
<li><a href="/en-us/categories/food/semi-prepared-meals-and-mixtures">Semi-prepared meals and mixtures</a></li>
<li><a href="/en-us/categories/food/bread-and-bakery-products">Bread and bakery products</a></li>
<li><a href="/en-us/categories/food/cereal-flakes">Cereal Flakes</a></li>
<li><a href="/en-us/categories/food/beverage">Beverage</a></li>
<li><a href="/en-us/categories/food/baby-food">Baby Food</a></li>
</ul>
</li>
</ul>
英文:
Seems like this is a one-liner.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.MainCategories {
list-style: none;
margin: 0;
padding: 0;
text-align: left;
max-width: 300px; /* demo only */
}
.MainCategories>li {
display: inline-block;
max-width: 837px;
max-width: 650px;
vertical-align: top;
margin: 20px;
padding: 20px;
border-radius: 20px;
border: 1px solid rgba(167, 204, 63, 0.5);
}
.MainCategories>li>div.CatHead {
display: flex; /* <---------------------------- HERE */
}
.MainCategories>li>div.CatHead>a.PhotoFrame .Photo {
display: inline-block;
width: 100px;
height: 75px;
margin-right: 20px;
border-radius: 10px;
padding: 0;
overflow: hidden;
font-size: 0;
line-height: 0;
border: 1px solid #ddd;
vertical-align: middle;
background-size: 100px 75px;
}
.MainCategories>li>div.CatHead>a.PhotoFrame:hover .Photo {
opacity: 0.5;
}
.MainCategories>li>div.CatHead>a.ParentCat {
vertical-align: middle;
font-size: xx-large;
color: #888;
white-space: normal;
}
.MainCategories>li>div.CatHead>a.ParentCat:hover {
text-decoration-thickness: 1px;
text-underline-offset: 4px;
}
.MainCategories>li>ul {
list-style: none;
margin-top: 20px;
}
.MainCategories>li>ul>li {
display: inline-block;
padding: 5px 0;
vertical-align: middle;
}
.MainCategories>li>ul>li:before {
display: inline-block;
padding: 0 10px;
vertical-align: bottom;
line-height: 60%;
content: "•";
font-size: xx-large;
color: #888;
}
.MainCategories>li>ul>li:first-child:before {
content: "";
padding: 0;
font-size: 0;
line-height: 0;
}
.MainCategories>li>ul>li>a {
font-size: large;
text-underline-offset: 3px;
}
<!-- language: lang-html -->
<ul class="MainCategories">
<li>
<div class="CatHead">
<a class="PhotoFrame" href="/en-us/categories/food"><span class="Photo" style="background-image:url(https://thevegcat.com/img/86460ca1-f14b-44ec-a96a-1aa47a3e39e8/small)"></span></a>
<a class="ParentCat" href="/en-us/categories/food">Food (additional words to break)</a>
</div>
<ul>
<li><a href="/en-us/categories/food/sweets">Sweets</a></li>
<li><a href="/en-us/categories/food/sauces-spices">Sauces &amp; Spices</a></li>
<li><a href="/en-us/categories/food/snacks-nibbles">Snacks &amp; Nibbles</a></li>
<li><a href="/en-us/categories/food/ready-made-meals">Ready-made meals</a></li>
<li><a href="/en-us/categories/food/spreads">Spreads</a></li>
<li><a href="/en-us/categories/food/milk-cream-yoghurt">Milk, cream, yoghurt</a></li>
<li><a href="/en-us/categories/food/semi-prepared-meals-and-mixtures">Semi-prepared meals and mixtures</a></li>
<li><a href="/en-us/categories/food/bread-and-bakery-products">Bread and bakery products</a></li>
<li><a href="/en-us/categories/food/cereal-flakes">Cereal Flakes</a></li>
<li><a href="/en-us/categories/food/beverage">Beverage</a></li>
<li><a href="/en-us/categories/food/baby-food">Baby Food</a>
</li>
</ul>
</li>
</ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论