Home

Remove line break from div element

I am currently working on a page using the CSS grid layouts, however I ran into a problem with div elements in which they automatically insert a line break into the output above and below the text. Is there any way to easily remove these with CSS? I tried changing the div element to span , but nothing changed. I also set the div elements to display: inline; but that didn’t work either.

html < background-color: black; >.grid-container < display: grid; width: 1200px; height: 600px; grid-template-columns: 300px 1fr 1fr; grid-template-rows: 110px 200fr 200fr 80px; grid-gap: 1rem; background-color: #2196F3; padding: 10px; margin: auto; >.grid-item < background-color: rgba(255, 255, 255, 0.8); padding: 20px; display: inline-block; >.grid-heading < grid-row: 1 / 2; grid-column: 1 / 4; font-size: 40px; padding: 5px; >.grid-sidebar < grid-row: 2 / 4; grid-column: 1 / 2; >.grid-content1 < grid-row: 2 / 3; grid-column: 2 / 4; >.grid-content2 < grid-row: 3 / 4; grid-column: 2 / 4; >.grid-footer < grid-row: 4 / 5; grid-column: 1 / 4; >
    


Heading

2

3
4
5

«a problem with div elements in which they automatically insert a line break into the output above and below the text» — that’s not a line break, that’s the default margin-top and -bottom the h1 element gets from the browser stylesheet. Overwrite/null those, if you don’t like the effect.

Источник

No newline after div?

Is there a way to not have a newline inserted before a div without using float: left on the previous element? Maybe some tag on the div that will just put it to the right?

Читайте также:  Javascript console log message

9 Answers 9

There is no newline, just the div is a block element.

You can make the div inline by adding display: inline , which may be what you want.

Have you considered using span instead of div ? It is the in-line version of div .

Quoting Mr Initial Man from here:

If you absoultely must use a , you can set

in your stylesheet.

Of course, that essentially makes the a .

Use span instead of div. Since span is inline element while div is the block element. So div is always going to add into the new line as it covers the entire width while span doesn’t.

This works like magic, use it in the CSS file on the div you want to have on the new line:

Or declare it in the html:

This works the best, in my case, I tried it all

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

CSS No Line Break

A Cascading Style Sheet (CSS) offers a property to display all the HTML items without any break, or you can say that all the HTML contents are displayed on a single line. HTML and CSS both contribute to accomplishing this phenomenon on the web page. The basic advantage of doing this is that the HTML contents will take less space when they are displayed. It also makes the contents aligned.

There is no any built-in feature in CSS like the other effects, for example the text decoration, list style type, etc. But we need to apply this no-line break effect by using a display property that’s valued as an inline-block property.

To implement the CSS no-line break property effect, you need to know the basics of HTML and CSS. We will use a text editor for the code and a browser to implement the code in the editor. Let us elaborate on the CSS property of inline-block and its effects on the “no line break” phenomenon.

CSS Inline-Block Property

This property is used to display an item in the inline-block container. In this approach, the block of elements is converted into the inline element. By doing this, there is the prevention of the line break. The syntax of the inline-block property is displayed in the following:

We will implement this property in two ways: through the list and the div.

Example 1: No Line Break in Lists

In the first example, we will apply the no-line break concept on the list. A list is an HTML content where the text is represented vertically along with the bullets, either the list is ordered or unordered. When we talk about lists, we always come up with the view of having each item of the list in a separate line with a line break. But sometimes, according to the scenario, we want to display the list items without bullets in a single line. This is done by displaying the list items using the CSS with the “no line break” property. Let us start the HTML code with the body section.

We used two headings of the text through the heading tags – and . After that, the unordered list is created with 5 items in each row.

The same syntax is followed for the 5 items to be entered in each row.

We use the “li” tag directly in the CSS to apply the inline-block effect on all the list items and prevent them from displaying on a separate line. Furthermore, we apply the font color to both headings. This is an additional style and is not mandatory to use.

For the body style, we use the background color and the font color collectively to add a style to the web page to make it aesthetic to the users. Another important feature that is used to apply the display property is to mention the HTML body width to display all the HTML contents in a single line.

Save the code in the text editor file with the html extension to make it an icon of the browser that depicts that it is a web page. Execute it in the browser. The expected web page has a list of all the items in a vertical notation if there is no inline-block property.

But as a result of this display CSS property, we see that all the items on the list are displayed in a single horizontal line without bullets. It no longer looks like a list. But it looks like a simple paragraph with the default spaces between the words.

Example 2: No Line Breaks in Div

Just like lists, when the list is converted into a paragraph, we are capable of applying the display effect on any other HTML content. So, we have chosen a div. A div is the container that contains the other HTML contents within it. First, consider a simple div tag in which we have not applied any CSS effect of “no line break “. But the div is supplied with its basic stylings like padding and color to display the existence of both divs on the web page.

The same goes for the div two.

Save the code and execute it in the browser. You will see that two divs are displayed vertically with the specifications that we applied as an inline CSS inside the tags. These divs are displayed without any break between them. As seen in the output, both divs are attached.

Whenever a div or a table is used in the HTML, they both lay one after the other in a vertical direction as we represented. This is because HTML generates the auto spacing by following the padding value applied by the user to keep them in a single line. Using the “no line break” phenomenon, we undergo some changes in the code.

First, we apply the display property with the inline-block property to both divs in CSS.

Moreover, we need to reduce the body width of the HTML so that both the divs can adjust to the given size of the body.

Upon execution, you will see that the two divs that we declared used the “no line break” CSS property of display.

Additional Ways:

Some additional information regarding the property of the “no line break” is accompanied through the HTML tags without the use of the CSS tags separately.

One way is to use the string&#160. Using this string in between two strings causes a space by making them stay on the same line instead of using a
tag that leads towards the following line. The and(&) operator is used for binding purposes.

Another way of a gap between two strings is that HTML uses a built-in feature “nbsp” or the “Non-breaking space”. We use the nbsp between two numbers. The results will have a space.

Now, upon execution, you will see the space between these three numbers without using the line break.

Conclusion

The CSS “no line break” article explains the use of the properties of CSS and HTML other than the break effect that is applied through the HTML break tag
. At the start, we gave a simple introduction about HTML and CSS. The property that is responsible mainly for the “no line break” effect is explained as the inline-block display property. We explained the inline-block display usage in the HTML lists and the div containers through examples. Moreover, some additional information regarding this topic is also added.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

prevent :after element from wrapping to next line

enter image description here

The problem: if the available width is too small, the icon wraps to the next line. I would want it to stay on the same line as the last word of the link it’s appended to: Is this doable, without adding ‘nowrap’ to the entire link (I want the words to wrap, just not the icon). See jsFiddle here.

11 Answers 11

Add a negative margin to the pseudo element, to compensate its width:

Now, that is going to make the icon overflow the container, and the line is going to break only when the text meet the end of the line. If you need to maintain the icon inside the original container width, you can add padding to compensate again:

IMPORTANT UPDATE:

For this method to work, there must be no white space between the text and the closing tag of the element holding the pseudo element. Even though the pseudo element’s width is compensated by a negative margin, the white space will make the line break when it meets the edge of the parent element. For example, this works:

and this doesn’t:

This is the more elegant solution in my opinion, since there is no extra markup required. My only complaint is that any spacing you want between the icon and text needs to be added to the image, margin/padding won’t work.

this is the way to go when dealing with table cells (where column width is determined at display time)

@brendanmckeown if you make the width larger, you can just change the background location. So width:26px; and 2px 0 as background position would move it. jsfiddle.net/Bk38F/23

@brendanmckeown: You CAN use margin to make space between the icon and text. Use the pseudo’s left margin to make the space, and make its negative right margin the sum of its width and the left margin: < width: 24px; margin-left: 10px; margin-right: -34px >

This solution isn’t working. At least in Chrome I can still find screen sizes where icon is wrapping before the last word. That just don’t happen so often with this solution but it’s still possible.

This is a little similar to a previous answer, but I thought that I’d flesh it out and explain it fully.

As soon as you set display: inline-block , :after becomes a non-text-binding element. This is why it wraps, and why nowrap has no effect. So leave display alone.

As it’s a text element by default, the content binds to previous text, as long as there’s no whitespace between them. So don’t add any, but make sure you have content: «» , or it’s the same as display: none . The only problem is height and width are controlled by the content , which in this case is collapsed. So width is 0 , and height is the line height.

Resize the area with padding; left or right, it doesn’t matter. Add a little margin so the icon doesn’t touch the text. Keep everything as relative sizes ( em , pt , etc.), and use background-size: contain , so that the icon scales with the text, like an emoji or font. Finally position the icon where you want with background-position .

I’ve used this for my external links ( a[href*=»://»]:after ), and it’s worked fine in Firefox, Chrome, and iOS. And since the icon remains a text element, even direct text after it binds, so any closing punctuation will wrap too.

Источник

Оцените статью