Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing an inline-block element to the next line

Tags:

html

css

I'm looking to clear an inline-block element (in this case an <a> within a <p>) to the next line, without having to set display:block and defining a width.

Here's an example: http://jsfiddle.net/alecrust/zstKf/

Here's the desired result (using display:block and defining a width): http://jsfiddle.net/alecrust/TmwhU/

like image 658
AlecRust Avatar asked Jun 13 '12 13:06

AlecRust


People also ask

How to remove the space between inline-block elements in HTML?

Tricks to Remove the Space Between Inline-Block Elements ¶ 1 Create a <ul> tag, which is used for specifying an unordered list. The <ul> tag is a block-level element. Create <li>... 2 Use the :nth-child () pseudo-class so as to put style to the next two <li> tags. More ...

What is the difference between display inline and inline block in CSS?

CSS Layout - display: inline-block. ❮ Previous Next ❯. Compared to display: inline, the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block, the top and bottom margins/paddings are respected, but with display: inline they are not.

Why don’t inline-block elements align to the top of the container?

Many developers have problems with the alignment of inline-block elements. The problem is when some inline-block elements have different heights, why does the shorter of them not align to the top of the container? We are going to show the solution to this problem with the help of CSS properties. Let’s discuss an example below.

How to prevent a Div from breaking to the next line?

An HTML <div> (division) is a block-level elementdesigned to not display any HTML elements next to it unless its default behavior is changed. Below are all the different methods of preventing a div from breaking to the next line. Tip Depending on why you want to break a div, also consider a <span> tag.


2 Answers

If you want to avoid setting an explicit width so you can style the background according to the actual length of the text, you can do the following:

Wrap your link:

 <p>To stay up to date <span><a href="#">Follow Us</a></span></p>

Note that I have added a <span> tag around the link.

Style your wrapper with CSS:

 span {
   display: inline-block;
   width: 100%;
 }

Setting the width to 100% forces the wrapper to take up the whole line. Keeping the <a> tag for the link set to inline-block allows it to have padding and a background applied while not having it expand to fit the container's width of 100%.

Forked JSFiddle: http://jsfiddle.net/Cm9kZ/

like image 167
highvolt Avatar answered Oct 04 '22 07:10

highvolt


It's a bit of a kludge, but it will work:

a {
    display: inline-block;
    padding: 5px 18px;
    background-color: #8C4AD5;
    text-decoration: none;
    position:relative;
    top:25px;
    left:-30%
}

You'll have to fudge the left position, but that basically puts you back into setting a known value, just like the width issue in your display:block example. Not really any better, just a different approach.

like image 42
Diodeus - James MacFarlane Avatar answered Oct 04 '22 06:10

Diodeus - James MacFarlane