我在文本框的左上角有一个文本框和一些按钮.我希望这些按钮直接位于文本框的顶部,而不是文本框的左上角.有没有办法做到这一点.我试着调整-Content:Center,但不起作用. 我正在try 制作一个类似谷歌文档的应用程序,所以我需要像谷歌文档一样的按钮在文本框上.

var boldBtn = document.querySelector(".bold");
var italicBtn = document.querySelector(".italic");
var underlineBtn = document.querySelector(".underline");
var colorPicker = document.querySelector(".color-picker");
var highlightBtn = document.querySelector("#highlight");

boldBtn.addEventListener("click", function () {
  boldBtn.classList.toggle("inUse");
});

italicBtn.addEventListener("click", function () {
  italicBtn.classList.toggle("inUse");
});

underlineBtn.addEventListener("click", function () {
  underlineBtn.classList.toggle("inUse");
});

highlightBtn.addEventListener("click", function () {
  highlightBtn.classList.toggle("inUse");
});

const changeColorText = (color) => {
  document.execCommand("styleWithCSS", false, true);
  document.execCommand("foreColor", false, color);
};

document
  .getElementById("highlight")
  .addEventListener("click", function () {
    var range = window.getSelection().getRangeAt(0),
      span = document.createElement("span");

    span.className = "highlight";
    span.appendChild(range.extractContents());
    range.insertNode(span);
  });
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;

  background-color: lightgrey;
}

.userInput {
  margin: 1px;
  padding: 1px;
  width: 1000px;
  height: 700px;
  resize: none;
  font-family: "Segoe UI", sans-serif;
  display: block;
  margin-left: auto;
  margin-right: auto;
  background-color: white;
}

.inUse {
  background-color: lightblue;
  width: default;
  height: default;
  border-width: thin;
}

button {
  width: 100px;
}

.dropbtn {
  /* background-color: #3498db; */
  color: black;
  /* padding: 16px; */
  /* font-size: 16px; */
  border: none;
  cursor: pointer;
  width: 100px;
  height: 30px;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {
  display: block;
}

.highlight {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Editor</title>
    <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <button class="bold" onclick="document.execCommand('bold',false,null);">
      ????
    </button>

    <button class="italic" onclick="document.execCommand('italic',false,null);">
      ????
    </button>

    <button
      class="underline"
      onclick="document.execCommand('underline',false,null);"
    >
      U̲
    </button>

    <input
      type="color"
      class="color-picker"
      id="colorPicker"
      oninput="changeColorText(this.value);"
    />
    <label>Select color</label>

    <button id="highlight"><mark>Highlight</mark></button>

    <fieldset class="userInput" contenteditable="true"></fieldset>

  </body>
</html>

推荐答案

将您的按钮包装到div中并使用flex-box,外观 创建所需的样式.

But why?元素(在显示为flex的容器中)支持有关如何分配其子元素的一些特殊行为,因此 Flex-box有很多用法,这里有什么可以帮助您的:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/

在您的例子中,简单的css样式

.toolbar{ /* class name of wrapper div */
  display: flex; /* enable flex-box */
  justify-content:center; /* here instruct, place all,*/
  /* side-by-side but centralized */    
}

var boldBtn = document.querySelector(".bold");
var italicBtn = document.querySelector(".italic");
var underlineBtn = document.querySelector(".underline");
var colorPicker = document.querySelector(".color-picker");
var highlightBtn = document.querySelector("#highlight");

boldBtn.addEventListener("click", function () {
  boldBtn.classList.toggle("inUse");
});

italicBtn.addEventListener("click", function () {
  italicBtn.classList.toggle("inUse");
});

underlineBtn.addEventListener("click", function () {
  underlineBtn.classList.toggle("inUse");
});

highlightBtn.addEventListener("click", function () {
  highlightBtn.classList.toggle("inUse");
});

const changeColorText = (color) => {
  document.execCommand("styleWithCSS", false, true);
  document.execCommand("foreColor", false, color);
};

document
  .getElementById("highlight")
  .addEventListener("click", function () {
    var range = window.getSelection().getRangeAt(0),
      span = document.createElement("span");

    span.className = "highlight";
    span.appendChild(range.extractContents());
    range.insertNode(span);
  });
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;

  background-color: lightgrey;
}

.userInput {
  margin: 1px;
  padding: 1px;
  width: 1000px;
  height: 700px;
  resize: none;
  font-family: "Segoe UI", sans-serif;
  display: block;
  margin-left: auto;
  margin-right: auto;
  background-color: white;
}

.inUse {
  background-color: lightblue;
  width: default;
  height: default;
  border-width: thin;
}

button {
  width: 100px;
}

.dropbtn {
  /* background-color: #3498db; */
  color: black;
  /* padding: 16px; */
  /* font-size: 16px; */
  border: none;
  cursor: pointer;
  width: 100px;
  height: 30px;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {
  display: block;
}

.highlight {
  background-color: yellow;
}
.toolbar{
  display: flex;
  justify-content:center;

}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Editor</title>
    <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <div class="toolbar">
    <button class="bold" onclick="document.execCommand('bold',false,null);">
      ????
    </button>

    <button class="italic" onclick="document.execCommand('italic',false,null);">
      ????
    </button>

    <button
      class="underline"
      onclick="document.execCommand('underline',false,null);"
    >
      U̲
    </button>

    <input
      type="color"
      class="color-picker"
      id="colorPicker"
      oninput="changeColorText(this.value);"
    />
    <label>Select color</label>

    <button id="highlight"><mark>Highlight</mark></button>
    </div>

    <fieldset class="userInput" contenteditable="true"></fieldset>

  </body>
</html>

Javascript相关问答推荐

仅圆角的甜甜圈图表

无法在nextjs应用程序中通过id从mongoDB删除'

在nextjs服务器端api调用中传递认证凭证

setcallback是什么时候放到macrotask队列上的?

如何在输入元素中附加一个属性为checkbox?

在执行异步导入之前判断模块是否已导入()

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

在VS代码上一次设置多个变量格式

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

Next.js中的服务器端组件列表筛选

Phaser3 preFX addGlow不支持zoom

处理app.param()中的多个参数

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

如何在Jest中模拟函数

无法使用npm install安装react-dom、react和next

递增/递减按钮React.js/Redux

在每次重新加载页面时更改指针光标

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有

TS node 找不到依赖的模块

如何在chartjs-plugin-data Labels v2.1.0插件中设置照片?