如何在移动设备上使WooCommerce标签水平对齐

huangapple go评论62阅读模式
英文:

How to make WooCommerce tabs align horizontally on mobile devices

问题

我们使用WooCommerce。在移动视图中,它显示描述和评论标签一个接一个地显示。我想要这两个标签在同一行上。
在我们的网站(移动视图)中显示如下

我们想要的输出如下

我们已经检查了WooCommerce插件的设置,但未能解决这个问题。

英文:

We use WooCommerce. It shows description & review tab one after another in mobile view.
I want both tabs in one line.
this is shown in our website(mobile view) tabs one after another

we want this output

We checked the WooCommerce plugin setting, but have not been able to solve the issue.

答案1

得分: 1

这是移动视图上标签的常见响应式行为。如果您的产品页面上有多个标签,这是理想的。

您可以使用CSS进行更改,我将为您提供两个示例。第一个示例是使用float,虽然在布局方面不是最佳选择,但可以完成工作。
第二个示例是使用flexbox,在布局方面更为正确,它是为了更容易设计灵活的响应式布局结构而创建的,而无需使用float或定位。

**请注意!**以下示例基于使用Storefront主题的WooCommerce演示商店:https://themes.woocommerce.com/storefront/。如果您使用下面的CSS代码,请记住这一点。它们可能无法在您自己的商店上运行,需要微调/调整。


示例1:以下代码是使用float的示例:

/* 在移动设备上使用float使产品页面上的描述标签水平对齐 */
@media screen and (max-width: 768px) {
    .woocommerce-tabs ul.tabs li {
        width: 50%;
        float: left;
    }
}

解释

下面的代码告诉浏览器仅在屏幕宽度最大为768像素的设备上应用样式,这是一个非常标准的数字,您可以根据需要进行更改。

@media screen and (max-width: 768px)

下面的代码定义了我们要定位的类,通过在浏览器中检查元素可以看到,具体外观可能因您使用的主题而异。下面的示例是从基本的WooCommerce/Storefront商店中获取的:

.woocommerce-tabs ul.tabs li /* li是每个标签 */

其余部分是样式本身。由于只有2个标签,您可以将每个标签的宽度设置为屏幕宽度的50%,然后使用float: left;属性指定每个标签应该浮动到其容器的左侧。

width: 50%;
float: left;

使用float时要注意

请注意,float 实际上不是用于布局的。因此,请不要开始使用浮动来布局整个网页。浮动设计的初衷是允许文本围绕图像流动(包裹文本和块级元素)。


示例2:以下代码是使用display: flex的示例:

/* 在移动设备上使用flex使产品页面上的描述标签水平对齐 */
@media screen and (max-width: 768px) {
    .woocommerce-tabs ul.tabs {
        display: flex;
    }
    .woocommerce-tabs ul.tabs li {
        -ms-flex: 1; /* IE 10 */
        flex: 1;
    }
}

解释

在下面的代码中,我们首先定位包含所有标签的父元素类,然后使用CSS Flexbox布局模块将其设置为flex容器。

.woocommerce-tabs ul.tabs {
    display: flex;
}

然后,我们定位每个标签,然后使用值设置为flex: 1;的flex属性,以使所有可伸缩项的长度相同,而不考虑其内容。

.woocommerce-tabs ul.tabs li {
    -ms-flex: 1; /* IE 10 */
    flex: 1;
}

Flexible Box布局模块使得更容易设计灵活的响应式布局结构,而无需使用float或定位。它是较新的CSS "工具"之一,这也意味着旧版浏览器可能不支持它。今天,大多数现代浏览器都支持flexbox。

英文:

This is common responsive behavior for the tabs on mobile view. It's ideal if you have multiple tabs on a product page.

You can change this with CSS, and I will give you two examples. The first one is the easiest using float, but not the best in terms of layout. But it will get the job done.
The second is correct one in terms of layout using flexbox which was made to make it easier to design flexible responsive layout structure without using float or positioning.

Please note! Below examples are based on a WooCommerce Demo store using the Storefront theme: https://themes.woocommerce.com/storefront/. This is important to remember if you use below CSS codes. They may not work on your own store, and will need tweaks /adjustments.


EXAMPLE 1: Below code is an example on how to use float:

/* Make description tabs on product page horizontally align on mobile device using float */
@media screen and (max-width: 768px) {
	.woocommerce-tabs ul.tabs li {
        width:50%;
		float: left;
	}
}

Explanation

Below line tells the browser to only apply the styling on devices with a screen width of maximum 768 pixels. A very standard number. You can change accordingly to your needs.

@media screen and (max-width: 768px)

Below line is defining which class we are targeting, by inspecting the element in the browser. It can look different depending on which theme you use. Below example is taken from a basic Woocommerce/Storefront store:

.woocommerce-tabs ul.tabs li /* li is each tab */

The rest is the styling itself. Since you have 2 tabs only, you can give each tab a width of 50% of the screen width. Then use float: left; property to specify that each tab should float to the left of its container

    width:50%;
	float: left;

Caution when using float

Please note that float is not really meant for layouts. So don't start using floats to lay out entire web pages. Floats by design were created to allow flowing text around an image (wrapping text and block level elements).


EXAMPLE 2 Below code is an example on how to use display: flex:

/* Make description tabs on product page horizontally align on mobile device using flex */
@media screen and (max-width: 768px) {
	.woocommerce-tabs ul.tabs {
		display: flex;
	}
	.woocommerce-tabs ul.tabs li {
        -ms-flex: 1; /* IE 10 */
		flex: 1;
	}
}

Explanation

In the line below, we are first targeting the parent element class, which contains all the tabs. We use the CSS Flexbox Layout Module and make it a flex container.

.woocommerce-tabs ul.tabs {
    display: flex;
}

We then target each tab, then use the flex property with the value set to flex: 1;, to let all the flexible items be the same length, regardless of its content.

	.woocommerce-tabs ul.tabs li {
        -ms-flex: 1; /* IE 10 */
		flex: 1;
	}

The Flexible Box Layout Module, makes it easier to design flexible responsive layout structure without using float or positioning. It is one of the newer css "tools" which also means that older browser might not support it. Today most modern browsers support flexbox.

huangapple
  • 本文由 发表于 2023年8月10日 18:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定