Home  >  Article  >  Web Front-end  >  How to implement css ul without line breaks

How to implement css ul without line breaks

藏色散人
藏色散人Original
2021-04-12 14:35:312922browse

How to implement css ul without line wrapping: 1. Set a wide enough width, and then set the float attribute of li to left; 2. Use "white-space:nowrap;" to process whitespace characters in block elements and newline characters.

How to implement css ul without line breaks

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

How to make ul achieve the effect of horizontal arrangement without line wrapping

Option 1:

Set a wide enough width, and then set the float attribute of li to left. This means that the li elements are floated to the left. The code is as follows:

    ul{
    width:2000px;//设置足够的宽度
    overflow:hidden;
    white-space:nowrap;//处理块元素中的空白符和换行符的,这个属性保证图片不换行
    }
    li{
    list-style:none;
    float:left;//向左排列
    margin-left:15px;
    width:130px;
    }

Option 2:

  ul{
    display:block;
    overflow:hidden;
    white-space:nowrap;//处理块元素中的空白符和换行符的,这个属性保证图片不换行
    }
    li{
    list-style:none;
    display:inline-block;//使li对象显示为一行
    margin-left:15px;
    width:130px;
    }

Above we can see that both solutions use the white-space attribute. Without this attribute, the effect of no line breaks cannot be achieved. You can see from the css manual that this attribute is: how to handle whitespace within elements. When nowrap is selected, the text will not wrap and the text will continue on the same line until the 0c6dc11e160d3b678d68754cc175188a tag is encountered. But it can also be used for non-text elements.

There is also the display attribute. In option 2, if it is not set to inline-block, the effect of no line breaks cannot be achieved.

inline-block: Render the object as an inline object, but the content of the object is rendered as a block object. Adjacent inline objects will be rendered on the same line, allowing spaces.

Features of inline-block: The object is presented as an inline object, but the content of the object is presented as a block object. Adjacent inline objects will be rendered on the same line, allowing spaces. (To be precise, the element to which this attribute is applied is rendered as an inline object, and surrounding elements remain on the same line, but the width and height properties of the plot element can be set).

This solves the problem of ul being arranged horizontally without wrapping. It seems that it is necessary to be familiar with the characteristics of various attributes.

【Recommended learning: css video tutorial

The above is the detailed content of How to implement css ul without line breaks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn