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/
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 ...
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.
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.
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With