Css display inline align right

CSS Align inline-block to the right without using float

The following tutorial shows you how to use CSS to do «CSS Align inline-block to the right without using float».

CSS Style

The CSS style to do «CSS Align inline-block to the right without using float» is

.LinkButtons !-- ww w. d e m o 2 s . c o m --> display:inline-block; *display:inline; padding:5px 10px 5px 10px; background-color:#00649c; color:#fff; text-decoration:none; font-weight:bold; font-size:12px; > .align-right < text-align:right; > .LinkButtons:hover, .linkbuttons:hover < background-color:#00718b; cursor:pointer; text-decoration:none; > .LinkButtons a, .linkbuttons a < color:#fff; text-decoration:none; > .LinkButtons a:hover, .linkbuttons a:hover < color:#fff; text-decoration:none; >

HTML Body

body> p>Text above div >"align-right"> p >"LinkButtons">MORE INFO  p>Text below gets the button over top   

The following iframe shows the result. You can view the full source code and open it in another tab.

html> head> meta name="viewport" content="width=device-width, initial-scale=1"> style id="compiled-css" type="text/css"> .LinkButtons !-- w w w . d e mo 2 s . c o m--> display: inline-block; *display: inline; padding:5px 10px 5px 10px; background-color:#00649c; color:#fff; text-decoration:none; font-weight:bold; font-size:12px; > .align-right < text-align:right; > .LinkButtons:hover, .linkbuttons:hover background-color:#00718b; cursor:pointer; text-decoration:none;> .LinkButtons a, .linkbuttons a color:#fff; text-decoration:none;> .LinkButtons a:hover, .linkbuttons a:hover color:#fff; text-decoration:none;>  body> p>Text above div >"align-right">p >"LinkButtons">MORE INFO  p>Text below gets the button over top    

  • CSS 2 divs floating right wrong aligment
  • CSS Align div right of floating div
  • CSS Align div to right, without using float
  • CSS Align inline-block to the right without using float
  • CSS Align inline-block to the right without using float (Demo 2)
  • CSS Align inline-block to the right without using float (Demo 3)
  • CSS align text center in a right floated div

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

How to use inline-block for layout

I originally published this article on 4th September 2011 . It has since been completely rewritten to have much more practical application and address the many techniques surrounding it.

The inline-block technique is a really handy alternative to a float based layouts. It has a few decent advantages, as float based layouts suffer from needing to be cleared and if height varies it can have undesired effects. With inline-block you don’t need to worry about the height or clearing of elements. However, there are still some things to watch out for, which I will cover in this post. There’s also a Sass inline-block grid for you to use in your projects (should you need one).

Why use inline-block over floats?

Showing the difference between float and inline block when a floated element has a different height to the other elements

It’s easy to say floats were never intended for layout, but that doesn’t stop you from using them. The main benefit to using inline-block over floats is your elements don’t require any kind of clearing and the layout doesn’t break when you have multiple items of different height.

The inline-block approach is quite dependable in that sense. The only issue is with the elements still technically being inline, any spaces in your code will appear to break the layout. I will cover solutions to fix that. It’s a method I have come to rely on to get ‘divs side by side’.

General usage

Usage is just a matter of changing the display property of the element. A width will be necessary to size your elements appropriately.

Vertical alignment

The flexibility of this method comes in with vertical-align . With this, we can change the how the layout is displayed when items aren’t the same height. A different alignment may be favourable. The are many values, for vertical-align . The ones that tend to favour grid layout are top , middle or bottom . More information can be found on MDN.

Adding width

If your element has enough content to span 100% width, then it will. So if you have two elements that have a bunch of content in and you want them in two columns, you can add a width.

However, unless your code is minified, the elements will be 50% width, but not side by side. I’ll explain how to fix that shortly.

Using inline-block to align to the left, right or centre

Another benefit to using this method is alignment tends to come without the issues of floating. You can easily align an element elements to the right or centre by using the relevant text-align value. However, this has to be applied to the parent, so our markup and CSS would look like the following:

.parent < text-align: right; >.inline-block-element < display: inline-block; vertical-align: top; text-align: left; /* optional */ width: 50%; >

The important thing to note about the CSS is you’ll find text also aligns to the right. So you may need to add text-align: left to counter this. I’d recommend setting up reusable class names for each text alignment, so you can apply them as and when.

.text-left < text-align: left; >.text-center < text-align: center; >.text-right

How to deal with spaces

Elements which are inline-block recognise spaces around them, this causes layout to break. In rare cases you may intend to have these spaces, but in this context, your indentation is purely for code formatting.

In turn, there are a few ways to deal with white space with display: inline-block . All of them encounter possible user error with implementation and it’s determining which is best of a bad bunch.

Comments/minifying HTML

If you have the availability of minified HTML always, great you don’t need to worry about this. Although if there is the chance that unmagnified HTML can be introduced at any point you’ll need a solution.

The issue with removing space is it can make our HTML unreadable. So this is where comments can come in. However, it doesn’t really make it that much better.
Overall, this approach isn’t very friendly. It relies on you knowing this is the approach when being involved in a project.

Negative margin

I would strongly recommend against this method. For completeness and as a last resort I mention it. The negative margin method involves adding a right margin that offsets the element the equivalent amount of a single space. This generally works out at -4px for all browsers except IE. Which worked out at -5px . I’ve not tested this recently as it’s not my go to method, but my past experiences proved this to be the case.

The fact there is a difference in space width, between browsers, is the major reason to not use this approach. Everything has to be 1px off to accommodate IE. It also means you have to be more vigilant in spotting spacing errors because of it.

font-size: 0 on the parent

This method involves setting the parent element to have no font size, which means any spaces, no longer exist. This is my preferred method due to it relying less on human error and browser annoyances.

The best thing about it is someone inheriting your code can just reference grids that have already been used and be good to go. Someone inheriting code that uses the comment or whitespace removed approach has to be aware of this. The same applies to the negative margin approach they need to be aware it could have impact visually.

Grid layout with Sass

Taking what has been explained in this post. I use Sass to generate my grid. I find it to be a reliable grid for me, with breakpoint adjustments. I have made everything have the ability to be changed without messing with the grid generation.

Sass

You can view the compiled version if you would like to just have the CSS.

Grid details

Usage: HTML

To start a grid it requires a containing element. With the class grid . If you wish to add spacing between elements add grid-with-gutter .

To create a layout, add an additional inside that with the relevant class name. For example, a two column layout at the first breakpoint and 4 column at the medium breakpoint (640px and up).

 
Content
Content
Content
Content

Usage: Sass

The grid can be included with the defaults in your Sass by including the grid mixin.

Overriding through the mixin

The grid mixin takes 3 parameters. $grid-columns which can be any number in reality. $grid-gutter a pixel value is recommended. $grid-map which is a map for your breakpoints.

@include grid( $grid-columns : $default-grid-columns, $grid-gutter : $default-grid-gutter, $grid-map : $default-grid-map ); 

Modifying breakpoints

I have set up a bunch of defaults to make it easy to override. So you don’t need to call the mixin with parameters. The first step will be changing or adding breakpoints. This can be done by changing the $default-grid-map variable.

$default-grid-map: ( small: 540px, medium: 720px, large: 1024px ); 

Remove !default from the variable and set it again with your other variables. You can also change the naming convention to more your liking too. It will generate grid classes based on the name. eg: grid-small-1 , grid-anything-3 .

Other variables to modify

You can modify the column amount and the gutter, with the following variables. The gutter number is half the value you want it to be overall.

$default-grid-columns: 12 !default; $default-grid-gutter: 6px !default; 

Example with modifications

Here is an example of a 6 column grid, with 24px gutter and 4 breakpoints named alphabetically.

$default-grid-columns: 6; $default-grid-gutter: 12px; $default-grid-map: ( a: 320px, b: 480px, c: 720px, d: 1080px ); @include grid; 

A dependable layout technique

So you know now how to handle the main ways to handle the issues with this technique for layout. Once you’re really familiar, you will find reliance on it for layout. Especially if you were using floats beforehand.

Hopefully, you find the grid useful too.

I’m a tea drinker, but it’s equally appreciated if you found the article useful.

Next to read

Get up to speed with CSS shapes

Get the articles

It’s my aim to help you be a better designer. Join my monthlyish email list and I’ll send new tutorials to help you design & code beautiful websites. You can unsubscribe anytime.

iamsteve is a blog written by Steve McKinney, focusing on the design and build of websites. The aim is to bridge the gap in building your design. It started — and remains — a way to encourage self learning and sharing, through a mixture of in depth tutorials and quick tips.

You can find me elsewhere
dribbble, twitter, behance and linkedin.

Источник

Читайте также:  Html how to put image in center
Оцените статью