In the float attribute of CSS, float:right floats to the right and float:left floats to the left, but the centered attribute is not set. So what should we do if we want to center the float attribute? This article will introduce to you how to center the float attribute in CSS.
We often use floating centering when making navigation bars. Below we will use the floating centering of navigation bars to introduce to you the float centering method in CSS. (Related recommendation: How to use the float attribute of css?)
How to center float in css: Use display:inline-block and float:left
Let’s look at the specific float centering example code:
<!DOCTYPE html> <html> <head> <meta charset=utf-8" /> <title></title> <style type="text/css"> * { margin:0; padding:0; list-style:none; } body { text-align:center; } li { float:left; font-size:12px; } a { float:left; border:1px solid #000; padding:5px 10px; text-decoration:none; color:#000; } ul { display:inline-block; *display:inline; zoom:1; } </style> </head> <body> <ul> <li><a href="#nogo">首页</a></li> <li><a href="#nogo">视频</a></li> <li><a href="#nogo">文章</a></li> <li><a href="#nogo">登录</a></li> <li><a href="#nogo">留言</a></li> </ul> </body> </html>
The effect of float centering is as follows:
Regarding the above code, we will see a *display:inline;, which is actually a CSS hack writing method; (if you don’t know the CSS hack writing method, you can search it on Baidu yourself) *{margin: 0;padding: 0;} is a common "reset" style in the past, which means to stick all the elements in the web page tightly together. Because different browsers will produce different default element styles, so *{ margin: 0; padding: 0;} The main purpose is to help you reset the default styles of different browsers, so that the results of different browsers displaying web pages will not be too different.
After reading the above methods, you may sometimes not think of using CSS hack to write, and the compatibility is not good,
So let’s take a look at the implementation method without using *display:inline;
How to set float centering code example in css (without using *display:inline;):
<!DOCTYPE html> <html> <head> <meta charset=utf-8" /> <title></title> <style type="text/css"> * { padding: 0px; margin: 0px; } .xlk-nav { width: 100%; height: 40px; text-align: center; background-color: lightblue; } .xlk-menu { height: 100%; float: left; line-height: 40px; padding: 0 20px; margin-right: 20px; background-color: pink; } </style> </head> <body> <div class="xlk-nav"> <div style="display: inline-block;"> <div class="xlk-menu">首页</div> <div class="xlk-menu">视频</div> <div class="xlk-menu">文章</div> <div class="xlk-menu">登录</div> <div class="xlk-menu">留言</div> </div> </div> </body> </html>
The centering effect is as follows:
The above is the detailed content of How to set float center in css?. For more information, please follow other related articles on the PHP Chinese website!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools